Article View: org.kernel.vger.linux-assembly
Article #1RE: [PATCH 2/3] arm64: add __READ_ONCE_EX()
From: David Laight
Date: Mon, 08 Apr 2024 14:51
Date: Mon, 08 Apr 2024 14:51
85 lines
6774 bytes
6774 bytes
From: Haris Okanovic
> Sent: 02 April 2024 02:47
>
> Perform an exclusive load, which atomically loads a word and arms the
> execusive monitor to enable wfe() polling of an address.
>
> Adding this macro in preparation for an arm64 cpuidle driver which
> supports a wfe() based polling state.
>
> https://developer.arm.com/documentation/dht0008/a/arm-synchronization-primitives/exclusive-
> accesses/exclusive-monitors
>
> Signed-off-by: Haris Okanovic <harisokn@amazon.com>
> ---
> arch/arm64/include/asm/readex.h | 46 +++++++++++++++++++++++++++++++++
> 1 file changed, 46 insertions(+)
> create mode 100644 arch/arm64/include/asm/readex.h
>
> diff --git a/arch/arm64/include/asm/readex.h b/arch/arm64/include/asm/readex.h
> new file mode 100644
> index 000000000000..51963c3107e1
> --- /dev/null
> +++ b/arch/arm64/include/asm/readex.h
> @@ -0,0 +1,46 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +/*
> + * Based on arch/arm64/include/asm/rwonce.h
> + *
> + * Copyright (C) 2020 Google LLC.
> + * Copyright (C) 2024 Amazon.com, Inc. or its affiliates.
> + */
> +
> +#ifndef __ASM_READEX_H
> +#define __ASM_READEX_H
> +
> +#define __LOAD_EX(sfx, regs...) "ldaxr" #sfx "\t" #regs
> +
> +#define __READ_ONCE_EX(x) \
> +({ \
> + typeof(&(x)) __x = &(x); \
> + int atomic = 1; \
> + union { __unqual_scalar_typeof(*__x) __val; char __c[1]; } __u; \
> + switch (sizeof(x)) { \
> + case 1: \
> + asm volatile(__LOAD_EX(b, %w0, %1) \
> + : "=r" (*(__u8 *)__u.__c) \
> + : "Q" (*__x) : "memory"); \
> + break; \
> + case 2: \
> + asm volatile(__LOAD_EX(h, %w0, %1) \
> + : "=r" (*(__u16 *)__u.__c) \
> + : "Q" (*__x) : "memory"); \
> + break; \
> + case 4: \
> + asm volatile(__LOAD_EX(, %w0, %1) \
> + : "=r" (*(__u32 *)__u.__c) \
> + : "Q" (*__x) : "memory"); \
> + break; \
> + case 8: \
> + asm volatile(__LOAD_EX(, %0, %1) \
> + : "=r" (*(__u64 *)__u.__c) \
> + : "Q" (*__x) : "memory"); \
> + break; \
> + default: \
> + atomic = 0; \
> + } \
> + atomic ? (typeof(*__x))__u.__val : (*(volatile typeof(__x))__x);\
I'm pretty sure that doesn't work the way you expect.
The ?: operator promotes 'unsigned char' to 'int'.
So you can fall foul of signedness tests (eg in min()).
It also isn't going to work for non-scalers.
Replacing the ?: with __builtin_choose_expr() may help.
(This is probably a bug in the code you copied.)
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
Technical Details
Message-ID:
<1b659ba3fa50482a8338b1a6942884cc@AcuMS.aculab.com>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!archive!apf9.newsdeef.eu!nntp.lore.kernel.org!not-for-mailReferences:
<20240402014706.3969151-1-harisokn@amazon.com> <20240402014706.3969151-2-harisokn@amazon.com>Show Full Headers
Complete Article Headers
Message-ID: <1b659ba3fa50482a8338b1a6942884cc@AcuMS.aculab.com> Subject: RE: [PATCH 2/3] arm64: add __READ_ONCE_EX() Date: Mon, 8 Apr 2024 14:51:13 +0000 From: David Laight <David.Laight@ACULAB.COM> References: <20240402014706.3969151-1-harisokn@amazon.com> <20240402014706.3969151-2-harisokn@amazon.com> Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!archive!apf9.newsdeef.eu!nntp.lore.kernel.org!not-for-mail Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.86.151]) (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 545DA13F00B Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.58.86.151 ARC-Seal: ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712587922; cv=none; b=P0ECEx3n6lJUsF8jPlfI6Fgvj6wfLXtRJ8D7Kx6EH7SvjgT7V6bUb+99icw41I+dku2OTV5VEWoSS+072gAB/auzPdBdBJHZFo2mp7tnjMIDKXrT6DQg63xKv0Mw5vZLKXAoe0+IQmjsdAAiN9E3Ad71eU+R5ik+He/rO317W/s= ARC-Message-Signature: ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712587922; c=relaxed/simple; bh=joBS4N+InEhxAxePrDedqkg1DBS7fYz8aPowxWmurS4=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: MIME-Version:Content-Type; b=QfVwqm15D6updEWfq9q0Un2qSNeRehIaOKJlg+C+Uw65jLAjwiqkpBIMQaoHHvsiWhy0PwILFOmftR+Ch0WaNA25+xs3la32rbbDGZekFVgf7o14BVqb6JcSvWhFNxBwA2to76n1tcGIidqVwQvKkVLcticqzopcMeLSmNXqYjQ= ARC-Authentication-Results: ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ACULAB.COM; spf=pass smtp.mailfrom=aculab.com; arc=none smtp.client-ip=185.58.86.151 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ACULAB.COM Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=aculab.com Received: from AcuMS.aculab.com (156.67.243.121 [156.67.243.121]) by relay.mimecast.com with ESMTP with both STARTTLS and AUTH (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id X-MC-Unique: 6TRe-qOWOSiRkJM3kA_YFw-1 Received: from AcuMS.Aculab.com (10.202.163.6) by AcuMS.aculab.com (10.202.163.6) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Mon, 8 Apr Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi To: 'Haris Okanovic' <harisokn@amazon.com>, "linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>, "linux-pm@vger.kernel.org" <linux-pm@vger.kernel.org>, "linux-assembly@vger.kernel.org" <linux-assembly@vger.kernel.org> CC: "peterz@infradead.org" <peterz@infradead.org> Thread-Topic: [PATCH 2/3] arm64: add __READ_ONCE_EX() Thread-Index: AQHahJ/HcAF4vsIbZ0CGgbWFvfkyfLFefKGg In-Reply-To: <20240402014706.3969151-2-harisokn@amazon.com> Accept-Language: en-GB, en-US X-ms-exchange-transport-fromentityheader: Hosted Precedence: bulk X-Mailing-List: linux-assembly@vger.kernel.org List-Id: <linux-assembly.vger.kernel.org> List-Subscribe: <mailto:linux-assembly+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-assembly+unsubscribe@vger.kernel.org> MIME-Version: 1.0 X-Mimecast-Originator: aculab.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Newsgroups: org.kernel.vger.linux-assembly,org.kernel.vger.linux-kernel,org.kernel.vger.linux-pm