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 — io_uring: do not release write memory that the kernel still owns by HwangRock · Pull Request #17238 · netty/netty

io_uring: do not release write memory that the kernel still owns #17238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
normanmaurer merged 34 commits into netty:4.2 from HwangRock:io-uring-shutdown-output-inflight-write
Aug 27, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

io_uring: do not release write memory that the kernel still owns #17238

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
fd98f18
io_uring: do not release write memory that the kernel still owns
HwangRock Aug 7, 2026
89a98ea
io_uring: remove redundant close race tests
HwangRock Aug 9, 2026
4f83b50
io_uring: allocate write-operation ids from a free list
HwangRock Aug 9, 2026
38ae434
io_uring: index write operations by slot instead of hashing them
HwangRock Aug 9, 2026
2ead7c5
io_uring: skip the iovec collector test when the native library is un…
HwangRock Aug 9, 2026
9e0e5a2
io_uring: guard the zero-copy test on native availability
HwangRock Aug 9, 2026
6371985
io_uring: retain in-flight write memory at shutdown rather than at su…
HwangRock Aug 10, 2026
1476a7b
io_uring: keep zero-copy write memory alive until its notification
HwangRock Aug 10, 2026
c6af0ce
io_uring: avoid pooled buffer reuse race in test
HwangRock Aug 10, 2026
9dc8dd1
io_uring: track non-zero-copy stream writes in a single slot
HwangRock Aug 11, 2026
18a530a
io_uring: fall back to a long user_data when the write-operation ids …
HwangRock Aug 11, 2026
a171b0d
io_uring: retain zero-copy write memory on errored completions too
HwangRock Aug 11, 2026
51a9a82
io_uring: bring the added comments back in line with the package
HwangRock Aug 11, 2026
4e165ce
io_uring: clear the dropped iovec references when the collector is reset
HwangRock Aug 13, 2026
8c9c7a1
io_uring: keep an in-flight splice's file and pipe descriptors alive
HwangRock Aug 13, 2026
7c8b1fc
io_uring: hold the write bookkeeping in one type
HwangRock Aug 13, 2026
627eea2
io_uring: cover the pooled slots the way the overflow map is covered
HwangRock Aug 13, 2026
f287dad
io_uring: keep one copy of the write-lifecycle test helpers
HwangRock Aug 13, 2026
1b16fda
io_uring: give the iovec reference collector the same scope as the Io…
HwangRock Aug 13, 2026
1206b1e
io_uring: keep the write bookkeeping out of the hot methods' way
HwangRock Aug 13, 2026
0c30e87
io_uring: bring the last step of the collector's write path under the…
HwangRock Aug 13, 2026
2dd62d8
io_uring: release only the references a shutdown actually retained
HwangRock Aug 13, 2026
2d56ff0
io_uring: drop the collector's references once the slot has copied them
HwangRock Aug 13, 2026
ab051ea
io_uring: lift the last duplicated helpers into the tracker test base
HwangRock Aug 13, 2026
a621ffc
io_uring: keep the moved unit tests off the Java 8 test run
HwangRock Aug 14, 2026
33e8cc9
io_uring: reset the iovec collector from a finally instead of on entry
HwangRock Aug 18, 2026
63f4bb4
io_uring: size the slot arrays with MathUtil
HwangRock Aug 18, 2026
8fae036
io_uring: assert the write user_data narrowings before they happen
HwangRock Aug 18, 2026
8589326
io_uring: assert the expected exception instead of swallowing it
HwangRock Aug 18, 2026
eae125d
Merge branch '4.2' into io-uring-shutdown-output-inflight-write
normanmaurer Aug 18, 2026
5e56be2
io_uring: describe what the write bookkeeping actually does
HwangRock Aug 18, 2026
c47b0c1
io_uring: let the connect completion carry the full user_data
HwangRock Aug 18, 2026
84d933c
io_uring: do not let one failed release strand the rest
HwangRock Aug 18, 2026
8adae2b
Merge branch '4.2' into io-uring-shutdown-output-inflight-write
normanmaurer Aug 25, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Prev Previous commit
Next Next commit
io_uring: allocate write-operation ids from a free list
Motivation:

`nextWriteOperationId()` walked the shared `nextOpsId()` sequence and re-probed whenever the value it
landed on was still owned by a write awaiting its CQE. That is about one iteration in the common case,
but the bound is 65535 and it runs on the event loop.

Modification:

Give write operations their own sequence and a free list. An id goes back on the list when its terminal
CQE removes the operation, allocation pops from the list first, and the sequence is only asked for ids
it has never handed out. Nothing has to be searched, so allocation is O(1) in the worst case, and the
list grows with the number of writes in flight rather than with the id space.

The datagram sendmsg path registers `MsgHdrMemoryArray` indices, which that array allocates and recycles
itself. Those ids go through `retainUnpooledWriteOperation(...)` so they never enter the free list --
index 0 would otherwise be handed out as an operation id, and 0 means "no id".

Result:

