GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix wrong nextBid computation in IoUringBufferRing for RECVSEND_BUNDLE · netty/netty@1412112

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • transport-classes-io_uring/src/main/java/io/netty/channel/uring
    • AbstractIoUringStreamChannel.java
    • IoUringBufferRing.java
  • transport-native-io_uring/src/test/java/io/netty/channel/uring
    • IoUringBufferRingTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎transport-classes-io_uring/src/main/java/io/netty/channel/uring/AbstractIoUringStreamChannel.java‎

Copy file name to clipboardExpand all lines: transport-classes-io_uring/src/main/java/io/netty/channel/uring/AbstractIoUringStreamChannel.java
+8-1Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,13 @@ protected void readComplete0(byte op, int res, int flags, short data, int outsta
571571
int read = res;
572572
for (;;) {
573573
int attemptedBytesRead = bufferRing.attemptedBytesRead(bid);
574+
// Snapshot the number of allocated buffers before calling useBuffer(...) as that call
575+
// might grow the ring (and so change allocatedBuffers) once the last posted buffer is
576+
// consumed. The bundle completion we are walking here was produced by the kernel using
577+
// the ring size as it was *before* any such growth, so the wrap-around for the next bid
578+
// in this bundle must be computed with that same (old) size to stay in sync with the
579+
// kernel.
580+
int allocatedBuffers = bufferRing.allocatedBuffers();
574581
byteBuf = bufferRing.useBuffer(bid, read, more);
575582
read -= byteBuf.readableBytes();
576583
allocHandle.attemptedBytesRead(attemptedBytesRead);
@@ -585,7 +592,7 @@ protected void readComplete0(byte op, int res, int flags, short data, int outsta
585592
allocHandle.incMessagesRead(1);
586593
pipeline.fireChannelRead(byteBuf);
587594
byteBuf = null;
588-
bid = bufferRing.nextBid(bid);
595+
bid = bufferRing.nextBid(bid, allocatedBuffers);
589596
if (!allocHandle.continueReading()) {
590597
// We should call fireChannelReadComplete() to mimic a normal read loop.
591598
allocHandle.readComplete();
Collapse file

‎transport-classes-io_uring/src/main/java/io/netty/channel/uring/IoUringBufferRing.java‎

Copy file name to clipboardExpand all lines: transport-classes-io_uring/src/main/java/io/netty/channel/uring/IoUringBufferRing.java
+32-4Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ private int calculateNextBufferBatch() {
214214
* @param read the number of bytes that could be read. This value might be larger then what a single
215215
* {@link ByteBuf} can hold. Because of this, the caller should call
216216
* @link #useBuffer(short, int, boolean)} in a loop (obtaining the next bid to use by calling
217-
* {@link #nextBid(short)}) until all buffers could be obtained.
217+
* {@link #nextBid(short, int)} with an {@link #allocatedBuffers()} snapshot taken before
218+
* this call) until all buffers could be obtained.
218219
* @return the buffer.
219220
*/
220221
ByteBuf useBuffer(short bid, int read, boolean more) {
@@ -224,7 +225,7 @@ ByteBuf useBuffer(short bid, int read, boolean more) {
224225
allocator.lastBytesRead(byteBuf.writableBytes(), read);
225226
// The kernel might report a bundle total (RECVSEND_BUNDLE) that spans more than this single ring
226227
// entry can hold. Clamp to what this entry can actually provide; the caller loops over the
227-
// remainder using the next bid (see nextBid(short)).
228+
// remainder using the next bid (see nextBid(short, int)).
228229
int len = Math.min(read, byteBuf.writableBytes());
229230
// We always slice so the user will not mess up things later.
230231
ByteBuf buffer = byteBuf.retainedSlice(byteBuf.writerIndex(), len);
@@ -268,8 +269,35 @@ ByteBuf useBuffer(short bid, int read, boolean more) {
268269
return buffer;
269270
}
270271

271-
short nextBid(short bid) {
272-
return (short) ((bid + 1) & allocatedBuffers - 1);
272+
/**
273+
* The number of buffers that are currently allocated (and so posted) in this buffer ring. This value can
274+
* change every time {@link #useBuffer(short, int, boolean)} is called as it might grow the ring. Because of
275+
* this, callers that need to walk a sequence of consecutive buffer ids that belong to the same completion
276+
* (for example a {@code IORING_RECVSEND_BUNDLE} completion) must capture this value <strong>before</strong>
277+
* calling {@link #useBuffer(short, int, boolean)} and pass it to {@link #nextBid(short, int)} afterwards,
278+
* so the wrap-around is computed using the size of the ring as the kernel observed it when it produced the
279+
* bundle, not the (possibly already grown) size afterwards.
280+
*
281+
* @return the number of allocated buffers.
282+
*/
283+
int allocatedBuffers() {
284+
return allocatedBuffers;
285+
}
286+
287+
/**
288+
* Return the next buffer id that follows {@code bid} in ring order, wrapping around after
289+
* {@code allocatedBuffers} ids.
290+
*
291+
* @param bid the current buffer id.
292+
* @param allocatedBuffers the number of allocated buffers to use for the wrap-around computation. This should
293+
* be a value obtained from {@link #allocatedBuffers()} <strong>before</strong> the
294+
* corresponding {@link #useBuffer(short, int, boolean)} call for {@code bid}, as that
295+
* call might have grown the ring already.
296+
* @return the next buffer id.
297+
*/
298+
short nextBid(short bid, int allocatedBuffers) {
299+
// Use the real modulo (and not a bitmask) as allocatedBuffers is not guaranteed to be a power of two.
300+
return (short) ((bid + 1) % allocatedBuffers);
273301
}
274302

275303
/**
Collapse file

‎transport-native-io_uring/src/test/java/io/netty/channel/uring/IoUringBufferRingTest.java‎

Copy file name to clipboardExpand all lines: transport-native-io_uring/src/test/java/io/netty/channel/uring/IoUringBufferRingTest.java
+90Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,96 @@ public void testUseBufferClampsReadLargerThanEntryCapacity() throws Exception {
289289
}
290290
}
291291

292+
@Test
293+
public void testNextBidWrapsCorrectlyWithNonPowerOfTwoAllocatedBuffers() throws Exception {
294+
// Regression test: allocatedBuffers grows by batchSize increments and so is not guaranteed to be a
295+
// power of two (e.g. ringSize=16, batchSize=4 goes 4 -> 8 -> 12 -> 16). nextBid(...) must wrap using
296+
// the real allocated buffer count and not a bitmask, which is only correct for power-of-two counts.
297+
short entries = 16;
298+
RingBuffer ringBuffer = Native.createRingBuffer(8, 0);
299+
try {
300+
int ringFd = ringBuffer.fd();
301+
long ioUringBufRingAddr = Native.ioUringRegisterBufRing(ringFd, entries, (short) 1, 0);
302+
assumeThat(ioUringBufRingAddr)
303+
.as("ioUringSetupBufRing result must be greater than 0, but now result is %d", ioUringBufRingAddr)
304+
.isGreaterThan(0);
305+
IoUringBufferRing bufferRing = new IoUringBufferRing(ringFd,
306+
Buffer.wrapMemoryAddressWithNativeOrder(ioUringBufRingAddr, Native.ioUringBufRingSize(entries)),
307+
entries, 4, (short) 1, false,
308+
new IoUringFixedBufferRingAllocator(64), false);
309+
try {
310+
bufferRing.initialize();
311+
assertEquals(4, bufferRing.allocatedBuffers());
312+
313+
assertEquals((short) 4, bufferRing.nextBid((short) 3, 12));
314+
assertEquals((short) 0, bufferRing.nextBid((short) 11, 12));
315+
} finally {
316+
// Releases the outstanding buffers and unregisters the ring.
317+
bufferRing.close();
318+
}
319+
} finally {
320+
ringBuffer.close();
321+
}
322+
}
323+
324+
@Test
325+
public void testNextBidUsesAllocatedBuffersSnapshotBeforeRingGrowsMidBundle() throws Exception {
326+
// Regression test: useBuffer(...) can grow the ring (change allocatedBuffers) as a side effect once the
327+
// last currently-posted buffer is consumed. When walking a multi-entry RECVSEND_BUNDLE completion, the
328+
// wrap-around for the *next* bid must be computed using the ring size as it was before that growth (i.e.
329+
// as the kernel observed it when producing the bundle), not the grown size, or the caller will jump to
330+
// the wrong ring slot. See AbstractIoUringStreamChannel, which snapshots allocatedBuffers() before
331+
// calling useBuffer(...) and passes it to nextBid(...) afterwards.
332+
short entries = 16;
333+
int batchSize = 8;
334+
RingBuffer ringBuffer = Native.createRingBuffer(8, 0);
335+
try {
336+
int ringFd = ringBuffer.fd();
337+
long ioUringBufRingAddr = Native.ioUringRegisterBufRing(ringFd, entries, (short) 1, 0);
338+
assumeThat(ioUringBufRingAddr)
339+
.as("ioUringSetupBufRing result must be greater than 0, but now result is %d", ioUringBufRingAddr)
340+
.isGreaterThan(0);
341+
IoUringBufferRing bufferRing = new IoUringBufferRing(ringFd,
342+
Buffer.wrapMemoryAddressWithNativeOrder(ioUringBufRingAddr, Native.ioUringBufRingSize(entries)),
343+
entries, batchSize, (short) 1, false,
344+
new IoUringFixedBufferRingAllocator(64), true);
345+
try {
346+
bufferRing.initialize();
347+
assertEquals(8, bufferRing.allocatedBuffers());
348+
349+
// Simulate the ENOBUFS handling in AbstractIoUringStreamChannel: signal that we should expand
350+
// the ring the next time all currently posted buffers have been consumed.
351+
assertTrue(bufferRing.expand());
352+
353+
// Consume bids 0..6, mimicking a bundle that walks through (almost) a whole lap of the ring.
354+
for (short bid = 0; bid < 7; bid++) {
355+
bufferRing.useBuffer(bid, 1, false).release();
356+
assertEquals(8, bufferRing.allocatedBuffers());
357+
}
358+
359+
// This is exactly what AbstractIoUringStreamChannel does: snapshot the ring size *before*
360+
// calling useBuffer(...) for the last bid of the bundle.
361+
int allocatedBuffersBeforeUseBuffer = bufferRing.allocatedBuffers();
362+
assertEquals(8, allocatedBuffersBeforeUseBuffer);
363+
364+
// Consuming the last (8th) posted buffer causes the ring to grow, since we called expand() above.
365+
bufferRing.useBuffer((short) 7, 1, false).release();
366+
assertEquals(16, bufferRing.allocatedBuffers());
367+
368+
// Using the pre-growth snapshot gives the correct wrap-around: bid 7 was the last of the
369+
// original 8 posted buffers, so the bundle continues at bid 0.
370+
assertEquals((short) 0, bufferRing.nextBid((short) 7, allocatedBuffersBeforeUseBuffer));
371+
// Using the already-grown size (the bug this guards against) would incorrectly jump to bid 8,
372+
// a slot that was never part of this bundle and belongs to a buffer the kernel never touched.
373+
assertEquals((short) 8, bufferRing.nextBid((short) 7, bufferRing.allocatedBuffers()));
374+
} finally {
375+
bufferRing.close();
376+
}
377+
} finally {
378+
ringBuffer.close();
379+
}
380+
}
381+
292382
private ByteBuf sendAndRecvMessage(Channel clientChannel, ByteBuf writeBuffer, BlockingQueue<ByteBuf> bufferSyncer)
293383
throws InterruptedException {
294384
//retain the buffer to assert

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.