Skip to contentSkip to conversation

nfs: kobject: use generic helpers and ownership

PATCH In review Alexander Aring 3 participants 10 messages Oct 3, 2024 – Oct 9, 2024

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

cover nfs: kobject: use generic helpers and ownership

Hi,

I currently have pending patches for fs/dlm (Distributed Lock Manager) subsystem to introduce some helpers to udev. However, it seems it takes more time that I can bring those changes upstream. I put those out now and already figured out that nfs can also take advantage of those changes.

With this patch-series I try to try to reduce my patch-series for DLM and already bring part of it upstream and nfs will be a user of it.

The ownership callback, I think it should be set as the kset_create_and_add() sets this callback as default. I never had any issues with it, but there might be container corner cases that requires those changes?

- Alex

Alexander Aring (4):
kobject: add kset_type_create_and_add() helper
kobject: export generic helper ops
nfs: sysfs: use kset_type_create_and_add()
nfs: sysfs: use default get_ownership() callback

 fs/nfs/sysfs.c          | 30 +++----------------
 include/linux/kobject.h | 10 +++++--
 lib/kobject.c           | 65 ++++++++++++++++++++++++++++++-----------
 3 files changed, 60 insertions(+), 45 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/4 kobject: add kset_type_create_and_add() helper

Currently there exists the kset_create_and_add() helper that does not allow to have a different ktype for the created kset kobject. To allow a different ktype this patch will introduce the function kset_type_create_and_add() that allows to set a different ktype instead of using the global default kset_ktype variable.

In my example I need to separate the created kobject inside the kset by net-namespaces. This patch allows me to do that by providing a user defined kobj_type structure that implements the necessary namespace functionality.

---
 include/linux/kobject.h |  8 ++++--
 lib/kobject.c           | 59 ++++++++++++++++++++++++++++++-----------
 2 files changed, 50 insertions(+), 17 deletions(-)
