Received: by 2002:a89:48b:0:b0:1f5:f2ab:c469 with SMTP id a11csp938849lqd; Thu, 25 Apr 2024 00:52:20 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXJbfSEpEvBU2/uCeVz+IKOfupO8WLAjsEqzjdIIt1NvCn8S9FyRHtAAVCyrmnLE0bXdS3yXjT1RhJ89XhhRxc/JYj9PU7jbsH2x+6m0g== X-Google-Smtp-Source: AGHT+IFfmSEpnuj6IQLGuI2sjSHaY6xUsKKbMTUT8dC/VcrPW5ejXFisE0gaz5llPWk4RelvXzAb X-Received: by 2002:a50:c90a:0:b0:572:3cc4:2dcf with SMTP id o10-20020a50c90a000000b005723cc42dcfmr1040432edh.14.1714031540483; Thu, 25 Apr 2024 00:52:20 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714031540; cv=pass; d=google.com; s=arc-20160816; b=FFd181KuS8gaUQ2pS8GeinMfkBdnyvWM+58JtVs4qoRhA0UW+l75oDufoDgAG/MrLW s/vzz3lyLsng5EqMtaqHpUXzZNoPmwiAmRNKCPg/3Y1qz9XBP+qnlJP5XJc0/1Cp1AVG o3GMMtqdR9F6Hge3XK5jChFjWCoFHk7mRfFh3ExK3NN3jkMXvYZ0WkIDHBYSoahZTlkA DnmLUpnHWyanMm5QbC2sMx/FZq4DROeYC7TbBjZ25wPriGXM5BWpMgN2Yf5/u+Tay3YR aNkvWyGFWFTGUT9J/IpYHEePmnLZYZKGW35DxhCG49w6zOpNwCsbBQIUYbrOjimUJdcw PdYg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:subject:date:from:message-id; bh=iavhiPocWJ+kdk4EfIKmCXgdNJ1eQV4T07VnzkFIiNQ=; fh=sm5GyoETMMU6Q5CiJtNyHRrH0SbxucgodeTEkaILLZg=; b=EvOKcupwpk83iPdK++Ol17VERKxBt/M8Ep9FGGNVw2yT9d6VFOieFL/n32x2TiABsB Lmai4KiXQsjeKTDLUduuAxQWGuaNWCbMWwKuXeORNr+rQ9H9AWgV/SiBsT3Up20PcTqk swyhPs8cEJ0pV9SwVeRuHkCUsa3XV+1o4GhqRdC7aKfTn+nyTnluHiNFKUBQxWqTmLSV h0R4S8WH+gkJddn8N88clZkfYb8SkzpzLi68hbKw/v/H6+GCZH/zW6e7zfAG4rWYKnmc /KgOMeUZ7b9ZL+TCzcQ9942SKhLXyFGVO5dVzl+1/wj9UFNrQpQHoeQIua5oFONeYbEd +vFA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=wunner.de); spf=pass (google.com: domain of linux-kernel+bounces-158153-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-158153-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id j12-20020a05640211cc00b0056e655e9b73si5331597edw.560.2024.04.25.00.52.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 00:52:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-158153-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=wunner.de); spf=pass (google.com: domain of linux-kernel+bounces-158153-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-158153-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 6C4F71F21679 for ; Thu, 25 Apr 2024 07:52:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6C0BF78C7F; Thu, 25 Apr 2024 07:51:37 +0000 (UTC) Received: from mailout2.hostsharing.net (mailout2.hostsharing.net [83.223.78.233]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 987A278B4C for ; Thu, 25 Apr 2024 07:51:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=83.223.78.233 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714031496; cv=none; b=lhGHtBHzez2igG+sUU2OKzfjAvhSgpqgqpBi6uTLF7Czrs9AGyOzJp26iwLtvN5JhEgxBg8HBxyahnKJVkSPxEf4g1Nf4OeThLbxM3/R/A3g964LdOlpmzI0HrSVgVQJx/wwMqAHEBKMCBdcnVr9q4eG4eG4I2er0psKIEan5wQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714031496; c=relaxed/simple; bh=Y9dx/WwcpcflNCFGmLro4NFAEdgR2KN+bCs5qWVd7bU=; h=Message-ID:From:Date:Subject:MIME-Version:Content-Type:To:Cc; b=H8OMrC7MBBH+gABqR3zt4bJf23ve/HoqeF6R1MK9+zJVJJhX22W51WwLtNnvuJGhx4w4vstXNGkYhH0sTXT0oUhF3XlW506texctPvsRamFD+o/ays67yRG50O1vWEaqPtBjluSo3dqVcnWkE0kjPD/GM4dPMGiSMadfmyHAFsE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=wunner.de; spf=pass smtp.mailfrom=wunner.de; arc=none smtp.client-ip=83.223.78.233 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=wunner.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=wunner.de Received: from h08.hostsharing.net (h08.hostsharing.net [IPv6:2a01:37:1000::53df:5f1c:0]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "*.hostsharing.net", Issuer "RapidSSL TLS RSA CA G1" (verified OK)) by mailout2.hostsharing.net (Postfix) with ESMTPS id D9B5D10189E13; Thu, 25 Apr 2024 09:44:42 +0200 (CEST) Received: from localhost (unknown [89.246.108.87]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by h08.hostsharing.net (Postfix) with ESMTPSA id A811E602F91F; Thu, 25 Apr 2024 09:44:42 +0200 (CEST) X-Mailbox-Line: From e140f6bf8dc99c24cc387f7ac3c7908c8830587f Mon Sep 17 00:00:00 2001 Message-ID: From: Lukas Wunner Date: Thu, 25 Apr 2024 09:44:22 +0200 Subject: [PATCH] sysfs: Allow bin_attributes to be added to groups Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To: Greg Kroah-Hartman Cc: "Rafael J. Wysocki" , linux-kernel@vger.kernel.org, Alan Stern , Jonathan Cameron , Dan Williams Commit dfa87c824a9a ("sysfs: allow attributes to be added to groups") introduced dynamic addition of sysfs attributes to groups. Allow the same for bin_attributes, in support of a forthcoming commit which adds various bin_attributes every time a PCI device is authenticated. Addition of bin_attributes to groups differs from regular attributes in that different kernfs_ops are selected by sysfs_add_bin_file_mode_ns() vis-à-vis sysfs_add_file_mode_ns(). So call either of those two functions from sysfs_add_file_to_group() based on an additional boolean parameter and add two wrapper functions, one for bin_attributes and another for regular attributes. Removal of bin_attributes from groups does not require a differentiation for bin_attributes and can use the same code path as regular attributes. Signed-off-by: Lukas Wunner Cc: Alan Stern --- Submitting this ahead of my PCI device authentication v2 patches. Not sure if the patch is acceptable without an accompanying user, but even if it's not, perhaps someone has early review feedback or wants to provide an Acked-by? Thank you! fs/sysfs/file.c | 69 ++++++++++++++++++++++++++++++++++++------- include/linux/sysfs.h | 19 ++++++++++++ 2 files changed, 78 insertions(+), 10 deletions(-) diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c index d1995e2d6c94..9268232781b5 100644 --- a/fs/sysfs/file.c +++ b/fs/sysfs/file.c @@ -383,14 +383,14 @@ int sysfs_create_files(struct kobject *kobj, const struct attribute * const *ptr } EXPORT_SYMBOL_GPL(sysfs_create_files); -/** - * sysfs_add_file_to_group - add an attribute file to a pre-existing group. - * @kobj: object we're acting for. - * @attr: attribute descriptor. - * @group: group name. - */ -int sysfs_add_file_to_group(struct kobject *kobj, - const struct attribute *attr, const char *group) +static const struct bin_attribute *to_bin_attr(const struct attribute *attr) +{ + return container_of(attr, struct bin_attribute, attr); +} + +static int __sysfs_add_file_to_group(struct kobject *kobj, + const struct attribute *attr, + const char *group, bool is_bin_attr) { struct kernfs_node *parent; kuid_t uid; @@ -408,14 +408,49 @@ int sysfs_add_file_to_group(struct kobject *kobj, return -ENOENT; kobject_get_ownership(kobj, &uid, &gid); - error = sysfs_add_file_mode_ns(parent, attr, attr->mode, uid, gid, - NULL); + if (is_bin_attr) + error = sysfs_add_bin_file_mode_ns(parent, to_bin_attr(attr), + attr->mode, uid, gid, NULL); + else + error = sysfs_add_file_mode_ns(parent, attr, + attr->mode, uid, gid, NULL); kernfs_put(parent); return error; } + +/** + * sysfs_add_file_to_group - add an attribute file to a pre-existing group. + * @kobj: object we're acting for. + * @attr: attribute descriptor. + * @group: group name. + * + * Returns 0 on success or error code on failure. + */ +int sysfs_add_file_to_group(struct kobject *kobj, + const struct attribute *attr, + const char *group) +{ + return __sysfs_add_file_to_group(kobj, attr, group, false); +} EXPORT_SYMBOL_GPL(sysfs_add_file_to_group); +/** + * sysfs_add_bin_file_to_group - add bin_attribute file to pre-existing group. + * @kobj: object we're acting for. + * @attr: attribute descriptor. + * @group: group name. + * + * Returns 0 on success or error code on failure. + */ +int sysfs_add_bin_file_to_group(struct kobject *kobj, + const struct bin_attribute *attr, + const char *group) +{ + return __sysfs_add_file_to_group(kobj, &attr->attr, group, true); +} +EXPORT_SYMBOL_GPL(sysfs_add_bin_file_to_group); + /** * sysfs_chmod_file - update the modified mode value on an object attribute. * @kobj: object we're acting for. @@ -565,6 +600,20 @@ void sysfs_remove_file_from_group(struct kobject *kobj, } EXPORT_SYMBOL_GPL(sysfs_remove_file_from_group); +/** + * sysfs_remove_bin_file_from_group - remove bin_attribute file from group. + * @kobj: object we're acting for. + * @attr: attribute descriptor. + * @group: group name. + */ +void sysfs_remove_bin_file_from_group(struct kobject *kobj, + const struct bin_attribute *attr, + const char *group) +{ + sysfs_remove_file_from_group(kobj, &attr->attr, group); +} +EXPORT_SYMBOL_GPL(sysfs_remove_bin_file_from_group); + /** * sysfs_create_bin_file - create binary file for object. * @kobj: object. diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h index a7d725fbf739..aff1d81e8971 100644 --- a/include/linux/sysfs.h +++ b/include/linux/sysfs.h @@ -451,6 +451,12 @@ int sysfs_add_file_to_group(struct kobject *kobj, const struct attribute *attr, const char *group); void sysfs_remove_file_from_group(struct kobject *kobj, const struct attribute *attr, const char *group); +int sysfs_add_bin_file_to_group(struct kobject *kobj, + const struct bin_attribute *attr, + const char *group); +void sysfs_remove_bin_file_from_group(struct kobject *kobj, + const struct bin_attribute *attr, + const char *group); int sysfs_merge_group(struct kobject *kobj, const struct attribute_group *grp); void sysfs_unmerge_group(struct kobject *kobj, @@ -660,6 +666,19 @@ static inline void sysfs_remove_file_from_group(struct kobject *kobj, { } +static inline int sysfs_add_bin_file_to_group(struct kobject *kobj, + const struct bin_attribute *attr, + const char *group) +{ + return 0; +} + +static inline void sysfs_remove_bin_file_from_group(struct kobject *kobj, + const struct bin_attribute *attr, + const char *group) +{ +} + static inline int sysfs_merge_group(struct kobject *kobj, const struct attribute_group *grp) { -- 2.43.0