From patchwork Wed Jun 14 06:30:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 107725 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1037254vqr; Tue, 13 Jun 2023 23:34:27 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7/CYm0R0V7E1b6pAz61kpRbm0LWgT9653v5Ipq7OypX6qYmiEa46zdAjC4rJQd/v4Vrcxq X-Received: by 2002:a05:651c:1039:b0:2af:d2ef:49d4 with SMTP id w25-20020a05651c103900b002afd2ef49d4mr5623789ljm.1.1686724467562; Tue, 13 Jun 2023 23:34:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686724467; cv=none; d=google.com; s=arc-20160816; b=nhNoVy8mV9nB/1TwcdCsmseBbTA+QFhE+1TFG7NdWDDBLJ3A7x+LIhYduHokxAxQ97 ShYrpQxQHvz1rTJ/AL0pMreDnBHaF2ckLIUcS3Zbasto9Lsh62iDd/QCaXdncaIOcgPp I2d+6qfaFIfrFLLzR4y+t4k6v+2ihgTg0kG74LCbJHNAdjWgI+1qpJop9cxp6cEBsrG1 tHzAI6cG0lS9BzIW046xFSE6JoMJajimnFc+4NWvOXpij8QKd7tKVw7kn6xVjFRiOx/C c84VwyOx1u1GKEy2pbhQIGJtViCzk4rTocWhLJVDlXZwfDAf4B3rQdf0PQsq0wAG0aHx cU8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Kf6zgkDUnecNfzu3j5XSu5/huKGd/SdarF/Qmk9HXkg=; b=E/j/N3xsUTgs+impT/JTuTRjwcHy1lfIpWWF0VCwTlB6bOeeFLa7Nx8dgCEnuWqc/I nmIepXM15XG/ilE8iiNTr7crSbGfnbPa3/Ckk8IgpmKi5csVHG2levM2p9vapHz8Ib32 6647hXJC+zdGb3zOwltPEhciMqoVA6j9nv30awq3JlE6F4zqznFeM9xRDuzgo/38taX4 3XxIMrzMNUBXtbTPRc8Wl/PU0c2HRFF9SWiOCyhzw/IgCV+fBRT4CuJIDYxpCV0/HScI RdIwCw2QHNMnHdFm998wUCKiv7dsWO+ITrFmKeVT4A76qHZaVdxjUJ7lvLxOB4itWKfN bwQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=ZgToFv6k; 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=bootlin.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o10-20020aa7c50a000000b005149c18e773si8474354edq.399.2023.06.13.23.34.00; Tue, 13 Jun 2023 23:34:27 -0700 (PDT) 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=@bootlin.com header.s=gm1 header.b=ZgToFv6k; 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=bootlin.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243162AbjFNGbJ (ORCPT + 99 others); Wed, 14 Jun 2023 02:31:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43986 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243154AbjFNGal (ORCPT ); Wed, 14 Jun 2023 02:30:41 -0400 Received: from relay9-d.mail.gandi.net (relay9-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::229]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 701421FCC for ; Tue, 13 Jun 2023 23:30:22 -0700 (PDT) X-GND-Sasl: miquel.raynal@bootlin.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1686724220; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Kf6zgkDUnecNfzu3j5XSu5/huKGd/SdarF/Qmk9HXkg=; b=ZgToFv6kXV0KhJSTl92ii6RlUbRLDMkIf2OuYuvSLGVaebwUuK5+kRvMfry1Rcq2lIj8Ot AmilI1hLbUuCrTC/xguPSLN5yJOM5WhMA0t0LL/LtaIVkR6/kyhk4lcSA8ILjkigNwP2Mh dTLbaKcRRnnfzhsIpLI1NfP81x8bXVc3dDmV3G0RoaGcpyM4AuUz5smiDioeHngUjqL2UL sgHvPHHZQKImn5RxxXLQhlZnUCqlrPO+mx/YaaALv0e5Zb2yon35hK33cXYMuwt6IXmkkm nt5rfa1vK19j5oO300bAmg6D2aFMP8/cvWu0I9+KIBTg4jLfN6Bi3yEBKAmCpg== X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com Received: by mail.gandi.net (Postfix) with ESMTPSA id 144CFFF806; Wed, 14 Jun 2023 06:30:19 +0000 (UTC) From: Miquel Raynal To: Srinivas Kandagatla Cc: Greg Kroah-Hartman , "Rafael J . Wysocki" , , Thomas Petazzoni , Luka Perkov , Robert Marko , Miquel Raynal Subject: [PATCH v4 1/4] sysfs: Improve readability by following the kernel coding style Date: Wed, 14 Jun 2023 08:30:15 +0200 Message-Id: <20230614063018.2419043-2-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230614063018.2419043-1-miquel.raynal@bootlin.com> References: <20230614063018.2419043-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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?1768658795259987309?= X-GMAIL-MSGID: =?utf-8?q?1768658795259987309?= The purpose of the if/else block is to select the right sysfs directory entry to be used for the files creation. At a first look when you have the file in front of you, it really seems like the "create_files()" lines right after the block are badly indented and the "else" does not guard. In practice the code is correct but lacks curly brackets to show where the big if/else block actually ends. Add these brackets to comply with the current kernel coding style and to ease the understanding of the whole logic. Signed-off-by: Miquel Raynal --- fs/sysfs/group.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/fs/sysfs/group.c b/fs/sysfs/group.c index eeb0e3099421..990309132c93 100644 --- a/fs/sysfs/group.c +++ b/fs/sysfs/group.c @@ -142,8 +142,10 @@ static int internal_create_group(struct kobject *kobj, int update, return PTR_ERR(kn); } } - } else + } else { kn = kobj->sd; + } + kernfs_get(kn); error = create_files(kn, kobj, uid, gid, grp, update); if (error) { From patchwork Wed Jun 14 06:30:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 107732 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1043293vqr; Tue, 13 Jun 2023 23:52:11 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7f/ob3hQ18k8k+CSXPD9J4eWHzLKESSqAjxPklgWc9REjTnbMJrwfbIM0WG5v4rdfaUoDa X-Received: by 2002:a17:902:8682:b0:1b1:9993:a0e1 with SMTP id g2-20020a170902868200b001b19993a0e1mr9050312plo.59.1686725531044; Tue, 13 Jun 2023 23:52:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686725531; cv=none; d=google.com; s=arc-20160816; b=thflktQdxeFJ//dwgNuxAXHiflbna4DhJLhLwnP854udHPzSLuUJduD7pzlDxmw3ya g604WFjFYUSBvsY+duaJm9JFVWDbX6UfdTWdq2kZJGQ1J/9RyFN5EwedJbrlZpOPP4CD wyk+DgjQ7vnh3U76/TF8eWybcKY7N8IfNa4Rf0mNCYM4s/jwTIGY8yqr7fGiJYa2WfK7 W8kuUsKardBC17ySgc/epBrNnWHRO+vdn+9UJbrRrEIhXwElnhsLsJMaIvQQXBKgJWca 9f7K38AL0fZPnGCdMID0ANLGdB7hduZGH65rGvoUmqNiKhB5o7f2kMWAPzlURqVSZTFz A6eg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=zxDjpsQsnHKyp+46A4IVR8UbFfLpDgOYCYup7v5oaqk=; b=BYj846hFHbtklThSDWg6Z2xUzx8GyVkd18ecLou6kqdkVrSFKygulQ+cFSGdb2gd7J VMmINnOMpz6epwWSQMW65HdMPrdkgu/FhPASeQDC06eZ8BTObEjAbbc4f5hjEITrWBbG kFxAgXHNAy2WDFSJWOnTQdOuvqGr+E2eqYDY6JadmM+m7pqR5GnL2HzhZpltN5WSR0nc JvgKN2e9+r3d/LPC7wkMqW8hdlKroL6BpCgxS1sxKKF+VyvfzBUousfYDr6vEubXoEz/ QgxL++knrWGA0A4zd5lP9WsN+wQ2PRfNTR4zzezBJbllUPyceisEjmPpeq/1EoWCiMik XO3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=W3Gfs6pV; 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=bootlin.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q17-20020a17090311d100b001aaea258805si10745734plh.134.2023.06.13.23.51.58; Tue, 13 Jun 2023 23:52:11 -0700 (PDT) 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=@bootlin.com header.s=gm1 header.b=W3Gfs6pV; 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=bootlin.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243217AbjFNGbQ (ORCPT + 99 others); Wed, 14 Jun 2023 02:31:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44028 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243216AbjFNGan (ORCPT ); Wed, 14 Jun 2023 02:30:43 -0400 Received: from relay9-d.mail.gandi.net (relay9-d.mail.gandi.net [217.70.183.199]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 440CC213F for ; Tue, 13 Jun 2023 23:30:22 -0700 (PDT) X-GND-Sasl: miquel.raynal@bootlin.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1686724221; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zxDjpsQsnHKyp+46A4IVR8UbFfLpDgOYCYup7v5oaqk=; b=W3Gfs6pVbddjBLmdgT5NvK6nsbXjzsjABQxhtvuX0NESqymciSDiL3+HcVhAaWEIaQh04w SlZXRj9DYw45Ek4PTgZWMb6HPM2nnZ7F0gVaqkinmaYULieA+doinqMaMfefDqZ9qv5vtj 1u6UeZ8aUF2cdCUtcizm5aJEkKQwhimJy3VgSjv5bZ2MSLkZWubUg+TN6QOlFTtIoWd1gg xWIMO6VeHqJhaXHwUJfGjRfczemlTfq64ixgAU4Q+Y/8goyaztUjtxz+YpxBY6gNgewhCg everkFlE9xeAQvFBsoOXHE2GIrO/MkeZx8NUv6HUT23wWV+SizyPTZ60IB0ZVw== X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com Received: by mail.gandi.net (Postfix) with ESMTPSA id D5174FF80B; Wed, 14 Jun 2023 06:30:20 +0000 (UTC) From: Miquel Raynal To: Srinivas Kandagatla Cc: Greg Kroah-Hartman , "Rafael J . Wysocki" , , Thomas Petazzoni , Luka Perkov , Robert Marko , Miquel Raynal Subject: [PATCH v4 2/4] sysfs: Skip empty folders creation Date: Wed, 14 Jun 2023 08:30:16 +0200 Message-Id: <20230614063018.2419043-3-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230614063018.2419043-1-miquel.raynal@bootlin.com> References: <20230614063018.2419043-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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?1768659910422716623?= X-GMAIL-MSGID: =?utf-8?q?1768659910422716623?= Most sysfs attributes are statically defined, the goal with this design being to be able to move all the filesystem description into read-only memory. Anyway, it may be relevant in some cases to populate attributes at run time. This leads to situation where an attribute may or may not be present depending on conditions which are not known at compile time, up to the point where no attribute at all gets added in a folder which then becomes "sometimes" empty. Problem is, providing an attribute group with a name and without .[bin_]attrs members will be loudly refused by the core, leading in most cases to a device registration failure. The simple way to support such situation right now is to dynamically allocate an empty attribute array, which is: * a (small) waste of space * a waste of time * disturbing, to say the least, as an empty sysfs folder will be created anyway. Another (even worse) possibility would be to dynamically overwrite a member of the attribute_group list, hopefully the last, which is also supposed to remain in the read-only section. In order to avoid these hackish situations, while still giving a little bit of flexibility, we might just check the validity of the .[bin_]attrs list and, if empty, just skip the attribute group creation instead of failing. This way, developers will not be tempted to workaround the core with useless allocations or strange writes on supposedly read-only structures. The content of the WARN() message is kept but turned into a debug message in order to help developers understanding why their sysfs folders might now silently fail to be created. Signed-off-by: Miquel Raynal --- fs/sysfs/group.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/fs/sysfs/group.c b/fs/sysfs/group.c index 990309132c93..138676463336 100644 --- a/fs/sysfs/group.c +++ b/fs/sysfs/group.c @@ -118,11 +118,13 @@ static int internal_create_group(struct kobject *kobj, int update, /* Updates may happen before the object has been instantiated */ if (unlikely(update && !kobj->sd)) return -EINVAL; + if (!grp->attrs && !grp->bin_attrs) { - WARN(1, "sysfs: (bin_)attrs not set by subsystem for group: %s/%s\n", - kobj->name, grp->name ?: ""); - return -EINVAL; + pr_debug("sysfs: (bin_)attrs not set by subsystem for group: %s/%s, skipping\n", + kobj->name, grp->name ?: ""); + return 0; } + kobject_get_ownership(kobj, &uid, &gid); if (grp->name) { if (update) { From patchwork Wed Jun 14 06:30:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 107733 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1043308vqr; Tue, 13 Jun 2023 23:52:12 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6fHumG/m/hdg2s1ZHzcADXK8+wC+TjK4d/eGlp0tB/guIzxSoBoUEQdcnNYtm/ECv0XNDK X-Received: by 2002:a17:903:41c8:b0:1b3:d759:d2dd with SMTP id u8-20020a17090341c800b001b3d759d2ddmr6165037ple.9.1686725532603; Tue, 13 Jun 2023 23:52:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686725532; cv=none; d=google.com; s=arc-20160816; b=QfVO4kacQz9yMbiH75o+qyNl1Ulwg3nGkuQjIlFZFGe+qOTqJ5ohh381Jou4lO87lV L7gQYSzbNTXkPimUNOg6/oPoKhg5tj+AsWGtfxWHWJEwvGOIDKhebkDixjzJrNwK/+HK h9uuguQXtsLgotIyUz+pd08Xr11FlAGj+h4w0w1NPDN1K0ZUuQD4n15R2uLB19Qa1JlF n5gTaQd161yRfN27IYZDB4gulrwm8csjkjxTIytbPyMH4WlK4JuA/OttXS2pyC690LFG 5rPVTgcKGFE7vSXe3zRf//FX/cLp5+5lttTrHcnWeLyMZCrfxg6yfCk05EBBLljbzFYz 62qA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=QUIYbUJt+Cl/OQINsVO/8Ug45EH5iacT/hNl2EpfQ/k=; b=WAo9uV/AzPnT6bEQb6edQcwBErPZ4wHlJFlV9Nm6e4uhEsei6xR5RsqyTlvDoMivlk HRBUDpfd+uTyIgjMDrSfe86vh2HJ+dWegg8FQn0FYsLMnXoXRPzNwQQbYExMBxZJ9e3X BYU5/DWFi4J04Q6wPeY7Z4HDw/qoYrbiUU0sZ8G85KuC8ZyoOg9qnF4lATUtyQRtargW ULcYRpLNcHIsJmA5uJ/qaG1RbncjpSuqBcQh4lpNLsW1q0IIPpBsRwRfiGXYJ8Q8p0M5 qwdINJ9oYRIWhalkMAh99CeGOCtiksBkEEGPcE60s0+/TEKyBuaW1NNh0mAIUDAGzx7C dbzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=mfakaeBi; 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=bootlin.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ju11-20020a170903428b00b001b07145bc30si1655212plb.312.2023.06.13.23.52.00; Tue, 13 Jun 2023 23:52:12 -0700 (PDT) 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=@bootlin.com header.s=gm1 header.b=mfakaeBi; 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=bootlin.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243216AbjFNGbT (ORCPT + 99 others); Wed, 14 Jun 2023 02:31:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44076 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243168AbjFNGap (ORCPT ); Wed, 14 Jun 2023 02:30:45 -0400 Received: from relay9-d.mail.gandi.net (relay9-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::229]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C9C562688 for ; Tue, 13 Jun 2023 23:30:23 -0700 (PDT) X-GND-Sasl: miquel.raynal@bootlin.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1686724222; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QUIYbUJt+Cl/OQINsVO/8Ug45EH5iacT/hNl2EpfQ/k=; b=mfakaeBitl8sXX9VzebECkRFdSsiGf2T8dggJsBcOT6ZnOkapFalhekJ1ozj3EJWsO1Z9o 5IxHEtOe3Mup/bKnMqr6BwGxTvllPwvOHQKl5VuXs+dopoYVA+Pb0kK4VB8bteOUEP4cGT ljDrRyjZogg3pH5BB9sVkwwXbQdafyo3aQeOUyAcIDjFDZSBlKdJ383DGLkh4ADBz4XHNh X1l79aSfjmQboxhyLYeALg894WeFSp+mMqX+QMMwb3UHbOUFtPLKXiNwAvENoLpmxRwMAN +ZqLYLrsWf2JhYvTryKMEUFWM6ocy4Jr+pg5RKEzhGLgWDoL54jfAfTblaW3pA== X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com Received: by mail.gandi.net (Postfix) with ESMTPSA id 95518FF812; Wed, 14 Jun 2023 06:30:21 +0000 (UTC) From: Miquel Raynal To: Srinivas Kandagatla Cc: Greg Kroah-Hartman , "Rafael J . Wysocki" , , Thomas Petazzoni , Luka Perkov , Robert Marko , Miquel Raynal Subject: [PATCH v4 3/4] ABI: sysfs-nvmem-cells: Expose cells through sysfs Date: Wed, 14 Jun 2023 08:30:17 +0200 Message-Id: <20230614063018.2419043-4-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230614063018.2419043-1-miquel.raynal@bootlin.com> References: <20230614063018.2419043-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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?1768659912059642819?= X-GMAIL-MSGID: =?utf-8?q?1768659912059642819?= The binary content of nvmem devices is available to the user so in the easiest cases, finding the content of a cell is rather easy as it is just a matter of looking at a known and fixed offset. However, nvmem layouts have been recently introduced to cope with more advanced situations, where the offset and size of the cells is not known in advance or is dynamic. When using layouts, more advanced parsers are used by the kernel in order to give direct access to the content of each cell regardless of their position/size in the underlying device, but these information were not accessible to the user. By exposing the nvmem cells to the user through a dedicated cell/ folder containing one file per cell, we provide a straightforward access to useful user information without the need for re-writing a userland parser. Content of nvmem cells is usually: product names, manufacturing date, MAC addresses, etc, Signed-off-by: Miquel Raynal --- Documentation/ABI/testing/sysfs-nvmem-cells | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-nvmem-cells diff --git a/Documentation/ABI/testing/sysfs-nvmem-cells b/Documentation/ABI/testing/sysfs-nvmem-cells new file mode 100644 index 000000000000..641a7d7dad76 --- /dev/null +++ b/Documentation/ABI/testing/sysfs-nvmem-cells @@ -0,0 +1,19 @@ +What: /sys/bus/nvmem/devices/.../cells/ +Date: May 2023 +KernelVersion: 6.5 +Contact: Miquel Raynal +Description: + The cells/ folder contains one file per cell exposed by + the nvmem device. The name of the file is the cell name. + The length of the file is the size of the cell (when + known). The content of the file is the binary content of + the cell (may sometimes be ASCII, likely without + trailing character). + Note: This file is only present if CONFIG_NVMEM_SYSFS + is enabled + + ex:: + + hexdump -C /sys/bus/nvmem/devices/1-00563/cells/product-name + 00000000 54 4e 34 38 4d 2d 50 2d 44 4e |TN48M-P-DN| + 0000000a From patchwork Wed Jun 14 06:30:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 107729 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1041107vqr; Tue, 13 Jun 2023 23:45:27 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6zikPOMfKI7jWh6axIgbxTyLK8QX9TeJJ9D+9Z6+5EMvdVwfee2nylHXT5hUhuofuA1fam X-Received: by 2002:a05:6a20:2590:b0:110:e7bc:b0cb with SMTP id k16-20020a056a20259000b00110e7bcb0cbmr1020395pzd.39.1686725127117; Tue, 13 Jun 2023 23:45:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686725127; cv=none; d=google.com; s=arc-20160816; b=M+y7so8MiYusBOq/YwL++m7Ifo4QrVyVz6cJ5mwjD6skaCtzppDmw+QtSQtC/YimJe BlsPqRrgfqEUNTqmbsdOuPMHknJBE/MDDN4J0CU28vS00F6OzzmS7Yrx7NS8aEpU7Pfj RpR9ik/Ky6X87ajlMgpk92xJMMor1YGMY1kgJPMCa+ENK8CnYl9YCJPxeZyR6oMUXtQP tX2XfHpqwlGdLQtbAYeBiZQOfPyBIUcfqtcO+i6km7Ddp2gRAuZILkJKW14hVcicTjUR XVz7aDkswcCygEYEKS42C85MbGfNlW2e0Yb//CGpheDy9VkSDGAUnW3eZ8lCas0WkDsj ezxg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=G/EwV9fvhxkTVfX06ik2E/XNt0KK6UdwlK2n4zvpmA8=; b=Auxv5LeKd4VgHAlWGbGPw8axodDmYfTyljWbqJIf7qZ8wzbr1HIwax3hVOZQI0dgoF 30YHkQP2j7FWLo6HQ1ztBUYbTHYMYMT7X7q1gVp5WCIlYf1DvFQQnsq2U0cUfRKvYSKv aRpTkyAro/L6YEL64AAfLeDQyFWoL5R0mWUWdSy3r42UvJDI5JpLlUDcGHy2n4HxApo0 fSHsaixv52P4fWMjHOw1zmLSz0gn5KEHSs35npULnkei4xL4H/BHVU+AotjwETy2fRC5 glW2gvMR0pnABbcA9g89uzZC6yIeAiMeJUOEfczcITvrZ2Uj6zN9QSjc/oKFVeDmvxdD pj6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=LJiGsaeJ; 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=bootlin.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l8-20020a63be08000000b0053f25f2d32csi9881131pgf.840.2023.06.13.23.45.14; Tue, 13 Jun 2023 23:45:27 -0700 (PDT) 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=@bootlin.com header.s=gm1 header.b=LJiGsaeJ; 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=bootlin.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243225AbjFNGb0 (ORCPT + 99 others); Wed, 14 Jun 2023 02:31:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44090 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234706AbjFNGaq (ORCPT ); Wed, 14 Jun 2023 02:30:46 -0400 Received: from relay9-d.mail.gandi.net (relay9-d.mail.gandi.net [IPv6:2001:4b98:dc4:8::229]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 96F30268F for ; Tue, 13 Jun 2023 23:30:24 -0700 (PDT) X-GND-Sasl: miquel.raynal@bootlin.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1686724223; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=G/EwV9fvhxkTVfX06ik2E/XNt0KK6UdwlK2n4zvpmA8=; b=LJiGsaeJLqYbqWrjDyxjDMkbMxs8VerzVfS/wkB3jcX9/7CAkYp8LJ4euYBsAfJ/qZn5/X 5KS4ceAy6KI2qwSaA0aS5yO8m3ESwCH8zfCoNu1rCf9SG4EMlwfTj4twJnbREovIhDDAs2 sFiXh800vE0DnSiAgkM2HZFctu8IefudxGK3Y0fW5hN7g+WfjKPDc//+49gKf/jcCZNFup xU9WBWEnYtliqZF67GfL9rDvdg1b1R0TcQLuNFLG56UsvFvmcdHjimmTTL0Ae14ZCoYoDD oLjRSp6R66lx7/BX2jekr376g5vfj0JL1dD6kZ8fcgll6xe1NXG6KCia8GMu1g== X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com X-GND-Sasl: miquel.raynal@bootlin.com Received: by mail.gandi.net (Postfix) with ESMTPSA id 5C881FF813; Wed, 14 Jun 2023 06:30:22 +0000 (UTC) From: Miquel Raynal To: Srinivas Kandagatla Cc: Greg Kroah-Hartman , "Rafael J . Wysocki" , , Thomas Petazzoni , Luka Perkov , Robert Marko , Miquel Raynal Subject: [PATCH v4 4/4] nvmem: core: Expose cells through sysfs Date: Wed, 14 Jun 2023 08:30:18 +0200 Message-Id: <20230614063018.2419043-5-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230614063018.2419043-1-miquel.raynal@bootlin.com> References: <20230614063018.2419043-1-miquel.raynal@bootlin.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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?1768659486892214367?= X-GMAIL-MSGID: =?utf-8?q?1768659486892214367?= The binary content of nvmem devices is available to the user so in the easiest cases, finding the content of a cell is rather easy as it is just a matter of looking at a known and fixed offset. However, nvmem layouts have been recently introduced to cope with more advanced situations, where the offset and size of the cells is not known in advance or is dynamic. When using layouts, more advanced parsers are used by the kernel in order to give direct access to the content of each cell, regardless of its position/size in the underlying device. Unfortunately, these information are not accessible by users, unless by fully re-implementing the parser logic in userland. Let's expose the cells and their content through sysfs to avoid these situations. Of course the relevant NVMEM sysfs Kconfig option must be enabled for this support to be available. Not all nvmem devices expose cells. Indeed, the .bin_attrs attribute group member will be filled at runtime only when relevant and will remain empty otherwise. In this case, as the cells attribute group will be empty, it will not lead to any additional folder/file creation. Exposed cells are read-only. There is, in practice, everything in the core to support a write path, but as I don't see any need for that, I prefer to keep the interface simple (and probably safer). The interface is documented as being in the "testing" state which means we can later add a write attribute if though relevant. There is one limitation though: if a layout is built as a module but is not properly installed in the system and loaded manually with insmod while the nvmem device driver was built-in, the cells won't appear in sysfs. But if done like that, the cells won't be usable by the built-in kernel drivers anyway. Signed-off-by: Miquel Raynal --- drivers/nvmem/core.c | 109 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 105 insertions(+), 4 deletions(-) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 342cd380b420..baa1f0038b28 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -325,6 +325,43 @@ static umode_t nvmem_bin_attr_is_visible(struct kobject *kobj, return nvmem_bin_attr_get_umode(nvmem); } +static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry, + const char *id, int index); + +static ssize_t nvmem_cell_attr_read(struct file *filp, struct kobject *kobj, + struct bin_attribute *attr, char *buf, + loff_t pos, size_t count) +{ + struct nvmem_cell_entry *entry; + struct nvmem_cell *cell = NULL; + size_t cell_sz, read_len; + void *content; + + entry = attr->private; + cell = nvmem_create_cell(entry, entry->name, 0); + if (IS_ERR(cell)) + return PTR_ERR(cell); + + if (!cell) + return -EINVAL; + + content = nvmem_cell_read(cell, &cell_sz); + if (IS_ERR(content)) { + read_len = PTR_ERR(content); + goto destroy_cell; + } + + read_len = min_t(unsigned int, cell_sz - pos, count); + memcpy(buf, content + pos, read_len); + kfree(content); + +destroy_cell: + kfree_const(cell->id); + kfree(cell); + + return read_len; +} + /* default read/write permissions */ static struct bin_attribute bin_attr_rw_nvmem = { .attr = { @@ -346,8 +383,14 @@ static const struct attribute_group nvmem_bin_group = { .is_bin_visible = nvmem_bin_attr_is_visible, }; +/* Cell attributes will be dynamically allocated */ +static struct attribute_group nvmem_cells_group = { + .name = "cells", +}; + static const struct attribute_group *nvmem_dev_groups[] = { &nvmem_bin_group, + &nvmem_cells_group, NULL, }; @@ -406,6 +449,58 @@ static void nvmem_sysfs_remove_compat(struct nvmem_device *nvmem, device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom); } +static int nvmem_populate_sysfs_cells(struct nvmem_device *nvmem) +{ + struct bin_attribute **cells_attrs, *attrs; + struct nvmem_cell_entry *entry; + unsigned int ncells = 0, i = 0; + int ret = 0; + + mutex_lock(&nvmem_mutex); + + if (list_empty(&nvmem->cells)) + goto unlock_mutex; + + /* Allocate an array of attributes with a sentinel */ + ncells = list_count_nodes(&nvmem->cells); + cells_attrs = devm_kcalloc(&nvmem->dev, ncells + 1, + sizeof(struct bin_attribute *), GFP_KERNEL); + if (!cells_attrs) { + ret = -ENOMEM; + goto unlock_mutex; + } + + attrs = devm_kcalloc(&nvmem->dev, ncells, sizeof(struct bin_attribute), GFP_KERNEL); + if (!attrs) { + ret = -ENOMEM; + goto unlock_mutex; + } + + /* Initialize each attribute to take the name and size of the cell */ + list_for_each_entry(entry, &nvmem->cells, node) { + sysfs_bin_attr_init(&attrs[i]); + attrs[i].attr.name = devm_kstrdup(&nvmem->dev, entry->name, GFP_KERNEL); + attrs[i].attr.mode = 0444; + attrs[i].size = entry->bytes; + attrs[i].read = &nvmem_cell_attr_read; + attrs[i].private = entry; + if (!attrs[i].attr.name) { + ret = -ENOMEM; + goto unlock_mutex; + } + + cells_attrs[i] = &attrs[i]; + i++; + } + + nvmem_cells_group.bin_attrs = cells_attrs; + +unlock_mutex: + mutex_unlock(&nvmem_mutex); + + return ret; +} + #else /* CONFIG_NVMEM_SYSFS */ static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem, @@ -976,16 +1071,22 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config) if (rval) goto err_remove_cells; + rval = nvmem_add_cells_from_layout(nvmem); + if (rval) + goto err_remove_cells; + +#ifdef CONFIG_NVMEM_SYSFS + rval = nvmem_populate_sysfs_cells(nvmem); + if (rval) + goto err_remove_cells; +#endif + dev_dbg(&nvmem->dev, "Registering nvmem device %s\n", config->name); rval = device_add(&nvmem->dev); if (rval) goto err_remove_cells; - rval = nvmem_add_cells_from_layout(nvmem); - if (rval) - goto err_remove_cells; - blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem); return nvmem;