include/linux/kobject.h +6 -2
index c8219505a79f..7504b7547ed2 100644
@@ -175,8 +175,12 @@ struct kset {
 void kset_init(struct kset *kset);
 int __must_check kset_register(struct kset *kset);
 void kset_unregister(struct kset *kset);
-struct kset * __must_check kset_create_and_add(const char *name, const struct kset_uevent_ops *u,
-					       struct kobject *parent_kobj);
+struct kset * __must_check
+kset_type_create_and_add(const char *name, const struct kset_uevent_ops *u,
+			 struct kobject *parent_kobj, const struct kobj_type *ktype);
+struct kset * __must_check
+kset_create_and_add(const char *name, const struct kset_uevent_ops *u,
+		    struct kobject *parent_kobj);
 
 static inline struct kset *to_kset(struct kobject *kobj)
 {
lib/kobject.c +44 -15

102 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/4 kobject: export generic helper ops

In reply to Alexander Aring

This patch exports generic helpers like kset_release() and kset_get_ownership() so users can use them in their own struct kobj_type implementation instead of implementing their own functions that do the same.

---
 include/linux/kobject.h | 2 ++
 lib/kobject.c           | 6 ++++--
 2 files changed, 6 insertions(+), 2 deletions(-)
include/linux/kobject.h +2 -0
index 7504b7547ed2..5fbc358e2be6 100644
@@ -181,6 +181,8 @@ kset_type_create_and_add(const char *name, const struct kset_uevent_ops *u,
 struct kset * __must_check
 kset_create_and_add(const char *name, const struct kset_uevent_ops *u,
 		    struct kobject *parent_kobj);
+void kset_release(struct kobject *kobj);
+void kset_get_ownership(const struct kobject *kobj, kuid_t *uid, kgid_t *gid);
 
 static inline struct kset *to_kset(struct kobject *kobj)
 {
lib/kobject.c +4 -2
index 09dd3d4c7f56..ccd2f6282c81 100644
@@ -920,19 +920,21 @@ struct kobject *kset_find_obj(struct kset *kset, const char *name)
 }
 EXPORT_SYMBOL_GPL(kset_find_obj);
 
-static void kset_release(struct kobject *kobj)
+void kset_release(struct kobject *kobj)
 {
 	struct kset *kset = container_of(kobj, struct kset, kobj);
 	pr_debug("'%s' (%p): %s\n",
 		 kobject_name(kobj), kobj, __func__);
 	kfree(kset);
 }
+EXPORT_SYMBOL_GPL(kset_release);
 
-static void kset_get_ownership(const struct kobject *kobj, kuid_t *uid, kgid_t *gid)
+void kset_get_ownership(const struct kobject *kobj, kuid_t *uid, kgid_t *gid)
 {
 	if (kobj->parent)
 		kobject_get_ownership(kobj->parent, uid, gid);
 }
+EXPORT_SYMBOL_GPL(kset_get_ownership);
 
 static const struct kobj_type kset_ktype = {
 	.sysfs_ops	= &kobj_sysfs_ops,
Signature · 1 line
2.43.0
#3 lore raw

PATCH 3/4 nfs: sysfs: use kset_type_create_and_add()

In reply to Alexander Aring

Since we have the new udev helper kset_type_create_and_add() helper we can use it as it allows to have an own kobj_type ktype that is necessary for nfs for the nfs_netns_object_child_ns_type() kset type callback.

We lose some errno information related to kset_register() in probably non-existing cases. We return always -ENOMEM as other uses of kset_create_and_add() does.

The nfs_kset_release() can be replaced by the default udev helper kset_release() as it does the same functionality.

---
 fs/nfs/sysfs.c | 29 +++--------------------------
 1 file changed, 3 insertions(+), 26 deletions(-)
fs/nfs/sysfs.c +3 -26
index bf378ecd5d9f..a6584203b7ff 100644
@@ -20,12 +20,6 @@
 
 static struct kset *nfs_kset;
 
-static void nfs_kset_release(struct kobject *kobj)
-{
-	struct kset *kset = container_of(kobj, struct kset, kobj);
-	kfree(kset);
-}
-
 static const struct kobj_ns_type_operations *nfs_netns_object_child_ns_type(
 		const struct kobject *kobj)
 {
@@ -33,35 +27,18 @@ static const struct kobj_ns_type_operations *nfs_netns_object_child_ns_type(
 }
 
 static struct kobj_type nfs_kset_type = {
-	.release = nfs_kset_release,
+	.release = kset_release,
 	.sysfs_ops = &kobj_sysfs_ops,
 	.child_ns_type = nfs_netns_object_child_ns_type,
 };
 
 int nfs_sysfs_init(void)
 {
-	int ret;
-
-	nfs_kset = kzalloc(sizeof(*nfs_kset), GFP_KERNEL);
+	nfs_kset = kset_type_create_and_add("nfs", NULL, fs_kobj,
+					    &nfs_kset_type);
 	if (!nfs_kset)
 		return -ENOMEM;
 
-	ret = kobject_set_name(&nfs_kset->kobj, "nfs");
-	if (ret) {
-		kfree(nfs_kset);
-		return ret;
-	}
-
-	nfs_kset->kobj.parent = fs_kobj;
-	nfs_kset->kobj.ktype = &nfs_kset_type;
-	nfs_kset->kobj.kset = NULL;
-
-	ret = kset_register(nfs_kset);
-	if (ret) {
-		kfree(nfs_kset);
-		return ret;
-	}
-
 	return 0;
 }
 
Signature · 1 line
2.43.0
#4 lore raw

PATCH 4/4 nfs: sysfs: use default get_ownership() callback

In reply to Alexander Aring

Since commit 5f81880d5204 ("sysfs, kobject: allow creating kobject belonging to arbitrary users") it seems that there could be cases for kobjects belonging to arbitrary users. This callback is set by default when using kset_create_and_add() to allow creating kobjects with different ownerships according to its parent.

This patch will assign the default callback now for nfs kobjects for cases when the parent has different ownership than the default one.

---
 fs/nfs/sysfs.c | 1 +
 1 file changed, 1 insertion(+)
fs/nfs/sysfs.c +1 -0
index a6584203b7ff..b5737464b892 100644
@@ -27,6 +27,7 @@ static const struct kobj_ns_type_operations *nfs_netns_object_child_ns_type(
 }
 
 static struct kobj_type nfs_kset_type = {
+	.get_ownership = kset_get_ownership,
 	.release = kset_release,
 	.sysfs_ops = &kobj_sysfs_ops,
 	.child_ns_type = nfs_netns_object_child_ns_type,
Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH 4/4] nfs: sysfs: use default get_ownership() callback

Quoted 28 lines · Alexander Aring

Since commit 5f81880d5204 ("sysfs, kobject: allow creating kobject belonging to arbitrary users") it seems that there could be cases for kobjects belonging to arbitrary users. This callback is set by default when using kset_create_and_add() to allow creating kobjects with different ownerships according to its parent.

This patch will assign the default callback now for nfs kobjects for cases when the parent has different ownership than the default one.

---
 fs/nfs/sysfs.c | 1 +
 1 file changed, 1 insertion(+)
fs/nfs/sysfs.c +1 -0
index a6584203b7ff..b5737464b892 100644
@@ -27,6 +27,7 @@ static const struct kobj_ns_type_operations *nfs_netns_object_child_ns_type(
 }
 
 static struct kobj_type nfs_kset_type = {
+	.get_ownership = kset_get_ownership,
 	.release = kset_release,
 	.sysfs_ops = &kobj_sysfs_ops,
 	.child_ns_type = nfs_netns_object_child_ns_type,
Signature · 1 line
2.43.0

Hi Alex, if I understand this correctly, this patch just punts the ownership callback up to fs_kobj, which, because it has no .get_ownership is just going to be the same result: root.

Does this patch add value?

Ben

#6 lore raw

Re: [PATCH 4/4] nfs: sysfs: use default get_ownership() callback

Hi,

Quoted 24 lines · Benjamin Coddington
Quoted 21 lines · Alexander Aring

Since commit 5f81880d5204 ("sysfs, kobject: allow creating kobject belonging to arbitrary users") it seems that there could be cases for kobjects belonging to arbitrary users. This callback is set by default when using kset_create_and_add() to allow creating kobjects with different ownerships according to its parent.

This patch will assign the default callback now for nfs kobjects for cases when the parent has different ownership than the default one.

---
 fs/nfs/sysfs.c | 1 +
 1 file changed, 1 insertion(+)
fs/nfs/sysfs.c +0 -0
index a6584203b7ff..b5737464b892 100644
@@ -27,6 +27,7 @@ static const struct kobj_ns_type_operations *nfs_netns_object_child_ns_type(
 }
 
Quoted 12 lines
Quoted 7 lines
fs/nfs/sysfs.c +1 -1
 static struct kobj_type nfs_kset_type = {
+     .get_ownership = kset_get_ownership,
      .release = kset_release,
      .sysfs_ops = &kobj_sysfs_ops,
      .child_ns_type = nfs_netns_object_child_ns_type,
--

2.43.0

Hi Alex, if I understand this correctly, this patch just punts the ownership callback up to fs_kobj, which, because it has no .get_ownership is just going to be the same result: root.

Yes, from my understanding this has to do with user namespaces and being able to write to the related sysfs/kobject files?

Quoted 2 lines

Does this patch add value?

For me no, but it is there now as default and I think somebody probably forgot to update all the other users that created their kset in a more low-level kind of way.
I think the whole "transitioning process" to make the same things with namespaces created under root vs user is still kind of still in process.

I am not sure if any of the nfs userspace namespace aware tools are also limited by other things that require additional changes to do something else as a "normal" user.

For me it's fine to drop this patch series and if somebody wants to make a user namespace working with nfs tooling can come back again and find out such patch might be necessary?

Thanks.

- Alex

#7 lore raw
In reply to Alexander Aring

Dear nfs tree maintainers,

Quoted 11 lines · Alexander Aring

Hi,

I currently have pending patches for fs/dlm (Distributed Lock Manager) subsystem to introduce some helpers to udev. However, it seems it takes more time that I can bring those changes upstream. I put those out now and already figured out that nfs can also take advantage of those changes.

With this patch-series I try to try to reduce my patch-series for DLM and already bring part of it upstream and nfs will be a user of it.

Quoted 7 lines

The ownership callback, I think it should be set as the kset_create_and_add() sets this callback as default. I never had any issues with it, but there might be container corner cases that requires those changes?

- Alex

Can we have those patches applied to the nfs tree? According to the udev/kobject maintainer it is fine to take this in any tree that it needs to go through. [0]
I made nfs as a first user of these new udev helpers functionality, so it should go through the nfs tree if there are no other issues with this series?

Thanks.

- Alex

[0] https://lore.kernel.org/gfs2/2024081519-caddy-monstrous-b37d@gregkh/

Reviewed-by on the v1 series
#8 lore raw
In reply to Alexander Aring
Quoted 23 lines · Alexander Aring

Hi,

I currently have pending patches for fs/dlm (Distributed Lock Manager) subsystem to introduce some helpers to udev. However, it seems it takes more time that I can bring those changes upstream. I put those out now and already figured out that nfs can also take advantage of those changes.

With this patch-series I try to try to reduce my patch-series for DLM and already bring part of it upstream and nfs will be a user of it.

The ownership callback, I think it should be set as the kset_create_and_add() sets this callback as default. I never had any issues with it, but there might be container corner cases that requires those changes?

- Alex

Alexander Aring (4):
kobject: add kset_type_create_and_add() helper
kobject: export generic helper ops
nfs: sysfs: use kset_type_create_and_add()
nfs: sysfs: use default get_ownership() callback

Quoted 7 lines
 fs/nfs/sysfs.c          | 30 +++----------------
 include/linux/kobject.h | 10 +++++--
 lib/kobject.c           | 65 ++++++++++++++++++++++++++++++-----------
 3 files changed, 60 insertions(+), 45 deletions(-)
Signature · 1 line
2.43.0

These look good to me, though patch 4/4 seems superfluous, I responded there.

For the series:

Ben

#9 lore raw
Quoted 30 lines · Benjamin Coddington
Quoted 28 lines · Alexander Aring

Hi,

I currently have pending patches for fs/dlm (Distributed Lock Manager) subsystem to introduce some helpers to udev. However, it seems it takes more time that I can bring those changes upstream. I put those out now and already figured out that nfs can also take advantage of those changes.

With this patch-series I try to try to reduce my patch-series for DLM and already bring part of it upstream and nfs will be a user of it.

The ownership callback, I think it should be set as the kset_create_and_add() sets this callback as default. I never had any issues with it, but there might be container corner cases that requires those changes?

- Alex

Alexander Aring (4):
kobject: add kset_type_create_and_add() helper
kobject: export generic helper ops
nfs: sysfs: use kset_type_create_and_add()
nfs: sysfs: use default get_ownership() callback

 fs/nfs/sysfs.c          | 30 +++----------------
 include/linux/kobject.h | 10 +++++--
 lib/kobject.c           | 65 ++++++++++++++++++++++++++++++-----------
 3 files changed, 60 insertions(+), 45 deletions(-)
Quoted 4 lines
Quoted 2 lines
Signature · 1 line
2.43.0

These look good to me, though patch 4/4 seems superfluous, I responded there.

They look good to me, too. They're not really a bugfix, so they'll probably go in through v6.13 (assuming Trond has no objections, it's his turn to do the merge) and not v6.12-rc.

Anna

Quoted 6 lines

For the series:

Ben