Skip to contentSkip to conversation

io_uring/kbuf: use WRITE_ONCE() for userspace-shared buffer ring fields

PATCH Applied Joanne Koong 3 participants 5 messages Dec 4, 2025 – Dec 5, 2025 io_uring

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 io_uring/kbuf: use WRITE_ONCE() for userspace-shared buffer ring fields

buf->addr and buf->len reside in memory shared with userspace. They should be written with WRITE_ONCE() to guarantee atomic stores and prevent tearing or other unsafe compiler optimizations.

Fixes ae98dbf43d75 ("io_uring/kbuf: add support for incremental buffer consumption")
Cc Caleb Sander Mateos <[email protected]>
---
 io_uring/kbuf.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
io_uring/kbuf.c +4 -4
index 52b636d00a6b..796d131107dd 100644
@@ -44,11 +44,11 @@ static bool io_kbuf_inc_commit(struct io_buffer_list *bl, int len)
 		buf_len -= this_len;
 		/* Stop looping for invalid buffer length of 0 */
 		if (buf_len || !this_len) {
-			buf->addr = READ_ONCE(buf->addr) + this_len;
-			buf->len = buf_len;
+			WRITE_ONCE(buf->addr, READ_ONCE(buf->addr) + this_len);
+			WRITE_ONCE(buf->len, buf_len);
 			return false;
 		}
-		buf->len = 0;
+		WRITE_ONCE(buf->len, 0);
 		bl->head++;
 		len -= this_len;
 	}
@@ -291,7 +291,7 @@ static int io_ring_buffers_peek(struct io_kiocb *req, struct buf_sel_arg *arg,
 				arg->partial_map = 1;
 				if (iov != arg->iovs)
 					break;
-				buf->len = len;
+				WRITE_ONCE(buf->len, len);
 			}
 		}
 
Signature · 1 line
2.47.3
maintainer
#1 lore raw
Quoted 3 lines · Joanne Koong

buf->addr and buf->len reside in memory shared with userspace. They should be written with WRITE_ONCE() to guarantee atomic stores and prevent tearing or other unsafe compiler optimizations.

It's not a real concern, but I think we should still do it purely for documentation purposes. But because of that, I think we should drop:

Quoted 1 line
Fixes ae98dbf43d75 ("io_uring/kbuf: add support for incremental buffer consumption")

to save me the trouble of ensuring older stable kernels get this applied.

Signature · 1 line
Jens Axboe
maintainer Applied
#2 lore raw
In reply to Joanne Koong
Quoted 5 lines · Joanne Koong

buf->addr and buf->len reside in memory shared with userspace. They should be written with WRITE_ONCE() to guarantee atomic stores and prevent tearing or other unsafe compiler optimizations.

Applied, thanks!

[1/1] io_uring/kbuf: use WRITE_ONCE() for userspace-shared buffer ring fields

      commit: a4c694bfc2455e82b7caf6045ca893d123e0ed11

Best regards,

Signature · 1 line
Jens Axboe
#3 lore raw
In reply to Joanne Koong
Quoted 4 lines · Joanne Koong

buf->addr and buf->len reside in memory shared with userspace. They should be written with WRITE_ONCE() to guarantee atomic stores and prevent tearing or other unsafe compiler optimizations.

I considered this too, but I'm not sure it's necessary. A correctly written userspace program won't access these fields concurrently with the kernel, right? I'm not too familiar with the buffer ring UAPI, but I would assume userspace is notified somehow (via a posted CQE?) that the io_uring_buf slots have been consumed and are available to reuse. In that case, a torn store here would only be observable to a buggy userspace program, so I don't think we need to add WRITE_ONCE().

Best,
Caleb

Quoted 39 lines
Fixes ae98dbf43d75 ("io_uring/kbuf: add support for incremental buffer consumption")
Cc Caleb Sander Mateos <[email protected]>
---
 io_uring/kbuf.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
io_uring/kbuf.c +4 -5
index 52b636d00a6b..796d131107dd 100644
@@ -44,11 +44,11 @@ static bool io_kbuf_inc_commit(struct io_buffer_list *bl, int len)
                buf_len -= this_len;
                /* Stop looping for invalid buffer length of 0 */
                if (buf_len || !this_len) {
-                       buf->addr = READ_ONCE(buf->addr) + this_len;
-                       buf->len = buf_len;
+                       WRITE_ONCE(buf->addr, READ_ONCE(buf->addr) + this_len);
+                       WRITE_ONCE(buf->len, buf_len);
                        return false;
                }
-               buf->len = 0;
+               WRITE_ONCE(buf->len, 0);
                bl->head++;
                len -= this_len;
        }
@@ -291,7 +291,7 @@ static int io_ring_buffers_peek(struct io_kiocb *req, struct buf_sel_arg *arg,
                                arg->partial_map = 1;
                                if (iov != arg->iovs)
                                        break;
-                               buf->len = len;
+                               WRITE_ONCE(buf->len, len);
                        }
                }
 
--

2.47.3

maintainer
#4 lore raw
Quoted 6 lines · Caleb Sander Mateos
Quoted 4 lines · On Thu, Dec 4, 2025 at 3:55?PM Joanne Koong <[email protected]> wrote:

buf->addr and buf->len reside in memory shared with userspace. They should be written with WRITE_ONCE() to guarantee atomic stores and prevent tearing or other unsafe compiler optimizations.

Quoted 7 lines

I considered this too, but I'm not sure it's necessary. A correctly written userspace program won't access these fields concurrently with the kernel, right? I'm not too familiar with the buffer ring UAPI, but I would assume userspace is notified somehow (via a posted CQE?) that the io_uring_buf slots have been consumed and are available to reuse. In that case, a torn store here would only be observable to a buggy userspace program, so I don't think we need to add WRITE_ONCE().

I agree, see my reply from a few minutes ago. But I do think it serves a documentation purpose ("shared memory is always read/write once"), which is why I applied it.

Signature · 1 line
Jens Axboe