From patchwork Fri Dec 2 22:47:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Allen Webb X-Patchwork-Id: 29137 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp1106454wrr; Fri, 2 Dec 2022 14:50:23 -0800 (PST) X-Google-Smtp-Source: AA0mqf6ETrLyEU0SVcvHK8ffPPb2d+uT1ggvbTHf3UWPEDr09qqbmC7alLjjnWkO7W/82lB23b/D X-Received: by 2002:a17:903:1314:b0:189:bcf7:1ed6 with SMTP id iy20-20020a170903131400b00189bcf71ed6mr4173051plb.143.1670021422830; Fri, 02 Dec 2022 14:50:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670021422; cv=none; d=google.com; s=arc-20160816; b=yyNUZojyGodAmFQsGxQYhQStQzOkpBJRjImXr31aXKZchmbhd9kQQZ6eEmovgK71Ds 1S0dRQryAlWJJQ1NDRJee2/It3hl8SOkQY1NgYe9E8cDiewIsA7sWs5OJEJmLwUCKDqL Gdh60M0VYT4K1cGwka2WqgDluEW7L2RN6bHUsH2OXJ9LbEG3QAQJxUY67nFrg14x8EvE x+3JwIm+jt3ha+BwjZbkn6Pv+MfU+GewwcYVj+GyqnPHeDQclL41dT2tu5VYDZlWkKBL chuy+Jdrgbis8I5QhTUkyUjhlWk4+1HQIfvA4uZiSCTw7hmISAEGJJlR6hJCv6YqyZA8 WO7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=kr2FFYjrlmKQpl0FW9cRYWBpZCQQ4WxEePYVPH7PqT0=; b=0EmH8jSbi+vpTIBKcvmxVZWdfzMTHkVlVeT3njizzCVD/QMw8bseK7hF3IaQg9o0jT 13DJOGfevqq9q4WYj0WCrVFpZ8uoVoks/8ZB1CpcQcErg3UVIWRnpKU4c9tMtGfJnvEx HCT60EVdiAE5ydHxKQJeUmC84XprNjOIJZWHG/WB837pUjOMMYm/zgNHb44lDk0C1wth IqNZa3Z03fkpUa59za3iyu8Mj5Bo/Q3FxQl4IVwG7AaJ6JHK8cBldIY+2xQZCpgu7jyW SaHFl+LtffJ53saBGd1qxJzTJEJa6SeBcaFzKzjTPjTn8TPbGgpzyPfz5mAwh7uXVaCe GC+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=aig6Am3X; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g14-20020a65580e000000b0047872d46782si5873927pgr.408.2022.12.02.14.50.09; Fri, 02 Dec 2022 14:50:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=aig6Am3X; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233308AbiLBWsD (ORCPT + 99 others); Fri, 2 Dec 2022 17:48:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234318AbiLBWsB (ORCPT ); Fri, 2 Dec 2022 17:48:01 -0500 Received: from mail-io1-xd49.google.com (mail-io1-xd49.google.com [IPv6:2607:f8b0:4864:20::d49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 633CC7BC08 for ; Fri, 2 Dec 2022 14:47:59 -0800 (PST) Received: by mail-io1-xd49.google.com with SMTP id y5-20020a056602120500b006cf628c14ddso5744556iot.15 for ; Fri, 02 Dec 2022 14:47:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=kr2FFYjrlmKQpl0FW9cRYWBpZCQQ4WxEePYVPH7PqT0=; b=aig6Am3XKh1f74fIoP78XmyYoMV06xOmH0SRrk9TCuJ30vcxCB6RIZVxlkWc4IW82q 2uIqiG3js6ixvzMtrv2w3qAX1gMUNHU9Jpd+qiPXSNFHKbehWNF7Yhw2hnsYzBVHiz+U /xJtJBId/qyG3lUq5Gebw3wMCtj97OgR8Iuh+NX2oUJTao+RSD97NbqgA6jJbdPMvs0d v0DCQHtlp3kq5Ddfnf3WHfDLXQoDLyzfX18K4SfCv7rdPhWhlGu0YOEWfi4OkK0rmIKe Vz8L0l0NKQWQ8QpriqE46JttBua6QTtUfOf6j36qUTSKJGD76Wfrp8mqX71XIjDSw2Ur J52w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=kr2FFYjrlmKQpl0FW9cRYWBpZCQQ4WxEePYVPH7PqT0=; b=mb3tCCh1B4KqqXLGNRJjMDssBaSKvoES5oQd6RAYsLuIK8kCwePXa/tbuEHOXW8poP t2DX8jWsb8Ds5qrpNB7IPGN5vQy13Q0zpMUBHxnj4rPz3kGYaSEakq4HpDsXYEY0KFaF oFVqOSk8KXaccqrqFL1mQR8nZWZHhpTjESHaZHSsqZekBHtoRq7q0jiA+LuTrr0KR+eo QcqZosY2ZUAs4s5kGfXfhr3oLxRIH7f1FKUe5B0/alXU5H1NYU5wD9snOqqp2obwMoAi kd7lCgclj5HFOP8+G7aACwgW+pCxs4yfYkkgL6clcKipvH2yMdxCB7HFgRHtmj0y1d9U 82Tw== X-Gm-Message-State: ANoB5plVqoqBI6MNx8HZEoyx6rlVyMcWueng3ZZziWU+Ep/EVDB5n4fL 1EqNuvhYHmKgvRAQrtRf5eMQFcJK+3R1H/I= X-Received: from allenwebb.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:12e8]) (user=allenwebb job=sendgmr) by 2002:a6b:fb13:0:b0:6de:383e:4146 with SMTP id h19-20020a6bfb13000000b006de383e4146mr27314176iog.48.1670021278767; Fri, 02 Dec 2022 14:47:58 -0800 (PST) Date: Fri, 2 Dec 2022 16:47:40 -0600 In-Reply-To: <20221202224540.1446952-1-allenwebb@google.com> Mime-Version: 1.0 References: <20221202224540.1446952-1-allenwebb@google.com> X-Mailer: git-send-email 2.39.0.rc0.267.gcb52ba06e7-goog Message-ID: <20221202224744.1447448-1-allenwebb@google.com> Subject: [PATCH v6 1/5] module: Add empty modalias sysfs attribute From: Allen Webb To: "linux-modules@vger.kernel.org" , "linux-usb@vger.kernel.org" , "linux-kernel@vger.kernel.org" Cc: Luis Chamberlain , Greg Kroah-Hartman , "Rafael J. Wysocki" , Allen Webb X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751144383311046943?= X-GMAIL-MSGID: =?utf-8?q?1751144383311046943?= This adds the modalias sysfs attribute in preparation for its implementation. Signed-off-by: Allen Webb --- include/linux/module.h | 1 + kernel/module/internal.h | 2 ++ kernel/module/sysfs.c | 33 +++++++++++++++++++++++++++++++++ kernel/params.c | 7 +++++++ 4 files changed, 43 insertions(+) diff --git a/include/linux/module.h b/include/linux/module.h index ec61fb53979a9..0bfa859a21566 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -47,6 +47,7 @@ struct module_kobject { struct kobject *drivers_dir; struct module_param_attrs *mp; struct completion *kobj_completion; + struct bin_attribute modalias_attr; } __randomize_layout; struct module_attribute { diff --git a/kernel/module/internal.h b/kernel/module/internal.h index 2e2bf236f5582..8d7ae37584868 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -259,11 +259,13 @@ static inline void add_kallsyms(struct module *mod, const struct load_info *info #endif /* CONFIG_KALLSYMS */ #ifdef CONFIG_SYSFS +void add_modalias_attr(struct module_kobject *mk); int mod_sysfs_setup(struct module *mod, const struct load_info *info, struct kernel_param *kparam, unsigned int num_params); void mod_sysfs_teardown(struct module *mod); void init_param_lock(struct module *mod); #else /* !CONFIG_SYSFS */ +static inline void add_modalias_attr(struct module_kobject *mk) {} static inline int mod_sysfs_setup(struct module *mod, const struct load_info *info, struct kernel_param *kparam, diff --git a/kernel/module/sysfs.c b/kernel/module/sysfs.c index ce68f821dcd12..8dafec7455fbe 100644 --- a/kernel/module/sysfs.c +++ b/kernel/module/sysfs.c @@ -240,6 +240,37 @@ static inline void add_notes_attrs(struct module *mod, const struct load_info *i static inline void remove_notes_attrs(struct module *mod) { } #endif /* CONFIG_KALLSYMS */ +static ssize_t module_modalias_read(struct file *filp, struct kobject *kobj, + struct bin_attribute *bin_attr, + char *buf, loff_t pos, size_t count) +{ + return 0; +} + +/* Used in kernel/params.c for builtin modules. + * + * `struct module_kobject` is used instead of `struct module` because for + * builtin modules, the `struct module` is not available when this is called. + */ +void add_modalias_attr(struct module_kobject *mk) +{ + sysfs_bin_attr_init(&mk->modalias_attr); + mk->modalias_attr.attr.name = "modalias"; + mk->modalias_attr.attr.mode = 0444; + mk->modalias_attr.read = module_modalias_read; + if (sysfs_create_bin_file(&mk->kobj, &mk->modalias_attr)) { + /* We shouldn't ignore the return type, but there is nothing to + * do. + */ + return; + } +} + +static void remove_modalias_attr(struct module_kobject *mk) +{ + sysfs_remove_bin_file(&mk->kobj, &mk->modalias_attr); +} + static void del_usage_links(struct module *mod) { #ifdef CONFIG_MODULE_UNLOAD @@ -398,6 +429,7 @@ int mod_sysfs_setup(struct module *mod, add_sect_attrs(mod, info); add_notes_attrs(mod, info); + add_modalias_attr(&mod->mkobj); return 0; @@ -415,6 +447,7 @@ int mod_sysfs_setup(struct module *mod, static void mod_sysfs_fini(struct module *mod) { + remove_modalias_attr(&mod->mkobj); remove_notes_attrs(mod); remove_sect_attrs(mod); mod_kobject_put(mod); diff --git a/kernel/params.c b/kernel/params.c index 5b92310425c50..b7fd5313a3118 100644 --- a/kernel/params.c +++ b/kernel/params.c @@ -14,6 +14,12 @@ #include #include +#ifdef CONFIG_MODULES +#include "module/internal.h" +#else +static inline void add_modalias_attr(struct module_kobject *mk) {} +#endif /* !CONFIG_MODULES */ + #ifdef CONFIG_SYSFS /* Protects all built-in parameters, modules use their own param_lock */ static DEFINE_MUTEX(param_lock); @@ -815,6 +821,7 @@ static void __init kernel_add_sysfs_param(const char *name, BUG_ON(err); kobject_uevent(&mk->kobj, KOBJ_ADD); kobject_put(&mk->kobj); + add_modalias_attr(mk); } /*