🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: muc.lists.freebsd.bugs
Article #818

[Bug 301208] fdalloc() increments fd_freefile only when fd equals fd_freefile, so refilling a closed low descriptor leaves fd_fr...

#818
From: 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
Technical Details
Message-ID: <bug-301208-227@https.bugs.freebsd.org/bugzilla/>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!newsfeed.bofh.team!weretis.net!feeder8.news.weretis.net!2.eu.feeder.erje.net!feeder.erje.net!news2.arglkargh.de!news.karotte.org!news.muc.de!.POSTED.news.muc.de!not-for-mail

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!newsfeed.bofh.team!weretis.net!feeder8.news.weretis.net!2.eu.feeder.erje.net!feeder.erje.net!news2.arglkargh.de!news.karotte.org!news.muc.de!.POSTED.news.muc.de!not-for-mail
From: bugzilla-noreply@freebsd.org
Newsgroups: muc.lists.freebsd.bugs
Subject: [Bug 301208] fdalloc() increments fd_freefile only when fd equals
 fd_freefile, so refilling a closed low descriptor leaves fd_freefile stuck
 behind the allocated run
Date: Fri, 27 Nov 2026 23:33:11 +0000
Organization: Newsgate at muc.de e.V.
Approved: news-admin@muc.de
Distribution: world
Message-ID: <bug-301208-227@https.bugs.freebsd.org/bugzilla/>
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Injection-Info: news.muc.de; posting-host="news.muc.de:193.149.48.2";
	logging-data="83776"; mail-complaints-to="news-admin@muc.de"
X-Original-To: bugs@mlmmj.nyi.freebsd.org
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim;
	t=1795822390;
	h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
	 to:to:cc:mime-version:mime-version:content-type:content-type:
	 content-transfer-encoding:content-transfer-encoding;
	bh=W0UOq0vWKxJ2lyOcgUFwFACC81gEzafCFI7z14qrBCw=;
	b=OCBggJTid4gtIMS2vUPqfTC8nJFUyNUVnV2tE3ACy1tAIRy0GrFxtT+us96ag/IayW/Kg0
	rm0KeMxizQgLWaISX72bjkI5ODbX/iI3o5UnoaiwyB0BHOgMwIlOp34zmIcVYRQYqR0YWN
	C6Y9bjn9gbOv+FLVhDhbG5PlVttTVjFm4358iuSWIJSBl54EpX+jZv19oS7Tk8FKRldLHb
	pG8EmjV3Hv/kOyAdSQASvHX3e/R5o7/maIzsa4CeFLUo9Ld4A/33NHAjEcV78/2Tf+DnxR
	+wS/Z/5tijlJ0b/J4YRB/H2Ph1f6/LytZhTSehvSP3/6w1ratmu2VLnKc2TyAg==
X-Authentication-Warning: kenobi.freebsd.org: www set sender to bugzilla-noreply@freebsd.org using -f
Auto-Submitted: auto-generated
Precedence: list
X-Newsgate-Id: f02c70b8849f+
X-No-Archive: Yes