From patchwork Fri Nov 5 20:37:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Morton X-Patchwork-Id: 12605457 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 7B080C433EF for ; Fri, 5 Nov 2021 20:37:10 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 365076056B for ; Fri, 5 Nov 2021 20:37:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 365076056B 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 B8F8B94002C; Fri, 5 Nov 2021 16:37:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AEF1A940007; Fri, 5 Nov 2021 16:37:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 969FF94002C; Fri, 5 Nov 2021 16:37:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0096.hostedemail.com [216.40.44.96]) by kanga.kvack.org (Postfix) with ESMTP id 7DB4E940007 for ; Fri, 5 Nov 2021 16:37:09 -0400 (EDT) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 400BF1849A3B5 for ; Fri, 5 Nov 2021 20:37:09 +0000 (UTC) X-FDA: 78776036178.28.4B0632D Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf18.hostedemail.com (Postfix) with ESMTP id DBF524002085 for ; Fri, 5 Nov 2021 20:37:08 +0000 (UTC) Received: by mail.kernel.org (Postfix) with ESMTPSA id E9CDB60E05; Fri, 5 Nov 2021 20:37:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1636144628; bh=k7WljE0iQVEK2JtPw8YULivJdqphpmtM/3TtjO4wdWc=; h=Date:From:To:Subject:In-Reply-To:From; b=sx4/h36y7mqB0iHe9hbEfNxMJs0UrlNaBNQvX9l+cdSRw2qreQ3w3LoSEr31PiSSV 9mBP838UTeG5NwRQoovDLEDTcAZhWQcXhugjy5dutQTBBkOW96+mJvhkVJ3dYrP7C2 osmfkhbqsIjr4Gqtze2d4i7Ebtf37/cUpYiyb9a4= Date: Fri, 05 Nov 2021 13:37:07 -0700 From: Andrew Morton To: akpm@linux-foundation.org, asml.silence@gmail.com, axboe@kernel.dk, clm@fb.com, david@fromorbit.com, jack@suse.cz, josef@redhat.com, linux-mm@kvack.org, mm-commits@vger.kernel.org, torvalds@linux-foundation.org Subject: [patch 047/262] mm: don't read i_size of inode unless we need it Message-ID: <20211105203707.MxPJku2CF%akpm@linux-foundation.org> In-Reply-To: <20211105133408.cccbb98b71a77d5e8430aba1@linux-foundation.org> User-Agent: s-nail v14.8.16 Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b="sx4/h36y"; dmarc=none; spf=pass (imf18.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: rspam02 X-Rspamd-Queue-Id: DBF524002085 X-Stat-Signature: 7gjxgrngxok14ag5rmiqw1x1wasoh1e7 X-HE-Tag: 1636144628-842400 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: Jens Axboe Subject: mm: don't read i_size of inode unless we need it 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. Link: https://lkml.kernel.org/r/6b67981f-57d4-c80e-bc07-6020aa601381@kernel.dk Signed-off-by: Jens Axboe Acked-by: Chris Mason Cc: Josef Bacik Cc: Dave Chinner Cc: Pavel Begunkov Cc: Jan Kara Signed-off-by: Andrew Morton --- mm/filemap.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) --- a/mm/filemap.c~mm-dont-read-i_size-of-inode-unless-we-need-it +++ a/mm/filemap.c @@ -2740,9 +2740,7 @@ generic_file_read_iter(struct kiocb *ioc 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)) @@ -2774,8 +2772,9 @@ generic_file_read_iter(struct kiocb *ioc * 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; }