NetBSD Problem Report #60729

From www@netbsd.org  Mon Sep 14 22:25:53 2026
Return-Path: <www@netbsd.org>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256
	 client-signature RSA-PSS (2048 bits) client-digest SHA256)
	(Client CN "mail.netbsd.org", Issuer "YR1" (not verified))
	by mollari.NetBSD.org (Postfix) with ESMTPS id E2C0F1A923B
	for <gnats-bugs@gnats.NetBSD.org>; Mon, 14 Sep 2026 22:25:52 +0000 (UTC)
Message-Id: <20260914222551.5DB951A923C@mollari.NetBSD.org>
Date: Mon, 14 Sep 2026 22:25:51 +0000 (UTC)
From: uwe@stderr.spb.ru
Reply-To: uwe@stderr.spb.ru
To: gnats-bugs@NetBSD.org
Subject: incosistent st_ino from the first stat(2) on a socket
X-Send-Pr-Version: www-1.0
X-From4GNATS: "uwe@stderr.spb.ru via gnats" <gnats-admin@NetBSD.org>

>Number:         60729
>Notify-List:    riastradh
>Category:       kern
>Synopsis:       incosistent st_ino from the first stat(2) on a socket
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    kern-bug-people
>State:          needs-pullups
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Mon Sep 14 22:30:00 +0000 2026
>Closed-Date:    
>Last-Modified:  Sat Sep 19 17:25:00 +0000 2026
>Originator:     Valery Ushakov
>Release:        NetBSD 11.99
>Organization:
>Environment:
>Description:
Came across this blog post on lobsters:

https://yuvalino.com/how-can-you-not-be-romantic-about-unix-domain-sockets

The absolutely first call of stat(2) on a socket returns st_ino of zero.
The next call of stat(2) on the same socket will return a different st_ino.
>How-To-Repeat:
# cat sockino.c
#include <sys/socket.h>
#include <sys/stat.h>

#include <err.h>
#include <stdio.h>
#include <stdlib.h>


int
main()
{
    int status;

    int sock[2];
    status = socketpair(PF_UNIX, SOCK_DGRAM, 0, sock);
    if (status < 0)
	err(EXIT_FAILURE, "socketpair");

    struct stat st;

    status = fstat(sock[0], &st);
    if (status < 0)
	err(EXIT_FAILURE, "fstat");

    printf("%lld/%llu\n",
	   (signed long long)st.st_dev,
	   (unsigned long long)st.st_ino);

    status = fstat(sock[0], &st);
    if (status < 0)
	err(EXIT_FAILURE, "fstat");

    printf("%lld/%llu\n",
	   (signed long long)st.st_dev,
	   (unsigned long long)st.st_ino);

    return 0;
}
# ./sockino 
-1/0
-1/1
# ./sockino 
-1/2
-1/2

>Fix:
A comment on lobsters

https://lobste.rs/s/uauns9/how_can_you_not_be_romantic_about_unix

pointed to

https://cgit.freebsd.org/src/commit/sys/kern/uipc_usrreq.c?id=6f782c4636672e40c45ef9c9f17635e6c110d290
https://cgit.freebsd.org/src/commit/sys/kern/uipc_usrreq.c?id=f218ac5087c7697321d3866d68c037cc88b0b005

>Release-Note:

