From patchwork Fri Oct 18 03:00:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 13841045 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A8992D3C545 for ; Fri, 18 Oct 2024 03:00:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5BC976B008A; Thu, 17 Oct 2024 23:00:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 54AD06B0095; Thu, 17 Oct 2024 23:00:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 370766B0088; Thu, 17 Oct 2024 23:00:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 1264D6B008C for ; Thu, 17 Oct 2024 23:00:43 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id BE4F01C750E for ; Fri, 18 Oct 2024 03:00:29 +0000 (UTC) X-FDA: 82685220030.12.24074FD Received: from out30-118.freemail.mail.aliyun.com (out30-118.freemail.mail.aliyun.com [115.124.30.118]) by imf27.hostedemail.com (Postfix) with ESMTP id 5CB6940008 for ; Fri, 18 Oct 2024 03:00:29 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=fSbvC9HG; spf=pass (imf27.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.118 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com; dmarc=pass (policy=none) header.from=linux.alibaba.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729220367; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Ynn20XvNY9Nu0PNOg+bwufz+Xr8j8cPFzYmz1b4byuY=; b=Hf7H0hnIMUCUb8iVnKapwvPtjaIQWAXnFNCTWudqj2eqnFj95LUqDpe9JcAiVXz6lBWYcQ w4WwXLRYOg5cVAGvh5N3YKZnji6HNLJ8qZZOho7as5+ShKoLAOK3wEh/sn0czuvgU5UXGs Lux4kWwsIpAN7r6On7WMEgsRt1hLytE= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=fSbvC9HG; spf=pass (imf27.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.118 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com; dmarc=pass (policy=none) header.from=linux.alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729220367; a=rsa-sha256; cv=none; b=GzcGLi80njHT6cz8rPqYuw9q4J35Xf2JOPu29cgf4ic73OR/OIuzXX22shfMgOJ01Uoo0r 76BXUbQ0EpW8BzU2vQaKxLKP5iE7T4TgRUEuUapcyV4l7yuxxZCU8kGogD7Eh+iM+McpeC C5RUAy8UcsQ6cgjvJ7inSR0eBe7TRFg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1729220437; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=Ynn20XvNY9Nu0PNOg+bwufz+Xr8j8cPFzYmz1b4byuY=; b=fSbvC9HG5suhD6RfCJyVwKm6oI611x8f60IpX/UgLJCht54xt8d0EGR+MXXr6FIWjkVeeVIe2eG97RtW37v10/A7iRvYQffqj0M4LRXIj7jjaEIYklbS5UUfNttlEjBbyxe61TegTswvyA4JqaJqQDUIYZXYHQI9R4Zm6RACfag= Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0WHMZWVd_1729220435 cluster:ay36) by smtp.aliyun-inc.com; Fri, 18 Oct 2024 11:00:36 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com Cc: willy@infradead.org, david@redhat.com, wangkefeng.wang@huawei.com, shy828301@gmail.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 1/2] mm: shmem: update iocb->ki_pos directly to simplify tmpfs read logic Date: Fri, 18 Oct 2024 11:00:27 +0800 Message-Id: X-Mailer: git-send-email 2.39.3 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 5CB6940008 X-Stat-Signature: kb65ht5tzshm7yp3c6y989nemd8p11m1 X-HE-Tag: 1729220429-927461 X-HE-Meta: U2FsdGVkX1+0gutO3meIAUHWJoyQRD+BCw09I8/yUnWbCvdFc25wRTV9Si1qU0zCMxMpOBIx8gyvBTW/ns0uPzn+LYzbyJ4dDGREhwwmq0lWVUj1YpVi7Fn7bhehTCGUYqjPeH16m5mNGNdjp0pArk1K0vtl2RnJryOeT1zxE63+OdtC2vQjAZeselwXF5yzVm5sJJzAjik0BWIIxcEQbe/Io1CfAfEPeyhn95l9IaLv6PR9Qg1RYhlxbKZrCSlU2OnP8iDpsbuXz/KAIIWhp76CtVVbomLvXAR5stHPhM/HbtbzIMEFFQShXFXrjpvrTiiJdKo6+JMLSZPbFkGKcRCT4VsMFscy+5XcKW1RBoorJFzmJntTngZLgz5LZjLLP8whrSh6a4/dGH4D8fA8GFnoaZBB+4CcT2zq/ExGSoWmgVj0v9tONOyYt2jh3Dv7XB+hsqlgVnt/CSe8y22xCJiLDjAOaKUTOFxxXjhgJ+VTVwiZj/ilcxHXRJb6NMQHRW+ibcZLkW3OtQhNZpxvt1GKYjy0Ymue4PYeYb+LOdUjusqTQ0ifAGszcRbzzBUu9GqlkdHI3Iy0Kt1+pgMWCWRgvSa16T6oElzjMukl0jSCidSMOLnpKJISDqeuWFDxA82d5eA7gn078atjnPOKaSvnSWdZLrJFZX1PkugZF6AQbhKyPh/nhMFHmA1WjFado7yAbBMN0TRAmC+n6XaY2XLowawXkrB+HwP1YZeEkjVPdK2qpqtl/W3d3BY+Ssjd+b0wwQtvteDoTkg9tDlv5uARbGJKy9Xw/7RN5GZAJTPWKw6lBeiIbpbFkbC1NhlvG2arBapb3vH/PTzFkp/ZTS8OgeLiHulVkaYcyjPWMOPts/Euqqi5kC8ylAgaAcw/uIbBmW4r9iqZtC56qNIUXsXnvKw0meAPe0oIb+5+2r0mAEntBR9xvYuByvv3G6xO6E7UrARnBOlrTVc1S2o 5dmcj3uG W5J+IfcHDmKI04zSEHstmNNAOhFrGpxrIe8WR6Bg8udyhxxByelqGZzSDgeqaev5ItreAN/h1yQVJQK/9QU5qCOGO4gmhhPfsBBG6SeT+BDrcpR2rhByzYsc1QkUURR+PCoD25ngdyVMxpDpQN+tBhNr9gByirN/771SOwBrXriRaLldl1Pt/votXN3F4YKa6kgeUDSTLJ/AYdiiYa4LRbzoqNR4Uy8n9GsFb80WaJ657ZvYRgw+PvRjhjVX23WryY9QXZIxis03R7Z5HIM/jJPd6KA== 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: List-Subscribe: List-Unsubscribe: Use iocb->ki_pos to check if the read bytes exceeds the file size and to calculate the bytes to be read can help simplify the code logic. Meanwhile, this is also a preparation for improving tmpfs large folios read performace in the following patch. Signed-off-by: Baolin Wang --- mm/shmem.c | 35 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 66eae800ffab..93642aa8d1aa 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -3106,27 +3106,19 @@ static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to) unsigned long offset; int error = 0; ssize_t retval = 0; - loff_t *ppos = &iocb->ki_pos; - index = *ppos >> PAGE_SHIFT; - offset = *ppos & ~PAGE_MASK; + offset = iocb->ki_pos & ~PAGE_MASK; for (;;) { struct folio *folio = NULL; struct page *page = NULL; - pgoff_t end_index; unsigned long nr, ret; - loff_t i_size = i_size_read(inode); + loff_t end_offset, i_size = i_size_read(inode); - end_index = i_size >> PAGE_SHIFT; - if (index > end_index) + if (unlikely(iocb->ki_pos >= i_size)) break; - if (index == end_index) { - nr = i_size & ~PAGE_MASK; - if (nr <= offset) - break; - } + index = iocb->ki_pos >> PAGE_SHIFT; error = shmem_get_folio(inode, index, 0, &folio, SGP_READ); if (error) { if (error == -EINVAL) @@ -3148,18 +3140,14 @@ static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to) * We must evaluate after, since reads (unlike writes) * are called without i_rwsem protection against truncate */ - nr = PAGE_SIZE; i_size = i_size_read(inode); - end_index = i_size >> PAGE_SHIFT; - if (index == end_index) { - nr = i_size & ~PAGE_MASK; - if (nr <= offset) { - if (folio) - folio_put(folio); - break; - } + if (unlikely(iocb->ki_pos >= i_size)) { + if (folio) + folio_put(folio); + break; } - nr -= offset; + end_offset = min_t(loff_t, i_size, iocb->ki_pos + to->count); + nr = min_t(loff_t, end_offset - iocb->ki_pos, PAGE_SIZE - offset); if (folio) { /* @@ -3199,8 +3187,8 @@ static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to) retval += ret; offset += ret; - index += offset >> PAGE_SHIFT; offset &= ~PAGE_MASK; + iocb->ki_pos += ret; if (!iov_iter_count(to)) break; @@ -3211,7 +3199,6 @@ static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to) cond_resched(); } - *ppos = ((loff_t) index << PAGE_SHIFT) + offset; file_accessed(file); return retval ? retval : error; }