From patchwork Mon Jun 5 13:34:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 103276 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2710671vqr; Mon, 5 Jun 2023 07:07:05 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4MPKLMr6/AXMTjEPaMiN9PCgrht3Go9fl6gqCS7svKqJF9HSeE5NlZTdHHK7sLNpc5tsPA X-Received: by 2002:a17:902:c206:b0:1b0:122f:672b with SMTP id 6-20020a170902c20600b001b0122f672bmr7492477pll.47.1685974025331; Mon, 05 Jun 2023 07:07:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685974025; cv=none; d=google.com; s=arc-20160816; b=mR+QRREhP9LBvjkYZAPahJE5W2GADKHUPaRhYPqc1E6WfEQKGFAbBPrgTO5lzH3RlJ /f63qCvnQQWKo3mXu0jwFyVoEDaBuQrmQNWxseoS3Io3CwC1sg3sXURHEPULEL24V+Zg 2+t+f5mVDastR+0qCwbL++crOXzoRsQ8/HLNFKj1mTyGYcoKthpDxYHHXUf8RBNc01A2 auJp3VhAfxNKNUsMewSnR83djamAgiCjKXDB3xUoeSwiFdYMLjSw2aJZiM6x1dB0QTF4 MgeCzcDDqnnc4cgvaETaebvlNI2o/2OA4LRXtCQtzKfE4Q0jcX3v2NowvBziWMU+lAWV Rmyg== 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=oDQJSXYuEx9lNEQf+DMXwQITwwv9AMC5AJhsx/UzkzMjfoiHRDuNC8wcRi1nnn6Xo3 nQ3Bhg6vJdogwcg49MRfUeQRe4f7PV8ry4Od0AootDKYwbZpCXYhUhGoKZPYcsGSuHBP BVGCcdRSdxPwZrRdczT04SxXGmJj0nczesnOsFw7a8OvVApJC2zGt3YU2NXwLffV8Ry8 UvTESxoG8M/s4O2J6XtQa3fE0fp49N4OFE4b1LklHt3GL/bYD9/tPtlsnUcU5X8udOoQ ttW07w1LU/4VeEUyPd/spZcUvXKKc1gBG8m0OvJIBbo6gug9fQNZF2fzBfUSY6lSjvwk ovAg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=e3vzuYc+; 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 a2-20020a170902ecc200b001b045d65aedsi5646299plh.228.2023.06.05.07.06.52; Mon, 05 Jun 2023 07:07:05 -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=e3vzuYc+; 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 S229815AbjFENeb (ORCPT + 99 others); Mon, 5 Jun 2023 09:34:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44848 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231370AbjFENe1 (ORCPT ); Mon, 5 Jun 2023 09:34:27 -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 EE1D8DB for ; Mon, 5 Jun 2023 06:34:25 -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=1685972064; 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=e3vzuYc+FQS4U26zWHOa1Nuw6wzI+Q4pu2QiWAwmFJXBtt41NvtzYC1g475fXEc3IgpvJw 8aUqcvLqwYSmAynnaUt/sHuvpppFiaCQzZj0lRYQTsVRz0jSFIYiCLVMsnZsquD3tbVbnd AKBla+3BywAN+pzMNKc+XyRR7WKQ85MGhQLX+aJKceuDSC0MlKHcfAQrG9LMLEI5HeHcWj bcB+XNWPssl6VnooV+EqkYBphWXFuj0Xz0nTp24ClEa6dN0bHzuTaQ/nBCwkbufGB6iWmX JRj0kBUsmLHWdPTDc6DzKYfhKMYyzNZXYhy1PsmJZgUNhgVLzE7pPjq95qi7Yw== 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 A553DFF80D; Mon, 5 Jun 2023 13:34:23 +0000 (UTC) From: Miquel Raynal To: Greg Kroah-Hartman Cc: Luka Perkov , Robert Marko , Thomas Petazzoni , , Srinivas Kandagatla , Rafael J Wysocki , Miquel Raynal Subject: [PATCH v3 1/4] sysfs: Improve readability by following the kernel coding style Date: Mon, 5 Jun 2023 15:34:19 +0200 Message-Id: <20230605133422.45840-2-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230605133422.45840-1-miquel.raynal@bootlin.com> References: <20230605133422.45840-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 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?1767871899644022783?= X-GMAIL-MSGID: =?utf-8?q?1767871899644022783?= 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 Mon Jun 5 13:34:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 103265 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2695388vqr; Mon, 5 Jun 2023 06:43:01 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6nn0q43SnpgHb4sXWh/c6H/oa7KihxRYXwfQuAA7StWkoG//g9WwVf6nYNP5uM7hQ/m0BX X-Received: by 2002:a05:6a00:1308:b0:645:fc7b:63db with SMTP id j8-20020a056a00130800b00645fc7b63dbmr12725521pfu.20.1685972581315; Mon, 05 Jun 2023 06:43:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685972581; cv=none; d=google.com; s=arc-20160816; b=Egk+A1zYl8+lHdcvOuTS4oqCumQ4hZ20uO/+vJ5q2eGzjqdlZo0OxuuBhA/Iw2YwxH Ep3acaQV47uW6VV6VvZ3BJRth0E8YB8iCE2fR1sAal+o81yvopd4tN6/XvZKyNtr2b/1 K1oIvc/GiLkgQTK7oYXXWIr8Dk5/GHzlXFIpT0SQ5gKxAx14Yk7+o6lPNtPPXvrQDDTn 5vtygvDzXstHC3bm+NBJ8ryD6OyVOqHp3F1PPsAnbEMPgBWgFKKTqh+vbWiaoZkkZQNc ApHrUD6fgkcg5xknyFQvU/S3yvH9qHqyNQLzmc6UrP8WXoDfOCcI+GSgI4n8JH3syfn8 HNbA== 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=BnxJRDgHOO186L7ZZTLYXtl0YeXqHFhN/6nM2DacTKg=; b=ufVYm9wBuzWr87LAEOQhBuLLBJQYi4J2CYKPeBhFPSSZeq9vdCRkkPUsjq7f+6uoMb o7t1fix9ZoYhyqOr3J7Bxv83drvrQ+w+pvxylLbBSfvaMF38Dm3wv6XnJivl8d2ZoBnR JKlA2C2CGO/8ad/FnqBbWeLiz2xow+i573eyvPNkVV/NDDn/lbY29iMCZJyscCt9Q4PG Bx/iQuGBlj2s4EjvXc6GSYOhQ5RF6ho7wqGIrXQ8FFQ/MDGa8s1lLiiLQ77bEGdZpYIJ cVdnGGaWX+iBHKZlg5yXbgrcm9SZVCQoqtSmFnB3TWZsHSEXCbsa2KjHL0t7x9dMY0qi TNtg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=TRpLEJJ5; 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-20020a655608000000b005307169868bsi5352748pgs.488.2023.06.05.06.42.23; Mon, 05 Jun 2023 06:43:01 -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=TRpLEJJ5; 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 S234106AbjFENei (ORCPT + 99 others); Mon, 5 Jun 2023 09:34:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44868 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233783AbjFENe2 (ORCPT ); Mon, 5 Jun 2023 09:34:28 -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 E385BE6 for ; Mon, 5 Jun 2023 06:34:26 -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=1685972065; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BnxJRDgHOO186L7ZZTLYXtl0YeXqHFhN/6nM2DacTKg=; b=TRpLEJJ5RPsMqlYRSwTrPxboO1YiZ9iFV9/0prjcNUVaAZxz218DVb2/7301Y5ZN8KPFD1 q9GMln5iiWA2M0x2gMo336cxE1jlh6OHdn7j6IZ4aW1lazo9OjiPpxqcMPSoL/bh9EAnKi pH1Ghaep30P5YJSCBzvD+rphACpH/BUrCj9ecM2rei9R6o9HwmXTDBtwNT09YCrm/75dvd 9XAVXeH8GNqKGQmIGcMweIDiB8aVl4M+7xY2lc8w57KPcIOV9+4fWTK0STLiq54DLGF/UX ZIsFDiceT+4m+HcKiwhMvGQkbbaIBKT4ha8upQQhxauX6rNYMbXjR2jGkdgTzA== 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 70937FF80C; Mon, 5 Jun 2023 13:34:24 +0000 (UTC) From: Miquel Raynal To: Greg Kroah-Hartman Cc: Luka Perkov , Robert Marko , Thomas Petazzoni , , Srinivas Kandagatla , Rafael J Wysocki , Miquel Raynal Subject: [PATCH v3 2/4] sysfs: Skip empty folders creation Date: Mon, 5 Jun 2023 15:34:20 +0200 Message-Id: <20230605133422.45840-3-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230605133422.45840-1-miquel.raynal@bootlin.com> References: <20230605133422.45840-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 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?1767870385391387048?= X-GMAIL-MSGID: =?utf-8?q?1767870385391387048?= 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 --- Hello Greg, This is the solution I opted for to avoid core splats when creating empty sysfs directories. The nice side effect is that the directories do not even get created which seem the right approach in this case and avoids to deal with any kind of cleanup either (or maybe I overlooked something?). Let me know what you think of it. This is a try of course, perhaps we need something way more robust, but at a first look it seemed perfectly consistent. Thanks, Miquèl 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 Mon Jun 5 13:34:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 103268 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2698200vqr; Mon, 5 Jun 2023 06:48:05 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7KxvYTYboJf0n1TUbqg/OO/ZK7G2/Qiva3/sBaDQiPV99/m/tMPowhdI5aCZJKmue+461M X-Received: by 2002:a05:6a20:7493:b0:10c:37ed:3e88 with SMTP id p19-20020a056a20749300b0010c37ed3e88mr7787960pzd.23.1685972885408; Mon, 05 Jun 2023 06:48:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685972885; cv=none; d=google.com; s=arc-20160816; b=jpoIeUf9H99R0R/vwGbSaPQ4QkE5Y1Xa4xBq3z5ttKyJ/e31nF8lQ9Gbo3DTZvrBmS xSORFdqgOVvd/eH8E3BvDXtwkyIhjuFuIjc/PB2DcsVKCCK1gqitqkPgrh41XN3MYN2T hdPTNwXjNbCeOQQ2xVZJHTM9tZCzB8U3Uz1Tu22J3Ev52/pXHqyezJpZO1wPv3hrH4yv 50IXF1b5M+YRcjxsZ68K5dajrvGym/gtreD3y0Inj/CX8gXbbwnKDcLr465VUD6Kc6Gf /VyYSbcriZZ1DQrJbCj0HuClTkGx/iUR62RbejMAZbrS3K1/LVFs6Ejvc/v5kOs16mV8 ktsw== 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=xQTK/9Tc1s5cTjZXN+kqVSQN89MVoOZijWNaHC2AhgL9PJtINFhPAU+ImjQCRuxmIV Qwhu1a2UXitCViPFsJs8sGUnAPNyi0NOzQZNmfmGaoe4ATmgwVwXgkvYIFUgwPqgN1W2 VDOMSG3aOXKBCfAr3KA5ntgHj3CmkH4XRfuDISyyUIhETIQIKlyEZRKEOj+sNOKLj9wn /FlbNwAIHBxXNx+ZICmlulMVuoU+/ChcBuFOq6mDf1Ec7R5Cx93ygWsGKxMcrOIWbCI4 r6Xdxg5P4Y/i2zcDNLFc1lmEORPngfiE3JRfQyTYZb2FyFVfOowfaCAnCaGQxOPvaWll wLBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=nX8jdpMV; 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 o5-20020a639205000000b0053075988265si5497279pgd.59.2023.06.05.06.47.50; Mon, 05 Jun 2023 06:48:05 -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=nX8jdpMV; 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 S234151AbjFENem (ORCPT + 99 others); Mon, 5 Jun 2023 09:34:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44872 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234098AbjFENe2 (ORCPT ); Mon, 5 Jun 2023 09:34:28 -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 C2ED0D2 for ; Mon, 5 Jun 2023 06:34:27 -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=1685972066; 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=nX8jdpMVZCkY0JwiNgTs2/AkkYvqZZn70B8zIx8fV36lLjX1u/q+PtFaQk17mqeCzZ9Y9q 4k9wefyFwIaRU6M6pN0ARpgZ5HslssfZe5panS+n0HArfzJDVAcYFSA4m/Xiu1r9QdY6sj eKRekgpftSP1oCaJQ2tFBai8FmQCNHHTtFCONXfZ3p2pynsr8bEMwqHmWOW5sipa5EKFFy qccypx4kWWWdfQ1Oe1aZObMw4D80tybwefnIJz1q46x0MZWr+jpcO+u0kU+nizOyge8G+S nY89es1Mlw2WrJ+rl4yYaoPreZw0TqtDpTWMoD3Kqt2yoerdRXk0OsXbcVrnng== 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 D2921FF80E; Mon, 5 Jun 2023 13:34:25 +0000 (UTC) From: Miquel Raynal To: Greg Kroah-Hartman Cc: Luka Perkov , Robert Marko , Thomas Petazzoni , , Srinivas Kandagatla , Rafael J Wysocki , Miquel Raynal Subject: [PATCH v3 3/4] ABI: sysfs-nvmem-cells: Expose cells through sysfs Date: Mon, 5 Jun 2023 15:34:21 +0200 Message-Id: <20230605133422.45840-4-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230605133422.45840-1-miquel.raynal@bootlin.com> References: <20230605133422.45840-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 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?1767870704112221439?= X-GMAIL-MSGID: =?utf-8?q?1767870704112221439?= 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 Mon Jun 5 13:34:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miquel Raynal X-Patchwork-Id: 103274 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2710136vqr; Mon, 5 Jun 2023 07:06:27 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4qaxrxC46UlMaqsKdc9lxN2+SesrFtWcBHhfFtfmGFauvNyZQJ5V2SO6GE6/+JhWrIsHBR X-Received: by 2002:a05:6a20:3d81:b0:10c:4ff5:38b7 with SMTP id s1-20020a056a203d8100b0010c4ff538b7mr2807390pzi.6.1685973987351; Mon, 05 Jun 2023 07:06:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685973987; cv=none; d=google.com; s=arc-20160816; b=NxLlmkiiRGstpO9vt05QBZEWd7F3ODbQ+8HCtZUiOSD11F3EYsbimI0X6RwpNLHXPa bfey3ztD9yB4+8qlcouXVyL3WBvvN5Ss59p06LL5pl7bOiCmSjz3VOsGiiNDi8aGTr8Q cP86Hpiqo9FFRDQBO7yedQf2L+GUic/47DzGGI2CjxIqlZJjaD0muaq5wrci5MS4g6j2 J0ejAoojaBDuWVbNceGq24PaS7vjbrDXT4oMh/rXV5/hc0tJLUgw7xsC/Tv3sB5yL2kO KTu0p2NRetoyW/2GdzFXTXfZN6GfQYugAcJJcoaqZFfEYlZ3VtocxKg0WzZhHyb0Qcvs v8/A== 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=ul0VaxBEo8g8rMrRqLfw/GSk/B+aKOEx/21Oio7OP10=; b=P0ebC6gtrLKYEYpkvMY2JJycIhGT+cqcORBeZ0WxI3OQ6Di8lipIY7XABtB0TKvqGh EcHYcWYo0hz0l982y1U3q9N+os0AzecFK69/RlmQB1RcSJdmKjKl3krvjGTK9cGaLyJw iSySifOKARPObs7uUW2tp5vzjm7jretVXTNg+nqEKFswA7+ZEyBCkmibqcOV9qwzqszs acLIi4gSu6hNaFWC0qp1otxEnVk+A72coIRKxJLq4WhmhvIfmzGR5DxXdDqnWJQIXMAc H30iEvJVmGKVhsv8imACLUWeyKYOZ+T2/u7djjuzq50OMKKKvmYFrNJbnFIQotq8VyiK /omQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=QalijX1c; 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 f13-20020a65550d000000b0053ef3c50929si5666028pgr.508.2023.06.05.07.06.14; Mon, 05 Jun 2023 07:06: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=QalijX1c; 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 S234187AbjFENes (ORCPT + 99 others); Mon, 5 Jun 2023 09:34:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234101AbjFENea (ORCPT ); Mon, 5 Jun 2023 09:34:30 -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 8A83EDA for ; Mon, 5 Jun 2023 06:34:28 -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=1685972067; 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=ul0VaxBEo8g8rMrRqLfw/GSk/B+aKOEx/21Oio7OP10=; b=QalijX1ciJf2AhowToySXhKEEnq6CMrQWEe9uwjZZt+YlTt7oC/3IIPTXWE58twZuB9P4T 5unLopqKTsNYZDXXITVKcUOheClxnnPj6CC/uIv1SlVG7fcKscgYKRkynPj3Bmkj+L00WH vIU5Xtz154IpL4UAXmNKi3hTqbmkED0lwgrvkXA43jnclNx1R5aBs3VsxERD3pZAuJAwfJ CU92KVGReiPVRK9b+FfcwjCni6dO4GVH9puOX/URaR4YDdtxFXK0A1xvIOw78S5V03xe5F 0LOA5t6CWhKHuziokEZ8f7FR+eNK57/NNd13K1Y16UDuMyNvWop63G/Z5iiHlw== 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 9DDA3FF810; Mon, 5 Jun 2023 13:34:26 +0000 (UTC) From: Miquel Raynal To: Greg Kroah-Hartman Cc: Luka Perkov , Robert Marko , Thomas Petazzoni , , Srinivas Kandagatla , Rafael J Wysocki , Miquel Raynal Subject: [PATCH v3 4/4] nvmem: core: Expose cells through sysfs Date: Mon, 5 Jun 2023 15:34:22 +0200 Message-Id: <20230605133422.45840-5-miquel.raynal@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230605133422.45840-1-miquel.raynal@bootlin.com> References: <20230605133422.45840-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 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?1767871859303834347?= X-GMAIL-MSGID: =?utf-8?q?1767871859303834347?= 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 | 128 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 124 insertions(+), 4 deletions(-) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 342cd380b420..625e3de273b7 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -325,6 +325,61 @@ 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; + struct nvmem_device *nvmem; + size_t cell_sz, read_len; + struct device *dev; + void *content; + + if (attr->private) + dev = attr->private; + else + dev = kobj_to_dev(kobj); + nvmem = to_nvmem_device(dev); + + mutex_lock(&nvmem_mutex); + list_for_each_entry(entry, &nvmem->cells, node) { + if (strncmp(entry->name, attr->attr.name, XATTR_NAME_MAX)) + continue; + + cell = nvmem_create_cell(entry, entry->name, 0); + if (IS_ERR(cell)) { + mutex_unlock(&nvmem_mutex); + return PTR_ERR(cell); + } + + break; + } + mutex_unlock(&nvmem_mutex); + + 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 +401,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 +467,59 @@ 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; + + list_for_each_entry(entry, &nvmem->cells, node) + ncells++; + + /* Allocate an array of attributes with a sentinel */ + 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; + 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 +1090,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;