>Audit-Trail:
From: Taylor R Campbell <riastradh@NetBSD.org>
To: uwe@NetBSD.org
Cc: gnats-bugs@NetBSD.org, netbsd-bugs@NetBSD.org
Subject: Re: kern/60729: incosistent st_ino from the first stat(2) on a socket
Date: Tue, 15 Sep 2026 16:49:49 +0000

 This is a multi-part message in MIME format.
 --=_yZfuFEUR5sBHB86OQy7idKn1uESPEWyt

 Try attached?

 --=_yZfuFEUR5sBHB86OQy7idKn1uESPEWyt
 Content-Type: text/plain; charset="ISO-8859-1"; name="pr60729-unixsockino"
 Content-Transfer-Encoding: quoted-printable
 Content-Disposition: attachment; filename="pr60729-unixsockino.patch"

 # HG changeset patch
 # User Taylor R Campbell <riastradh@NetBSD.org>
 # Date 1789490537 0
 #      Tue Sep 15 16:42:17 2026 +0000
 # Branch trunk
 # Node ID 3fe1ba99ddf50e054ee728621b78ed85d138cc5d
 # Parent  4fbd288ac1da9e7d9592ddecf04f7513e244152f
 # EXP-Topic riastradh-pr60729-unixsockino
 uipc_usrreq: Fix unix-domain socket inode assignment.

 1. `if (unp->unp_ino =3D=3D 0) unp->unp_ino =3D unp_ino++' would assign
    zero the first time around by mistake, and then assign nonzero
    after that.  `if (unp->unp_ino =3D=3D 0) unp->unp_ino =3D ++unp_ino'
    avoids this silliness.

 2. Make the ++unp_ino part atomic.

 PR kern/60729: incosistent st_ino from the first stat(2) on a socket

 diff -r 4fbd288ac1da -r 3fe1ba99ddf5 sys/kern/uipc_usrreq.c
 --- a/sys/kern/uipc_usrreq.c	Mon Aug 10 13:36:10 2026 +0000
 +++ b/sys/kern/uipc_usrreq.c	Tue Sep 15 16:42:17 2026 +0000
 @@ -179,7 +179,16 @@ const struct sockaddr_un sun_noname =3D {
  	.sun_len =3D offsetof(struct sockaddr_un, sun_path),
  	.sun_family =3D AF_LOCAL,
  };
 -ino_t	unp_ino;			/* prototype for fake inode numbers */
 +
 +/* prototype for fake inode numbers */
 +#ifdef __HAVE_ATOMIC64_OPS
 +volatile uint64_t	unp_ino __cacheline_aligned;
 +#else
 +struct {
 +	uint64_t	num;
 +	kmutex_t	lock;
 +} unp_ino __cacheline_aligned;
 +#endif
 =20
  static struct mbuf * unp_addsockcred(struct lwp *, struct mbuf *);
  static void   unp_discard_later(file_t *);
 @@ -233,6 +242,10 @@ uipc_init(void)
  	    NULL, &unp_thread_lwp, "unpgc");
  	if (error !=3D 0)
  		panic("uipc_init %d", error);
 +
 +#ifndef __HAVE_ATOMIC64_OPS
 +	mutex_init(&unp_ino.lock, MUTEX_DEFAULT, IPL_NONE);
 +#endif
  }
 =20
  static void
 @@ -906,8 +919,15 @@ unp_stat(struct socket *so, struct stat=20
  		break;
  	}
  	ub->st_dev =3D NODEV;
 -	if (unp->unp_ino =3D=3D 0)
 -		unp->unp_ino =3D unp_ino++;
 +	if (unp->unp_ino =3D=3D 0) {
 +#ifdef __HAVE_ATOMIC64_OPS
 +		unp->unp_ino =3D atomic_inc_64_nv(&unp_ino);
 +#else
 +		mutex_enter(&unp_ino.lock);
 +		unp->unp_ino =3D ++unp_ino.num;
 +		mutex_exit(&unp_ino.lock);
 +#endif
 +	}
  	ub->st_atimespec =3D ub->st_mtimespec =3D ub->st_ctimespec =3D unp->unp_c=
 time;
  	ub->st_ino =3D unp->unp_ino;
  	ub->st_uid =3D so->so_uidinfo->ui_uid;

 --=_yZfuFEUR5sBHB86OQy7idKn1uESPEWyt--

State-Changed-From-To: open->feedback
State-Changed-By: riastradh@NetBSD.org
State-Changed-When: Tue, 15 Sep 2026 16:56:28 +0000
State-Changed-Why:
patch posted


From: Valery Ushakov <uwe@stderr.spb.ru>
To: gnats-bugs@NetBSD.org
Cc: Taylor R Campbell <riastradh@NetBSD.org>
Subject: Re: kern/60729: incosistent st_ino from the first stat(2) on a socket
Date: Tue, 15 Sep 2026 22:11:09 +0300

 On Tue, Sep 15, 2026 at 16:49:49 +0000, Taylor R Campbell wrote:

 > Try attached?

 Alas, I don't have bandwith to test.  I just dropped a quick PR when I
 read the blog and verified we have it.  Just commit it (I trust you
 did test it...).

 Thanks!

 -uwe

State-Changed-From-To: feedback->needs-pullups
State-Changed-By: riastradh@NetBSD.org
State-Changed-When: Thu, 17 Sep 2026 01:22:35 +0000
State-Changed-Why:
fixed in HEAD, needs pullup-11, pullup-10


