Skip to content

Commit a53bfbb

Browse files
author
Fox Snowpatch
committed
1 parent 4db4221 commit a53bfbb

File tree

10 files changed

+56
-85
lines changed

10 files changed

+56
-85
lines changed

arch/powerpc/platforms/powernv/opal.c

+1-9
Original file line numberDiff line numberDiff line change
@@ -792,14 +792,6 @@ static int __init opal_sysfs_init(void)
792792
return 0;
793793
}
794794

795-
static ssize_t export_attr_read(struct file *fp, struct kobject *kobj,
796-
struct bin_attribute *bin_attr, char *buf,
797-
loff_t off, size_t count)
798-
{
799-
return memory_read_from_buffer(buf, count, &off, bin_attr->private,
800-
bin_attr->size);
801-
}
802-
803795
static int opal_add_one_export(struct kobject *parent, const char *export_name,
804796
struct device_node *np, const char *prop_name)
805797
{
@@ -826,7 +818,7 @@ static int opal_add_one_export(struct kobject *parent, const char *export_name,
826818
sysfs_bin_attr_init(attr);
827819
attr->attr.name = name;
828820
attr->attr.mode = 0400;
829-
attr->read = export_attr_read;
821+
attr->read = sysfs_bin_attr_simple_read;
830822
attr->private = __va(vals[0]);
831823
attr->size = vals[1];
832824

drivers/acpi/bgrt.c

+1-8
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,7 @@ BGRT_SHOW(type, image_type);
2929
BGRT_SHOW(xoffset, image_offset_x);
3030
BGRT_SHOW(yoffset, image_offset_y);
3131

32-
static ssize_t image_read(struct file *file, struct kobject *kobj,
33-
struct bin_attribute *attr, char *buf, loff_t off, size_t count)
34-
{
35-
memcpy(buf, attr->private + off, count);
36-
return count;
37-
}
38-
39-
static BIN_ATTR_RO(image, 0); /* size gets filled in later */
32+
static BIN_ATTR_SIMPLE_RO(image);
4033

4134
static struct attribute *bgrt_attributes[] = {
4235
&bgrt_attr_version.attr,

drivers/firmware/dmi_scan.c

+2-10
Original file line numberDiff line numberDiff line change
@@ -746,16 +746,8 @@ static void __init dmi_scan_machine(void)
746746
pr_info("DMI not present or invalid.\n");
747747
}
748748

749-
static ssize_t raw_table_read(struct file *file, struct kobject *kobj,
750-
struct bin_attribute *attr, char *buf,
751-
loff_t pos, size_t count)
752-
{
753-
memcpy(buf, attr->private + pos, count);
754-
return count;
755-
}
756-
757-
static BIN_ATTR(smbios_entry_point, S_IRUSR, raw_table_read, NULL, 0);
758-
static BIN_ATTR(DMI, S_IRUSR, raw_table_read, NULL, 0);
749+
static BIN_ATTR_SIMPLE_ADMIN_RO(smbios_entry_point);
750+
static BIN_ATTR_SIMPLE_ADMIN_RO(DMI);
759751

760752
static int __init dmi_init(void)
761753
{

drivers/firmware/efi/rci2-table.c

+1-9
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,7 @@ static u8 *rci2_base;
4040
static u32 rci2_table_len;
4141
unsigned long rci2_table_phys __ro_after_init = EFI_INVALID_TABLE_ADDR;
4242

43-
static ssize_t raw_table_read(struct file *file, struct kobject *kobj,
44-
struct bin_attribute *attr, char *buf,
45-
loff_t pos, size_t count)
46-
{
47-
memcpy(buf, attr->private + pos, count);
48-
return count;
49-
}
50-
51-
static BIN_ATTR(rci2, S_IRUSR, raw_table_read, NULL, 0);
43+
static BIN_ATTR_SIMPLE_ADMIN_RO(rci2);
5244

5345
static u16 checksum(void)
5446
{

drivers/gpu/drm/i915/gvt/firmware.c

+6-20
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,7 @@ struct gvt_firmware_header {
5050

5151
#define dev_to_drm_minor(d) dev_get_drvdata((d))
5252

53-
static ssize_t
54-
gvt_firmware_read(struct file *filp, struct kobject *kobj,
55-
struct bin_attribute *attr, char *buf,
56-
loff_t offset, size_t count)
57-
{
58-
memcpy(buf, attr->private + offset, count);
59-
return count;
60-
}
61-
62-
static struct bin_attribute firmware_attr = {
63-
.attr = {.name = "gvt_firmware", .mode = (S_IRUSR)},
64-
.read = gvt_firmware_read,
65-
.write = NULL,
66-
.mmap = NULL,
67-
};
53+
static BIN_ATTR_SIMPLE_ADMIN_RO(gvt_firmware);
6854

6955
static int expose_firmware_sysfs(struct intel_gvt *gvt)
7056
{
@@ -107,10 +93,10 @@ static int expose_firmware_sysfs(struct intel_gvt *gvt)
10793
crc32_start = offsetof(struct gvt_firmware_header, version);
10894
h->crc32 = crc32_le(0, firmware + crc32_start, size - crc32_start);
10995

110-
firmware_attr.size = size;
111-
firmware_attr.private = firmware;
96+
bin_attr_gvt_firmware.size = size;
97+
bin_attr_gvt_firmware.private = firmware;
11298

113-
ret = device_create_bin_file(&pdev->dev, &firmware_attr);
99+
ret = device_create_bin_file(&pdev->dev, &bin_attr_gvt_firmware);
114100
if (ret) {
115101
vfree(firmware);
116102
return ret;
@@ -122,8 +108,8 @@ static void clean_firmware_sysfs(struct intel_gvt *gvt)
122108
{
123109
struct pci_dev *pdev = to_pci_dev(gvt->gt->i915->drm.dev);
124110

125-
device_remove_bin_file(&pdev->dev, &firmware_attr);
126-
vfree(firmware_attr.private);
111+
device_remove_bin_file(&pdev->dev, &bin_attr_gvt_firmware);
112+
vfree(bin_attr_gvt_firmware.private);
127113
}
128114

129115
/**

drivers/thermal/intel/int340x_thermal/int3400_thermal.c

+1-8
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,7 @@ struct odvp_attr {
7373
struct device_attribute attr;
7474
};
7575

76-
static ssize_t data_vault_read(struct file *file, struct kobject *kobj,
77-
struct bin_attribute *attr, char *buf, loff_t off, size_t count)
78-
{
79-
memcpy(buf, attr->private + off, count);
80-
return count;
81-
}
82-
83-
static BIN_ATTR_RO(data_vault, 0);
76+
static BIN_ATTR_SIMPLE_RO(data_vault);
8477

8578
static struct bin_attribute *data_attributes[] = {
8679
&bin_attr_data_vault,

fs/sysfs/file.c

+27
Original file line numberDiff line numberDiff line change
@@ -783,3 +783,30 @@ int sysfs_emit_at(char *buf, int at, const char *fmt, ...)
783783
return len;
784784
}
785785
EXPORT_SYMBOL_GPL(sysfs_emit_at);
786+
787+
/**
788+
* sysfs_bin_attr_simple_read - read callback to simply copy from memory.
789+
* @file: attribute file which is being read.
790+
* @kobj: object to which the attribute belongs.
791+
* @attr: attribute descriptor.
792+
* @buf: destination buffer.
793+
* @off: offset in bytes from which to read.
794+
* @count: maximum number of bytes to read.
795+
*
796+
* Simple ->read() callback for bin_attributes backed by a buffer in memory.
797+
* The @private and @size members in struct bin_attribute must be set to the
798+
* buffer's location and size before the bin_attribute is created in sysfs.
799+
*
800+
* Bounds check for @off and @count is done in sysfs_kf_bin_read().
801+
* Negative value check for @off is done in vfs_setpos() and default_llseek().
802+
*
803+
* Returns number of bytes written to @buf.
804+
*/
805+
ssize_t sysfs_bin_attr_simple_read(struct file *file, struct kobject *kobj,
806+
struct bin_attribute *attr, char *buf,
807+
loff_t off, size_t count)
808+
{
809+
memcpy(buf, attr->private + off, count);
810+
return count;
811+
}
812+
EXPORT_SYMBOL_GPL(sysfs_bin_attr_simple_read);

include/linux/sysfs.h

+15
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,17 @@ struct bin_attribute bin_attr_##_name = __BIN_ATTR_ADMIN_RO(_name, _size)
371371
#define BIN_ATTR_ADMIN_RW(_name, _size) \
372372
struct bin_attribute bin_attr_##_name = __BIN_ATTR_ADMIN_RW(_name, _size)
373373

374+
#define __BIN_ATTR_SIMPLE_RO(_name, _mode) { \
375+
.attr = { .name = __stringify(_name), .mode = _mode }, \
376+
.read = sysfs_bin_attr_simple_read, \
377+
}
378+
379+
#define BIN_ATTR_SIMPLE_RO(_name) \
380+
struct bin_attribute bin_attr_##_name = __BIN_ATTR_SIMPLE_RO(_name, 0444)
381+
382+
#define BIN_ATTR_SIMPLE_ADMIN_RO(_name) \
383+
struct bin_attribute bin_attr_##_name = __BIN_ATTR_SIMPLE_RO(_name, 0400)
384+
374385
struct sysfs_ops {
375386
ssize_t (*show)(struct kobject *, struct attribute *, char *);
376387
ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t);
@@ -478,6 +489,10 @@ int sysfs_emit(char *buf, const char *fmt, ...);
478489
__printf(3, 4)
479490
int sysfs_emit_at(char *buf, int at, const char *fmt, ...);
480491

492+
ssize_t sysfs_bin_attr_simple_read(struct file *file, struct kobject *kobj,
493+
struct bin_attribute *attr, char *buf,
494+
loff_t off, size_t count);
495+
481496
#else /* CONFIG_SYSFS */
482497

483498
static inline int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)

init/initramfs.c

+1-9
Original file line numberDiff line numberDiff line change
@@ -575,15 +575,7 @@ extern unsigned long __initramfs_size;
575575
#include <linux/initrd.h>
576576
#include <linux/kexec.h>
577577

578-
static ssize_t raw_read(struct file *file, struct kobject *kobj,
579-
struct bin_attribute *attr, char *buf,
580-
loff_t pos, size_t count)
581-
{
582-
memcpy(buf, attr->private + pos, count);
583-
return count;
584-
}
585-
586-
static BIN_ATTR(initrd, 0440, raw_read, NULL, 0);
578+
static BIN_ATTR(initrd, 0440, sysfs_bin_attr_simple_read, NULL, 0);
587579

588580
void __init reserve_initrd_mem(void)
589581
{

kernel/module/sysfs.c

+1-12
Original file line numberDiff line numberDiff line change
@@ -146,17 +146,6 @@ struct module_notes_attrs {
146146
struct bin_attribute attrs[] __counted_by(notes);
147147
};
148148

149-
static ssize_t module_notes_read(struct file *filp, struct kobject *kobj,
150-
struct bin_attribute *bin_attr,
151-
char *buf, loff_t pos, size_t count)
152-
{
153-
/*
154-
* The caller checked the pos and count against our size.
155-
*/
156-
memcpy(buf, bin_attr->private + pos, count);
157-
return count;
158-
}
159-
160149
static void free_notes_attrs(struct module_notes_attrs *notes_attrs,
161150
unsigned int i)
162151
{
@@ -205,7 +194,7 @@ static void add_notes_attrs(struct module *mod, const struct load_info *info)
205194
nattr->attr.mode = 0444;
206195
nattr->size = info->sechdrs[i].sh_size;
207196
nattr->private = (void *)info->sechdrs[i].sh_addr;
208-
nattr->read = module_notes_read;
197+
nattr->read = sysfs_bin_attr_simple_read;
209198
++nattr;
210199
}
211200
++loaded;

0 commit comments

Comments
 (0)