LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Joe Perches <joe@perches.com>
To: Thomas Gleixner <tglx@linutronix.de>
Cc: Craig Gallek <kraig@google.com>, LKML <linux-kernel@vger.kernel.org>
Subject: [PATCH] genirq/irqdesc: Use sysfs_emit in the <foo>_show functions
Date: Tue, 10 Aug 2021 10:55:37 -0700	[thread overview]
Message-ID: <5a7cf87075177ab374c55e15f677eac167ac767a.camel@perches.com> (raw)

Convert the nominally unbounded sprintf output to use sysfs_emit and the
scnprintf uses to sysfs_emit_at.

Miscellanea:

o sysfs_emit: Use the more common int len not ssize_t ret
o hwirq_show: Add a minimum newline output when irq_data.domain is not set
  and remove an unnecessary cast of an unsigned long to int ("%d" -> "%lu)
o name_show: Add a minimum newline output when desc->name is not set

trivially reduces object size (x86-64 defconfig, gcc 10.3)

$ size kernel/irq/irqdesc.o*
   text	   data	    bss	    dec	    hex	filename
   5809	    576	   1608	   7993	   1f39	kernel/irq/irqdesc.o.new
   5896	    576	   1608	   8080	   1f90	kernel/irq/irqdesc.o.old

Signed-off-by: Joe Perches <joe@perches.com>
---
 kernel/irq/irqdesc.c | 65 ++++++++++++++++++++++++++--------------------------
 1 file changed, 32 insertions(+), 33 deletions(-)

diff --git a/kernel/irq/irqdesc.c b/kernel/irq/irqdesc.c
index fadb937660202..8d47b8667c989 100644
--- a/kernel/irq/irqdesc.c
+++ b/kernel/irq/irqdesc.c
@@ -147,19 +147,20 @@ static ssize_t per_cpu_count_show(struct kobject *kobj,
 				  struct kobj_attribute *attr, char *buf)
 {
 	struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
-	ssize_t ret = 0;
-	char *p = "";
+	int len = 0;
+	const char *p = "";
 	int cpu;
 
 	for_each_possible_cpu(cpu) {
 		unsigned int c = irq_desc_kstat_cpu(desc, cpu);
 
-		ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%u", p, c);
+		len += sysfs_emit_at(buf, len, "%s%u", p, c);
 		p = ",";
 	}
 
-	ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
-	return ret;
+	len += sysfs_emit_at(buf, len, "\n");
+
+	return len;
 }
 IRQ_ATTR_RO(per_cpu_count);
 
@@ -167,16 +168,15 @@ static ssize_t chip_name_show(struct kobject *kobj,
 			      struct kobj_attribute *attr, char *buf)
 {
 	struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
-	ssize_t ret = 0;
+	int len = 0;
 
 	raw_spin_lock_irq(&desc->lock);
-	if (desc->irq_data.chip && desc->irq_data.chip->name) {
-		ret = scnprintf(buf, PAGE_SIZE, "%s\n",
-				desc->irq_data.chip->name);
-	}
+	len = sysfs_emit(buf, "%s\n",
+			 desc->irq_data.chip && desc->irq_data.chip->name ?
+			 desc->irq_data.chip->name : "");
 	raw_spin_unlock_irq(&desc->lock);
 
-	return ret;
+	return len;
 }
 IRQ_ATTR_RO(chip_name);
 
@@ -184,14 +184,16 @@ static ssize_t hwirq_show(struct kobject *kobj,
 			  struct kobj_attribute *attr, char *buf)
 {
 	struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
-	ssize_t ret = 0;
+	int len = 0;
 
 	raw_spin_lock_irq(&desc->lock);
 	if (desc->irq_data.domain)
-		ret = sprintf(buf, "%d\n", (int)desc->irq_data.hwirq);
+		len = sysfs_emit(buf, "%lu\n", desc->irq_data.hwirq);
+	else
+		len = sysfs_emit(buf, "\n");
 	raw_spin_unlock_irq(&desc->lock);
 
-	return ret;
+	return len;
 }
 IRQ_ATTR_RO(hwirq);
 
@@ -199,14 +201,14 @@ static ssize_t type_show(struct kobject *kobj,
 			 struct kobj_attribute *attr, char *buf)
 {
 	struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
-	ssize_t ret = 0;
+	int len = 0;
 
 	raw_spin_lock_irq(&desc->lock);
-	ret = sprintf(buf, "%s\n",
-		      irqd_is_level_type(&desc->irq_data) ? "level" : "edge");
+	len = sysfs_emit(buf, "%s\n",
+			 irqd_is_level_type(&desc->irq_data) ? "level" : "edge");
 	raw_spin_unlock_irq(&desc->lock);
 
-	return ret;
+	return len;
 
 }
 IRQ_ATTR_RO(type);
@@ -215,14 +217,14 @@ static ssize_t wakeup_show(struct kobject *kobj,
 			   struct kobj_attribute *attr, char *buf)
 {
 	struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
-	ssize_t ret = 0;
+	int len = 0;
 
 	raw_spin_lock_irq(&desc->lock);
-	ret = sprintf(buf, "%s\n",
-		      irqd_is_wakeup_set(&desc->irq_data) ? "enabled" : "disabled");
+	len = sysfs_emit(buf, "%s\n",
+			 irqd_is_wakeup_set(&desc->irq_data) ? "enabled" : "disabled");
 	raw_spin_unlock_irq(&desc->lock);
 
-	return ret;
+	return len;
 
 }
 IRQ_ATTR_RO(wakeup);
@@ -231,14 +233,13 @@ static ssize_t name_show(struct kobject *kobj,
 			 struct kobj_attribute *attr, char *buf)
 {
 	struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
-	ssize_t ret = 0;
+	int len = 0;
 
 	raw_spin_lock_irq(&desc->lock);
-	if (desc->name)
-		ret = scnprintf(buf, PAGE_SIZE, "%s\n", desc->name);
+	len = sysfs_emit(buf, "%s\n", desc->name ?: "");
 	raw_spin_unlock_irq(&desc->lock);
 
-	return ret;
+	return len;
 }
 IRQ_ATTR_RO(name);
 
@@ -247,21 +248,19 @@ static ssize_t actions_show(struct kobject *kobj,
 {
 	struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
 	struct irqaction *action;
-	ssize_t ret = 0;
-	char *p = "";
+	int len = 0;
+	const char *p = "";
 
 	raw_spin_lock_irq(&desc->lock);
 	for (action = desc->action; action != NULL; action = action->next) {
-		ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
-				 p, action->name);
+		len += sysfs_emit_at(buf, len, "%s%s", p, action->name);
 		p = ",";
 	}
 	raw_spin_unlock_irq(&desc->lock);
 
-	if (ret)
-		ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
+	len += sysfs_emit_at(buf, len, "\n");
 
-	return ret;
+	return len;
 }
 IRQ_ATTR_RO(actions);
 


             reply	other threads:[~2021-08-10 17:59 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-10 17:55 Joe Perches [this message]
2021-08-10 21:46 ` Thomas Gleixner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5a7cf87075177ab374c55e15f677eac167ac767a.camel@perches.com \
    --to=joe@perches.com \
    --cc=kraig@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tglx@linutronix.de \
    --subject='Re: [PATCH] genirq/irqdesc: Use sysfs_emit in the <foo>_show functions' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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).