From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/60729 CVS commit: src/sys/kern
Date: Thu, 17 Sep 2026 01:21:34 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Thu Sep 17 01:21:34 UTC 2026

 Modified Files:
 	src/sys/kern: uipc_usrreq.c

 Log Message:
 uipc_usrreq: Fix unix-domain socket inode assignment.

 1. `if (unp->unp_ino == 0) unp->unp_ino = unp_ino++' would assign
    zero the first time around by mistake, and then assign nonzero
    after that.  `if (unp->unp_ino == 0) unp->unp_ino = ++unp_ino'
    avoids this silliness.

 2. Make the ++unp_ino part atomic.

 PR kern/60729: incosistent st_ino from the first stat(2) on a socket


 To generate a diff of this commit:
 cvs rdiff -u -r1.209 -r1.210 src/sys/kern/uipc_usrreq.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/60729 CVS commit: src/sys/rump/librump/rumpkern
Date: Fri, 18 Sep 2026 14:26:12 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Fri Sep 18 14:26:12 UTC 2026

 Modified Files:
 	src/sys/rump/librump/rumpkern: atomic_cas_generic.c

 Log Message:
 rump/atomic_cas_*: Use __HAVE_ATOMIC64_OPS, not _LP64.

 Should fix rump build on hppa since a new use of atomic_inc_64 under
 __HAVE_ATOMIC64_OPS sprouted a few days ago:

 /home/builds/ab/HEAD/hppa/20260917233410Z-tools/lib/gcc/hppa--netbsd/14.3.0/../../../../hppa--netbsd/bin/ld: /home/builds/ab/HEAD/hppa/20260917233410Z-dest/usr/lib/librumpnet_local.so: undefined reference to `rumpns_atomic_inc_64_nv'

 Followup for:

 PR kern/60729: incosistent st_ino from the first stat(2) on a socket


 To generate a diff of this commit:
 cvs rdiff -u -r1.3 -r1.4 src/sys/rump/librump/rumpkern/atomic_cas_generic.c

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/60729 CVS commit: src/common/lib/libc/arch/hppa/atomic
Date: Fri, 18 Sep 2026 15:02:53 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Fri Sep 18 15:02:53 UTC 2026

 Modified Files:
 	src/common/lib/libc/arch/hppa/atomic: Makefile.inc

 Log Message:
 hppa: Add some rumplish 64-bit atomics to librump.so.

 Should really fix rump build on hppa since a new use of atomic_inc_64
 under __HAVE_ATOMIC64_OPS sprouted a few days ago:

 /home/builds/ab/HEAD/hppa/20260917233410Z-tools/lib/gcc/hppa--netbsd/14.3.0/../../../../hppa--netbsd/bin/ld: /home/builds/ab/HEAD/hppa/20260917233410Z-dest/usr/lib/librumpnet_local.so: undefined reference to `rumpns_atomic_inc_64_nv'

 Followup for:

 PR kern/60729: incosistent st_ino from the first stat(2) on a socket


 To generate a diff of this commit:
 cvs rdiff -u -r1.17 -r1.18 src/common/lib/libc/arch/hppa/atomic/Makefile.inc

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/60729 CVS commit: src
Date: Sat, 19 Sep 2026 17:24:11 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Sat Sep 19 17:24:11 UTC 2026

 Modified Files:
 	src/common/lib/libc/arch/hppa/atomic: Makefile.inc
 	src/sys/arch/hppa/include: types.h

 Log Message:
 hppa: Limit __HAVE_ATOMIC64_OPS to userland.

 And revert changes to include 64-bit atomic r/m/w operations in the
 rump build, since they're not available in the hard kernel anyway.

 The 64-bit CAS logic relies on 64-bit floating-point load/store (plus
 RAS or hash-locking), so it doesn't work in the kernel.

 Further followup to fix the hppa build after:

 PR kern/60729: incosistent st_ino from the first stat(2) on a socket


 To generate a diff of this commit:
 cvs rdiff -u -r1.18 -r1.19 src/common/lib/libc/arch/hppa/atomic/Makefile.inc
 cvs rdiff -u -r1.35 -r1.36 src/sys/arch/hppa/include/types.h

 Please note that diffs are not public domain; they are subject to the
 copyright notices on the relevant files.

>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.51 2026/08/10 02:28:17 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.