From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.5 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,NICE_REPLY_A, SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D9CADC433E0 for ; Tue, 9 Feb 2021 11:17:04 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 2681F64E6B for ; Tue, 9 Feb 2021 11:17:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2681F64E6B Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 813976B0071; Tue, 9 Feb 2021 06:17:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7C36F6B0072; Tue, 9 Feb 2021 06:17:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7010F6B0073; Tue, 9 Feb 2021 06:17:03 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0215.hostedemail.com [216.40.44.215]) by kanga.kvack.org (Postfix) with ESMTP id 5B52F6B0071 for ; Tue, 9 Feb 2021 06:17:03 -0500 (EST) Received: from smtpin17.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 2211EA2BD for ; Tue, 9 Feb 2021 11:17:03 +0000 (UTC) X-FDA: 77798477526.17.slope10_50017f927606 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin17.hostedemail.com (Postfix) with ESMTP id 09C77181F2708 for ; Tue, 9 Feb 2021 11:17:03 +0000 (UTC) X-HE-Tag: slope10_50017f927606 X-Filterd-Recvd-Size: 3667 Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by imf22.hostedemail.com (Postfix) with ESMTP for ; Tue, 9 Feb 2021 11:17:02 +0000 (UTC) X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 1589BAD6A; Tue, 9 Feb 2021 11:17:01 +0000 (UTC) Subject: Re: "cat /proc/sys/kernel/random/entropy_avail" sometimes causes page allocation failure To: Matthew Wilcox Cc: Steven Noonan , "linux-mm@kvack.org" , Christoph Hellwig , Josef Bacik References: <592ccd73-9152-0748-a7ce-be663593f8da@suse.cz> <20210201135206.GQ308988@casper.infradead.org> From: Vlastimil Babka Message-ID: <3e4e6543-802c-fc1e-db53-d085163d41d0@suse.cz> Date: Tue, 9 Feb 2021 12:17:00 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.0 MIME-Version: 1.0 In-Reply-To: <20210201135206.GQ308988@casper.infradead.org> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On 2/1/21 2:52 PM, Matthew Wilcox wrote: > 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 ... Looks ok to me, but I'm not that familiar with iov_iter stuff. Care to send a proper patch? > @@ -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, > >