public inbox for [email protected]
 help / color / mirror / Atom feed
* [PATCH] io_uring/kbuf: Fix size for shared buffer ring
@ 2023-04-27 14:31 Tudor Cretu
  2023-04-27 14:35 ` Jens Axboe
  2023-04-27 18:42 ` Jens Axboe
  0 siblings, 2 replies; 4+ messages in thread
From: Tudor Cretu @ 2023-04-27 14:31 UTC (permalink / raw)
  To: io-uring; +Cc: =axboe, asml.silence, kevin.brodsky, linux-kernel, Tudor Cretu

The size of the ring is the product of ring_entries and the size of
struct io_uring_buf. Using struct_size is equivalent to
  (ring_entries + 1) * sizeof(struct io_uring_buf)
and generates an off-by-one error. Fix it by using size_mul directly.

Signed-off-by: Tudor Cretu <[email protected]>
---
 io_uring/kbuf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/io_uring/kbuf.c b/io_uring/kbuf.c
index 4a6401080c1f..9770757c89a0 100644
--- a/io_uring/kbuf.c
+++ b/io_uring/kbuf.c
@@ -505,7 +505,7 @@ int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
 	}
 
 	pages = io_pin_pages(reg.ring_addr,
-			     struct_size(br, bufs, reg.ring_entries),
+			     size_mul(sizeof(struct io_uring_buf), reg.ring_entries),
 			     &nr_pages);
 	if (IS_ERR(pages)) {
 		kfree(free_bl);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] io_uring/kbuf: Fix size for shared buffer ring
  2023-04-27 14:31 [PATCH] io_uring/kbuf: Fix size for shared buffer ring Tudor Cretu
@ 2023-04-27 14:35 ` Jens Axboe
  2023-04-27 18:42 ` Jens Axboe
  1 sibling, 0 replies; 4+ messages in thread
From: Jens Axboe @ 2023-04-27 14:35 UTC (permalink / raw)
  To: Tudor Cretu, io-uring; +Cc: =axboe, asml.silence, kevin.brodsky, linux-kernel

On 4/27/23 8:31?AM, Tudor Cretu wrote:
> The size of the ring is the product of ring_entries and the size of
> struct io_uring_buf. Using struct_size is equivalent to
>   (ring_entries + 1) * sizeof(struct io_uring_buf)
> and generates an off-by-one error. Fix it by using size_mul directly.

Looks right, so we're doing one entry too much. This is probably a
remnant of when the initial version had it done a bit differently, but
we overlaid the tail with the first entry reserved field.

-- 
Jens Axboe


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] io_uring/kbuf: Fix size for shared buffer ring
  2023-04-27 14:31 [PATCH] io_uring/kbuf: Fix size for shared buffer ring Tudor Cretu
  2023-04-27 14:35 ` Jens Axboe
@ 2023-04-27 18:42 ` Jens Axboe
  2023-04-28 10:29   ` Tudor Cretu
  1 sibling, 1 reply; 4+ messages in thread
From: Jens Axboe @ 2023-04-27 18:42 UTC (permalink / raw)
  To: Tudor Cretu, io-uring; +Cc: =axboe, asml.silence, kevin.brodsky, linux-kernel

On 4/27/23 8:31 AM, Tudor Cretu wrote:
> The size of the ring is the product of ring_entries and the size of
> struct io_uring_buf. Using struct_size is equivalent to
>   (ring_entries + 1) * sizeof(struct io_uring_buf)
> and generates an off-by-one error. Fix it by using size_mul directly.
> 
> Signed-off-by: Tudor Cretu <[email protected]>
> ---
>  io_uring/kbuf.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/io_uring/kbuf.c b/io_uring/kbuf.c
> index 4a6401080c1f..9770757c89a0 100644
> --- a/io_uring/kbuf.c
> +++ b/io_uring/kbuf.c
> @@ -505,7 +505,7 @@ int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
>  	}
>  
>  	pages = io_pin_pages(reg.ring_addr,
> -			     struct_size(br, bufs, reg.ring_entries),
> +			     size_mul(sizeof(struct io_uring_buf), reg.ring_entries),
>  			     &nr_pages);
>  	if (IS_ERR(pages)) {
>  		kfree(free_bl);

Looking into this again, and some bells ringing in the back of my head,
we do have:

commit 48ba08374e779421ca34bd14b4834aae19fc3e6a
Author: Wojciech Lukowicz <[email protected]>
Date:   Sat Feb 18 18:41:41 2023 +0000

    io_uring: fix size calculation when registering buf ring

which should have fixed that issue. What kernel version are you looking at?

-- 
Jens Axboe



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] io_uring/kbuf: Fix size for shared buffer ring
  2023-04-27 18:42 ` Jens Axboe
@ 2023-04-28 10:29   ` Tudor Cretu
  0 siblings, 0 replies; 4+ messages in thread
From: Tudor Cretu @ 2023-04-28 10:29 UTC (permalink / raw)
  To: Jens Axboe, io-uring; +Cc: asml.silence, kevin.brodsky, linux-kernel



On 27-04-2023 19:42, Jens Axboe wrote:
> On 4/27/23 8:31 AM, Tudor Cretu wrote:
>> The size of the ring is the product of ring_entries and the size of
>> struct io_uring_buf. Using struct_size is equivalent to
>>    (ring_entries + 1) * sizeof(struct io_uring_buf)
>> and generates an off-by-one error. Fix it by using size_mul directly.
>>
>> Signed-off-by: Tudor Cretu <[email protected]>
>> ---
>>   io_uring/kbuf.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/io_uring/kbuf.c b/io_uring/kbuf.c
>> index 4a6401080c1f..9770757c89a0 100644
>> --- a/io_uring/kbuf.c
>> +++ b/io_uring/kbuf.c
>> @@ -505,7 +505,7 @@ int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
>>   	}
>>   
>>   	pages = io_pin_pages(reg.ring_addr,
>> -			     struct_size(br, bufs, reg.ring_entries),
>> +			     size_mul(sizeof(struct io_uring_buf), reg.ring_entries),
>>   			     &nr_pages);
>>   	if (IS_ERR(pages)) {
>>   		kfree(free_bl);
> 
> Looking into this again, and some bells ringing in the back of my head,
> we do have:
> 
> commit 48ba08374e779421ca34bd14b4834aae19fc3e6a
> Author: Wojciech Lukowicz <[email protected]>
> Date:   Sat Feb 18 18:41:41 2023 +0000
> 
>      io_uring: fix size calculation when registering buf ring
> 
> which should have fixed that issue. What kernel version are you looking at?

Hi Jens,

Thank you for your message. Indeed I was looking at a slightly older 
version of the kernel. Apologies for the noise!

Kind regards,
Tudor

> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-04-28 10:29 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-04-27 14:31 [PATCH] io_uring/kbuf: Fix size for shared buffer ring Tudor Cretu
2023-04-27 14:35 ` Jens Axboe
2023-04-27 18:42 ` Jens Axboe
2023-04-28 10:29   ` Tudor Cretu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox