linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] proc: Avoid costly high-order page allocations when reading proc files
@ 2025-04-01  7:30 Yafang Shao
  2025-04-01 14:01 ` Kees Cook
  0 siblings, 1 reply; 35+ messages in thread
From: Yafang Shao @ 2025-04-01  7:30 UTC (permalink / raw)
  To: kees, joel.granados; +Cc: linux-fsdevel, linux-kernel, Yafang Shao, Josef Bacik

While investigating a kcompactd 100% CPU utilization issue in production, I
observed frequent costly high-order (order-6) page allocations triggered by
proc file reads from monitoring tools. This can be reproduced with a simple
test case:

  fd = open(PROC_FILE, O_RDONLY);
  size = read(fd, buff, 256KB);
  close(fd);

Although we should modify the monitoring tools to use smaller buffer sizes,
we should also enhance the kernel to prevent these expensive high-order
allocations.

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
Cc: Josef Bacik <josef@toxicpanda.com>
---
 fs/proc/proc_sysctl.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
index cc9d74a06ff0..c53ba733bda5 100644
--- a/fs/proc/proc_sysctl.c
+++ b/fs/proc/proc_sysctl.c
@@ -581,7 +581,15 @@ static ssize_t proc_sys_call_handler(struct kiocb *iocb, struct iov_iter *iter,
 	error = -ENOMEM;
 	if (count >= KMALLOC_MAX_SIZE)
 		goto out;
-	kbuf = kvzalloc(count + 1, GFP_KERNEL);
+
+	/*
+	 * Use vmalloc if the count is too large to avoid costly high-order page
+	 * allocations.
+	 */
+	if (count < (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
+		kbuf = kvzalloc(count + 1, GFP_KERNEL);
+	else
+		kbuf = vmalloc(count + 1);
 	if (!kbuf)
 		goto out;
 
-- 
2.43.5


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

end of thread, other threads:[~2025-06-04 18:42 UTC | newest]

Thread overview: 35+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-01  7:30 [PATCH] proc: Avoid costly high-order page allocations when reading proc files Yafang Shao
2025-04-01 14:01 ` Kees Cook
2025-04-01 14:50   ` Yafang Shao
2025-04-02  4:15   ` Harry Yoo
2025-04-02  8:42     ` Yafang Shao
2025-04-02  9:25       ` Vlastimil Babka
2025-04-02 12:17         ` Michal Hocko
2025-04-02 18:25         ` Shakeel Butt
2025-04-02 11:32       ` Dave Chinner
2025-04-02 12:24         ` Michal Hocko
2025-04-02 17:24           ` Matthew Wilcox
2025-04-02 18:30             ` Shakeel Butt
2025-04-02 22:38             ` Dave Chinner
2025-04-02 21:16           ` Dave Chinner
2025-04-02 23:10             ` Shakeel Butt
2025-04-03  1:22               ` Dave Chinner
2025-04-03  3:32                 ` Yafang Shao
2025-04-03  5:05                 ` Shakeel Butt
2025-04-03  7:20                   ` Michal Hocko
2025-04-03  4:37           ` Shakeel Butt
2025-04-03  7:22             ` Michal Hocko
2025-04-03  7:43               ` [PATCH] mm: kvmalloc: make kmalloc fast path real fast path Michal Hocko
2025-04-03  8:24                 ` Vlastimil Babka
2025-04-03  8:59                   ` Michal Hocko
2025-04-03 16:21                 ` Kees Cook
2025-04-03 19:49                   ` Michal Hocko
2025-04-04 15:33                   ` Darrick J. Wong
2025-04-03 18:30                 ` Shakeel Butt
2025-04-03 19:51                 ` Michal Hocko
2025-04-09  1:10                   ` Dave Chinner
2025-06-04 18:42                     ` Matthew Wilcox
2025-04-09  7:35                   ` Michal Hocko
2025-04-09  9:11                     ` Vlastimil Babka
2025-04-09 12:20                       ` Michal Hocko
2025-04-09 12:23                         ` Vlastimil Babka

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