Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp1391980lqt; Sat, 20 Apr 2024 13:03:02 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVvXksQHkBEP2nO5WPOntkQ3C2QJOPNEruw7UGEjp263UOAQBy+/4UwFyitljkrdSZo1xxm+01v9NzU3GeCMlOPiwagDvnIzh8FiNCImw== X-Google-Smtp-Source: AGHT+IEzcU4pQX9kb/2Ofytr1igQBBN7A3f5ZjGsuj9xcYfeyGKMBi657D9FizJ8bW3GG8tOm0hU X-Received: by 2002:aa7:9a0e:0:b0:6f0:c79f:c844 with SMTP id w14-20020aa79a0e000000b006f0c79fc844mr6916094pfj.0.1713643382676; Sat, 20 Apr 2024 13:03:02 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713643382; cv=pass; d=google.com; s=arc-20160816; b=sxU8IN5DxJox13A0i2LBA7cKMhztnRBHZo5ERlZlagr5WDElCcUUyOIw8IhYRgqQ3w G+Enaqfe1iERZQFbo81kxD7abHJ2cqKeXCzwHbESm7io0sNi7X++AqpkL2iSvAKd5Sxt CUr7x2Bz0GlnTTIZtUfABlCc7lWd2yz2klYNdeBUAZEBZahF83W1XGWTeJ6TnlhTH1UT Abb3o/s9bw/g+E7Jm0e66HGiouOz8gCMdPWIcedeGpeBI56btHwfwNG5bfTgDy7SeS2C SyMGCsGLyuJLaSE/KaLmKQYSKmAYa9HFjXWt3pp9XAj0Ybgw757AYtrO36z4XzQ0ms5f AZXg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-id:precedence:cc:to:subject :date:from:references:in-reply-to:message-id; bh=t/M8yTt4c+tlkNOISBx4uRRHyBSr3OpRLJxr4GyHcE0=; fh=6qqKR4SAj4WKIkRYV07K/QgE7MucCwfcg+uytR0hh3w=; b=XbZhBBjH3b3moFzLiXMSZgAe1GFZpr5XdtiZeuTsbgR2LyVyA8GUx7n5sqQbiF6yDE hcjlWCU2t5Dp0KIOBAd+i6VxmsCBTnJdm8FsReJuBBffo4Z+pqwH1dYozSpYHdbmpSq5 fssGoKVV1jAAGKSV7m1ZZJ+NzgVROSaC+QUFLUKqBdOyf/ODgOq+Ji+/M4A8uoc5JWE2 /0lj0o5ZsLzs0CIeEAeUvnuZznBYwxhnG7rBrHFvxLHTs8p2kSmtiVJGTazgPh3WoHHA bHbB34h2Ml0gVO294cQWFJ5RCQ/3NSZnM31eIWGHsigk2JFLfW4gKVkxl09aUGZ5ad3U wFug==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=wunner.de); spf=pass (google.com: domain of linux-kernel+bounces-152365-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-152365-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id q19-20020a056a00085300b006eae33e2aa5si5300446pfk.401.2024.04.20.13.03.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Apr 2024 13:03:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-152365-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=wunner.de); spf=pass (google.com: domain of linux-kernel+bounces-152365-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-152365-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 47277281604 for ; Sat, 20 Apr 2024 20:03:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C31E5446D5; Sat, 20 Apr 2024 20:02:57 +0000 (UTC) Received: from bmailout1.hostsharing.net (bmailout1.hostsharing.net [83.223.95.100]) (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 D716C8F6C for ; Sat, 20 Apr 2024 20:02:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=83.223.95.100 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713643377; cv=none; b=Iukrm6OoU37BKkZeddeM088T+x+BCgXZGsmbWTttKarb50NF3HeIzU/+4ghbeOmeut1+8Y0DC4NftuIpBT1haYIx95cJWmMSU/DOxs3O/y26ReOpmlSn3sZjMeYMRW0GLoiPU41qRRr2TFYddCFrURHJhj+Qh+tCq37xFH+qeGs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713643377; c=relaxed/simple; bh=+JxH8dFXAsGZnl1VN1Unl6FLp+2yGtti2ERqBLuV1AQ=; h=Message-ID:In-Reply-To:References:From:Date:Subject:To:Cc; b=k7ymzgtz0WEVa+p/a9O6C5K5TP+OcOSoV2SUyuGuzN+3YE5HRZpNJ7VZ7BH22p1cQ+qyOHRjApqdyW+F093N6Kwv9z6dZO1eRi/eF5LpqNXegRVGckfDI8x5Z20h/y4Bfw+YXZq6gNVYNWM0X+pbw2bW3BSVRgzLsgnYepApAxY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=wunner.de; spf=pass smtp.mailfrom=wunner.de; arc=none smtp.client-ip=83.223.95.100 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=wunner.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=wunner.de Received: from h08.hostsharing.net (h08.hostsharing.net [83.223.95.28]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "*.hostsharing.net", Issuer "RapidSSL TLS RSA CA G1" (verified OK)) by bmailout1.hostsharing.net (Postfix) with ESMTPS id A1B9C30008F1A; Sat, 20 Apr 2024 22:02:46 +0200 (CEST) Received: by h08.hostsharing.net (Postfix, from userid 100393) id 9AB0ACC66D; Sat, 20 Apr 2024 22:02:46 +0200 (CEST) Message-ID: <2e3eaaf2600bb55c0415c23ba301e809403a7aa2.1713608122.git.lukas@wunner.de> In-Reply-To: References: From: Lukas Wunner Date: Sat, 20 Apr 2024 22:00:01 +0200 Subject: [PATCH 1/6] driver core: Add device_show_string() helper for sysfs attributes To: Greg Kroah-Hartman , "Rafael J. Wysocki" , linux-kernel@vger.kernel.org Cc: Michael Ellerman , linuxppc-dev@lists.ozlabs.org Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: For drivers wishing to expose an unsigned long, int or bool at a static memory location in sysfs, the driver core provides ready-made helpers such as device_show_ulong() to be used as ->show() callback. Some drivers need to expose a string and so far they all provide their own ->show() implementation. arch/powerpc/perf/hv-24x7.c went so far as to create a device_show_string() helper but kept it private. Make it public for reuse by other drivers. The pattern seems to be sufficiently frequent to merit a public helper. Add a DEVICE_STRING_ATTR_RO() macro in line with the existing DEVICE_ULONG_ATTR() and similar macros to ease declaration of string attributes. Signed-off-by: Lukas Wunner --- arch/powerpc/perf/hv-24x7.c | 10 ---------- drivers/base/core.c | 9 +++++++++ include/linux/device.h | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/arch/powerpc/perf/hv-24x7.c b/arch/powerpc/perf/hv-24x7.c index 057ec2e3451d..d400fa391c27 100644 --- a/arch/powerpc/perf/hv-24x7.c +++ b/arch/powerpc/perf/hv-24x7.c @@ -425,16 +425,6 @@ static char *memdup_to_str(char *maybe_str, int max_len, gfp_t gfp) return kasprintf(gfp, "%.*s", max_len, maybe_str); } -static ssize_t device_show_string(struct device *dev, - struct device_attribute *attr, char *buf) -{ - struct dev_ext_attribute *d; - - d = container_of(attr, struct dev_ext_attribute, attr); - - return sprintf(buf, "%s\n", (char *)d->var); -} - static ssize_t cpumask_show(struct device *dev, struct device_attribute *attr, char *buf) { diff --git a/drivers/base/core.c b/drivers/base/core.c index 78dfa74ee18b..190d4a39c6a8 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -2523,6 +2523,15 @@ ssize_t device_show_bool(struct device *dev, struct device_attribute *attr, } EXPORT_SYMBOL_GPL(device_show_bool); +ssize_t device_show_string(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct dev_ext_attribute *ea = to_ext_attr(attr); + + return sysfs_emit(buf, "%s\n", (char *)ea->var); +} +EXPORT_SYMBOL_GPL(device_show_string); + /** * device_release - free device structure. * @kobj: device's kobject. diff --git a/include/linux/device.h b/include/linux/device.h index c515ba5756e4..63ac65db3ecb 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -132,6 +132,8 @@ ssize_t device_show_bool(struct device *dev, struct device_attribute *attr, char *buf); ssize_t device_store_bool(struct device *dev, struct device_attribute *attr, const char *buf, size_t count); +ssize_t device_show_string(struct device *dev, struct device_attribute *attr, + char *buf); /** * DEVICE_ATTR - Define a device attribute. @@ -251,6 +253,19 @@ ssize_t device_store_bool(struct device *dev, struct device_attribute *attr, struct dev_ext_attribute dev_attr_##_name = \ { __ATTR(_name, _mode, device_show_bool, device_store_bool), &(_var) } +/** + * DEVICE_STRING_ATTR_RO - Define a device attribute backed by a r/o string. + * @_name: Attribute name. + * @_mode: File mode. + * @_var: Identifier of string. + * + * Like DEVICE_ULONG_ATTR(), but @_var is a string. Because the length of the + * string allocation is unknown, the attribute must be read-only. + */ +#define DEVICE_STRING_ATTR_RO(_name, _mode, _var) \ + struct dev_ext_attribute dev_attr_##_name = \ + { __ATTR(_name, (_mode) & ~0222, device_show_string, NULL), (_var) } + #define DEVICE_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \ struct device_attribute dev_attr_##_name = \ __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) -- 2.43.0