linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Matthew Wilcox <willy@infradead.org>
To: Vlastimil Babka <vbabka@suse.cz>
Cc: Steven Noonan <steven@uplinklabs.net>,
	"linux-mm@kvack.org" <linux-mm@kvack.org>,
	Christoph Hellwig <hch@infradead.org>,
	Josef Bacik <josef@toxicpanda.com>
Subject: Re: "cat /proc/sys/kernel/random/entropy_avail" sometimes causes page allocation failure
Date: Mon, 1 Feb 2021 13:52:06 +0000	[thread overview]
Message-ID: <20210201135206.GQ308988@casper.infradead.org> (raw)
In-Reply-To: <592ccd73-9152-0748-a7ce-be663593f8da@suse.cz>

On Mon, Feb 01, 2021 at 01:20:04PM +0100, Vlastimil Babka wrote:
> On 2/1/21 12:26 PM, Steven Noonan wrote:
> > (Please CC me on replies, I'm not subscribed to the list.)
> > 
> > I started seeing this problem several months ago, but after some Google searches I concluded that someone had already root caused and fixed it, and I just needed to wait for a newer kernel to come along:
> > 
> > https://www.spinics.net/lists/linux-mm/msg226311.html
> 
> Yeah, that thread mentions Josef's series [1], but I don't see that it made it
> into git log. What happened to it? Meanwhile Matthew refactored it with
> "4bd6a7353ee1 ("sysctl: Convert to iter interfaces")" which left the kzalloc()
> but changed count to count+1, which would explain the change from order-5 to
> order-6. In my quick test, strace cat tells me it uses 128k sized read, which is
> order-5.
> 
> So miminally there should be kvzalloc(), but it's still somewhat unfortunate to
> do that for reading a few bytes.
> Also the check for KMALLOC_MAX_SIZE happens before the +1. Meh. Matthew?

That's why the check is >= KMALLOC_MAX_SIZE.

Switching to kvzalloc() means we'll only allocate one extra page, not twice
as many pages.  We can't know how large the proc file is going to want the
buffer to be, but perhaps we can do this ...

@@ -569,17 +569,16 @@ static ssize_t proc_sys_call_handler(struct kiocb *iocb, struct iov_iter *iter,
 
        /* don't even try if the size is too large */
        error = -ENOMEM;
-       if (count >= KMALLOC_MAX_SIZE)
+       if (count > KMALLOC_MAX_SIZE)
                goto out;
-       kbuf = kzalloc(count + 1, GFP_KERNEL);
+       kbuf = kvzalloc(count, GFP_KERNEL);
        if (!kbuf)
                goto out;
 
        if (write) {
                error = -EFAULT;
-               if (!copy_from_iter_full(kbuf, count, iter))
+               if (!copy_from_iter_full(kbuf, count - 1, iter))
                        goto out_free_buf;
-               kbuf[count] = '\0';
        }
 
        error = BPF_CGROUP_RUN_PROG_SYSCTL(head, table, write, &kbuf, &count,




  reply	other threads:[~2021-02-01 13:52 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-01 11:26 "cat /proc/sys/kernel/random/entropy_avail" sometimes causes page allocation failure Steven Noonan
2021-02-01 12:20 ` Vlastimil Babka
2021-02-01 13:52   ` Matthew Wilcox [this message]
2021-02-09 11:17     ` Vlastimil Babka

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=20210201135206.GQ308988@casper.infradead.org \
    --to=willy@infradead.org \
    --cc=hch@infradead.org \
    --cc=josef@toxicpanda.com \
    --cc=linux-mm@kvack.org \
    --cc=steven@uplinklabs.net \
    --cc=vbabka@suse.cz \
    /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 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).