Received: by 2002:ab2:60d1:0:b0:1f7:5705:b850 with SMTP id i17csp780951lqm; Wed, 1 May 2024 16:04:23 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX9JAS7aX2Oz607Ab701gx6Dqt/4WB6hG/wTkpOMdP6ScS9YhbrJLw1Mx7Cd6ZIpWO3E1tFWoGHeeMOMZ+AEvSPJbUo65YVjkjGhvNjHA== X-Google-Smtp-Source: AGHT+IGekicHLGu8KQ4gSF3M0f02Jx0jWi0wKmYeRMGN4YGmSo2nMEVP7p6HQcYX7dmbse45k85Y X-Received: by 2002:a05:6358:7213:b0:17f:72ff:221e with SMTP id h19-20020a056358721300b0017f72ff221emr646135rwa.4.1714604662850; Wed, 01 May 2024 16:04:22 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714604662; cv=pass; d=google.com; s=arc-20160816; b=T73rD3uGC0fOSHDg1HaqgxOLU04hSNNKmJTg5/9HqEFHpcf954VwGPP1tAZo/3JzZv wgL9sU9KesgILqVZ3qR4HXxP5LK/cAkB43KXqi+BhWy+jZ/cEAH21JZ/Pj5F6+2jfm7b RX83vv1sTu5Xa17xfdVhYWLB+//kxMsDeuBQCTPhomPchK6SWhgjANdbQ2rVX8O711f3 2EHmzSGyNQ9q7PE+cBD+jkVS3SjZZhgAfahDF/KiEAlRRzthXlQ63r/pwvdNUpMxq1CJ yVl0x2q+lcK7vb0YKDK5hiUgJEwJbGey+R22wTmp7Q+ZNjVWyJcpG3Ti5W+7cqWZYPFA YwHg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=c+tATO1bSSfqmB/5Q525Ytl3H5QqCDHqG4coxD4StEE=; fh=vaKSyE8ewISOXnV9KREFR0VqEL5LYQk92TAAWWHYkGU=; b=xhqio65/8T7N64y1yZKr1qu4LnNCpgDeCO2jHoJiv4rlrPg3xpVSAPs7a21dQTCQ5v GXwgsbK5Xnes1fFiYjClcp3iYWQ5jIUMoCYinfMVJoUrQEo817kogF6+N362pU3UZUmS ZnYtsjRO7C6v/bun90+4xfCQP1kg0/q/7TnUXlMOWIcKQTtt5knIf9PkbGuIttHhMxcH V6V7ftCW9NbS7BTvvE2rAgjV51hQ1Dh6Y2km5h1XNqfWy1cZT0I2xSDHU/d4L4UpRhg7 tvX4o/UqBBA9YIBuI17e7P8drTOixaJ8UZuRht1z1XJpdNEc8KRhHgKu9mWfmuYcsrh9 QYaw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=XH9vIivI; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-165810-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-165810-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id bv6-20020a632e06000000b005e2c2fc5125si24695929pgb.249.2024.05.01.16.04.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 May 2024 16:04:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-165810-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=XH9vIivI; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-165810-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-165810-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 1F28F28121B for ; Wed, 1 May 2024 23:03:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D718216DED9; Wed, 1 May 2024 23:01:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="XH9vIivI" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EC33E168AE6; Wed, 1 May 2024 23:01:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714604493; cv=none; b=PcdmXomb7tV+QV9oJxlSO1wdcxFr0OqIOM0Ns1zaVUrRfZ5HEeJQEy0uae5K8nCIgZr8Y0Y95zD1SN1FR7ixMsuYz1sTqBzmn7uIufYIRDcorTp9PogcwpqlcdnbF5GxNtdsNRe7qkhjcIFOLBI8DAi2Dzgm0gC9nZVDe3SnRQ4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714604493; c=relaxed/simple; bh=Zs6ak+wPmG4tPQkLrLcc6JxAJjrKyJZjKo6tat/+S/g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ubU8WkWbUWzO2Lw01U4Y122Alo4yFplFsIR8mVpiijMhKlEbDTdAoUNHSMdF7F+nY2tFSIBA4xNli4O2oAcxS0y9zg8jIaHVHX+o3o6vrZndsl5Leigcw2x1MLP7BF3wpRXaeKbiPR7J4T9TLXM8vcPj3C1G4lyDbHgHUbNToXY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=XH9vIivI; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 95074C4AF51; Wed, 1 May 2024 23:01:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714604492; bh=Zs6ak+wPmG4tPQkLrLcc6JxAJjrKyJZjKo6tat/+S/g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XH9vIivIX47KRQExC5xnU+LZC/uG5D0FeEfAoXkVPwo/PTEd9yqIYpCPKiZcS7ifl 3ERfMzuFeuTI57s+bH5QGzcUtcsD0t7XyR0NyJeMNKEesobUphmk37JASgd+wTWv9h jeCYJ+rXWPe8/H/pmlpj8BziFD6iXQYJ34DCpQAvMFw8tl4VZnG/kWqSOGfqulujH4 VrvpnKeTVU8fHuwPcCU/cFmzzdvGWyfOUKt+GjhBXzpXhXHgpVtwfnork9aIxwRNhk wfYkihPp4voUhbeAJqC5JMczxccduyhU+c+6FuCRt/x2j5E/AR8jAI+ntK/qSgNUx5 oXHnotmaZeiMQ== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id D3895CE2281; Wed, 1 May 2024 16:01:31 -0700 (PDT) From: "Paul E. McKenney" To: linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org Cc: elver@google.com, akpm@linux-foundation.org, tglx@linutronix.de, peterz@infradead.org, dianders@chromium.org, pmladek@suse.com, arnd@arndb.de, torvalds@linux-foundation.org, kernel-team@meta.com, "Paul E. McKenney" Subject: [PATCH v2 cmpxchg 09/13] lib: Add one-byte emulation function Date: Wed, 1 May 2024 16:01:26 -0700 Message-Id: <20240501230130.1111603-9-paulmck@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Architectures are required to provide four-byte cmpxchg() and 64-bit architectures are additionally required to provide eight-byte cmpxchg(). However, there are cases where one-byte cmpxchg() would be extremely useful. Therefore, provide cmpxchg_emu_u8() that emulates one-byte cmpxchg() in terms of four-byte cmpxchg(). Note that this emulations is fully ordered, and can (for example) cause one-byte cmpxchg_relaxed() to incur the overhead of full ordering. If this causes problems for a given architecture, that architecture is free to provide its own lighter-weight primitives. [ paulmck: Apply Marco Elver feedback. ] [ paulmck: Apply kernel test robot feedback. ] [ paulmck: Drop two-byte support per Arnd Bergmann feedback. ] Link: https://lore.kernel.org/all/0733eb10-5e7a-4450-9b8a-527b97c842ff@paulmck-laptop/ Signed-off-by: Paul E. McKenney Acked-by: Marco Elver Cc: Andrew Morton Cc: Thomas Gleixner Cc: "Peter Zijlstra (Intel)" Cc: Douglas Anderson Cc: Petr Mladek Cc: Arnd Bergmann Cc: --- arch/Kconfig | 3 +++ include/linux/cmpxchg-emu.h | 15 +++++++++++++ lib/Makefile | 1 + lib/cmpxchg-emu.c | 45 +++++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+) create mode 100644 include/linux/cmpxchg-emu.h create mode 100644 lib/cmpxchg-emu.c diff --git a/arch/Kconfig b/arch/Kconfig index 9f066785bb71d..284663392eef8 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -1609,4 +1609,7 @@ config CC_HAS_SANE_FUNCTION_ALIGNMENT # strict alignment always, even with -falign-functions. def_bool CC_HAS_MIN_FUNCTION_ALIGNMENT || CC_IS_CLANG +config ARCH_NEED_CMPXCHG_1_EMU + bool + endmenu diff --git a/include/linux/cmpxchg-emu.h b/include/linux/cmpxchg-emu.h new file mode 100644 index 0000000000000..998deec67740a --- /dev/null +++ b/include/linux/cmpxchg-emu.h @@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* + * Emulated 1-byte and 2-byte cmpxchg operations for architectures + * lacking direct support for these sizes. These are implemented in terms + * of 4-byte cmpxchg operations. + * + * Copyright (C) 2024 Paul E. McKenney. + */ + +#ifndef __LINUX_CMPXCHG_EMU_H +#define __LINUX_CMPXCHG_EMU_H + +uintptr_t cmpxchg_emu_u8(volatile u8 *p, uintptr_t old, uintptr_t new); + +#endif /* __LINUX_CMPXCHG_EMU_H */ diff --git a/lib/Makefile b/lib/Makefile index ffc6b2341b45a..cc3d52fdb477d 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -236,6 +236,7 @@ obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o lib-$(CONFIG_GENERIC_BUG) += bug.o obj-$(CONFIG_HAVE_ARCH_TRACEHOOK) += syscall.o +obj-$(CONFIG_ARCH_NEED_CMPXCHG_1_EMU) += cmpxchg-emu.o obj-$(CONFIG_DYNAMIC_DEBUG_CORE) += dynamic_debug.o #ensure exported functions have prototypes diff --git a/lib/cmpxchg-emu.c b/lib/cmpxchg-emu.c new file mode 100644 index 0000000000000..27f6f97cb60dd --- /dev/null +++ b/lib/cmpxchg-emu.c @@ -0,0 +1,45 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Emulated 1-byte cmpxchg operation for architectures lacking direct + * support for this size. This is implemented in terms of 4-byte cmpxchg + * operations. + * + * Copyright (C) 2024 Paul E. McKenney. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +union u8_32 { + u8 b[4]; + u32 w; +}; + +/* Emulate one-byte cmpxchg() in terms of 4-byte cmpxchg. */ +uintptr_t cmpxchg_emu_u8(volatile u8 *p, uintptr_t old, uintptr_t new) +{ + u32 *p32 = (u32 *)(((uintptr_t)p) & ~0x3); + int i = ((uintptr_t)p) & 0x3; + union u8_32 old32; + union u8_32 new32; + u32 ret; + + ret = READ_ONCE(*p32); + do { + old32.w = ret; + if (old32.b[i] != old) + return old32.b[i]; + new32.w = old32.w; + new32.b[i] = new; + instrument_atomic_read_write(p, 1); + ret = data_race(cmpxchg(p32, old32.w, new32.w)); // Overridden above. + } while (ret != old32.w); + return old; +} +EXPORT_SYMBOL_GPL(cmpxchg_emu_u8); -- 2.40.1