linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] char: misc:use DEFINE_PROC_SHOW_ATTRIBUTE micro to simplify misc proc_fops
@ 2022-03-27 14:32 wujunwen
  2022-03-28  5:51 ` Greg KH
  0 siblings, 1 reply; 6+ messages in thread
From: wujunwen @ 2022-03-27 14:32 UTC (permalink / raw)
  To: arnd, gregkh; +Cc: linux-kernel, wujunwen

DEFINE_PROC_SHOW_ATTRIBUTE is used to simply seq_file flow ,so
we can use it to simplify misc proc_fops.

Signed-off-by: wujunwen <wudaemon@163.com>
---
 drivers/char/misc.c | 41 ++++++++++++++---------------------------
 1 file changed, 14 insertions(+), 27 deletions(-)

diff --git a/drivers/char/misc.c b/drivers/char/misc.c
index ca5141ed5ef3..076b7f08aa7a 100644
--- a/drivers/char/misc.c
+++ b/drivers/char/misc.c
@@ -64,40 +64,23 @@ static DEFINE_MUTEX(misc_mtx);
 static DECLARE_BITMAP(misc_minors, DYNAMIC_MINORS);
 
 #ifdef CONFIG_PROC_FS
-static void *misc_seq_start(struct seq_file *seq, loff_t *pos)
-{
-	mutex_lock(&misc_mtx);
-	return seq_list_start(&misc_list, *pos);
-}
 
-static void *misc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
+static int misc_show(struct seq_file *seq, void *v)
 {
-	return seq_list_next(v, &misc_list, pos);
-}
+	const struct miscdevice *p;
 
-static void misc_seq_stop(struct seq_file *seq, void *v)
-{
+	mutex_lock(&misc_mtx);
+	list_for_each_entry(p, &misc_list, list) {
+		seq_printf(seq, "%3i %s\n", p->minor, p->name ? p->name : "");
+	}
 	mutex_unlock(&misc_mtx);
-}
-
-static int misc_seq_show(struct seq_file *seq, void *v)
-{
-	const struct miscdevice *p = list_entry(v, struct miscdevice, list);
-
-	seq_printf(seq, "%3i %s\n", p->minor, p->name ? p->name : "");
 	return 0;
 }
 
-
-static const struct seq_operations misc_seq_ops = {
-	.start = misc_seq_start,
-	.next  = misc_seq_next,
-	.stop  = misc_seq_stop,
-	.show  = misc_seq_show,
-};
+DEFINE_PROC_SHOW_ATTRIBUTE(misc);
 #endif
 
-static int misc_open(struct inode *inode, struct file *file)
+static int misc_fops_open(struct inode *inode, struct file *file)
 {
 	int minor = iminor(inode);
 	struct miscdevice *c;
@@ -148,7 +131,7 @@ static struct class *misc_class;
 
 static const struct file_operations misc_fops = {
 	.owner		= THIS_MODULE,
-	.open		= misc_open,
+	.open		= misc_fops_open,
 	.llseek		= noop_llseek,
 };
 
@@ -266,9 +249,11 @@ static char *misc_devnode(struct device *dev, umode_t *mode)
 static int __init misc_init(void)
 {
 	int err;
+#ifdef CONFIG_PROC_FS
 	struct proc_dir_entry *ret;
 
-	ret = proc_create_seq("misc", 0, NULL, &misc_seq_ops);
+	ret = proc_create("misc", 0, NULL, &misc_proc_ops);
+#endif
 	misc_class = class_create(THIS_MODULE, "misc");
 	err = PTR_ERR(misc_class);
 	if (IS_ERR(misc_class))
@@ -284,8 +269,10 @@ static int __init misc_init(void)
 	pr_err("unable to get major %d for misc devices\n", MISC_MAJOR);
 	class_destroy(misc_class);
 fail_remove:
+#ifdef CONFIG_PROC_FS
 	if (ret)
 		remove_proc_entry("misc", NULL);
+#endif
 	return err;
 }
 subsys_initcall(misc_init);
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread
* [PATCH v2] char: misc:use DEFINE_PROC_SHOW_ATTRIBUTE micro to simplify misc proc_fops
@ 2022-03-31 15:15 wujunwen
  2022-04-24 15:35 ` Greg KH
  0 siblings, 1 reply; 6+ messages in thread
From: wujunwen @ 2022-03-31 15:15 UTC (permalink / raw)
  To: arnd, gregkh; +Cc: linux-kernel, wujunwen

