From patchwork Tue Oct 26 18:15:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 12585465 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 B1C76C433EF for ; Tue, 26 Oct 2021 18:15:09 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5AC9F6103C for ; Tue, 26 Oct 2021 18:15:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 5AC9F6103C Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=kernel.dk Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id F2F20940009; Tue, 26 Oct 2021 14:15:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EDDC6940007; Tue, 26 Oct 2021 14:15:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DCBFF940009; Tue, 26 Oct 2021 14:15:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0125.hostedemail.com [216.40.44.125]) by kanga.kvack.org (Postfix) with ESMTP id CD5AA940007 for ; Tue, 26 Oct 2021 14:15:08 -0400 (EDT) Received: from smtpin33.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 8073F1DE9D for ; Tue, 26 Oct 2021 18:15:08 +0000 (UTC) X-FDA: 78739390296.33.BFA7BE8 Received: from mail-io1-f49.google.com (mail-io1-f49.google.com [209.85.166.49]) by imf07.hostedemail.com (Postfix) with ESMTP id 1D9BE10004C7 for ; Tue, 26 Oct 2021 18:15:08 +0000 (UTC) Received: by mail-io1-f49.google.com with SMTP id v65so463651ioe.5 for ; Tue, 26 Oct 2021 11:15:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20210112.gappssmtp.com; s=20210112; h=to:from:subject:message-id:date:user-agent:mime-version :content-language:content-transfer-encoding; bh=NsCoSRUU4kSoqJKBkCULN+BKiJAeIiTrQ7Zf4BRmkLU=; b=D1RR9QJEaDD2zrBai/+61pncjmtnDAOJhYZoisg4LSpPLL0Y6w+UajRDewdGE5aJpu nOXQneSOZeV8B8tn+OaASBzeUKn81JrjRDrwoaveuduIm9UNMWT/xbf7+0WYmSctA9YB RoSrKR3dLnhrfQSHI4sPB7E6NCvEx8MBQMmwyq32g0IV6yACSGHpPIZLicC3ZrG0Lb9D uYs+0Gry8RLx6eUZqMODvh5l0odGju1SQnJEYC2uompo8rWmjOzi4bUkcNqimKIsI2nZ T2WXZx8wQ2dw442SGmyJj1ZkkYJ5g9Tc/hswiPJ0jxi9dy9WhBQg7dAD+B//mrhkTbDB CNiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-language:content-transfer-encoding; bh=NsCoSRUU4kSoqJKBkCULN+BKiJAeIiTrQ7Zf4BRmkLU=; b=uyMwxS/QhE+7vcY3lDybOOoBjsmMQWBppAFX/Xj97GBhuTGeLqd5pYJZMsy7hPtoHg OxbWkn+6mtXQ/LWKD6PGBrt5SwWsXhP6wyonugxv+yY+Lc0AXehERqbKeVH982Euhgd9 zeoWAWjQWszDatRDIzxokx+T2TWAT13mIIjWRc+hc2lMqKKH9ow6pJUeQxTGsHOUDfLx RdrpuDl1ZTZHuEyGUKTNVpc55X3lmeyOY3hMfg5xlSa9zepfPcY08eB9NKPf59rQJlXD 5B+AqDB7fPee0LHR7abFtZVNflgRXbbjHqAWE9vCHqcTMXbh/yrVzzX82Ay79FO/x207 QsZg== X-Gm-Message-State: AOAM53100xapXFpQS9u1U58X1pORxXrkbxbksU12y0URbh5E0KDsWPMJ h4AwSTa6FBkMLzrlXtx6SW+cqQ== X-Google-Smtp-Source: ABdhPJxkgWbGaUfFjw5Lssk59vMnzkeEHdQbgnLlkkMVlyiIZSdtEWOH6BPbcrwtKnoFwXG0CdGX3Q== X-Received: by 2002:a05:6638:c45:: with SMTP id g5mr12391809jal.16.1635272107353; Tue, 26 Oct 2021 11:15:07 -0700 (PDT) Received: from [192.168.1.30] ([207.135.234.126]) by smtp.gmail.com with ESMTPSA id x13sm4632914ile.9.2021.10.26.11.15.06 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 26 Oct 2021 11:15:07 -0700 (PDT) To: Andrew Morton , Dave Chinner , "linux-fsdevel@vger.kernel.org" , Linux Memory Management List , Chris Mason , Pavel Begunkov From: Jens Axboe Subject: mm: don't read i_size of inode unless we need it Message-ID: <6b67981f-57d4-c80e-bc07-6020aa601381@kernel.dk> Date: Tue, 26 Oct 2021 12:15:05 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 Content-Language: en-US X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 1D9BE10004C7 X-Stat-Signature: rhzjczb7eqeirmnaepfiik177uwnsa5u Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=kernel-dk.20210112.gappssmtp.com header.s=20210112 header.b=D1RR9QJE; dmarc=none; spf=pass (imf07.hostedemail.com: domain of axboe@kernel.dk designates 209.85.166.49 as permitted sender) smtp.mailfrom=axboe@kernel.dk X-HE-Tag: 1635272108-795285 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: We always go through i_size_read(), and we rarely end up needing it. Push the read to down where we need to check it, which avoids it for most cases. It looks like we can even remove this check entirely, which might be worth pursuing. But at least this takes it out of the hot path. Acked-by: Chris Mason Signed-off-by: Jens Axboe --- I came across this and wrote the patch the other day, then Pavel pointed me at his original posting of a very similar patch back in August. Discussed it with Chris, and it sure _seems_ like this would be fine. In an attempt to move the original discussion forward, here's this posting. diff --git a/mm/filemap.c b/mm/filemap.c index 44b4b551e430..850920276846 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2736,9 +2736,7 @@ generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) struct file *file = iocb->ki_filp; struct address_space *mapping = file->f_mapping; struct inode *inode = mapping->host; - loff_t size; - size = i_size_read(inode); if (iocb->ki_flags & IOCB_NOWAIT) { if (filemap_range_needs_writeback(mapping, iocb->ki_pos, iocb->ki_pos + count - 1)) @@ -2770,8 +2768,9 @@ generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) * the rest of the read. Buffered reads will not work for * DAX files, so don't bother trying. */ - if (retval < 0 || !count || iocb->ki_pos >= size || - IS_DAX(inode)) + if (retval < 0 || !count || IS_DAX(inode)) + return retval; + if (iocb->ki_pos >= i_size_read(inode)) return retval; }