Re: [PATCH v2 14/18] sysfs: Allow symlinks to be added between sibling groups

Jonathan Cameron
2024-07-18T15:36:16Z

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));
> +}
> +