All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/3] seq_file: Introduce DEFINE_SEQ_ATTRIBUTE() helper macro
@ 2020-05-09  6:40 Kefeng Wang
  2020-05-09  6:40 ` [PATCH 1/3] " Kefeng Wang
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Kefeng Wang @ 2020-05-09  6:40 UTC (permalink / raw)
  To: Greg KH, linux-kernel; +Cc: Ingo Molnar, Andrew Morton, Kefeng Wang

As discussion in https://lore.kernel.org/lkml/20191129222310.GA3712618@kroah.com/,
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(-)

-- 
2.26.2


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/3] seq_file: Introduce DEFINE_SEQ_ATTRIBUTE() helper macro
  2020-05-09  6:40 [PATCH 0/3] seq_file: Introduce DEFINE_SEQ_ATTRIBUTE() helper macro Kefeng Wang
@ 2020-05-09  6:40 ` Kefeng Wang
  2020-05-09  6:40 ` [PATCH 2/3] mm: vmstat: Convert to use DEFINE_SEQ_ATTRIBUTE macro Kefeng Wang
  2020-05-09  6:40 ` [PATCH 3/3] kernel: kprobes: " Kefeng Wang
  2 siblings, 0 replies; 4+ messages in thread
From: Kefeng Wang @ 2020-05-09  6:40 UTC (permalink / raw)
  To: Greg KH, linux-kernel; +Cc: Ingo Molnar, Andrew Morton, Kefeng Wang

Introduce DEFINE_SEQ_ATTRIBUTE() helper macro to decrease code duplication.

Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
---
 include/linux/seq_file.h | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/include/linux/seq_file.h b/include/linux/seq_file.h
index 1672cf6f7614..c77869cf7d10 100644
--- a/include/linux/seq_file.h
+++ b/include/linux/seq_file.h
@@ -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)	\
 {									\
-- 
2.26.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/3] mm: vmstat: Convert to use DEFINE_SEQ_ATTRIBUTE macro
  2020-05-09  6:40 [PATCH 0/3] seq_file: Introduce DEFINE_SEQ_ATTRIBUTE() helper macro Kefeng Wang
  2020-05-09  6:40 ` [PATCH 1/3] " Kefeng Wang
@ 2020-05-09  6:40 ` Kefeng Wang
  2020-05-09  6:40 ` [PATCH 3/3] kernel: kprobes: " Kefeng Wang
  2 siblings, 0 replies; 4+ messages in thread
From: Kefeng Wang @ 2020-05-09  6:40 UTC (permalink / raw)
  To: Greg KH, linux-kernel; +Cc: Ingo Molnar, Andrew Morton, Kefeng Wang

Use DEFINE_SEQ_ATTRIBUTE macro to simplify the code.

Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
---
 mm/vmstat.c | 32 ++++++--------------------------
 1 file changed, 6 insertions(+), 26 deletions(-)

diff --git a/mm/vmstat.c b/mm/vmstat.c
index 7f2e87cb7049..35219271796f 100644
--- a/mm/vmstat.c
+++ b/mm/vmstat.c
@@ -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;
 }
-- 
2.26.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 3/3] kernel: kprobes: Convert to use DEFINE_SEQ_ATTRIBUTE macro
  2020-05-09  6:40 [PATCH 0/3] seq_file: Introduce DEFINE_SEQ_ATTRIBUTE() helper macro Kefeng Wang
  2020-05-09  6:40 ` [PATCH 1/3] " Kefeng Wang
  2020-05-09  6:40 ` [PATCH 2/3] mm: vmstat: Convert to use DEFINE_SEQ_ATTRIBUTE macro Kefeng Wang
@ 2020-05-09  6:40 ` Kefeng Wang
  2 siblings, 0 replies; 4+ messages in thread
From: Kefeng Wang @ 2020-05-09  6:40 UTC (permalink / raw)
  To: Greg KH, linux-kernel
  Cc: Ingo Molnar, Andrew Morton, Kefeng Wang, Anil S Keshavamurthy,
	David S. Miller, Masami Hiramatsu

Use DEFINE_SEQ_ATTRIBUTE macro to simplify the code.

Cc: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
---
 kernel/kprobes.c | 33 ++++++---------------------------
 1 file changed, 6 insertions(+), 27 deletions(-)

diff --git a/kernel/kprobes.c b/kernel/kprobes.c
index ffbe03a45c16..f961f5b13f43 100644
--- a/kernel/kprobes.c
+++ b/kernel/kprobes.c
@@ -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;
 }
-- 
2.26.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-05-09  6:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-09  6:40 [PATCH 0/3] seq_file: Introduce DEFINE_SEQ_ATTRIBUTE() helper macro Kefeng Wang
2020-05-09  6:40 ` [PATCH 1/3] " Kefeng Wang
2020-05-09  6:40 ` [PATCH 2/3] mm: vmstat: Convert to use DEFINE_SEQ_ATTRIBUTE macro Kefeng Wang
2020-05-09  6:40 ` [PATCH 3/3] kernel: kprobes: " Kefeng Wang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.