NetBSD Problem Report #60393

From www@netbsd.org  Thu Jul  2 13:59:08 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)
	 client-signature RSA-PSS (2048 bits))
	(Client CN "mail.netbsd.org", Issuer "R13" (not verified))
	by mollari.NetBSD.org (Postfix) with ESMTPS id 137F91A923A
	for <gnats-bugs@gnats.NetBSD.org>; Thu,  2 Jul 2026 13:59:08 +0000 (UTC)
Message-Id: <20260702135906.DBF891A9243@mollari.NetBSD.org>
Date: Thu,  2 Jul 2026 13:59:06 +0000 (UTC)
From: campbell+netbsd@mumble.net
Reply-To: campbell+netbsd@mumble.net
To: gnats-bugs@NetBSD.org
Subject: hash-locked atomic_store_* is all kinds of busted
X-Send-Pr-Version: www-1.0
X-From4GNATS: "campbell+netbsd@mumble.net via gnats" <gnats-admin@NetBSD.org>

>Number:         60393
>Category:       kern
>Synopsis:       hash-locked atomic_store_* is all kinds of busted
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    kern-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Jul 02 14:00:00 +0000 2026
>Last-Modified:  Thu Jul 02 14:40:01 +0000 2026
>Originator:     Taylor R Campbell
>Release:        current, 11, 10, 9
>Organization:
.cnI ,egarotS cimotA
>Environment:
>Description:

	Byte-sized atomic_store_* on hash-locked architectures with
	test-and-set but no compare-and-swap (sparc, hppa) work as
	follows:

		uint8_t v;
		unsigned s = 8 * ((uintptr_t)p & 3);
		uint32_t o, n, m = ~(0xffU << s);

		memcpy(&v, q, 1);
		do {
			o = atomic_load_relaxed(p32);
			n = (o & m) | ((uint32_t)v << s);
		} while (atomic_cas_32(p32, o, n) != o);

	This approach is necessary in order for atomic_cas_* to
	cooperate with atomic_cas_* -- if a regular store could happen
	during a concurrent atomic_cas_* on another CPU, it might catch
	the atomic_cas_* in the middle of a hash-locked sequence,
	violating the atomicity.

	But it is broken for two reasons.

	1. It is endian-dependent.  Need

		s = 8 * (3 - ((uintptr_t)p & 3))

	   to work on big-endian.

	2. It cooperates with atomic_cas_* on the same word, but it
	   doesn't cooperate with regular stores _of other bytes_ on
	   the same word.

	The 16-bit path is also broken for a third reason:

		unsigned s = 8 * (((uintptr_t)p & 2) >> 1);

	This either needs to be 16 * (((uintptr_t)p & 2) >> 1), or just
	8 * ((uintptr_t)p & 2).

	(Thanks to yamaguchi@ for figuring out my stupid mistakes!)

>How-To-Repeat:

	cd /usr/tests/net/if_pppoe && atf-run t_pppoe | atf-report

	We should really have direct tests of atomc_load/store_* too.
	Would've saved me some embarrassment.

>Fix:

	Yes, please!

>Release-Note:

>Audit-Trail:

From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc: 
Subject: PR/60393 CVS commit: src/sys/sys
Date: Thu, 2 Jul 2026 14:38:19 +0000

 Module Name:	src
 Committed By:	riastradh
 Date:		Thu Jul  2 14:38:19 UTC 2026

 Modified Files:
 	src/sys/sys: atomic.h

 Log Message:
 atomic_store(9): Fix byte indexing on hash-locked architectures.

 Fixes the easy part of:

 PR kern/60393: hash-locked atomic_store_* is all kinds of busted

 The hard part will require redoing these to just hash-lock the whole
 store.


 To generate a diff of this commit:
 cvs rdiff -u -r1.29 -r1.30 src/sys/sys/atomic.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.