From patchwork Wed Oct 20 10:10:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Howells X-Patchwork-Id: 12571975 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 3292FC433EF for ; Wed, 20 Oct 2021 10:10:38 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id AFEFA611EF for ; Wed, 20 Oct 2021 10:10:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org AFEFA611EF Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 31B47900002; Wed, 20 Oct 2021 06:10:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2CB586B0072; Wed, 20 Oct 2021 06:10:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1B9E5900002; Wed, 20 Oct 2021 06:10:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0158.hostedemail.com [216.40.44.158]) by kanga.kvack.org (Postfix) with ESMTP id 0C09A6B0071 for ; Wed, 20 Oct 2021 06:10:37 -0400 (EDT) Received: from smtpin07.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id BE3898249980 for ; Wed, 20 Oct 2021 10:10:36 +0000 (UTC) X-FDA: 78716396472.07.D086E6F Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf25.hostedemail.com (Postfix) with ESMTP id 761F1B000180 for ; Wed, 20 Oct 2021 10:10:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1634724635; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=kN+rmXjeIN5RoOoqhjSuUAR3s+V7flhqvRXBT3LvpfY=; b=Y5T/HKkfyvSzoAVDSlA7rfe1UeMi5ZPUeORyDGS9tPswmourh+N2VGddfn8dtiYB37vAU6 2VoBzwZjyngRm98wX38r+cFx36RxQFZmPli2SoAtt7tFIa7Cfbsgr3C8SXu15xYjFhoAXr kgnFWNbGZtNOXwgrUpsDOm3Jn270VBU= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-285-Q6BXm1oFMY-Na_Om9Svjow-1; Wed, 20 Oct 2021 06:10:34 -0400 X-MC-Unique: Q6BXm1oFMY-Na_Om9Svjow-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 71DC710A8E02; Wed, 20 Oct 2021 10:10:33 +0000 (UTC) Received: from warthog.procyon.org.uk (unknown [10.33.36.19]) by smtp.corp.redhat.com (Postfix) with ESMTP id E5A2BADD9; Wed, 20 Oct 2021 10:10:31 +0000 (UTC) Organization: Red Hat UK Ltd. Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 3798903 Subject: [PATCH v2] mm: Stop filemap_read() from grabbing a superfluous page From: David Howells To: kent.overstreet@gmail.com, willy@infradead.org Cc: Andrew Morton , Jeff Layton , linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, dhowells@redhat.com, linux-kernel@vger.kernel.org Date: Wed, 20 Oct 2021 11:10:31 +0100 Message-ID: <163472463105.3126792.7056099385135786492.stgit@warthog.procyon.org.uk> User-Agent: StGit/0.23 MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 761F1B000180 X-Stat-Signature: ar5w1qfr6hwnxqe47srbb66189ohb4f8 Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="Y5T/HKkf"; spf=none (imf25.hostedemail.com: domain of dhowells@redhat.com has no SPF policy when checking 216.205.24.124) smtp.mailfrom=dhowells@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-HE-Tag: 1634724632-44877 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: 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. Changes: v2) Break out of the loop immediately rather than going to put_pages (the pvec is unoccupied). Setting isize is then unnecessary. Signed-off-by: David Howells cc: Kent Overstreet cc: Matthew Wilcox (Oracle) cc: Andrew Morton cc: Jeff Layton cc: linux-mm@kvack.org cc: linux-fsdevel@vger.kernel.org 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/ Acked-by: Jeff Layton Reviewed-by: Matthew Wilcox (Oracle) --- mm/filemap.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mm/filemap.c b/mm/filemap.c index dae481293b5d..e50be519f6a4 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2625,6 +2625,9 @@ ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter, 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;