DEFINE_PROC_SHOW_ATTRIBUTE is used to simply seq_file flow ,so
we can use it to simplify misc proc_fops.

Signed-off-by: wujunwen <wudaemon@163.com>
---
V1 -> V2: Fix compile error with no "CONFIG_PROC_FS"

 drivers/char/misc.c | 41 ++++++++++++++---------------------------
 1 file changed, 14 insertions(+), 27 deletions(-)

diff --git a/drivers/char/misc.c b/drivers/char/misc.c
index ca5141ed5ef3..076b7f08aa7a 100644
--- a/drivers/char/misc.c
+++ b/drivers/char/misc.c
@@ -64,40 +64,23 @@ static DEFINE_MUTEX(misc_mtx);
 static DECLARE_BITMAP(misc_minors, DYNAMIC_MINORS);
 
 #ifdef CONFIG_PROC_FS
-static void *misc_seq_start(struct seq_file *seq, loff_t *pos)
-{
-	mutex_lock(&misc_mtx);
-	return seq_list_start(&misc_list, *pos);
-}
 
-static void *misc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
+static int misc_show(struct seq_file *seq, void *v)
 {
-	return seq_list_next(v, &misc_list, pos);
-}
+	const struct miscdevice *p;
 
-static void misc_seq_stop(struct seq_file *seq, void *v)
-{
+	mutex_lock(&misc_mtx);
+	list_for_each_entry(p, &misc_list, list) {
+		seq_printf(seq, "%3i %s\n", p->minor, p->name ? p->name : "");
+	}
 	mutex_unlock(&misc_mtx);
-}
-
-static int misc_seq_show(struct seq_file *seq, void *v)
-{
-	const struct miscdevice *p = list_entry(v, struct miscdevice, list);
-
-	seq_printf(seq, "%3i %s\n", p->minor, p->name ? p->name : "");
 	return 0;
 }
 
-
-static const struct seq_operations misc_seq_ops = {
-	.start = misc_seq_start,
-	.next  = misc_seq_next,
-	.stop  = misc_seq_stop,
-	.show  = misc_seq_show,
-};
+DEFINE_PROC_SHOW_ATTRIBUTE(misc);
 #endif
 
-static int misc_open(struct inode *inode, struct file *file)
+static int misc_fops_open(struct inode *inode, struct file *file)
 {
 	int minor = iminor(inode);
 	struct miscdevice *c;
@@ -148,7 +131,7 @@ static struct class *misc_class;
 
 static const struct file_operations misc_fops = {
 	.owner		= THIS_MODULE,
-	.open		= misc_open,
+	.open		= misc_fops_open,
 	.llseek		= noop_llseek,
 };
 
@@ -266,9 +249,11 @@ static char *misc_devnode(struct device *dev, umode_t *mode)
 static int __init misc_init(void)
 {
 	int err;
+#ifdef CONFIG_PROC_FS
 	struct proc_dir_entry *ret;
 
-	ret = proc_create_seq("misc", 0, NULL, &misc_seq_ops);
+	ret = proc_create("misc", 0, NULL, &misc_proc_ops);
+#endif
 	misc_class = class_create(THIS_MODULE, "misc");
 	err = PTR_ERR(misc_class);
 	if (IS_ERR(misc_class))
@@ -284,8 +269,10 @@ static int __init misc_init(void)
 	pr_err("unable to get major %d for misc devices\n", MISC_MAJOR);
 	class_destroy(misc_class);
 fail_remove:
+#ifdef CONFIG_PROC_FS
 	if (ret)
 		remove_proc_entry("misc", NULL);
+#endif
 	return err;
 }
 subsys_initcall(misc_init);
-- 
2.25.1


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

end of thread, other threads:[~2022-04-24 15:38 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-27 14:32 [PATCH v2] char: misc:use DEFINE_PROC_SHOW_ATTRIBUTE micro to simplify misc proc_fops wujunwen
2022-03-28  5:51 ` Greg KH
     [not found]   ` <4ac4969e.8288.17fdb6206ea.Coremail.wudaemon@163.com>
2022-03-30 15:37     ` Greg KH
     [not found]       ` <51686fcb.83c6.17fdb7fd327.Coremail.wudaemon@163.com>
2022-03-30 16:31         ` Greg KH
2022-03-31 15:15 wujunwen
2022-04-24 15:35 ` Greg KH

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).