Received: by 2002:ab2:6a05:0:b0:1f8:1780:a4ed with SMTP id w5csp2093192lqo; Mon, 13 May 2024 07:44:32 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCU+EUwWnAF2r3uo7FCjRkPsEiQAR0I4NF4fy3BdKylS18DfS37AHmEnifg3SuvE51KMxOkrByvIqvGtM9pFSe9Ikq9gD8ITY7F2AQIObg== X-Google-Smtp-Source: AGHT+IGzQn1MV7JtbvWMihc6DVmbQtZmFSuiFEiUiFNTbIfEiJTot5Xun8GT2L5vaf21gX0dGnp/ X-Received: by 2002:a05:6512:e94:b0:521:cee5:db36 with SMTP id 2adb3069b0e04-52210074834mr8187233e87.48.1715611472334; Mon, 13 May 2024 07:44:32 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715611472; cv=pass; d=google.com; s=arc-20160816; b=IIO7cngtpmgZbsDnh6pxInrgObJygIQMoSP2EK0/n8tHTasMJev6D/lR4MiQpI6PSa 9eM1Rvo73Y/HziUleQAmZtaPVYw6gV7LR12KB0M2i9eqQ7WucJIW05YNPtGrQZ0FvVJe BiJGdJgF3Z59sipnXd1snU2H/jptTcKZu+mmTZcQMN8hpO4BEEb4y5ujQxUtcTqvPJaS RRu8LGiVSQwQtaSoV5Unsn6/lzt8pGQJIMkOdtKqwWjwK8JkbpdVozbmrleo4d7vG8Op 4NDspPMY27nSgKm/ZgQ+/YYgpWYX9qnw2Bq1Z+RDTpt4kFuHJFb14LLmeMkhH2BEQJLi O1Lw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:feedback-id:dkim-signature; bh=klxyG+w31HTv55FUS4dEXXQle51LU0Dzt6wJnMJ2rRw=; fh=WLlp3V+cnxD3oTenn3MDgyQMvOeFwNMT52v7lUFNX+I=; b=Lu016thoakILD1+t+c9bWBdurnOnuj+PrONNZkibGwbaTM0C9WWW2hXCn1HupHf0wh RN6onwE/yZexRSmQtwRROuAxEdPKiALYokA+S8OFFwJ0oNokcva2rkXuj2EIjqzOSFuj DYoTZtoT6pf0KItr2hPo5W4yeR64x+8yJx8090/62rMRsE/7QJxJUYEthQHAqtv4WeFs jWqqP0YdHXR0Ypi57xZBoHPKjiLIcrNBEH/5l63na0xL6gDIK6JkgyJBre1IURW0hfF4 x+TkLuP2nvmXKHKokSFwblUetd2KFIrrR9Ck86II/48TnuG6VgSm/Jg/cLmUXg4OQcAj IyIg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=SPndpuG6; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-177705-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-177705-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id a640c23a62f3a-a5a17ba3af6si495169966b.600.2024.05.13.07.44.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 May 2024 07:44:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-177705-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=SPndpuG6; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-177705-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-177705-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com 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 am.mirrors.kernel.org (Postfix) with ESMTPS id DE3E91F2237D for ; Mon, 13 May 2024 14:44:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CD5A3568A; Mon, 13 May 2024 14:44:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="SPndpuG6" Received: from mail-qt1-f179.google.com (mail-qt1-f179.google.com [209.85.160.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3900B28F7; Mon, 13 May 2024 14:44:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715611454; cv=none; b=RtNn48MBei94oqapwlffckSWM4s7xi2ccO6cJKUq7nFUQftMjK88J1JZBnFeXGirG4r1Q35sdI33L6XYP9krhZyAuzgwlDLOnFH/fXcimBdYmfm9lykBPr1kMGQ9uXUygpePX0BFvkUm56j3c0M4Vo5jgCWEYFWpjmrcrg9nYzE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715611454; c=relaxed/simple; bh=Wtg/SCzNy96+c2gHSPdp5I9ooMI7e9h6W3vFbDe5cjE=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=qX1YjDY6HYKr8Gd4vmVyW2PhRiE0waPBQa5MaXttR9kDYbUFjhO01ebcKh7gIT4EyLdgcqWgPTnIy6liLMCFBnMAvcTrnSDWXj2QH+llW9U7q3zQBWwuHgmeXNe6LI0MiWyKO948w4vR1GkhAsrBJqUqsdLloRkG2ruamKEKS4g= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=SPndpuG6; arc=none smtp.client-ip=209.85.160.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-qt1-f179.google.com with SMTP id d75a77b69052e-43df7f42ac9so25541601cf.2; Mon, 13 May 2024 07:44:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715611452; x=1716216252; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:from:to:cc:subject:date :message-id:reply-to; bh=klxyG+w31HTv55FUS4dEXXQle51LU0Dzt6wJnMJ2rRw=; b=SPndpuG6ehwX6z0etLw21/zwYSVVi+/Han6sJQEGl5Z/5MDW2/3LE6Tn7SzgS9SPmH 4VuELCxDbHMuzJC4xrKUkADxQEnRCVeBi/E7ga/A0X7Za39Gjy2DlXX3od6vUOBNJ1Ix +/ybMTYhHLi1QQpljyOqNE1BkszXPPdEsgmrmmOVYdqKjqOD+WS/svRpHkQzxzFE1lvh AIfHcvc8T7AizrsSz53qGDAXuZuTxOrDVfoR7EIv/pdYdppnMQfn9RpnpKjDPe6tOHmX W249mVyzSs0WBGKPcLG5Zuy3/ulnXS4k9+zMLrK3vCzqwP0By4qmwhxgvrBqz3sYQaZm +rYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715611452; x=1716216252; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=klxyG+w31HTv55FUS4dEXXQle51LU0Dzt6wJnMJ2rRw=; b=k51xsu05a6zBKHF4cbiupoXt7kq0rsXTWEQwawmJzt846fNNDjSwsKrmD5CVOCFXsS OWrIi+c2OMujXRzw+tuOcMyDKgK8jZz1zLVu2xYpQTE2swP1HghiFiudf5f7MW+Evg9/ svhXSrAaHZGptHwjDi4zNO4zl6OX9YG+07hNd45DSA6qP1KpTmMtoBXp9rC71oWBeu7t ZMfxIQBvV47avRgy6y6M2LEU8lmn8B/GkYcxq5CfI5bLY0KypQ3EPv2Oolk4+1HDahpL b0FB2kmwWW3VLxUc+azEULOnmFLealtrLqb7pEDEFXXUnm2JlKC3G4f/ZOvtyfN/RKCd aMew== X-Forwarded-Encrypted: i=1; AJvYcCUzQc8LwrjZgKZnlG76JkJ83FuuPfFFxqSS247F5JfRATnVff6BvKgexake6PtGOS4oLxSP+nbREJXjOkFJ327G90CToNDpXsVQv1mo X-Gm-Message-State: AOJu0YwkGezksN/hc8Xsy8jXLzmddlYZA8pm5e8GMo2mSbQGlj7GqA/j DlqDfmSHB7Cc4OnmCw7uuqO6iCZkZgPe9DV36/7p+QgKIijretLi X-Received: by 2002:ac8:5a8c:0:b0:43a:ea41:c9cd with SMTP id d75a77b69052e-43dfdb70c38mr144182491cf.59.1715611452216; Mon, 13 May 2024 07:44:12 -0700 (PDT) Received: from fauth2-smtp.messagingengine.com (fauth2-smtp.messagingengine.com. [103.168.172.201]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-43df56d0396sm56146551cf.86.2024.05.13.07.44.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 May 2024 07:44:10 -0700 (PDT) Received: from compute7.internal (compute7.nyi.internal [10.202.2.48]) by mailfauth.nyi.internal (Postfix) with ESMTP id 17EB11200066; Mon, 13 May 2024 10:44:10 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute7.internal (MEProxy); Mon, 13 May 2024 10:44:10 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvdeggedgjeelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvdenucfhrhhomhepuehoqhhu nhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrg htthgvrhhnpefhtedvgfdtueekvdekieetieetjeeihedvteehuddujedvkedtkeefgedv vdehtdenucffohhmrghinhepkhgvrhhnvghlrdhorhhgnecuvehluhhsthgvrhfuihiivg eptdenucfrrghrrghmpehmrghilhhfrhhomhepsghoqhhunhdomhgvshhmthhprghuthhh phgvrhhsohhnrghlihhthidqieelvdeghedtieegqddujeejkeehheehvddqsghoqhhunh drfhgvnhhgpeepghhmrghilhdrtghomhesfhhigihmvgdrnhgrmhgv X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 13 May 2024 10:44:09 -0400 (EDT) Date: Mon, 13 May 2024 07:44:00 -0700 From: Boqun Feng To: "Paul E. McKenney" Cc: linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, 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 Subject: Re: [PATCH v2 cmpxchg 09/13] lib: Add one-byte emulation function Message-ID: References: <20240501230130.1111603-9-paulmck@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240501230130.1111603-9-paulmck@kernel.org> On Wed, May 01, 2024 at 04:01:26PM -0700, Paul E. McKenney wrote: > 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. Just out of curiosity, why is this `data_race` needed? cmpxchg is atomic so there should be no chance for a data race? Regards, Boqun > + } while (ret != old32.w); > + return old; > +} > +EXPORT_SYMBOL_GPL(cmpxchg_emu_u8); > -- > 2.40.1 >