From patchwork Fri Nov 5 20:36:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Morton X-Patchwork-Id: 12605445 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0EE91C433EF for ; Fri, 5 Nov 2021 20:36:52 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B5A0D611EE for ; Fri, 5 Nov 2021 20:36:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org B5A0D611EE Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=linux-foundation.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 59B98940028; Fri, 5 Nov 2021 16:36:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 52299940007; Fri, 5 Nov 2021 16:36:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3C338940028; Fri, 5 Nov 2021 16:36:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0032.hostedemail.com [216.40.44.32]) by kanga.kvack.org (Postfix) with ESMTP id 2E021940007 for ; Fri, 5 Nov 2021 16:36:51 -0400 (EDT) Received: from smtpin29.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id DCBDF75807 for ; Fri, 5 Nov 2021 20:36:50 +0000 (UTC) X-FDA: 78776035380.29.DDFFB92 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf25.hostedemail.com (Postfix) with ESMTP id F2C92B000188 for ; Fri, 5 Nov 2021 20:36:40 +0000 (UTC) Received: by mail.kernel.org (Postfix) with ESMTPSA id 99ECB6127A; Fri, 5 Nov 2021 20:36:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1636144609; bh=uhBELLkR39FKXoBKx2krq9Lq6sztERzbvXjBlZH47K0=; h=Date:From:To:Subject:In-Reply-To:From; b=gY+GAuJAyVtKLlQlVJNdAF49s9cSxTpohchxSe3IT5jw+eLTzWc1InXVTMIj2UDGH kxL2sWLXtkCyixW4USENl/ykcBobeLuS2fU6nMEGWUkxf/4q3EhnEKsnWb4VN27VPo h1CuxsiNzXFi9vQYXBNY6zcTpyjOkZrSy8SRprbk= Date: Fri, 05 Nov 2021 13:36:49 -0700 From: Andrew Morton To: akpm@linux-foundation.org, dhowells@redhat.com, jlayton@kernel.org, kent.overstreet@gmail.com, linux-mm@kvack.org, mm-commits@vger.kernel.org, torvalds@linux-foundation.org, willy@infradead.org Subject: [patch 041/262] mm: stop filemap_read() from grabbing a superfluous page Message-ID: <20211105203649.fsgWgNvjd%akpm@linux-foundation.org> In-Reply-To: <20211105133408.cccbb98b71a77d5e8430aba1@linux-foundation.org> User-Agent: s-nail v14.8.16 Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=gY+GAuJA; dmarc=none; spf=pass (imf25.hostedemail.com: domain of akpm@linux-foundation.org designates 198.145.29.99 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: F2C92B000188 X-Stat-Signature: tadd4zgx8cnkwiaawxryhq3jas8ut9w6 X-HE-Tag: 1636144600-812850 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: From: David Howells Subject: mm: stop filemap_read() from grabbing a superfluous page Under some circumstances, filemap_read() will allocate sufficient pages to read to the end of the file, call readahead/readpages on them and copy the data over - and then it will allocate another page at the EOF and call readpage on that and then ignore it. This is unnecessary and a waste of time and resources. filemap_read() *does* check for this, but only after it has already done the allocation and I/O. Fix this by checking before calling filemap_get_pages() also. Link: https://lkml.kernel.org/r/163472463105.3126792.7056099385135786492.stgit@warthog.procyon.org.uk Link: https://lore.kernel.org/r/160588481358.3465195.16552616179674485179.stgit@warthog.procyon.org.uk/ Link: https://lore.kernel.org/r/163456863216.2614702.6384850026368833133.stgit@warthog.procyon.org.uk/ Signed-off-by: David Howells Acked-by: Jeff Layton Reviewed-by: Matthew Wilcox (Oracle) Cc: Kent Overstreet Signed-off-by: Andrew Morton --- mm/filemap.c | 3 +++ 1 file changed, 3 insertions(+) --- a/mm/filemap.c~mm-stop-filemap_read-from-grabbing-a-superfluous-page +++ a/mm/filemap.c @@ -2625,6 +2625,9 @@ ssize_t filemap_read(struct kiocb *iocb, if ((iocb->ki_flags & IOCB_WAITQ) && already_read) iocb->ki_flags |= IOCB_NOWAIT; + if (unlikely(iocb->ki_pos >= i_size_read(inode))) + break; + error = filemap_get_pages(iocb, iter, &pvec); if (error < 0) break;