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 (#17… · netty/netty@ef66d7a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • transport-classes-io_uring/src
    • main/java/io/netty/channel/uring
      • AbstractIoUringChannel.java
      • AbstractIoUringServerChannel.java
      • AbstractIoUringStreamChannel.java
      • IoUringDatagramChannel.java
      • IoUringDomainSocketChannel.java
      • IoUringIoHandler.java
      • IoUringIoOps.java
      • IoUringSocketChannel.java
      • IovArrayReferenceCollector.java
      • WriteOperation.java
      • WriteOperationTracker.java
    • test/java/io/netty/channel/uring
      • IoUringIoOpsTest.java
      • WriteOperationTest.java
  • transport-native-io_uring/src/test/java/io/netty/channel/uring
    • AbstractWriteOperationTrackerTest.java
    • IoUringDatagramWriteLifecycleTest.java
    • IoUringRefCntZeroAwaiter.java
    • IoUringShutdownOutputDuringInFlightSpliceTest.java
    • IoUringShutdownOutputDuringInFlightWriteTest.java
    • IoUringSocketSendSzSendmsgZcTest.java
    • IovArrayReferenceCollectorTest.java
    • WriteOperationTrackerOverflowTest.java
    • WriteOperationTrackerTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: transport-classes-io_uring/src/main/java/io/netty/channel/uring/AbstractIoUringChannel.java
+74-23Lines changed: 74 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@
7474
abstract class AbstractIoUringChannel extends AbstractChannel implements UnixChannel {
7575
private static final InternalLogger logger = InternalLoggerFactory.getInstance(AbstractIoUringChannel.class);
7676
final LinuxSocket socket;
77+
// Owns every in-flight write operation this channel is tracking -- the pooled slot array, the overflow map,
78+
// the foreign slot array, and the single stream slot. See WriteOperationTracker for the four namespaces.
79+
final WriteOperationTracker writeTracker = new WriteOperationTracker();
7780
protected volatile boolean active;
7881

7982
// Different masks for outstanding I/O operations.
@@ -314,6 +317,24 @@ protected void doClose() throws Exception {
314317
}
315318
}
316319

320+
/**
321+
* Retains every in-flight write's references before handing off to {@link #doShutdownOutput0()}, so a write
322+
* completion that races the shutdown still finds a live reference to release instead of one the outbound buffer
323+
* already dropped.
324+
*/
325+
@Override
326+
protected final void doShutdownOutput() throws Exception {
327+
writeTracker.retainAll();
328+
doShutdownOutput0();
329+
}
330+
331+
/**
332+
* Performs the actual output shutdown. Overridden by subclasses that support it.
333+
*/
334+
protected void doShutdownOutput0() throws Exception {
335+
super.doShutdownOutput();
336+
}
337+
317338
@Override
318339
protected final void doBeginRead() {
319340
if (inputClosedSeenErrorOnRead) {
@@ -385,17 +406,23 @@ private int scheduleWrite(ChannelOutboundBuffer in) {
385406
}
386407
Object msg = in.current();
387408

409+
int scheduled;
388410
if (msgCount > 1 && in.current() instanceof ByteBuf) {
389-
numOutstandingWrites = (short) ioUringUnsafe().scheduleWriteMultiple(in);
411+
scheduled = ioUringUnsafe().scheduleWriteMultiple(in);
390412
} else if (msg instanceof ByteBuf && ((ByteBuf) msg).nioBufferCount() > 1 ||
391413
(msg instanceof ByteBufHolder && ((ByteBufHolder) msg).content().nioBufferCount() > 1)) {
392414
// We also need some special handling for CompositeByteBuf
393-
numOutstandingWrites = (short) ioUringUnsafe().scheduleWriteMultiple(in);
415+
scheduled = ioUringUnsafe().scheduleWriteMultiple(in);
394416
} else {
395-
numOutstandingWrites = (short) ioUringUnsafe().scheduleWriteSingle(msg);
396-
}
397-
// Ensure we never overflow
398-
assert numOutstandingWrites > 0;
417+
scheduled = ioUringUnsafe().scheduleWriteSingle(msg);
418+
}
419+
// A zero return means the write could not be scheduled: registration.submit(...) failed because the
420+
// registration is no longer valid, a FileRegion's open() threw, or transferTo(...) produced no bytes or
421+
// threw. numOutstandingWrites is a short, so guard the narrowing before it happens: a future
422+
// scheduleWriteSingle/scheduleWriteMultiple override that batches more writes than a short can hold must
423+
// not silently wrap around.
424+
assert scheduled <= Short.MAX_VALUE;
425+
numOutstandingWrites = (short) scheduled;
399426
return numOutstandingWrites;
400427
}
401428

@@ -432,6 +459,15 @@ final void resetCachedAddresses() {
432459
remote = socket.remoteAddress();
433460
}
434461

462+
// Write completions may carry an id that fell back out of the short range, so they get the untruncated
463+
// user_data. Connect completions always submit a short id, but connectComplete(...) takes a long to stay
464+
// consistent with writeComplete(...), so it is also handed the untruncated value. The remaining completions
465+
// (read, poll, cancel) only ever submit short ids, so narrowUserData(...) below asserts and narrows those.
466+
private static short narrowUserData(long userData) {
467+
assert userData == (short) userData : "user_data does not fit a short: " + userData;
468+
return (short) userData;
469+
}
470+
435471
protected abstract class AbstractUringUnsafe extends AbstractUnsafe implements IoUringIoHandle {
436472
private IoUringRecvByteAllocatorHandle allocHandle;
437473
private boolean closed;
@@ -440,13 +476,13 @@ protected abstract class AbstractUringUnsafe extends AbstractUnsafe implements I
440476

441477
/**
442478
* Schedule the write of multiple messages in the {@link ChannelOutboundBuffer} and returns the number of
443-
* {@link #writeComplete(byte, int, int, short)} calls that are expected because of the scheduled write.
479+
* {@link #writeComplete(byte, int, int, long)} calls that are expected because of the scheduled write.
444480
*/
445481
protected abstract int scheduleWriteMultiple(ChannelOutboundBuffer in);
446482

447483
/**
448484
* Schedule the write of a single message and returns the number of
449-
* {@link #writeComplete(byte, int, int, short)} calls that are expected because of the scheduled write.
485+
* {@link #writeComplete(byte, int, int, long)} calls that are expected because of the scheduled write.
450486
*/
451487
protected abstract int scheduleWriteSingle(Object msg);
452488

@@ -456,13 +492,13 @@ public final void handle(IoRegistration registration, IoEvent ioEvent) {
456492
byte op = event.opcode();
457493
int res = event.res();
458494
int flags = event.flags();
459-
short data = (short) event.userData();
495+
long userData = event.userData();
460496
switch (op) {
461497
case Native.IORING_OP_RECV:
462498
case Native.IORING_OP_ACCEPT:
463499
case Native.IORING_OP_RECVMSG:
464500
case Native.IORING_OP_READ:
465-
readComplete(op, res, flags, data);
501+
readComplete(op, res, flags, narrowUserData(userData));
466502
break;
467503
case Native.IORING_OP_WRITEV:
468504
case Native.IORING_OP_SEND:
@@ -471,16 +507,16 @@ public final void handle(IoRegistration registration, IoEvent ioEvent) {
471507
case Native.IORING_OP_SPLICE:
472508
case Native.IORING_OP_SEND_ZC:
473509
case Native.IORING_OP_SENDMSG_ZC:
474-
writeComplete(op, res, flags, data);
510+
writeComplete(op, res, flags, userData);
475511
break;
476512
case Native.IORING_OP_POLL_ADD:
477-
pollAddComplete(res, flags, data);
513+
pollAddComplete(res, flags, narrowUserData(userData));
478514
break;
479515
case Native.IORING_OP_ASYNC_CANCEL:
480-
cancelComplete0(op, res, flags, data);
516+
cancelComplete0(op, res, flags, narrowUserData(userData));
481517
break;
482518
case Native.IORING_OP_CONNECT:
483-
connectComplete(op, res, flags, data);
519+
connectComplete(op, res, flags, userData);
484520

485521
// once the connect was completed we can also free some resources that are not needed anymore.
486522
freeMsgHdrArray();
@@ -512,6 +548,7 @@ public final void handle(IoRegistration registration, IoEvent ioEvent) {
512548
public void unregistered() {
513549
freeMsgHdrArray();
514550
freeRemoteAddressMemory();
551+
writeTracker.releaseAll();
515552

516553
// Check if we need to notify about the deregistration.
517554
if (deregisterPromise != null) {
@@ -971,14 +1008,20 @@ private void pollOut(int res) {
9711008
* @param flags the flags.
9721009
* @param data the data that was passed when submitting the op.
9731010
*/
974-
private void writeComplete(byte op, int res, int flags, short data) {
1011+
private void writeComplete(byte op, int res, int flags, long data) {
1012+
writeTracker.complete(data, op, flags);
9751013
if ((ioState & CONNECT_SCHEDULED) != 0) {
9761014
// The writeComplete(...) callback was called because of a sendmsg(...) result that was used for
9771015
// TCP_FASTOPEN_CONNECT.
9781016
freeMsgHdrArray();
9791017
if (res > 0) {
9801018
// Connect complete!
981-
outboundBuffer().removeBytes(res);
1019+
// The completion may arrive after close() or shutdownOutput() already dropped the
1020+
// outbound buffer, in which case there is nothing left to remove.
1021+
ChannelOutboundBuffer channelOutboundBuffer = outboundBuffer();
1022+
if (channelOutboundBuffer != null) {
1023+
channelOutboundBuffer.removeBytes(res);
1024+
}
9821025

9831026
// Explicit pass in 0 as this is returned by a connect(...) call when it was successful.
9841027
connectComplete(op, 0, flags, data);
@@ -1027,7 +1070,7 @@ private void writeComplete(byte op, int res, int flags, short data) {
10271070
* @param data the data that was passed when submitting the op.
10281071
* @param outstanding the outstanding write completions.
10291072
*/
1030-
abstract boolean writeComplete0(byte op, int res, int flags, short data, int outstanding);
1073+
abstract boolean writeComplete0(byte op, int res, int flags, long data, int outstanding);
10311074

10321075
/**
10331076
* Called once a cancel was completed.
@@ -1048,7 +1091,7 @@ void cancelComplete0(byte op, int res, int flags, short data) {
10481091
* @param flags the flags.
10491092
* @param data the data that was passed when submitting the op.
10501093
*/
1051-
void connectComplete(byte op, int res, int flags, short data) {
1094+
void connectComplete(byte op, int res, int flags, long data) {
10521095
ioState &= ~CONNECT_SCHEDULED;
10531096
freeRemoteAddressMemory();
10541097

@@ -1134,12 +1177,20 @@ public void connect(
11341177

11351178
int fd = fd().intValue();
11361179
IoRegistration registration = registration();
1137-
IoUringIoOps ops = IoUringIoOps.newSendmsg(fd, (byte) 0, Native.MSG_FASTOPEN,
1138-
hdr.address(), hdr.idx());
1139-
connectId = registration.submit(ops);
1140-
if (connectId == 0) {
1141-
// Directly release the memory if submitting failed.
1180+
short opsId = writeTracker.nextId();
1181+
if (opsId == 0) {
11421182
freeMsgHdrArray();
1183+
submitConnect(inetSocketAddress);
1184+
} else {
1185+
IoUringIoOps ops = IoUringIoOps.newSendmsg(fd, (byte) 0, Native.MSG_FASTOPEN,
1186+
hdr.address(), opsId);
1187+
writeTracker.record(opsId, ops.opcode(), initialData);
1188+
connectId = registration.submit(ops);
1189+
if (connectId == 0) {
1190+
writeTracker.abandon(opsId, ops.opcode());
1191+
// Directly release the memory if submitting failed.
1192+
freeMsgHdrArray();
1193+
}
11431194
}
11441195
} else {
11451196
submitConnect(inetSocketAddress);
Collapse file

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

Copy file name to clipboardExpand all lines: transport-classes-io_uring/src/main/java/io/netty/channel/uring/AbstractIoUringServerChannel.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ protected int scheduleWriteSingle(Object msg) {
147147
}
148148

149149
@Override
150-
boolean writeComplete0(byte op, int res, int flags, short data, int outstanding) {
150+
boolean writeComplete0(byte op, int res, int flags, long data, int outstanding) {
151151
throw new UnsupportedOperationException();
152152
}
153153

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.