Message ID | 7b4e324bdcd5910c9460bb5fc37aaf354f596ebf.1719771133.git.lukas@wunner.de |
---|---|
State | New, archived |
Headers | show |
Series | PCI device authentication | expand |
On Sun, Jun 30, 2024 at 09:49:00PM +0200, Lukas Wunner wrote: > A subsequent commit has the need to create a symlink from an attribute > in a first group to an attribute in a second group. Both groups belong > to the same kobject. > > More specifically, each signature received from an authentication- > capable device is going to be represented by a file in the first group > and shall be accompanied by a symlink pointing to the certificate slot > in the second group which was used to generate the signature (a device > may have multiple certificate slots and each is represented by a > separate file in the second group): > > /sys/devices/.../signatures/0_certificate_chain -> .../certificates/slot0 > > There is already a sysfs_add_link_to_group() helper to add a symlink to > a group which points to another kobject, but this isn't what's needed > here. > > So add a new function to add a symlink among sibling groups of the same > kobject. > > The existing sysfs_add_link_to_group() helper goes through a locking > dance of acquiring sysfs_symlink_target_lock in order to acquire a > reference on the target kobject. That's unnecessary for the present > use case as the link itself and its target reside below the same > kobject. > > To simplify error handling in the newly introduced function, add a > DEFINE_FREE() clause for kernfs_put(). Nice! > > Signed-off-by: Lukas Wunner <lukas@wunner.de> Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
On Sun, 30 Jun 2024 21:49:00 +0200 Lukas Wunner <lukas@wunner.de> wrote: > A subsequent commit has the need to create a symlink from an attribute > in a first group to an attribute in a second group. Both groups belong > to the same kobject. > > More specifically, each signature received from an authentication- > capable device is going to be represented by a file in the first group > and shall be accompanied by a symlink pointing to the certificate slot > in the second group which was used to generate the signature (a device > may have multiple certificate slots and each is represented by a > separate file in the second group): > > /sys/devices/.../signatures/0_certificate_chain -> .../certificates/slot0 > > There is already a sysfs_add_link_to_group() helper to add a symlink to > a group which points to another kobject, but this isn't what's needed > here. > > So add a new function to add a symlink among sibling groups of the same > kobject. > > The existing sysfs_add_link_to_group() helper goes through a locking > dance of acquiring sysfs_symlink_target_lock in order to acquire a > reference on the target kobject. That's unnecessary for the present > use case as the link itself and its target reside below the same > kobject. > > To simplify error handling in the newly introduced function, add a > DEFINE_FREE() clause for kernfs_put(). > > Signed-off-by: Lukas Wunner <lukas@wunner.de> Nice in general. A few minor comments inline. > --- > fs/sysfs/group.c | 33 +++++++++++++++++++++++++++++++++ > include/linux/kernfs.h | 2 ++ > include/linux/sysfs.h | 10 ++++++++++ > 3 files changed, 45 insertions(+) > > diff --git a/fs/sysfs/group.c b/fs/sysfs/group.c > index d22ad67a0f32..0cb52c9b9e19 100644 > --- a/fs/sysfs/group.c > +++ b/fs/sysfs/group.c > @@ -445,6 +445,39 @@ void sysfs_remove_link_from_group(struct kobject *kobj, const char *group_name, > } > EXPORT_SYMBOL_GPL(sysfs_remove_link_from_group); > > +/** > + * sysfs_add_link_to_sibling_group - add a symlink to a sibling attribute group. > + * @kobj: The kobject containing the groups. > + * @link_grp: The name of the group in which to create the symlink. > + * @link: The name of the symlink to create. Maybe should go with the link_name naming used in sysfs_add_link_to group. > + * @target_grp: The name of the target group. > + * @target: The name of the target attribute. > + * > + * Returns 0 on success or error code on failure. > + */ > +int sysfs_add_link_to_sibling_group(struct kobject *kobj, > + const char *link_grp, const char *link, > + const char *target_grp, const char *target) > +{ > + struct kernfs_node *target_grp_kn __free(kernfs_put), > + *target_kn __free(kernfs_put) = NULL, > + *link_grp_kn __free(kernfs_put) = NULL; Maybe just define these when used (similar to earlier reviews) rather than in one clump up here. Given they are all doing the same thing maybe it's not worth the effort though. > + > + target_grp_kn = kernfs_find_and_get(kobj->sd, target_grp); > + if (!target_grp_kn) > + return -ENOENT; > + > + target_kn = kernfs_find_and_get(target_grp_kn, target); > + if (!target_kn) > + return -ENOENT; > + > + link_grp_kn = kernfs_find_and_get(kobj->sd, link_grp); > + if (!link_grp_kn) > + return -ENOENT; > + > + return PTR_ERR_OR_ZERO(kernfs_create_link(link_grp_kn, link, target_kn)); > +} > +
diff --git a/fs/sysfs/group.c b/fs/sysfs/group.c index d22ad67a0f32..0cb52c9b9e19 100644 --- a/fs/sysfs/group.c +++ b/fs/sysfs/group.c @@ -445,6 +445,39 @@ void sysfs_remove_link_from_group(struct kobject *kobj, const char *group_name, } EXPORT_SYMBOL_GPL(sysfs_remove_link_from_group); +/** + * sysfs_add_link_to_sibling_group - add a symlink to a sibling attribute group. + * @kobj: The kobject containing the groups. + * @link_grp: The name of the group in which to create the symlink. + * @link: The name of the symlink to create. + * @target_grp: The name of the target group. + * @target: The name of the target attribute. + * + * Returns 0 on success or error code on failure. + */ +int sysfs_add_link_to_sibling_group(struct kobject *kobj, + const char *link_grp, const char *link, + const char *target_grp, const char *target) +{ + struct kernfs_node *target_grp_kn __free(kernfs_put), + *target_kn __free(kernfs_put) = NULL, + *link_grp_kn __free(kernfs_put) = NULL; + + target_grp_kn = kernfs_find_and_get(kobj->sd, target_grp); + if (!target_grp_kn) + return -ENOENT; + + target_kn = kernfs_find_and_get(target_grp_kn, target); + if (!target_kn) + return -ENOENT; + + link_grp_kn = kernfs_find_and_get(kobj->sd, link_grp); + if (!link_grp_kn) + return -ENOENT; + + return PTR_ERR_OR_ZERO(kernfs_create_link(link_grp_kn, link, target_kn)); +} + /** * compat_only_sysfs_link_entry_to_kobj - add a symlink to a kobject pointing * to a group or an attribute diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h index 87c79d076d6d..d5726d070dba 100644 --- a/include/linux/kernfs.h +++ b/include/linux/kernfs.h @@ -407,6 +407,8 @@ struct kernfs_node *kernfs_walk_and_get_ns(struct kernfs_node *parent, void kernfs_get(struct kernfs_node *kn); void kernfs_put(struct kernfs_node *kn); +DEFINE_FREE(kernfs_put, struct kernfs_node *, if (_T) kernfs_put(_T)) + struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry); struct kernfs_root *kernfs_root_from_sb(struct super_block *sb); struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn); diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h index aff1d81e8971..6f970832bd36 100644 --- a/include/linux/sysfs.h +++ b/include/linux/sysfs.h @@ -465,6 +465,9 @@ int sysfs_add_link_to_group(struct kobject *kobj, const char *group_name, struct kobject *target, const char *link_name); void sysfs_remove_link_from_group(struct kobject *kobj, const char *group_name, const char *link_name); +int sysfs_add_link_to_sibling_group(struct kobject *kobj, + const char *link_grp, const char *link, + const char *target_grp, const char *target); int compat_only_sysfs_link_entry_to_kobj(struct kobject *kobj, struct kobject *target_kobj, const char *target_name, @@ -702,6 +705,13 @@ static inline void sysfs_remove_link_from_group(struct kobject *kobj, { } +static inline int sysfs_add_link_to_sibling_group(struct kobject *kobj, + const char *link_grp, const char *link, + const char *target_grp, const char *target) +{ + return 0; +} + static inline int compat_only_sysfs_link_entry_to_kobj(struct kobject *kobj, struct kobject *target_kobj, const char *target_name,
A subsequent commit has the need to create a symlink from an attribute in a first group to an attribute in a second group. Both groups belong to the same kobject. More specifically, each signature received from an authentication- capable device is going to be represented by a file in the first group and shall be accompanied by a symlink pointing to the certificate slot in the second group which was used to generate the signature (a device may have multiple certificate slots and each is represented by a separate file in the second group): /sys/devices/.../signatures/0_certificate_chain -> .../certificates/slot0 There is already a sysfs_add_link_to_group() helper to add a symlink to a group which points to another kobject, but this isn't what's needed here. So add a new function to add a symlink among sibling groups of the same kobject. The existing sysfs_add_link_to_group() helper goes through a locking dance of acquiring sysfs_symlink_target_lock in order to acquire a reference on the target kobject. That's unnecessary for the present use case as the link itself and its target reside below the same kobject. To simplify error handling in the newly introduced function, add a DEFINE_FREE() clause for kernfs_put(). Signed-off-by: Lukas Wunner <lukas@wunner.de> --- fs/sysfs/group.c | 33 +++++++++++++++++++++++++++++++++ include/linux/kernfs.h | 2 ++ include/linux/sysfs.h | 10 ++++++++++ 3 files changed, 45 insertions(+)