🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: muc.lists.freebsd.bugs
4 messages
4 total messages Started by bugzilla-noreply Fri, 27 Nov 2026 23:33
[Bug 301208] fdalloc() increments fd_freefile only when fd equals fd_freefile, so refilling a closed low descriptor leaves fd_fr...
#818
Author: bugzilla-noreply
Date: Fri, 27 Nov 2026 23:33
97 lines
6416 bytes
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=301208

            Bug ID: 301208
           Summary: fdalloc() increments fd_freefile only when fd equals
                    fd_freefile, so refilling a closed low descriptor
                    leaves fd_freefile stuck behind the allocated run
           Product: Base System
           Version: 16.0-CURRENT
          Hardware: Any
                OS: Any
            Status: New
          Severity: Affects Only Me
          Priority: ---
         Component: kern
          Assignee: bugs@FreeBSD.org
          Reporter: laamalif@proton.me

fdalloc() in sys/kern/kern_descrip.c clamps minfd to fdp->fd_freefile,
scans the descriptor bitmap, and updates fd_freefile through fdused().
At main 2534dcb233ad:

        if (fdp->fd_freefile > minfd)
                minfd = fdp->fd_freefile;
        ...
        fd = fd_first_free(fdp, minfd, fdp->fd_nfiles);
        ...
        fdused(fdp, fd);

where fdused() is:

        fdused_init(fdp, fd);
        if (fd == fdp->fd_freefile)
                fdp->fd_freefile++;

When descriptors 0..N-1 are open (fd_freefile == N), closing a low
descriptor k sets fd_freefile = k, and refilling slot k increments
fd_freefile to k + 1 while k + 1..N-1 are still in use. On the next
fdalloc(td, 0, &fd), fd_first_free() scans k + 1..N-1 and returns
fd = N. Because N == k + 1 is false in fdused(), and N < k + 1 is also
false when slot N is later closed, fd_freefile remains stuck at k + 1
and every subsequent open(), dup(), or accept() rescans the bitmap from
k + 1 to N under FILEDESC_XLOCK.

On FreeBSD 16.0-CURRENT amd64 GENERIC-NODEBUG (main 2534dcb233ad, Xeon
6975P-C), timing 100000 dup(0) + close() pairs (ns/pair) with 131072
open descriptors after closing and refilling one low descriptor:

cc -O2 -Wall -Wextra -Werror -o fdalloc_freefile fdalloc_freefile.c
for i in $(jot 10); do cpuset -l 2 ./fdalloc_freefile reopen 131072 100000;
done

x base_reopen_131k.txt
+ patched_reopen_131k.txt
+--------------------------------------------------------------------------+
|+                                                                        x|
|+                                                                        x|
|+                                                                        x|
|+                                                                        x|
|+                                                                        x|
|+                                                                        x|
|+                                                                        x|
|+                                                                        x|
|+                                                                        x|
|+                                                                        x|
|A                                                                        A|
+--------------------------------------------------------------------------+
    N           Min           Max        Median           Avg        Stddev
x  10         696.9         697.4       697.145       697.158    0.13579396
+  10         167.3         167.6        167.52       167.506   0.093950341
Difference at 95.0% confidence
        -529.652 +/- 0.109709
        -75.973% +/- 0.00947806%
        (Student's t, pooled s = 0.116762)

At 32768 open descriptors (200000 pairs), mean latency drops from
302.51 ns to 167.29 ns (-44.70% +/- 0.07%), while sequential allocation
without a prior low close stays at 167.1 ns vs 167.3 ns. Under debug
GENERIC (INVARIANTS and WITNESS, plus kern.racct.enable=1), fcntl(0,
F_DUPFD, high) with minfd > fd_freefile, table-growth boundaries, RACCT
and RLIMIT_NOFILE EMFILE recovery, 8-thread concurrent descriptor churn
in fdalloc_stress.c, and Kyua's dup_test and fdgrowtable_test all pass.

Advancing fdp->fd_freefile to fd + 1 after fdused_init() whenever
minfd == fdp->fd_freefile restores the lower-bound invariant once
fd_first_free() has verified that [0, fd) are allocated.

Source inspection shows the same fdalloc() and fdused() logic on
stable/15 and stable/14; I have only runtime-tested 16.0-CURRENT amd64.

-- 
You are receiving this mail because:
You are the assignee for the bug.

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
[Bug 301208] fdalloc() increments fd_freefile only when fd equals fd_freefile, so refilling a closed low descriptor leaves fd_fr...
#820
Author: bugzilla-noreply
Date: Fri, 27 Nov 2026 23:33
17 lines
2598 bytes
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=301208

--- Comment #1 from laamalif@proton.me ---
Created attachment 277840
  --> https://bugs.freebsd.org/bugzilla/attachment.cgi?id=277840&action=edit
advance fd_freefile to fd + 1 after fdused_init() when minfd ==
fdp->fd_freefile

-- 
You are receiving this mail because:
You are the assignee for the bug.

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
[Bug 301208] fdalloc() increments fd_freefile only when fd equals fd_freefile, so refilling a closed low descriptor leaves fd_fr...
#819
Author: bugzilla-noreply
Date: Fri, 27 Nov 2026 23:33
17 lines
2625 bytes
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=301208

--- Comment #2 from laamalif@proton.me ---
Created attachment 277841
  --> https://bugs.freebsd.org/bugzilla/attachment.cgi?id=277841&action=edit
benchmark: dup(0) + close() latency after closing and refilling a low
descriptor (reopen) vs sequential control

-- 
You are receiving this mail because:
You are the assignee for the bug.

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
[Bug 301208] fdalloc() increments fd_freefile only when fd equals fd_freefile, so refilling a closed low descriptor leaves fd_fr...
#821
Author: bugzilla-noreply
Date: Fri, 27 Nov 2026 23:33
18 lines
2659 bytes
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=301208

--- Comment #3 from laamalif@proton.me ---
Created attachment 277842
  --> https://bugs.freebsd.org/bugzilla/attachment.cgi?id=277842&action=edit
validation: table-growth boundaries (20, 64, 128, 256) and 8-thread concurrent
open/dup/F_DUPFD/accept/close + fork check; build with -pthread

-- 
You are receiving this mail because:
You are the assignee for the bug.

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads