treewide: Use sysfs_bin_attr_simple_read() helper
authorLukas Wunner <lukas@wunner.de>
Sat, 6 Apr 2024 13:52:02 +0000 (15:52 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 11 Apr 2024 14:02:25 +0000 (16:02 +0200)
Deduplicate ->read() callbacks of bin_attributes which are backed by a
simple buffer in memory:

Use the newly introduced sysfs_bin_attr_simple_read() helper instead,
either by referencing it directly or by declaring such bin_attributes
with BIN_ATTR_SIMPLE_RO() or BIN_ATTR_SIMPLE_ADMIN_RO().

Aside from a reduction of LoC, this shaves off a few bytes from vmlinux
(304 bytes on an x86_64 allyesconfig).

No functional change intended.

Signed-off-by: Lukas Wunner <lukas@wunner.de>
Acked-by: Zhi Wang <zhiwang@kernel.org>
Acked-by: Michael Ellerman <mpe@ellerman.id.au>
Acked-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Ard Biesheuvel <ardb@kernel.org>
Link: https://lore.kernel.org/r/92ee0a0e83a5a3f3474845db6c8575297698933a.1712410202.git.lukas@wunner.de
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
arch/powerpc/platforms/powernv/opal.c
drivers/acpi/bgrt.c
drivers/firmware/dmi_scan.c
drivers/firmware/efi/rci2-table.c
drivers/gpu/drm/i915/gvt/firmware.c
drivers/thermal/intel/int340x_thermal/int3400_thermal.c
init/initramfs.c
kernel/module/sysfs.c

index 45dd77e3ccf6f8dff9c074ce3e58788e73e7104e..5d0f35bb917ebced8c741cd3af2c511949a1d2ef 100644 (file)
@@ -792,14 +792,6 @@ static int __init opal_sysfs_init(void)
        return 0;
 }
 
