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) {