From nobody Sun Dec 14 11:33:23 2025 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 301F722DC2B for ; Thu, 16 Jan 2025 17:32:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737048757; cv=none; b=HK9ZND9NfS7xWMx77NGJTyXs2RbbgRulL98vgwmtakmxt2RQ1zKV8+xbBNZFElAYqtrthFLHUs3087luD+AdgsRKyHD27wt0ROMe8j6/8lysazUkmugo3/NCGuiPAy6dh81T5cmEolOo7f3RvgLj3qr41Q0/7d5TZr/8ljjmgWM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737048757; c=relaxed/simple; bh=i0sjLXnG8vqYxeNMyCTWDNGEmZsA83vTN7vXSRsg/GQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=IiM6H98c9YWOQLzRTOd3OmmTosR58x2Kxm773sVSncxKJ2zlYeR0030yiR2WQVefC0mqZc6H/zb86WZNB+JIhKHBSxdytZSKntAaQ2ncrte4c7jj8O9NSHKChsRqLqMrc+eZPAiBilRfcnfjZXhjQ6ckutkzDoymKciiaNne32w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=kfAZ/8vB; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="kfAZ/8vB" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1737048750; bh=i0sjLXnG8vqYxeNMyCTWDNGEmZsA83vTN7vXSRsg/GQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=kfAZ/8vBRbnnBtcgUhChztUMDYyp7wHPPnY9MLUbvCUyhr/XUCwZb1wb/KcgmNssB BSw4eQxvxOvVxCnBHvivMxzQeqpONTJVy6OPewpWCNS2Ahw/gerKNX3lEzp8Tgjog1 ua+bHvu2zHFVNwTH4BTaqnZITC8bHtrgOXbJL/M0= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Thu, 16 Jan 2025 18:32:32 +0100 Subject: [PATCH 6/6] samples/kobject: constify 'struct foo_attribute' Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250116-sysfs-const-attr-prep-v1-6-15e72dba4205@weissschuh.net> References: <20250116-sysfs-const-attr-prep-v1-0-15e72dba4205@weissschuh.net> In-Reply-To: <20250116-sysfs-const-attr-prep-v1-0-15e72dba4205@weissschuh.net> To: Greg Kroah-Hartman , "Rafael J. Wysocki" Cc: linux-kernel@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1737048749; l=4840; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=i0sjLXnG8vqYxeNMyCTWDNGEmZsA83vTN7vXSRsg/GQ=; b=3P/7fYRI1JgpHoaefigxCIMoDiMSyawYMi29HqzFKLNbRNoAO5Ljes+L8TvhtwRNVKRoQrmMK 3QMR5J2pI9zBobgJgCKySD0TJ0waMG/3kpSNDikmU/kl2lxX9stI2Yv X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= Showcase and test the new 'struct attribute' constification facilities. Signed-off-by: Thomas Wei=C3=9Fschuh --- samples/kobject/kset-example.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/samples/kobject/kset-example.c b/samples/kobject/kset-example.c index 7d8c68763ff8af696cbafaed1d5f1b173bb4860e..cbcfa628cc2801a7c4fc0d86889= 8c4cfa539825c 100644 --- a/samples/kobject/kset-example.c +++ b/samples/kobject/kset-example.c @@ -37,10 +37,11 @@ struct foo_obj { /* a custom attribute that works just for a struct foo_obj. */ struct foo_attribute { struct attribute attr; - ssize_t (*show)(struct foo_obj *foo, struct foo_attribute *attr, char *bu= f); - ssize_t (*store)(struct foo_obj *foo, struct foo_attribute *attr, const c= har *buf, size_t count); + ssize_t (*show)(struct foo_obj *foo, const struct foo_attribute *attr, ch= ar *buf); + ssize_t (*store)(struct foo_obj *foo, const struct foo_attribute *attr, + const char *buf, size_t count); }; -#define to_foo_attr(x) container_of(x, struct foo_attribute, attr) +#define to_foo_attr(x) container_of_const(x, struct foo_attribute, attr) =20 /* * The default show function that must be passed to sysfs. This will be @@ -53,7 +54,7 @@ static ssize_t foo_attr_show(struct kobject *kobj, struct attribute *attr, char *buf) { - struct foo_attribute *attribute; + const struct foo_attribute *attribute; struct foo_obj *foo; =20 attribute =3D to_foo_attr(attr); @@ -73,7 +74,7 @@ static ssize_t foo_attr_store(struct kobject *kobj, struct attribute *attr, const char *buf, size_t len) { - struct foo_attribute *attribute; + const struct foo_attribute *attribute; struct foo_obj *foo; =20 attribute =3D to_foo_attr(attr); @@ -109,13 +110,13 @@ static void foo_release(struct kobject *kobj) /* * The "foo" file where the .foo variable is read from and written to. */ -static ssize_t foo_show(struct foo_obj *foo_obj, struct foo_attribute *att= r, +static ssize_t foo_show(struct foo_obj *foo_obj, const struct foo_attribut= e *attr, char *buf) { return sysfs_emit(buf, "%d\n", foo_obj->foo); } =20 -static ssize_t foo_store(struct foo_obj *foo_obj, struct foo_attribute *at= tr, +static ssize_t foo_store(struct foo_obj *foo_obj, const struct foo_attribu= te *attr, const char *buf, size_t count) { int ret; @@ -128,14 +129,14 @@ static ssize_t foo_store(struct foo_obj *foo_obj, str= uct foo_attribute *attr, } =20 /* Sysfs attributes cannot be world-writable. */ -static struct foo_attribute foo_attribute =3D +static const struct foo_attribute foo_attribute =3D __ATTR(foo, 0664, foo_show, foo_store); =20 /* * More complex function where we determine which variable is being access= ed by * looking at the attribute for the "baz" and "bar" files. */ -static ssize_t b_show(struct foo_obj *foo_obj, struct foo_attribute *attr, +static ssize_t b_show(struct foo_obj *foo_obj, const struct foo_attribute = *attr, char *buf) { int var; @@ -147,7 +148,7 @@ static ssize_t b_show(struct foo_obj *foo_obj, struct f= oo_attribute *attr, return sysfs_emit(buf, "%d\n", var); } =20 -static ssize_t b_store(struct foo_obj *foo_obj, struct foo_attribute *attr, +static ssize_t b_store(struct foo_obj *foo_obj, const struct foo_attribute= *attr, const char *buf, size_t count) { int var, ret; @@ -163,16 +164,16 @@ static ssize_t b_store(struct foo_obj *foo_obj, struc= t foo_attribute *attr, return count; } =20 -static struct foo_attribute baz_attribute =3D +static const struct foo_attribute baz_attribute =3D __ATTR(baz, 0664, b_show, b_store); -static struct foo_attribute bar_attribute =3D +static const struct foo_attribute bar_attribute =3D __ATTR(bar, 0664, b_show, b_store); =20 /* * Create a group of attributes so that we can create and destroy them all * at once. */ -static struct attribute *foo_default_attrs[] =3D { +static const struct attribute *const foo_default_attrs[] =3D { &foo_attribute.attr, &baz_attribute.attr, &bar_attribute.attr, @@ -180,7 +181,7 @@ static struct attribute *foo_default_attrs[] =3D { }; =20 static umode_t foo_default_attrs_is_visible(struct kobject *kobj, - struct attribute *attr, + const struct attribute *attr, int n) { /* Hide attributes with the same name as the kobject. */ @@ -190,8 +191,8 @@ static umode_t foo_default_attrs_is_visible(struct kobj= ect *kobj, } =20 static const struct attribute_group foo_default_group =3D { - .attrs =3D foo_default_attrs, - .is_visible =3D foo_default_attrs_is_visible, + .attrs_new =3D foo_default_attrs, + .is_visible_new =3D foo_default_attrs_is_visible, }; __ATTRIBUTE_GROUPS(foo_default); =20 --=20 2.48.1