Skip to contentSkip to conversation

kernfs: avoid READ_ONCE() on a kgid_t in kernfs_new_node()

PATCH Applied Shakeel Butt 3 participants 3 messages Oct 27 – Nov 2

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kernfs: avoid READ_ONCE() on a kgid_t in kernfs_new_node()

kernfs_new_node() reads the parent's gid with READ_ONCE(attrs->ia_gid). That gid is a kgid_t, which is a struct.

On SMP alpha, READ_ONCE() ends by casting the value it loaded back to the type of its argument. Here that is a cast from a struct to the same struct. GCC accepts that as an extension, but ISO C does not allow a cast to or from a struct type, so sparse warns:

fs/kernfs/dir.c:751:31: sparse: sparse: cast to non-scalar fs/kernfs/dir.c:751:31: sparse: sparse: cast from non-scalar

Read the gid_t inside the kgid_t instead, and wrap it back up with KGIDT_INIT(). This is still a single marked load of the same four bytes.

There is no functional change. With GCC 12 for alpha (the config from the report) and GCC 14 for x86-64 (with and without KCSAN), fs/kernfs/dir.o has the same object code before and after this change.

Fixes 81bdc85b4c08 ("kernfs: fix up the unlocked attribute reads on the creation paths")
---
 fs/kernfs/dir.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/kernfs/dir.c +1 -1
index 324d61a00545..800af10d9511 100644
@@ -750,7 +750,7 @@ struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
 		struct kernfs_iattrs *attrs = READ_ONCE(parent->iattr);
 
 		if (attrs)
-			gid = READ_ONCE(attrs->ia_gid);
+			gid = KGIDT_INIT(READ_ONCE(attrs->ia_gid.val));
 
 		if (flags & KERNFS_DIR)
 			mode |= S_ISGID;
Signature · 1 line
2.53.0-Meta
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 14 lines · Shakeel Butt

kernfs_new_node() reads the parent's gid with READ_ONCE(attrs->ia_gid). That gid is a kgid_t, which is a struct.

On SMP alpha, READ_ONCE() ends by casting the value it loaded back to the type of its argument. Here that is a cast from a struct to the same struct. GCC accepts that as an extension, but ISO C does not allow a cast to or from a struct type, so sparse warns:

fs/kernfs/dir.c:751:31: sparse: sparse: cast to non-scalar fs/kernfs/dir.c:751:31: sparse: sparse: cast from non-scalar

Read the gid_t inside the kgid_t instead, and wrap it back up with KGIDT_INIT(). This is still a single marked load of the same four bytes.

Quoted 8 lines

There is no functional change. With GCC 12 for alpha (the config from the report) and GCC 14 for x86-64 (with and without KCSAN), fs/kernfs/dir.o has the same object code before and after this change.

Fixes 81bdc85b4c08 ("kernfs: fix up the unlocked attribute reads on the creation paths")

Thanks.

Signature · 1 line
tejun
maintainer Applied
#2 lore raw
In reply to Shakeel Butt
Quoted 1 line · Shakeel Butt

[PATCH] kernfs: avoid READ_ONCE() on a kgid_t in kernfs_new_node()

Applied, thanks!

Branch: driver-core-linus
Tree: git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git

[1/1] kernfs: avoid READ_ONCE() on a kgid_t in kernfs_new_node()

      commit: ef179363b043

The patch will appear in the next linux-next integration (typically within 24 hours on weekdays).

The patch is queued up for Linus's tree and should land in the next -rc release.