-static ssize_t export_attr_read(struct file *fp, struct kobject *kobj,
-                               struct bin_attribute *bin_attr, char *buf,
-                               loff_t off, size_t count)
-{
-       return memory_read_from_buffer(buf, count, &off, bin_attr->private,
-                                      bin_attr->size);
-}
-
 static int opal_add_one_export(struct kobject *parent, const char *export_name,
                               struct device_node *np, const char *prop_name)
 {
@@ -826,7 +818,7 @@ static int opal_add_one_export(struct kobject *parent, const char *export_name,
        sysfs_bin_attr_init(attr);
        attr->attr.name = name;
        attr->attr.mode = 0400;
-       attr->read = export_attr_read;
+       attr->read = sysfs_bin_attr_simple_read;
        attr->private = __va(vals[0]);
        attr->size = vals[1];
 
index e4fb9e225ddfcb6830e96e67a90dd02c2b3f0e92..d1d9c9289087a1ca6b36e8497cb104bad3282737 100644 (file)
@@ -29,14 +29,7 @@ BGRT_SHOW(type, image_type);
 BGRT_SHOW(xoffset, image_offset_x);
 BGRT_SHOW(yoffset, image_offset_y);
 
-static ssize_t image_read(struct file *file, struct kobject *kobj,
-              struct bin_attribute *attr, char *buf, loff_t off, size_t count)
-{
-       memcpy(buf, attr->private + off, count);
-       return count;
-}
-
-static BIN_ATTR_RO(image, 0);  /* size gets filled in later */
+static BIN_ATTR_SIMPLE_RO(image);
 
 static struct attribute *bgrt_attributes[] = {
        &bgrt_attr_version.attr,
index 015c95a825d315e02b586ece662d8c43cd484428..3d0f773ab876b03e03be8c22e3cffc818bb62269 100644 (file)
@@ -746,16 +746,8 @@ static void __init dmi_scan_machine(void)
        pr_info("DMI not present or invalid.\n");
 }
 
-static ssize_t raw_table_read(struct file *file, struct kobject *kobj,
-                             struct bin_attribute *attr, char *buf,
-                             loff_t pos, size_t count)
-{
-       memcpy(buf, attr->private + pos, count);
-       return count;
-}
-
-static BIN_ATTR(smbios_entry_point, S_IRUSR, raw_table_read, NULL, 0);
-static BIN_ATTR(DMI, S_IRUSR, raw_table_read, NULL, 0);
+static BIN_ATTR_SIMPLE_ADMIN_RO(smbios_entry_point);
+static BIN_ATTR_SIMPLE_ADMIN_RO(DMI);
 
 static int __init dmi_init(void)
 {
index de1a9a1f9f146743e1aff99fc50b36aa49a6e162..4fd45d6f69a4d8b3233b2e49dc91f0b734a338e9 100644 (file)
@@ -40,15 +40,7 @@ static u8 *rci2_base;
 static u32 rci2_table_len;
 unsigned long rci2_table_phys __ro_after_init = EFI_INVALID_TABLE_ADDR;
 
-static ssize_t raw_table_read(struct file *file, struct kobject *kobj,
-                             struct bin_attribute *attr, char *buf,
-                             loff_t pos, size_t count)
-{
-       memcpy(buf, attr->private + pos, count);
-       return count;
-}
-
-static BIN_ATTR(rci2, S_IRUSR, raw_table_read, NULL, 0);
+static BIN_ATTR_SIMPLE_ADMIN_RO(rci2);
 
 static u16 checksum(void)
 {
index 4dd52ac2043e7ac4b3ee23ff6c2fa6e3dfbc693d..5e66a260df121f107e219b996ff3bce233034532 100644 (file)
@@ -50,21 +50,7 @@ struct gvt_firmware_header {
 
 #define dev_to_drm_minor(d) dev_get_drvdata((d))
 
-static ssize_t
-gvt_firmware_read(struct file *filp, struct kobject *kobj,
-            struct bin_attribute *attr, char *buf,
-            loff_t offset, size_t count)
-{
-       memcpy(buf, attr->private + offset, count);
-       return count;
-}
-
-static struct bin_attribute firmware_attr = {
-       .attr = {.name = "gvt_firmware", .mode = (S_IRUSR)},
-       .read = gvt_firmware_read,
-       .write = NULL,
-       .mmap = NULL,
-};
+static BIN_ATTR_SIMPLE_ADMIN_RO(gvt_firmware);
 
 static int expose_firmware_sysfs(struct intel_gvt *gvt)
 {
@@ -107,10 +93,10 @@ static int expose_firmware_sysfs(struct intel_gvt *gvt)
        crc32_start = offsetof(struct gvt_firmware_header, version);
        h->crc32 = crc32_le(0, firmware + crc32_start, size - crc32_start);
 
-       firmware_attr.size = size;
-       firmware_attr.private = firmware;
+       bin_attr_gvt_firmware.size = size;
+       bin_attr_gvt_firmware.private = firmware;
 
-       ret = device_create_bin_file(&pdev->dev, &firmware_attr);
+       ret = device_create_bin_file(&pdev->dev, &bin_attr_gvt_firmware);
        if (ret) {
                vfree(firmware);
                return ret;
@@ -122,8 +108,8 @@ static void clean_firmware_sysfs(struct intel_gvt *gvt)
 {
        struct pci_dev *pdev = to_pci_dev(gvt->gt->i915->drm.dev);
 
-       device_remove_bin_file(&pdev->dev, &firmware_attr);
-       vfree(firmware_attr.private);
+       device_remove_bin_file(&pdev->dev, &bin_attr_gvt_firmware);
+       vfree(bin_attr_gvt_firmware.private);
 }
 
 /**
index 427d370648d5af4414c6f0f03d57dffdf12e51e3..6d4b51a122bc338af92fa3c405b67e44accc82cb 100644 (file)
@@ -73,14 +73,7 @@ struct odvp_attr {
        struct device_attribute attr;
 };
 
-static ssize_t data_vault_read(struct file *file, struct kobject *kobj,
-            struct bin_attribute *attr, char *buf, loff_t off, size_t count)
-{
-       memcpy(buf, attr->private + off, count);
-       return count;
-}
-
-static BIN_ATTR_RO(data_vault, 0);
+static BIN_ATTR_SIMPLE_RO(data_vault);
 
 static struct bin_attribute *data_attributes[] = {
        &bin_attr_data_vault,
index da79760b8be3a67378deb49a8ae8588bc19ddce0..5193fae330c37eb447f78ec54749a4c8aebf045c 100644 (file)
@@ -575,15 +575,7 @@ extern unsigned long __initramfs_size;
 #include <linux/initrd.h>
 #include <linux/kexec.h>
 
-static ssize_t raw_read(struct file *file, struct kobject *kobj,
-                       struct bin_attribute *attr, char *buf,
-                       loff_t pos, size_t count)
-{
-       memcpy(buf, attr->private + pos, count);
-       return count;
-}
-
-static BIN_ATTR(initrd, 0440, raw_read, NULL, 0);
+static BIN_ATTR(initrd, 0440, sysfs_bin_attr_simple_read, NULL, 0);
 
 void __init reserve_initrd_mem(void)
 {
index d964167c66586807a783316dc1873ea1d824e3e0..26efe1305c120e228b2fe9471900316b12a86852 100644 (file)
@@ -146,17 +146,6 @@ struct module_notes_attrs {
        struct bin_attribute attrs[] __counted_by(notes);
 };
 
-static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
-                                struct bin_attribute *bin_attr,
-                                char *buf, loff_t pos, size_t count)
-{
-       /*
-        * The caller checked the pos and count against our size.
-        */
-       memcpy(buf, bin_attr->private + pos, count);
-       return count;
-}
-
 static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
                             unsigned int i)
 {
@@ -205,7 +194,7 @@ static void add_notes_attrs(struct module *mod, const struct load_info *info)
                        nattr->attr.mode = 0444;
                        nattr->size = info->sechdrs[i].sh_size;
                        nattr->private = (void *)info->sechdrs[i].sh_addr;
-                       nattr->read = module_notes_read;
+                       nattr->read = sysfs_bin_attr_simple_read;
                        ++nattr;
                }
                ++loaded;