🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

[Bug 301487] vfs: missing release ordering in file_v_unlock() slow path

#908
From: bugzilla-noreply
Date: Fri, 04 Dec 2026 09:55
63 lines
4734 bytes
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=301487

            Bug ID: 301487
           Summary: vfs: missing release ordering in file_v_unlock() slow
                    path
           Product: Base System
           Version: Unspecified
          Hardware: arm64
                OS: Any
            Status: New
          Severity: Affects Only Me
          Priority: ---
         Component: kern
          Assignee: bugs@FreeBSD.org
          Reporter: hanguidong02@gmail.com

In sys/kern/vfs_vnops.c, file_v_unlock() releases lock_bit on the fast path
with atomic_fcmpset_rel_16(), pairing with atomic_fcmpset_acq_16() in
file_v_lock(). However, when lock_wait_bit is set, the slow path clears
lock_bit | lock_wait_bit with relaxed atomic_clear_16() under sleepq_lock().

Because sleepq_lock() only provides acquire semantics, prior stores before
file_v_unlock() can be reordered after the relaxed atomic_clear_16() on
weak-memory architectures (e.g., arm64, riscv, powerpc), while a concurrent
locker on the fast path of file_v_lock() does not take sleepq_lock():

  /* lock_wait_bit is already set by a sleeping waiter */

  CPU 0                                CPU 1
  -----                                -----
  foffset_unlock()                     foffset_lock()
    atomic_store_long(&fp->f_offset, v);
    file_v_unlock()                      file_v_lock() /* fast path */
      sleepq_lock(flagsp); /* acq */       /* does not take sleepq_lock */
      atomic_clear_16(flagsp,       --->   atomic_fcmpset_acq_16(flagsp,
          lock_bit | lock_wait_bit);           &state, state | lock_bit);
                                         atomic_load_long(&fp->f_offset);
                                           /* may read stale f_offset */
    /* f_offset store becomes visible */

Impact, affected versions, and fix:
Concurrent read()/write() sharing the same struct file can observe a stale
fp->f_offset, causing duplicate reads or overwritten writes (and similarly
stale F_SETFL state via fsetfl_unlock()). This affects main, stable/15, and
stable/14. Changing atomic_clear_16() to atomic_clear_rel_16() in
file_v_unlock() fixes the ordering.

I have recently been hunting weak-memory ordering bugs of this kind in OS
kernels (here is a similar bug I found and reproduced in Linux:
https://lore.kernel.org/all/CALbr=LaOMuba+oQ6W7A93VBJB+wSaV7knhL2Axvgi-ao9n6QJw@mail.gmail.com/).
I am also looking into reproducing this race in QEMU and will follow up if I
get a reproducer, though weak-memory ordering races are hard to trigger in
practice and the issue seems clear from the code itself.

-- 
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-301487-227@https.bugs.freebsd.org/bugzilla/>
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!3.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!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!3.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 301487] vfs: missing release ordering in file_v_unlock() slow
 path
Date: Fri, 04 Dec 2026 09:55:07 +0000
Organization: Newsgate at muc.de e.V.
Approved: news-admin@muc.de
Distribution: world
Message-ID: <bug-301487-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="88474"; 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=1796378107;
	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=9M6jVyEaihIkBHZW7MQntUhlG7/mgTJJDTnGI1YlYi0=;
	b=Hy8Jof2C7wcmzEeOMi9hwESJw9YXKcMn+vvx5XK9nQdx6kUoIktZ7zpMeHAhsts8kevwNd
	YHwmW2VUKB0wkfxAJYNof6sqT17PDvxN2uTImvrpctss3QA/gjPAH9xgPgB/KZL28lqu+j
	oCxPntNYr82SRlI8EyXqURQF6AxjCd0mDBagkFyjJ5jLX7wrK+glKgehAd4+a3bPWMG/nF
	8FjxZ9BjoVvKwOmNLlCE8sTtbLzB33cdiSUE0fomPvEifHh0yUp8zdrEzYXZ2zu8cA78qC
	+Rxypz+TJ8R38Jxt1MpqR6fOMx7dqxc98ZUNb89W7Bqvq8NXVQA8bZy3rgH9uA==
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