All of lore.kernel.org
 help / color / mirror / Atom feed
From: Joe Perches <joe@perches.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	"Rafael J . Wysocki" <rafael@kernel.org>,
	Mike Kravetz <mike.kravetz@oracle.com>,
	Andrew Morton <akpm@linux-foundation.org>
Cc: Denis Efremov <efremov@linux.com>,
	Julia Lawall <julia.lawall@inria.fr>,
	Alex Dewar <alex.dewar90@gmail.com>,
	linux-kernel@vger.kernel.org, linux-mm@kvack.org
Subject: [PATCH V3 6/8] mm: and drivers core: Convert hugetlb_report_node_meminfo to sysfs_emit
Date: Wed, 16 Sep 2020 13:40:43 -0700	[thread overview]
Message-ID: <894b351b82da6013cde7f36ff4b5493cd0ec30d0.1600285923.git.joe@perches.com> (raw)
In-Reply-To: <cover.1600285923.git.joe@perches.com>

Convert the unbound sprintf in hugetlb_report_node_meminfo to use
sysfs_emit_at so that no possible overrun of a PAGE_SIZE buf can occur.

Signed-off-by: Joe Perches <joe@perches.com>
---
 drivers/base/node.c     |  2 +-
 include/linux/hugetlb.h |  4 ++--
 mm/hugetlb.c            | 18 ++++++++++--------
 3 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/drivers/base/node.c b/drivers/base/node.c
index b33526a9fcfc..dafe03e82e7c 100644
--- a/drivers/base/node.c
+++ b/drivers/base/node.c
@@ -473,7 +473,7 @@ static ssize_t node_read_meminfo(struct device *dev,
 				    HPAGE_PMD_NR)
 #endif
 			    );
-	len += hugetlb_report_node_meminfo(nid, buf + len);
+	len += hugetlb_report_node_meminfo(buf, len, nid);
 	return len;
 }
 
diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
index d5cc5f802dd4..ebca2ef02212 100644
--- a/include/linux/hugetlb.h
+++ b/include/linux/hugetlb.h
@@ -129,7 +129,7 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
 				unsigned long start, unsigned long end,
 				struct page *ref_page);
 void hugetlb_report_meminfo(struct seq_file *);
-int hugetlb_report_node_meminfo(int, char *);
+int hugetlb_report_node_meminfo(char *buf, int len, int nid);
 void hugetlb_show_meminfo(void);
 unsigned long hugetlb_total_pages(void);
 vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
@@ -245,7 +245,7 @@ static inline void hugetlb_report_meminfo(struct seq_file *m)
 {
 }
 
-static inline int hugetlb_report_node_meminfo(int nid, char *buf)
+static inline int hugetlb_report_node_meminfo(char *buf, int len, int nid)
 {
 	return 0;
 }
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 61469fd3ad92..fe76f8fd5a73 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -3571,18 +3571,20 @@ void hugetlb_report_meminfo(struct seq_file *m)
 	seq_printf(m, "Hugetlb:        %8lu kB\n", total / 1024);
 }
 
-int hugetlb_report_node_meminfo(int nid, char *buf)
+int hugetlb_report_node_meminfo(char *buf, int len, int nid)
 {
 	struct hstate *h = &default_hstate;
+
 	if (!hugepages_supported())
 		return 0;
-	return sprintf(buf,
-		"Node %d HugePages_Total: %5u\n"
-		"Node %d HugePages_Free:  %5u\n"
-		"Node %d HugePages_Surp:  %5u\n",
-		nid, h->nr_huge_pages_node[nid],
-		nid, h->free_huge_pages_node[nid],
-		nid, h->surplus_huge_pages_node[nid]);
+
+	return sysfs_emit_at(buf, len,
+			     "Node %d HugePages_Total: %5u\n"
+			     "Node %d HugePages_Free:  %5u\n"
+			     "Node %d HugePages_Surp:  %5u\n",
+			     nid, h->nr_huge_pages_node[nid],
+			     nid, h->free_huge_pages_node[nid],
+			     nid, h->surplus_huge_pages_node[nid]);
 }
 
 void hugetlb_show_meminfo(void)
-- 
2.26.0


  parent reply	other threads:[~2020-09-16 20:41 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-16 20:40 [PATCH V3 0/8] sysfs: drivers core: Add and use sysfs_emit and sysfs_emit_at Joe Perches
2020-09-16 20:40 ` [PATCH V3 1/8] sysfs: Add sysfs_emit and sysfs_emit_at to format sysfs output Joe Perches
2020-09-30 11:57   ` Greg Kroah-Hartman
2020-09-30 13:58     ` Joe Perches
2020-10-02 11:25       ` Greg Kroah-Hartman
2020-10-01  4:17     ` Kees Cook
2020-10-01  4:22       ` Joe Perches
2020-10-02 22:30         ` Kees Cook
2020-10-03 13:55           ` Greg Kroah-Hartman
2020-10-03 14:59             ` Joe Perches
2020-10-03 22:15             ` Kees Cook
2020-10-01 20:50       ` Greg Kroah-Hartman
2020-10-02 22:27         ` Kees Cook
2020-09-16 20:40 ` [PATCH V3 2/8] drivers core: Use sysfs_emit and sysfs_emit_at for show(device *...) functions Joe Perches
2020-09-16 20:40 ` [PATCH V3 3/8] drivers core: Remove strcat uses around sysfs_emit and neaten Joe Perches
2020-09-16 20:40 ` [PATCH V3 4/8] drivers core: Reindent a couple uses around sysfs_emit Joe Perches
2020-09-16 20:40 ` [PATCH V3 5/8] drivers core: Miscellaneous changes for sysfs_emit Joe Perches
2020-09-16 20:40 ` Joe Perches [this message]
2020-09-18 20:29   ` [PATCH V3 6/8] mm: and drivers core: Convert hugetlb_report_node_meminfo to sysfs_emit Mike Kravetz
2020-09-19  6:22   ` Greg Kroah-Hartman
2020-09-19 16:51     ` Joe Perches
2020-09-25 18:32     ` Joe Perches
2020-09-29  0:53     ` Joe Perches
2020-09-29  4:17       ` Greg Kroah-Hartman
2020-09-16 20:40 ` [PATCH V3 7/8] drivers core: Use sysfs_emit for shared_cpu_map_show and shared_cpu_list_show Joe Perches
2020-09-16 20:40 ` [PATCH V3 8/8] drivers core: node: Use a more typical macro definition style for ACCESS_ATTR Joe Perches

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=894b351b82da6013cde7f36ff4b5493cd0ec30d0.1600285923.git.joe@perches.com \
    --to=joe@perches.com \
    --cc=akpm@linux-foundation.org \
    --cc=alex.dewar90@gmail.com \
    --cc=efremov@linux.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=julia.lawall@inria.fr \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mike.kravetz@oracle.com \
    --cc=rafael@kernel.org \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.