From patchwork Mon Feb 17 16:14:27 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 212837 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E010CC34021 for ; Mon, 17 Feb 2020 16:14:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id BDAED208C4 for ; Mon, 17 Feb 2020 16:14:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728731AbgBQQOq (ORCPT ); Mon, 17 Feb 2020 11:14:46 -0500 Received: from youngberry.canonical.com ([91.189.89.112]:49196 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726646AbgBQQOp (ORCPT ); Mon, 17 Feb 2020 11:14:45 -0500 Received: from ip5f5bf7ec.dynamic.kabel-deutschland.de ([95.91.247.236] helo=wittgenstein.fritz.box) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1j3j2g-0003Cp-3h; Mon, 17 Feb 2020 16:14:42 +0000 From: Christian Brauner To: "David S. Miller" , Greg Kroah-Hartman , linux-kernel@vger.kernel.org, netdev@vger.kernel.org Cc: "Rafael J. Wysocki" , Pavel Machek , Jakub Kicinski , Eric Dumazet , Stephen Hemminger , linux-pm@vger.kernel.org, Christian Brauner Subject: [PATCH net-next v2 01/10] sysfs: add sysfs_file_change_owner{_by_name}() Date: Mon, 17 Feb 2020 17:14:27 +0100 Message-Id: <20200217161436.1748598-2-christian.brauner@ubuntu.com> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200217161436.1748598-1-christian.brauner@ubuntu.com> References: <20200217161436.1748598-1-christian.brauner@ubuntu.com> MIME-Version: 1.0 Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Add helpers to change owner of a sysfs files. The ownership of a sysfs object is determined based on the ownership of the corresponding kobject, i.e. only if the ownership of a kobject is changed will this function change the ownership of the corresponding sysfs entry. This function will be used to correctly account for kobject ownership changes, e.g. when moving network devices between network namespaces. Signed-off-by: Christian Brauner --- /* v2 */ - Greg Kroah-Hartman : - Better naming for sysfs_file_change_owner() to reflect the fact that it can be used to change the owner of the kobject itself by passing NULL as argument. - Christian Brauner : - Split sysfs_file_change_owner() into two helpers sysfs_change_owner() and sysfs_change_owner_by_name(). The former changes the owner of the kobject itself, the latter the owner of the kobject looked up via the name argument. --- fs/sysfs/file.c | 82 +++++++++++++++++++++++++++++++++++++++++++ include/linux/sysfs.h | 14 ++++++++ 2 files changed, 96 insertions(+) diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c index 130fc6fbcc03..8f2607de2456 100644 --- a/fs/sysfs/file.c +++ b/fs/sysfs/file.c @@ -558,3 +558,85 @@ void sysfs_remove_bin_file(struct kobject *kobj, kernfs_remove_by_name(kobj->sd, attr->attr.name); } EXPORT_SYMBOL_GPL(sysfs_remove_bin_file); + +static int internal_change_owner(struct kernfs_node *kn, struct kobject *kobj) +{ + kuid_t uid; + kgid_t gid; + struct iattr newattrs = { + .ia_valid = ATTR_UID | ATTR_GID, + }; + + kobject_get_ownership(kobj, &uid, &gid); + newattrs.ia_uid = uid; + newattrs.ia_gid = gid; + + return kernfs_setattr(kn, &newattrs); +} + +/** + * sysfs_file_change_owner_by_name - change owner of a file. + * @kobj: object. + * @name: name of the file to change. + * + * To change the ownership of a sysfs object, the caller must first change the + * uid/gid of the kobject and then call this function. Usually this will be + * taken care of by the relevant subsystem, e.g. moving a network device + * between network namespaces owned by different user namespaces will change + * the uid/gid of the kobject to the uid/gid of the root user in the user + * namespace. Calling this function afterwards will cause the sysfs object to + * reflect the new uid/gid. + */ +int sysfs_file_change_owner_by_name(struct kobject *kobj, const char *name) +{ + struct kernfs_node *kn; + int error; + + if (!name) + return -EINVAL; + + if (!kobj->state_in_sysfs) + return -EINVAL; + + kn = kernfs_find_and_get(kobj->sd, name); + if (!kn) + return -ENOENT; + + error = internal_change_owner(kn, kobj); + + kernfs_put(kn); + + return error; +} +EXPORT_SYMBOL_GPL(sysfs_file_change_owner_by_name); + +/** + * sysfs_file_change_owner - change owner of a file. + * @kobj: object. + * + * To change the ownership of a sysfs object, the caller must first change the + * uid/gid of the kobject and then call this function. Usually this will be + * taken care of by the relevant subsystem, e.g. moving a network device + * between network namespaces owned by different user namespaces will change + * the uid/gid of the kobject to the uid/gid of the root user in the user + * namespace. Calling this function afterwards will cause the sysfs object to + * reflect the new uid/gid. + */ +int sysfs_file_change_owner(struct kobject *kobj) +{ + struct kernfs_node *kn; + int error; + + if (!kobj->state_in_sysfs) + return -EINVAL; + + kernfs_get(kobj->sd); + + kn = kobj->sd; + error = internal_change_owner(kn, kobj); + + kernfs_put(kn); + + return error; +} +EXPORT_SYMBOL_GPL(sysfs_file_change_owner); diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h index fa7ee503fb76..4b3c3b76ff80 100644 --- a/include/linux/sysfs.h +++ b/include/linux/sysfs.h @@ -310,6 +310,9 @@ static inline void sysfs_enable_ns(struct kernfs_node *kn) return kernfs_enable_ns(kn); } +int sysfs_file_change_owner(struct kobject *kobj); +int sysfs_file_change_owner_by_name(struct kobject *kobj, const char *name); + #else /* CONFIG_SYSFS */ static inline int sysfs_create_dir_ns(struct kobject *kobj, const void *ns) @@ -522,6 +525,17 @@ static inline void sysfs_enable_ns(struct kernfs_node *kn) { } +static inline int int sysfs_file_change_owner(struct kobject *kobj) +{ + return 0; +} + +static inline int sysfs_file_change_owner_by_name(struct kobject *kobj, + const char *name) +{ + return 0; +} + #endif /* CONFIG_SYSFS */ static inline int __must_check sysfs_create_file(struct kobject *kobj, From patchwork Mon Feb 17 16:14:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 212833 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 31C14C34021 for ; Mon, 17 Feb 2020 16:15:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 16172208C4 for ; Mon, 17 Feb 2020 16:15:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728778AbgBQQPc (ORCPT ); Mon, 17 Feb 2020 11:15:32 -0500 Received: from youngberry.canonical.com ([91.189.89.112]:49206 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727217AbgBQQOq (ORCPT ); Mon, 17 Feb 2020 11:14:46 -0500 Received: from ip5f5bf7ec.dynamic.kabel-deutschland.de ([95.91.247.236] helo=wittgenstein.fritz.box) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1j3j2h-0003Cp-Tm; Mon, 17 Feb 2020 16:14:44 +0000 From: Christian Brauner To: "David S. Miller" , Greg Kroah-Hartman , linux-kernel@vger.kernel.org, netdev@vger.kernel.org Cc: "Rafael J. Wysocki" , Pavel Machek , Jakub Kicinski , Eric Dumazet , Stephen Hemminger , linux-pm@vger.kernel.org, Christian Brauner Subject: [PATCH net-next v2 04/10] sysfs: add sysfs_groups_change_owner() Date: Mon, 17 Feb 2020 17:14:30 +0100 Message-Id: <20200217161436.1748598-5-christian.brauner@ubuntu.com> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200217161436.1748598-1-christian.brauner@ubuntu.com> References: <20200217161436.1748598-1-christian.brauner@ubuntu.com> MIME-Version: 1.0 Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Add a helper to change the owner of a set of sysfs groups file. The ownership of a sysfs object is determined based on the ownership of the corresponding kobject, i.e. only if the ownership of a kobject is changed will this function change the ownership of the corresponding sysfs entry. This function will be used to correctly account for kobject ownership changes, e.g. when moving network devices between network namespaces. Signed-off-by: Christian Brauner --- /* v2 */ - Greg Kroah-Hartman : - Add comment how ownership of sysfs object is changed. --- fs/sysfs/group.c | 36 ++++++++++++++++++++++++++++++++++++ include/linux/sysfs.h | 8 ++++++++ 2 files changed, 44 insertions(+) diff --git a/fs/sysfs/group.c b/fs/sysfs/group.c index 7d158d59d097..0fc7b119a5dd 100644 --- a/fs/sysfs/group.c +++ b/fs/sysfs/group.c @@ -550,3 +550,39 @@ int sysfs_group_change_owner(struct kobject *kobj, return error; } EXPORT_SYMBOL_GPL(sysfs_group_change_owner); + +/** + * sysfs_groups_change_owner - change owner of a set of attribute groups. + * @kobj: The kobject containing the groups. + * @groups: The attribute groups. + * + * To change the ownership of a sysfs object, the caller must first change the + * uid/gid of the kobject and then call this function. Usually this will be + * taken care of by the relevant subsystem, e.g. moving a network device + * between network namespaces owned by different user namespaces will change + * the uid/gid of the kobject to the uid/gid of the root user in the user + * namespace. Calling this function afterwards will cause the sysfs object to + * reflect the new uid/gid. + * + * Returns 0 on success or error code on failure. + */ +int sysfs_groups_change_owner(struct kobject *kobj, + const struct attribute_group **groups) +{ + int error = 0, i; + + if (!kobj->state_in_sysfs) + return -EINVAL; + + if (!groups) + return 0; + + for (i = 0; groups[i]; i++) { + error = sysfs_group_change_owner(kobj, groups[i]); + if (error) + break; + } + + return error; +} +EXPORT_SYMBOL_GPL(sysfs_groups_change_owner); diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h index dd211f0a654b..d13c6cc8e487 100644 --- a/include/linux/sysfs.h +++ b/include/linux/sysfs.h @@ -314,6 +314,8 @@ int sysfs_file_change_owner(struct kobject *kobj); int sysfs_file_change_owner_by_name(struct kobject *kobj, const char *name); int sysfs_link_change_owner(struct kobject *kobj, struct kobject *targ, const char *name); +int sysfs_groups_change_owner(struct kobject *kobj, + const struct attribute_group **groups); int sysfs_group_change_owner(struct kobject *kobj, const struct attribute_group *groups); @@ -547,6 +549,12 @@ static inline int sysfs_link_change_owner(struct kobject *kobj, return 0; } +static inline int sysfs_groups_change_owner(struct kobject *kobj, + const struct attribute_group **groups) +{ + return 0; +} + static inline int sysfs_group_change_owner(struct kobject *kobj, const struct attribute_group **groups) { From patchwork Mon Feb 17 16:14:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 212835 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6F40BC34021 for ; Mon, 17 Feb 2020 16:15:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 516EB20718 for ; Mon, 17 Feb 2020 16:15:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729100AbgBQQOt (ORCPT ); Mon, 17 Feb 2020 11:14:49 -0500 Received: from youngberry.canonical.com ([91.189.89.112]:49212 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727926AbgBQQOr (ORCPT ); Mon, 17 Feb 2020 11:14:47 -0500 Received: from ip5f5bf7ec.dynamic.kabel-deutschland.de ([95.91.247.236] helo=wittgenstein.fritz.box) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1j3j2i-0003Cp-Fz; Mon, 17 Feb 2020 16:14:44 +0000 From: Christian Brauner To: "David S. Miller" , Greg Kroah-Hartman , linux-kernel@vger.kernel.org, netdev@vger.kernel.org Cc: "Rafael J. Wysocki" , Pavel Machek , Jakub Kicinski , Eric Dumazet , Stephen Hemminger , linux-pm@vger.kernel.org, Christian Brauner Subject: [PATCH net-next v2 05/10] sysfs: add sysfs_change_owner() Date: Mon, 17 Feb 2020 17:14:31 +0100 Message-Id: <20200217161436.1748598-6-christian.brauner@ubuntu.com> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200217161436.1748598-1-christian.brauner@ubuntu.com> References: <20200217161436.1748598-1-christian.brauner@ubuntu.com> MIME-Version: 1.0 Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Add a helper to change the owner of sysfs objects. The ownership of a sysfs object is determined based on the ownership of the corresponding kobject, i.e. only if the ownership of a kobject is changed will this function change the ownership of the corresponding sysfs entry. This function will be used to correctly account for kobject ownership changes, e.g. when moving network devices between network namespaces. Signed-off-by: Christian Brauner --- /* v2 */ - Greg Kroah-Hartman : - Add comment how ownership of sysfs object is changed. --- fs/sysfs/file.c | 43 +++++++++++++++++++++++++++++++++++++++++++ include/linux/sysfs.h | 6 ++++++ 2 files changed, 49 insertions(+) diff --git a/fs/sysfs/file.c b/fs/sysfs/file.c index 8b20245f359c..f88b8bf2fcc3 100644 --- a/fs/sysfs/file.c +++ b/fs/sysfs/file.c @@ -686,3 +686,46 @@ int sysfs_file_change_owner(struct kobject *kobj) return error; } EXPORT_SYMBOL_GPL(sysfs_file_change_owner); + +/** + * sysfs_change_owner - change owner of the given object. + * @kobj: object. + * + * To change the ownership of a sysfs object, the caller must first change the + * uid/gid of the kobject and then call this function. Usually this will be + * taken care of by the relevant subsystem, e.g. moving a network device + * between network namespaces owned by different user namespaces will change + * the uid/gid of the kobject to the uid/gid of the root user in the user + * namespace. Calling this function afterwards will cause the sysfs object to + * reflect the new uid/gid. + */ +int sysfs_change_owner(struct kobject *kobj) +{ + int error; + const struct kobj_type *ktype; + + if (!kobj->state_in_sysfs) + return -EINVAL; + + error = sysfs_file_change_owner(kobj); + if (error) + return error; + + ktype = get_ktype(kobj); + if (ktype) { + struct attribute **kattr; + + for (kattr = ktype->default_attrs; kattr && *kattr; kattr++) { + error = sysfs_file_change_owner_by_name(kobj, (*kattr)->name); + if (error) + return error; + } + + error = sysfs_groups_change_owner(kobj, ktype->default_groups); + if (error) + return error; + } + + return 0; +} +EXPORT_SYMBOL_GPL(sysfs_change_owner); diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h index d13c6cc8e487..ce5165a111bc 100644 --- a/include/linux/sysfs.h +++ b/include/linux/sysfs.h @@ -312,6 +312,7 @@ static inline void sysfs_enable_ns(struct kernfs_node *kn) int sysfs_file_change_owner(struct kobject *kobj); int sysfs_file_change_owner_by_name(struct kobject *kobj, const char *name); +int sysfs_change_owner(struct kobject *kobj); int sysfs_link_change_owner(struct kobject *kobj, struct kobject *targ, const char *name); int sysfs_groups_change_owner(struct kobject *kobj, @@ -549,6 +550,11 @@ static inline int sysfs_link_change_owner(struct kobject *kobj, return 0; } +static inline int sysfs_change_owner(struct kobject *kobj) +{ + return 0; +} + static inline int sysfs_groups_change_owner(struct kobject *kobj, const struct attribute_group **groups) { From patchwork Mon Feb 17 16:14:32 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 212834 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E95AFC34021 for ; Mon, 17 Feb 2020 16:15:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id CA70A214D8 for ; Mon, 17 Feb 2020 16:15:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727280AbgBQQPU (ORCPT ); Mon, 17 Feb 2020 11:15:20 -0500 Received: from youngberry.canonical.com ([91.189.89.112]:49218 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728778AbgBQQOr (ORCPT ); Mon, 17 Feb 2020 11:14:47 -0500 Received: from ip5f5bf7ec.dynamic.kabel-deutschland.de ([95.91.247.236] helo=wittgenstein.fritz.box) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1j3j2j-0003Cp-F4; Mon, 17 Feb 2020 16:14:45 +0000 From: Christian Brauner To: "David S. Miller" , Greg Kroah-Hartman , linux-kernel@vger.kernel.org, netdev@vger.kernel.org Cc: "Rafael J. Wysocki" , Pavel Machek , Jakub Kicinski , Eric Dumazet , Stephen Hemminger , linux-pm@vger.kernel.org, Christian Brauner Subject: [PATCH net-next v2 06/10] device: add device_change_owner() Date: Mon, 17 Feb 2020 17:14:32 +0100 Message-Id: <20200217161436.1748598-7-christian.brauner@ubuntu.com> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200217161436.1748598-1-christian.brauner@ubuntu.com> References: <20200217161436.1748598-1-christian.brauner@ubuntu.com> MIME-Version: 1.0 Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Add a helper to change the owner of a device's sysfs entries. This needs to happen when the ownership of a device is changed, e.g. when moving network devices between network namespaces. The ownership of a device is determined based on the ownership of the corresponding kobject, i.e. only if the ownership of a kobject is changed will this function change the ownership of the corresponding device's sysfs entries. This function will be used to correctly account for ownership changes, e.g. when moving network devices between network namespaces. Signed-off-by: Christian Brauner --- /* v2 */ unchanged --- drivers/base/core.c | 73 ++++++++++++++++++++++++++++++++++++++++++ include/linux/device.h | 1 + 2 files changed, 74 insertions(+) diff --git a/drivers/base/core.c b/drivers/base/core.c index 42a672456432..915766c20a47 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -3458,6 +3458,79 @@ int device_move(struct device *dev, struct device *new_parent, } EXPORT_SYMBOL_GPL(device_move); +static int device_attrs_change_owner(struct device *dev) +{ + struct kobject *kobj = &dev->kobj; + struct class *class = dev->class; + const struct device_type *type = dev->type; + int error; + + if (class) { + error = sysfs_groups_change_owner(kobj, class->dev_groups); + if (error) + return error; + } + + if (type) { + error = sysfs_groups_change_owner(kobj, type->groups); + if (error) + return error; + } + + error = sysfs_groups_change_owner(kobj, dev->groups); + if (error) + return error; + + if (device_supports_offline(dev) && !dev->offline_disabled) { + error = sysfs_file_change_owner_by_name(kobj, dev_attr_online.attr.name); + if (error) + return error; + } + + return 0; +} + +/** + * device_change_owner - change the owner of an existing device. + * @dev: device. + */ +int device_change_owner(struct device *dev) +{ + int error; + struct kobject *kobj = &dev->kobj; + + dev = get_device(dev); + if (!dev) + return -EINVAL; + + error = sysfs_change_owner(kobj); + if (error) + goto out; + + error = sysfs_file_change_owner_by_name(kobj, dev_attr_uevent.attr.name); + if (error) + goto out; + + error = device_attrs_change_owner(dev); + if (error) + goto out; + +#ifdef CONFIG_BLOCK + if (sysfs_deprecated && dev->class == &block_class) + goto out; +#endif + + error = sysfs_link_change_owner(&dev->class->p->subsys.kobj, &dev->kobj, + dev_name(dev)); + if (error) + goto out; + +out: + put_device(dev); + return error; +} +EXPORT_SYMBOL_GPL(device_change_owner); + /** * device_shutdown - call ->shutdown() on each device to shutdown. */ diff --git a/include/linux/device.h b/include/linux/device.h index 0cd7c647c16c..63ebd9e3f6b0 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -817,6 +817,7 @@ extern struct device *device_find_child_by_name(struct device *parent, extern int device_rename(struct device *dev, const char *new_name); extern int device_move(struct device *dev, struct device *new_parent, enum dpm_order dpm_order); +extern int device_change_owner(struct device *dev); extern const char *device_get_devnode(struct device *dev, umode_t *mode, kuid_t *uid, kgid_t *gid, const char **tmp); From patchwork Mon Feb 17 16:14:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 212836 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0E1A8C34022 for ; Mon, 17 Feb 2020 16:15:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id E0CF720718 for ; Mon, 17 Feb 2020 16:15:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729411AbgBQQOu (ORCPT ); Mon, 17 Feb 2020 11:14:50 -0500 Received: from youngberry.canonical.com ([91.189.89.112]:49250 "EHLO youngberry.canonical.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729056AbgBQQOt (ORCPT ); Mon, 17 Feb 2020 11:14:49 -0500 Received: from ip5f5bf7ec.dynamic.kabel-deutschland.de ([95.91.247.236] helo=wittgenstein.fritz.box) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1j3j2l-0003Cp-Ac; Mon, 17 Feb 2020 16:14:47 +0000 From: Christian Brauner To: "David S. Miller" , Greg Kroah-Hartman , linux-kernel@vger.kernel.org, netdev@vger.kernel.org Cc: "Rafael J. Wysocki" , Pavel Machek , Jakub Kicinski , Eric Dumazet , Stephen Hemminger , linux-pm@vger.kernel.org, Christian Brauner Subject: [PATCH net-next v2 09/10] net-sysfs: add queue_change_owner() Date: Mon, 17 Feb 2020 17:14:35 +0100 Message-Id: <20200217161436.1748598-10-christian.brauner@ubuntu.com> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200217161436.1748598-1-christian.brauner@ubuntu.com> References: <20200217161436.1748598-1-christian.brauner@ubuntu.com> MIME-Version: 1.0 Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Add a function to change the owner of the queue entries for a network device when it is moved between network namespaces. Currently, when moving network devices between network namespaces the ownership of the corresponding queue sysfs entries are not changed. This leads to problems when tools try to operate on the corresponding sysfs files. Fix this. Signed-off-by: Christian Brauner --- /* v2 */ - kbuild test robot via sparse: - Make net_rx_queue_change_owner() static since it's not exported. --- net/core/net-sysfs.c | 101 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/net/core/net-sysfs.c b/net/core/net-sysfs.c index 4fda021edf6d..3f93f996c7a3 100644 --- a/net/core/net-sysfs.c +++ b/net/core/net-sysfs.c @@ -944,6 +944,22 @@ static int rx_queue_add_kobject(struct net_device *dev, int index) kobject_put(kobj); return error; } + +static int rx_queue_change_owner(struct net_device *dev, int index) +{ + struct netdev_rx_queue *queue = dev->_rx + index; + struct kobject *kobj = &queue->kobj; + int error; + + error = sysfs_change_owner(kobj); + if (error) + return error; + + if (dev->sysfs_rx_queue_group) + error = sysfs_group_change_owner(kobj, dev->sysfs_rx_queue_group); + + return error; +} #endif /* CONFIG_SYSFS */ int @@ -981,6 +997,28 @@ net_rx_queue_update_kobjects(struct net_device *dev, int old_num, int new_num) #endif } +static int net_rx_queue_change_owner(struct net_device *dev, int num) +{ +#ifdef CONFIG_SYSFS + int error = 0; + int i; + +#ifndef CONFIG_RPS + if (!dev->sysfs_rx_queue_group) + return 0; +#endif + for (i = 0; i < num; i++) { + error = rx_queue_change_owner(dev, i); + if (error) + break; + } + + return error; +#else + return 0; +#endif +} + #ifdef CONFIG_SYSFS /* * netdev_queue sysfs structures and functions. @@ -1486,6 +1524,22 @@ static int netdev_queue_add_kobject(struct net_device *dev, int index) kobject_put(kobj); return error; } + +static int tx_queue_change_owner(struct net_device *ndev, int index) +{ + struct netdev_queue *queue = ndev->_tx + index; + struct kobject *kobj = &queue->kobj; + int error; + + error = sysfs_change_owner(kobj); + if (error) + return error; + +#ifdef CONFIG_BQL + error = sysfs_group_change_owner(kobj, &dql_group); +#endif + return error; +} #endif /* CONFIG_SYSFS */ int @@ -1520,6 +1574,24 @@ netdev_queue_update_kobjects(struct net_device *dev, int old_num, int new_num) #endif /* CONFIG_SYSFS */ } +static int net_tx_queue_change_owner(struct net_device *dev, int num) +{ +#ifdef CONFIG_SYSFS + int error = 0; + int i; + + for (i = 0; i < num; i++) { + error = tx_queue_change_owner(dev, i); + if (error) + break; + } + + return error; +#else + return 0; +#endif /* CONFIG_SYSFS */ +} + static int register_queue_kobjects(struct net_device *dev) { int error = 0, txq = 0, rxq = 0, real_rx = 0, real_tx = 0; @@ -1554,6 +1626,31 @@ static int register_queue_kobjects(struct net_device *dev) return error; } +static int queue_change_owner(struct net_device *ndev) +{ + int error = 0, real_rx = 0, real_tx = 0; + +#ifdef CONFIG_SYSFS + if (ndev->queues_kset) { + error = sysfs_change_owner(&ndev->queues_kset->kobj); + if (error) + return error; + } + real_rx = ndev->real_num_rx_queues; +#endif + real_tx = ndev->real_num_tx_queues; + + error = net_rx_queue_change_owner(ndev, real_rx); + if (error) + return error; + + error = net_tx_queue_change_owner(ndev, real_tx); + if (error) + return error; + + return 0; +} + static void remove_queue_kobjects(struct net_device *dev) { int real_rx = 0, real_tx = 0; @@ -1791,6 +1888,10 @@ int netdev_change_owner(struct net_device *ndev, const struct net *net_old, if (error) return error; + error = queue_change_owner(ndev); + if (error) + return error; + return 0; }