seq_file: Introduce DEFINE_SEQ_ATTRIBUTE() helper macro
Subsystems (3)
cover seq_file: Introduce DEFINE_SEQ_ATTRIBUTE() helper macro
As discussion in https://lore.kernel.org/lkml/[email protected]/,
we could introduce a new helper macro to reduce losts of boilerplate code, vmstat and kprobes is the example which covert to use it, if this is accepted, I will send out more clean ups.
Kefeng Wang (3):
seq_file: Introduce DEFINE_SEQ_ATTRIBUTE() helper macro
mm: vmstat: Convert to use DEFINE_SEQ_ATTRIBUTE macro
kernel: kprobes: Convert to use DEFINE_SEQ_ATTRIBUTE macro
include/linux/seq_file.h | 19 +++++++++++++++++++ kernel/kprobes.c | 33 ++++++--------------------------- mm/vmstat.c | 32 ++++++-------------------------- 3 files changed, 31 insertions(+), 53 deletions(-)
Signature · 1 line
2.26.2
PATCH 1/3 seq_file: Introduce DEFINE_SEQ_ATTRIBUTE() helper macro
Introduce DEFINE_SEQ_ATTRIBUTE() helper macro to decrease code duplication.
--- include/linux/seq_file.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
include/linux/seq_file.h +19 -0
@@ -145,6 +145,25 @@ void *__seq_open_private(struct file *, const struct seq_operations *, int);int seq_open_private (struct file *,const struct seq_operations *,int );int seq_release_private (struct inode *,struct file *); +#define DEFINE_SEQ_ATTRIBUTE (__name) \ +static int __name ## _open(struct inode *inode, struct file *file) \ +{ \ + int ret = seq_open(file, &__name ## _sops); \ + if (!ret && inode->i_private) { \ + struct seq_file *seq_f = file->private_data; \ + seq_f->private = inode->i_private; \ + } \ + return ret; \ +} \ + \ +static const struct file_operations __name ## _fops = { \ + .owner = THIS_MODULE, \ + .open = __name ## _open, \ + .read = seq_read, \ + .llseek = seq_lseek, \ + .release = seq_release, \ +} +#define DEFINE_SHOW_ATTRIBUTE (__name) \ static int __name ## _open(struct inode *inode, struct file *file) \ { \
Signature · 1 line
2.26.2
PATCH 2/3 mm: vmstat: Convert to use DEFINE_SEQ_ATTRIBUTE macro
In reply to Kefeng WangUse DEFINE_SEQ_ATTRIBUTE macro to simplify the code.
--- mm/vmstat.c | 32 ++++++-------------------------- 1 file changed, 6 insertions(+), 26 deletions(-)
mm/vmstat.c +6 -26
@@ -2055,24 +2055,14 @@ static int unusable_show(struct seq_file *m, void *arg)return 0 ; } -static const struct seq_operations unusable_op = { +static const struct seq_operations unusable_sops = { .start = frag_start, .next = frag_next, .stop = frag_stop, .show = unusable_show, }; -static int unusable_open (struct inode *inode,struct file *file) -{ -return seq_open (file, &unusable_op); -} - -static const struct file_operations unusable_file_ops = { - .open = unusable_open, - .read = seq_read, - .llseek = seq_lseek, - .release = seq_release, -}; +DEFINE_SEQ_ATTRIBUTE (unusable);static void extfrag_show_print (struct seq_file *m,pg_data_t *pgdat,struct zone *zone)
@@ -2107,24 +2097,14 @@ static int extfrag_show(struct seq_file *m, void *arg)return 0 ; } -static const struct seq_operations extfrag_op = { +static const struct seq_operations extfrag_sops = { .start = frag_start, .next = frag_next, .stop = frag_stop, .show = extfrag_show, }; -static int extfrag_open (struct inode *inode,struct file *file) -{ -return seq_open (file, &extfrag_op); -} - -static const struct file_operations extfrag_file_ops = { - .open = extfrag_open, - .read = seq_read, - .llseek = seq_lseek, - .release = seq_release, -}; +DEFINE_SEQ_ATTRIBUTE (extfrag);static int __init extfrag_debug_init (void ) {
@@ -2133,10 +2113,10 @@ static int __init extfrag_debug_init(void) extfrag_debug_root =debugfs_create_dir ("extfrag" ,NULL );debugfs_create_file ("unusable_index" ,0444 , extfrag_debug_root,NULL , - &unusable_file_ops); + &unusable_fops);debugfs_create_file ("extfrag_index" ,0444 , extfrag_debug_root,NULL , - &extfrag_file_ops); + &extfrag_fops);return 0 ; }
Signature · 1 line
2.26.2
PATCH 3/3 kernel: kprobes: Convert to use DEFINE_SEQ_ATTRIBUTE macro
In reply to Kefeng WangUse DEFINE_SEQ_ATTRIBUTE macro to simplify the code.
--- kernel/kprobes.c | 33 ++++++--------------------------- 1 file changed, 6 insertions(+), 27 deletions(-)
kernel/kprobes.c +6 -27
@@ -2398,24 +2398,14 @@ static int show_kprobe_addr(struct seq_file *pi, void *v)return 0 ; } -static const struct seq_operations kprobes_seq_ops = { +static const struct seq_operations kprobes_sops = { .start = kprobe_seq_start, .next = kprobe_seq_next, .stop = kprobe_seq_stop, .show = show_kprobe_addr }; -static int kprobes_open (struct inode *inode,struct file *filp) -{ -return seq_open (filp, &kprobes_seq_ops); -} - -static const struct file_operations debugfs_kprobes_operations = { - .open = kprobes_open, - .read = seq_read, - .llseek = seq_lseek, - .release = seq_release, -}; +DEFINE_SEQ_ATTRIBUTE (kprobes);/* kprobes/blacklist -- shows which functions can not be probed */ static void *kprobe_blacklist_seq_start (struct seq_file *m,loff_t *pos)
@@ -2446,24 +2436,14 @@ static int kprobe_blacklist_seq_show(struct seq_file *m, void *v)return 0 ; } -static const struct seq_operations kprobe_blacklist_seq_ops = { +static const struct seq_operations kprobe_blacklist_sops = { .start = kprobe_blacklist_seq_start, .next = kprobe_blacklist_seq_next, .stop = kprobe_seq_stop,/* Reuse void function */ .show = kprobe_blacklist_seq_show, }; -static int kprobe_blacklist_open (struct inode *inode,struct file *filp) -{ -return seq_open (filp, &kprobe_blacklist_seq_ops); -} - -static const struct file_operations debugfs_kprobe_blacklist_ops = { - .open = kprobe_blacklist_open, - .read = seq_read, - .llseek = seq_lseek, - .release = seq_release, -}; +DEFINE_SEQ_ATTRIBUTE (kprobe_blacklist);static int arm_all_kprobes (void ) {
@@ -2622,13 +2602,12 @@ static int __init debugfs_kprobe_init(void) dir =debugfs_create_dir ("kprobes" ,NULL ); -debugfs_create_file ("list" ,0400 , dir,NULL , - &debugfs_kprobes_operations); +debugfs_create_file ("list" ,0400 , dir,NULL , &kprobes_fops);debugfs_create_file ("enabled" ,0600 , dir, &value, &fops_kp);debugfs_create_file ("blacklist" ,0400 , dir,NULL , - &debugfs_kprobe_blacklist_ops); + &kprobe_blacklist_fops);return 0 ; }
Signature · 1 line
2.26.2