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=-3.9 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=no 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 DAF56CA9EC0 for ; Mon, 28 Oct 2019 12:40:08 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8D2A6208C0 for ; Mon, 28 Oct 2019 12:40:08 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b="QlzDd1Bh" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8D2A6208C0 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=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 32B0A6B0007; Mon, 28 Oct 2019 08:40:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2DBAD6B0008; Mon, 28 Oct 2019 08:40:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1CA2C6B000A; Mon, 28 Oct 2019 08:40:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0144.hostedemail.com [216.40.44.144]) by kanga.kvack.org (Postfix) with ESMTP id E87636B0007 for ; Mon, 28 Oct 2019 08:40:07 -0400 (EDT) Received: from smtpin02.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with SMTP id 3B0513ABE for ; Mon, 28 Oct 2019 12:40:07 +0000 (UTC) X-FDA: 76093150854.02.error73_3b2a89accb62f X-HE-Tag: error73_3b2a89accb62f X-Filterd-Recvd-Size: 4970 Received: from mail-lj1-f195.google.com (mail-lj1-f195.google.com [209.85.208.195]) by imf46.hostedemail.com (Postfix) with ESMTP for ; Mon, 28 Oct 2019 12:40:06 +0000 (UTC) Received: by mail-lj1-f195.google.com with SMTP id y3so11176816ljj.6 for ; Mon, 28 Oct 2019 05:40:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=ATDKJMcIxCSwydf+1lUKxJ7Ftg71NmlGmbozwsqMkmc=; b=QlzDd1Bh3krOIKFjcDBdZoayUKXaWxUvGFEq2duRqI6syPWfllKIuNiRiwFp3Rsvlm nxqwqgYi8hs/NEk8mASxHt8carAtxDUSL6DW6fwUayesh7ZvlNihXqYJHT/hp5a3B/c/ sAj/eEL2RfuIZGHXujz05q+R3v1iYQI3WZw6c= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=ATDKJMcIxCSwydf+1lUKxJ7Ftg71NmlGmbozwsqMkmc=; b=Y3BYbKdKXBiMZwbG2vpKvPUFGULuNA7e/LFqYJp2gkw7IlPYb54UNbrlvBfOqArCUV PemqEPMqdM48NdIFEhEGDIYCWQPbo008ndIPB3uqeJbJrHA6r0lF4QypWQX0tXrVw1DG bL14BRq2NsyiZMMa+oxcXu1wxHV73QBPduCt90SLb5aHuqIOEh/J4MTQ454w5rxcHYO9 crX0M6BBHZLZR5l1W5hF8D6ous3dDTcZiw53zOiMhla7CsjfT/2rARDRtfTQSNqFYVXr NGi3k3QvcwgAhMNHX5ghEgmy1TkoKn2UFSYbfvVxLJ7fHDg+3+phK8YxftTC8t6vIrXW 1ulg== X-Gm-Message-State: APjAAAUfW+i2aUrVFa7DQQsADlBgma04YvFLrHgN4HbwZNlnq1qtS8h+ Z7T5Oa9PHdvis8ySS1Z5KQAOzztsOuM9Ug== X-Google-Smtp-Source: APXvYqxz7n1YUUMX2tU9cjD3gg/2/c0N2YqhFoaSPl8MhshELV4oQqKuEdez7eCwcNQZrYBcLETxwA== X-Received: by 2002:a2e:8505:: with SMTP id j5mr11650470lji.154.1572266404661; Mon, 28 Oct 2019 05:40:04 -0700 (PDT) Received: from mail-lj1-f181.google.com (mail-lj1-f181.google.com. [209.85.208.181]) by smtp.gmail.com with ESMTPSA id g5sm5414506ljk.22.2019.10.28.05.40.02 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 28 Oct 2019 05:40:02 -0700 (PDT) Received: by mail-lj1-f181.google.com with SMTP id 139so10984282ljf.1 for ; Mon, 28 Oct 2019 05:40:02 -0700 (PDT) X-Received: by 2002:a2e:3e18:: with SMTP id l24mr8529100lja.48.1572266402291; Mon, 28 Oct 2019 05:40:02 -0700 (PDT) MIME-Version: 1.0 References: <157225677483.3442.4227193290486305330.stgit@buzz> In-Reply-To: <157225677483.3442.4227193290486305330.stgit@buzz> From: Linus Torvalds Date: Mon, 28 Oct 2019 13:39:46 +0100 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] mm/filemap: do not allocate cache pages beyond end of file at read To: Konstantin Khlebnikov Cc: Linux-MM , Andrew Morton , Linux Kernel Mailing List , linux-fsdevel , Alexander Viro , Johannes Weiner Content-Type: text/plain; charset="UTF-8" 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 Mon, Oct 28, 2019 at 10:59 AM Konstantin Khlebnikov wrote: > > Page cache could contain pages beyond end of file during write or > if read races with truncate. But generic_file_buffered_read() always > allocates unneeded pages beyond eof if somebody reads here and one > extra page at the end if file size is page-aligned. I wonder if we could just do something like this instead: diff --git a/mm/filemap.c b/mm/filemap.c index 85b7d087eb45..80b08433c93a 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2013,7 +2013,7 @@ static ssize_t generic_file_buffered_read( struct address_space *mapping = filp->f_mapping; struct inode *inode = mapping->host; struct file_ra_state *ra = &filp->f_ra; - loff_t *ppos = &iocb->ki_pos; + loff_t *ppos = &iocb->ki_pos, size; pgoff_t index; pgoff_t last_index; pgoff_t prev_index; @@ -2021,9 +2021,10 @@ static ssize_t generic_file_buffered_read( unsigned int prev_offset; int error = 0; - if (unlikely(*ppos >= inode->i_sb->s_maxbytes)) + size = i_size_read(inode); + if (unlikely(*ppos >= size)) return 0; - iov_iter_truncate(iter, inode->i_sb->s_maxbytes); + iov_iter_truncate(iter, size); index = *ppos >> PAGE_SHIFT; prev_index = ra->prev_pos >> PAGE_SHIFT; and yes, we still need to re-check the inode size after we've read the page cache page (since it might have changed during the IO), but the above seems fairly benign and simple. Hmm? Linus