Write-operation id allocation is constant time. Ids are also reused as soon as they are released, so the
range in use stays proportional to the writes in flight.
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@HwangRock
HwangRock committed Aug 9, 2026
commit 4f83b50e273a8af4dd8b6a59b983d7cac0fd62f4

Some comments aren't visible on the classic Files Changed page.

There are no files selected for viewing

69 changes: 58 additions & 11 deletions 69 transport-classes-io_uring/src/main/java/io/netty/channel/uring/AbstractIoUringChannel.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import io.netty.util.collection.IntObjectHashMap;
import io.netty.util.concurrent.PromiseNotifier;
import io.netty.util.internal.CleanableDirectBuffer;
import java.util.Arrays;
import io.netty.util.internal.StringUtil;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
Expand Down Expand Up @@ -86,8 +87,18 @@ abstract class AbstractIoUringChannel extends AbstractChannel implements UnixCha
private static final int READ_SCHEDULED = 1 << 5;
private static final int CONNECT_SCHEDULED = 1 << 6;

// Every non-zero short, as 0 is reserved for "no id".
private static final int MAX_WRITE_OPERATION_IDS = 65535;

private short opsId = Short.MIN_VALUE;

// Write-operation ids are allocated from their own sequence and recycled through freeWriteOperationIds once a
// terminal CQE releases them, so an allocation never has to search for a free value.
private short writeOpsId = Short.MIN_VALUE;
private IntObjectHashMap<WriteOperation> writeOperations;
private short[] freeWriteOperationIds;
private int freeWriteOperationIdCount;
private int issuedWriteOperationIds;

private long pollInId;
private long pollOutId;
Expand Down Expand Up @@ -206,32 +217,55 @@ protected final short nextOpsId() {
}

