Message ID | 20210724162429.394792-1-sven@narfation.org (mailing list archive) |
---|---|
State | Not Applicable, archived |
Delegated to: | Simon Wunderlich |
Headers |
Return-Path: <b.a.t.m.a.n-bounces@lists.open-mesh.org> X-Original-To: patchwork@open-mesh.org Delivered-To: patchwork@open-mesh.org Received: from diktynna.open-mesh.org (localhost [IPv6:::1]) by diktynna.open-mesh.org (Postfix) with ESMTP id 86E848237A; Sat, 24 Jul 2021 18:24:41 +0200 (CEST) Received: from dvalin.narfation.org (dvalin.narfation.org [213.160.73.56]) by diktynna.open-mesh.org (Postfix) with ESMTPS id 8FAF68026E for <b.a.t.m.a.n@lists.open-mesh.org>; Sat, 24 Jul 2021 18:24:37 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=narfation.org; s=20121; t=1627143876; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=VlraKBfCz4HcEEphEEUFJxpqY7WKDh5jCjkPEkIoSOo=; b=vraSgcfngtPJ5cG1c7K18mNFPKbUyQjtOynHdMdNi/mqG7QZjlTI4A9H+HrArp5Xd3oJW4 3WH9aXK5UEKQeKJUTAwqd55zEujLFbycKYr8elquAXYlfNOQ2GlEpzNoQzAv3bBlJGGtvR 5mXHQ5YkNe+mpvctxK67f4mfT4hjRBQ= From: Sven Eckelmann <sven@narfation.org> To: Arnd Bergmann <arnd@arndb.de> Subject: [PATCH] asm-generic: avoid sparse {get,put}_unaligned warning Date: Sat, 24 Jul 2021 18:24:29 +0200 Message-Id: <20210724162429.394792-1-sven@narfation.org> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=open-mesh.org; s=20121; t=1627143877; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding:dkim-signature; bh=VlraKBfCz4HcEEphEEUFJxpqY7WKDh5jCjkPEkIoSOo=; b=U0wtRpwLbco0hyWylPQcGFd/T7K95k63Js9PvXbxrOrci1heWcBJf+Imm2S89YvbShTHv/ Fj3ITL0fAi9RFHlGD8NLKxZnHHM2ebMkfP180Sf5xQLLQ29JrSXhzR+FJS4dBmzVrUN84e Zx/T5gEDi0EB7mM3BlUUtlUWvEFDsPM= ARC-Seal: i=1; s=20121; d=open-mesh.org; t=1627143877; a=rsa-sha256; cv=none; b=a9/r8j0MMliHv7ZsFqB3L6UWT1ZejHDaEhXlSu31NoEnlFT6ewMuukgbcVc2fTpBIqVgiX tnCDh70eaagIdZ4g/sdbU9Mfc3ZvOJDHwbv9eeMeyqSswN+CpUvqoehmQlOjcnFY29A8u6 i2QoaELtP6hZZdVM8j4afOEFQKMcR90= ARC-Authentication-Results: i=1; diktynna.open-mesh.org; dkim=pass header.d=narfation.org header.s=20121 header.b=vraSgcfn; spf=pass (diktynna.open-mesh.org: domain of sven@narfation.org designates 213.160.73.56 as permitted sender) smtp.mailfrom=sven@narfation.org Content-Transfer-Encoding: quoted-printable Message-ID-Hash: YZBJRJUXUJIMOHE4ZROTPV75QFI2J23E X-Message-ID-Hash: YZBJRJUXUJIMOHE4ZROTPV75QFI2J23E X-MailFrom: sven@narfation.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-b.a.t.m.a.n.lists.open-mesh.org-0; header-match-b.a.t.m.a.n.lists.open-mesh.org-1; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header CC: b.a.t.m.a.n@lists.open-mesh.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org X-Mailman-Version: 3.2.1 Precedence: list Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking <b.a.t.m.a.n@lists.open-mesh.org> List-Id: The list for a Better Approach To Mobile Ad-hoc Networking <b.a.t.m.a.n.lists.open-mesh.org> Archived-At: <https://lists.open-mesh.org/mailman3/hyperkitty/list/b.a.t.m.a.n@lists.open-mesh.org/message/YZBJRJUXUJIMOHE4ZROTPV75QFI2J23E/> List-Archive: <https://lists.open-mesh.org/mailman3/hyperkitty/list/b.a.t.m.a.n@lists.open-mesh.org/> List-Help: <mailto:b.a.t.m.a.n-request@lists.open-mesh.org?subject=help> List-Post: <mailto:b.a.t.m.a.n@lists.open-mesh.org> List-Subscribe: <mailto:b.a.t.m.a.n-join@lists.open-mesh.org> List-Unsubscribe: <mailto:b.a.t.m.a.n-leave@lists.open-mesh.org> |
Series |
asm-generic: avoid sparse {get,put}_unaligned warning
|
|
Commit Message
Sven Eckelmann
July 24, 2021, 4:24 p.m. UTC
Sparse will try to check casting of simple integer types which are marked
as __bitwise. This for example "disallows" simple casting of __be{16,32,64}
or __le{16,32,64} to other types. This is also true for pointers to
variables with this type.
But the new generic {get,put}_unaligned is doing that by (reinterpret)
casting the original pointer to a new (anonymous) struct pointer. This will
then create warnings like:
net/batman-adv/distributed-arp-table.c:1461:19: warning: cast from restricted __be32 *
net/batman-adv/distributed-arp-table.c:1510:23: warning: cast from restricted __be32 [usertype] *[assigned] magic
net/batman-adv/distributed-arp-table.c:1588:24: warning: cast from restricted __be32 [usertype] *[assigned] yiaddr
The special attribute force must be used in such statements when the cast
is known to be safe to avoid these warnings.
Fixes: 803f4e1eab7a ("asm-generic: simplify asm/unaligned.h")
Signed-off-by: Sven Eckelmann <sven@narfation.org>
---
include/asm-generic/unaligned.h | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
Comments
On Sat, Jul 24, 2021 at 06:24:29PM +0200, Sven Eckelmann wrote: > The special attribute force must be used in such statements when the cast > is known to be safe to avoid these warnings. How about container_of(ptr, typeof(*__pptr), x) instead of a cast? Would be easier to follow...
On Sat, Jul 24, 2021 at 7:01 PM Al Viro <viro@zeniv.linux.org.uk> wrote: > > On Sat, Jul 24, 2021 at 06:24:29PM +0200, Sven Eckelmann wrote: > > > The special attribute force must be used in such statements when the cast > > is known to be safe to avoid these warnings. I can see why this would warn, but I'm having trouble reproducing the warning on linux-next. > How about container_of(ptr, typeof(*__pptr), x) instead of a cast? > Would be easier to follow... If both work equally well, I'd prefer Sven's patch since that only expands 'type' once, while container_of() expands it three more times. This may not make much of a difference, but I've seen a number of cases where nested macros can explode the preprocessed code size enough to slow down kernel compilation over all, and it's quite possible to have get_unaligned()/put_unaligned in the middle of that, with a complex expression passed into that. Arnd
On Monday, 26 July 2021 14:57:31 CEST Arnd Bergmann wrote: > > > > > The special attribute force must be used in such statements when the cast > > > is known to be safe to avoid these warnings. > > I can see why this would warn, but I'm having trouble reproducing the > warning on linux-next. I have sparse 0.6.3 on an Debian bullseye amd64 system. Sources are from linux-next next-20210723 make allnoconfig cat >> .config << "EOF" CONFIG_NET=y CONFIG_INET=y CONFIG_BATMAN_ADV=y CONFIG_BATMAN_ADV_DAT=y EOF make olddefconfig make CHECK="sparse -Wbitwise-pointer" C=1 I should maybe have made this clearer in the last sentence of the first paragraph: "This is also true for pointers to variables with this type when -Wbitwise-pointer is activated." Kind regards, Sven
On Mon, Jul 26, 2021 at 5:04 PM Sven Eckelmann <sven@narfation.org> wrote: > > On Monday, 26 July 2021 14:57:31 CEST Arnd Bergmann wrote: > > > > > > > The special attribute force must be used in such statements when the cast > > > > is known to be safe to avoid these warnings. > > > > I can see why this would warn, but I'm having trouble reproducing the > > warning on linux-next. > > I have sparse 0.6.3 on an Debian bullseye amd64 system. Sources are from > linux-next next-20210723 > > make allnoconfig > cat >> .config << "EOF" > CONFIG_NET=y > CONFIG_INET=y > CONFIG_BATMAN_ADV=y > CONFIG_BATMAN_ADV_DAT=y > EOF > make olddefconfig > make CHECK="sparse -Wbitwise-pointer" C=1 > > I should maybe have made this clearer in the last sentence of the first > paragraph: "This is also true for pointers to variables with this type when > -Wbitwise-pointer is activated." Ok, got it. I assumed this would be turned on by an 'allmodconfig' build. > > If both work equally well, I'd prefer Sven's patch since that only > > expands 'type' once, while container_of() expands it three more times Not sure what I was thinking here, as it's not 'type' that gets expanded here but 'ptr'. We could do Al's suggestion to avoid the __force without multiple expansions, using diff --git a/include/asm-generic/unaligned.h b/include/asm-generic/unaligned.h index 1c4242416c9f..d138dc5fd8e3 100644 --- a/include/asm-generic/unaligned.h +++ b/include/asm-generic/unaligned.h @@ -10,17 +10,25 @@ #include <asm/byteorder.h> #define __get_unaligned_t(type, ptr) ({ \ - const struct { type x; } __packed *__pptr = (typeof(__pptr))(ptr); \ + const struct { type x; } __packed *__pptr = \ + container_of(ptr, typeof(*__pptr), x); \ __pptr->x; \ }) #define __put_unaligned_t(type, val, ptr) do { \ - struct { type x; } __packed *__pptr = (typeof(__pptr))(ptr); \ + struct { type x; } __packed *__pptr = \ + container_of(ptr, typeof(*__pptr), x); \ __pptr->x = (val); \ } while (0) -#define get_unaligned(ptr) __get_unaligned_t(typeof(*(ptr)), (ptr)) -#define put_unaligned(val, ptr) __put_unaligned_t(typeof(*(ptr)), (val), (ptr)) +#define get_unaligned(ptr) ({ \ + __auto_type _ptr = (ptr); \ + __get_unaligned_t(typeof(*(_ptr)), (_ptr)); \ +}) +#define put_unaligned(val, ptr) ({ \ + __auto_type _ptr = (ptr); \ + __put_unaligned_t(typeof(*(_ptr)), (val), (_ptr)); \ +}) static inline u16 get_unaligned_le16(const void *p) { Not sure if this is any better. Arnd
From: Sven Eckelmann > Sent: 24 July 2021 17:24 > > Sparse will try to check casting of simple integer types which are marked > as __bitwise. This for example "disallows" simple casting of __be{16,32,64} > or __le{16,32,64} to other types. This is also true for pointers to > variables with this type. > > But the new generic {get,put}_unaligned is doing that by (reinterpret) > casting the original pointer to a new (anonymous) struct pointer. This will > then create warnings like: > > net/batman-adv/distributed-arp-table.c:1461:19: warning: cast from restricted __be32 * > net/batman-adv/distributed-arp-table.c:1510:23: warning: cast from restricted __be32 [usertype] > *[assigned] magic > net/batman-adv/distributed-arp-table.c:1588:24: warning: cast from restricted __be32 [usertype] > *[assigned] yiaddr > > The special attribute force must be used in such statements when the cast > is known to be safe to avoid these warnings. At least the __force is being added to an existing cast. The real problems are when a (__force __le32)value cast is used to silence sparse. These should really be something like: __tell_sparce(__le32, value) so that the whole thing can be removed by the preprocessor when compiling the code. David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
diff --git a/include/asm-generic/unaligned.h b/include/asm-generic/unaligned.h index 1c4242416c9f..e2b23e5bf945 100644 --- a/include/asm-generic/unaligned.h +++ b/include/asm-generic/unaligned.h @@ -10,12 +10,13 @@ #include <asm/byteorder.h> #define __get_unaligned_t(type, ptr) ({ \ - const struct { type x; } __packed *__pptr = (typeof(__pptr))(ptr); \ + const struct { type x; } __packed *__pptr; \ + __pptr = (__force typeof(__pptr))(ptr); \ __pptr->x; \ }) #define __put_unaligned_t(type, val, ptr) do { \ - struct { type x; } __packed *__pptr = (typeof(__pptr))(ptr); \ + struct { type x; } __packed *__pptr = (__force typeof(__pptr))(ptr); \ __pptr->x = (val); \ } while (0)