/**
* Returns an operation id that is not owned by a pending write CQE, or {@code 0} when all write-operation ids
* are in use.
* Returns a write-operation id that no in-flight write owns, or {@code 0} when every id is held by a write that
* has not seen its terminal CQE yet. Released ids come back through a free list, so this does not search.
*/
protected final short nextWriteOperationId() {
for (int attempts = 0; attempts < 65535; ++attempts) {
short id = nextOpsId();
if (writeOperations == null || !writeOperations.containsKey(id)) {
return id;
}
if (freeWriteOperationIdCount > 0) {
return freeWriteOperationIds[--freeWriteOperationIdCount];
}
return 0;
if (issuedWriteOperationIds == MAX_WRITE_OPERATION_IDS) {
// The sequence has handed out every id and none of them came back, so there is nothing to submit with.
return 0;
}
++issuedWriteOperationIds;
short id = writeOpsId++;
// We use 0 for "none".
if (id == 0) {
id = writeOpsId++;
}
return id;
}

/**
* Registers a write whose id came from {@link #nextWriteOperationId()}. The id is recycled once the terminal CQE
* arrives.
*/
final void retainWriteOperation(short id, byte opCode, ReferenceCounted... references) {
retainWriteOperation(id, opCode, true, references);
}

/**
* Registers a write whose id is owned by another allocator, such as the {@link MsgHdrMemoryArray} index used by
* the datagram sendmsg path. That id must not enter the free list.
*/
final void retainUnpooledWriteOperation(short id, byte opCode, ReferenceCounted... references) {
retainWriteOperation(id, opCode, false, references);
}

private void retainWriteOperation(short id, byte opCode, boolean recyclableId, ReferenceCounted... references) {
if (writeOperations == null) {
writeOperations = new IntObjectHashMap<>();
}
WriteOperation operation = new WriteOperation();
operation.retain(opCode, references);
operation.retain(opCode, recyclableId, references);
writeOperations.put(id, operation);
}

final void rollbackWriteOperation(short id, byte opCode) {
WriteOperation operation = writeOperation(id, opCode);
if (operation != null) {
writeOperations.remove(id);
removeWriteOperation(id, operation);
operation.rollback();
}
}
Expand All @@ -241,11 +275,24 @@ final void completeWriteOperation(short id, byte opCode, int flags) {
if (operation != null) {
operation.complete(flags);
if (operation.isDone()) {
writeOperations.remove(id);
removeWriteOperation(id, operation);
}
}
}

private void removeWriteOperation(short id, WriteOperation operation) {
writeOperations.remove(id);
if (!operation.hasRecyclableId()) {
return;
}
if (freeWriteOperationIds == null) {
freeWriteOperationIds = new short[8];
} else if (freeWriteOperationIdCount == freeWriteOperationIds.length) {
freeWriteOperationIds = Arrays.copyOf(freeWriteOperationIds, freeWriteOperationIdCount << 1);
}
freeWriteOperationIds[freeWriteOperationIdCount++] = id;
}

private WriteOperation writeOperation(short id, byte opCode) {
if (writeOperations == null) {
return null;
Expand Down
3 changes: 2 additions & 1 deletion 3 transport-classes-io_uring/src/main/java/io/netty/channel/uring/IoUringDatagramChannel.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,8 @@ private boolean scheduleSendmsg(ByteBuf data, InetSocketAddress remoteAddress, l
IoRegistration registration = registration();
IoUringIoOps ops = IoUringIoOps.newSendmsg(fd, (byte) 0, msgFlags, hdr.address(), hdr.idx());
short opsId = ops.data();
retainWriteOperation(opsId, ops.opcode(), data);
// The id is the MsgHdrMemoryArray index, which that array allocates and recycles itself.
retainUnpooledWriteOperation(opsId, ops.opcode(), data);
long id = registration.submit(ops);
if (id == 0) {
// Submission failed we don't used the MsgHdrMemory and so should give it back.
Expand Down
12 changes: 11 additions & 1 deletion 12 transport-classes-io_uring/src/main/java/io/netty/channel/uring/WriteOperation.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,30 @@

final class WriteOperation {
private byte opCode;
private boolean recyclableId;
private ReferenceCounted[] references;
private boolean done;

void retain(byte opCode, ReferenceCounted... references) {
void retain(byte opCode, boolean recyclableId, ReferenceCounted... references) {
if (this.references != null || done) {
throw new IllegalStateException("operation already completed or retained");
}
for (ReferenceCounted reference : references) {
reference.retain();
}
this.opCode = opCode;
this.recyclableId = recyclableId;
this.references = references;
}

/**
* Whether the id this operation was registered under belongs to the channel's write-operation sequence and can
* go back on its free list once the terminal CQE arrives.
*/
boolean hasRecyclableId() {
return recyclableId;
}

/**
* The opcode of the SQE that owns this operation. Write completions are keyed by the submitted
* {@code user_data}, which is not allocated from a single namespace: the splice stages and the
Expand Down
44 changes: 44 additions & 0 deletions 44 ...ort-native-io_uring/src/test/java/io/netty/channel/uring/IoUringWriteOperationIdTest.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

public class IoUringWriteOperationIdTest {
Expand Down Expand Up @@ -63,6 +64,49 @@ public void run(IoUringSocketChannel channel) {
});
}

@Test
@Timeout(value = 10, unit = TimeUnit.SECONDS)
public void terminalCompletionRecyclesTheWriteOperationId() throws Exception {
runOnEventLoop(new BookkeepingTask() {
@Override
public void run(IoUringSocketChannel channel) {
ByteBuf buffer = Unpooled.buffer(1).writeByte(1);
try {
short id = channel.nextWriteOperationId();
channel.retainWriteOperation(id, Native.IORING_OP_SEND, buffer);
channel.completeWriteOperation(id, Native.IORING_OP_SEND, 0);

assertEquals(1, buffer.refCnt());
assertEquals(id, channel.nextWriteOperationId());
} finally {
buffer.release();
}
}
});
}

@Test
@Timeout(value = 10, unit = TimeUnit.SECONDS)
public void anIdOwnedByAnotherAllocatorNeverEntersTheFreeList() throws Exception {
runOnEventLoop(new BookkeepingTask() {
@Override
public void run(IoUringSocketChannel channel) {
ByteBuf buffer = Unpooled.buffer(1).writeByte(1);
try {
// The datagram sendmsg path registers MsgHdrMemoryArray indices, which start at 0. Recycling
// one would hand out 0, the value reserved for "no id".
channel.retainUnpooledWriteOperation((short) 0, Native.IORING_OP_SENDMSG, buffer);
channel.completeWriteOperation((short) 0, Native.IORING_OP_SENDMSG, 0);

assertEquals(1, buffer.refCnt());
assertNotEquals(0, channel.nextWriteOperationId());
} finally {
buffer.release();
}
}
});
}

private interface BookkeepingTask {
void run(IoUringSocketChannel channel);
}
Expand Down
6 changes: 3 additions & 3 deletions 6 transport-native-io_uring/src/test/java/io/netty/channel/uring/WriteOperationTest.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void retainIncrementsRefCntAndNormalCompletionReleasesExactlyOnce() {
ByteBuf buffer = Unpooled.buffer();
WriteOperation operation = new WriteOperation();

operation.retain(Native.IORING_OP_SEND, buffer);
operation.retain(Native.IORING_OP_SEND, true, buffer);

assertEquals(Native.IORING_OP_SEND, operation.opCode());
assertEquals(2, buffer.refCnt());
Expand All @@ -48,7 +48,7 @@ void zeroCopyMoreCompletionDefersReleaseUntilNotification() {
ByteBuf first = Unpooled.buffer();
ByteBuf second = Unpooled.buffer();
WriteOperation operation = new WriteOperation();
operation.retain(Native.IORING_OP_SENDMSG_ZC, first, second);
operation.retain(Native.IORING_OP_SENDMSG_ZC, true, first, second);

operation.complete(Native.IORING_CQE_F_MORE);

Expand All @@ -70,7 +70,7 @@ void zeroCopyMoreCompletionDefersReleaseUntilNotification() {
void rollbackReleasesExactlyOnce() {
ByteBuf buffer = Unpooled.buffer();
WriteOperation operation = new WriteOperation();
operation.retain(Native.IORING_OP_SEND, buffer);
operation.retain(Native.IORING_OP_SEND, true, buffer);

// A failed submission never produces a CQE, so the rollback is the only release.
operation.rollback();
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402894 — 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.