From nobody Fri Jun 12 11:37:34 2026 Received: from m16.mail.163.com (m16.mail.163.com [117.135.210.3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5C79C3C8C65; Fri, 15 May 2026 09:48:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=117.135.210.3 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778838515; cv=none; b=sf1ZQybXWmWhLbUiIYaWTXLoXWc74srYuRoI5ruuRPEFOSLRg86uHiC4hK3KqSNLtepYNI4h3TX+oyXSDRnMK1LJqLCxW2XFTJMFc6Ql43SWPQtFEY34my0+mJYLCT1CFYl+Xt/qj06hMZFSIFa849uDpAxtR6fIUnKAdaYeSQU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778838515; c=relaxed/simple; bh=TH6ahl9ulw0pL6T5JDaH6lMv268eBsUF4xzVPOcOwdI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ML+tGKHF7cQfqM9yDmNIRTKnZFOpAoskGHQ5V1n9kyOPJKjyTrjMMxnOVnLf9aPOqF4jSsPkbLKnOB6g4EqLYV9QQ+a9vRt/CngikzPTtRWKvdv+JK7m3jGWkE6gebabX3h4SPlc9idPV0YyddrxzuL3edM3pteMMSzKhMexHKw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=hcz3fYBk; arc=none smtp.client-ip=117.135.210.3 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="hcz3fYBk" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=Al ESWM+x9CBZx/ibVibR9kzkEAIwGUCZhksgluuowXA=; b=hcz3fYBkFNRAnwc4mg 6gbOq6nbAGobeo+RnyqgkioZ97HqWpMaXTrbZfUwYyQkEDrnI79hKk8BBisRqeIN k7R5EQc2GsuWTgspXYc2hwjwyyDo+Szb9HVzyGPQXmMbh5g46+YM3OIG6Qbmn1Xo Zgm9G7t0vBKZAXszc/2z+3kXA= Received: from czl-ubuntu-pc.. (unknown []) by gzga-smtp-mtada-g0-4 (Coremail) with SMTP id _____wBnVxek6wZq6RobBg--.38248S3; Fri, 15 May 2026 17:47:18 +0800 (CST) From: Chi Zhiling To: linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Hugh Dickins , Baolin Wang , Andrew Morton , David Hildenbrand , Lorenzo Stoakes , Zi Yan , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , "Matthew Wilcox (Oracle)" , Jan Kara , Chi Zhiling Subject: [RFC PATCH 1/4] mm/shmem: add SGP_GET to get unlocked folio Date: Fri, 15 May 2026 17:46:59 +0800 Message-ID: <20260515094702.1092355-2-chizhiling@163.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260515094702.1092355-1-chizhiling@163.com> References: <20260515094702.1092355-1-chizhiling@163.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: _____wBnVxek6wZq6RobBg--.38248S3 X-Coremail-Antispam: 1Uf129KBjvJXoWxGr4DtF1ktw1kXrWxWFWkXrb_yoW5ZFWUpr ZrKF97trZ5K34xCwn7Zw48Zw1ru3s3KryUK34agr1IyFn8X3Wvkr10yr1UKF18trW3G3WF qw4UCa15uF1DZFJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jIYLkUUUUU= X-CM-SenderInfo: hfkl6xxlol0wi6rwjhhfrp/xtbC3AZA3WoG66bf3wAA3m Content-Type: text/plain; charset="utf-8" From: Chi Zhiling Add a new sgp_type SGP_GET which is similar to SGP_READ but returns the folio unlocked with an increased refcount. This eliminates the lock/unlock overhead for read-only operations. SGP_GET skips folio lock and mapping check, suitable only for short-lived access. Caller must not rely on folio->mapping validity as it can become invalid due to concurrent truncate. Safety relies on refcount and uptodate flag (truncate doesn't clear content). Signed-off-by: Chi Zhiling --- include/linux/shmem_fs.h | 3 ++- mm/shmem.c | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h index 93a0ba872ebe..24698faea5a4 100644 --- a/include/linux/shmem_fs.h +++ b/include/linux/shmem_fs.h @@ -164,7 +164,8 @@ extern unsigned long shmem_partial_swap_usage(struct ad= dress_space *mapping, =20 /* Flag allocation requirements to shmem_get_folio */ enum sgp_type { - SGP_READ, /* don't exceed i_size, don't allocate page */ + SGP_GET, /* don't exceed i_size, don't allocate page, don't lock */ + SGP_READ, /* don't exceed i_size, don't allocate page, lock folio */ SGP_NOALLOC, /* similar, but fail on hole or use fallocated page */ SGP_CACHE, /* don't exceed i_size, may allocate page */ SGP_WRITE, /* may exceed i_size, may allocate !Uptodate page */ diff --git a/mm/shmem.c b/mm/shmem.c index 3b5dc21b323c..ef19968cc51c 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2504,6 +2504,13 @@ static int shmem_get_folio_gfp(struct inode *inode, = pgoff_t index, } =20 if (folio) { + if (sgp =3D=3D SGP_GET) { + if (!folio_test_uptodate(folio)) { + folio_put(folio); + folio =3D NULL; + } + goto out; + } folio_lock(folio); =20 /* Has the folio been truncated or swapped out? */ @@ -2524,11 +2531,11 @@ static int shmem_get_folio_gfp(struct inode *inode,= pgoff_t index, } =20 /* - * SGP_READ: succeed on hole, with NULL folio, letting caller zero. + * SGP_READ/SGP_GET: succeed on hole, with NULL folio, letting caller zer= o. * SGP_NOALLOC: fail on hole, with NULL folio, letting caller fail. */ *foliop =3D NULL; - if (sgp =3D=3D SGP_READ) + if (sgp =3D=3D SGP_READ || sgp =3D=3D SGP_GET) return 0; if (sgp =3D=3D SGP_NOALLOC) return -ENOENT; @@ -2649,13 +2656,15 @@ static int shmem_get_folio_gfp(struct inode *inode,= pgoff_t index, * @sgp: SGP_* flags to control behavior * * Looks up the page cache entry at @inode & @index. If a folio is - * present, it is returned locked with an increased refcount. + * present, it is returned locked with an increased refcount, except + * for SGP_GET which returns the folio unlocked with an increased refcount. * * If the caller modifies data in the folio, it must call folio_mark_dirty= () * before unlocking the folio to ensure that the folio is not reclaimed. * There is no need to reserve space before calling folio_mark_dirty(). * * When no folio is found, the behavior depends on @sgp: + * - for SGP_GET, *@foliop is %NULL and 0 is returned * - for SGP_READ, *@foliop is %NULL and 0 is returned * - for SGP_NOALLOC, *@foliop is %NULL and -ENOENT is returned * - for all other flags a new folio is allocated, inserted into the --=20 2.43.0 From nobody Fri Jun 12 11:37:34 2026 Received: from m16.mail.163.com (m16.mail.163.com [220.197.31.3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 098383C8C65; Fri, 15 May 2026 09:48:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=220.197.31.3 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778838521; cv=none; b=qZCOLY709gR5tgh/lrix0IECWKEzgStZqdgwqPUf9Amu3Tzj3pAm0kjN3DPjvDiWoy9zsusaIIfayLGgPbjkzh1yRAl4qNh8choTD4eEvpMHyzWp92I0XZ1Aog2bbgpk5knkg548V8a+4kTEdbwV80T4Lbj8i8YvEivjbMj8UV0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778838521; c=relaxed/simple; bh=HcuFBNmn2JjTP3n0RT/JaV3j1Ngq/Nn8k9pauhPa2j8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=prqV1p/nz7WS0yYgb1YbXvrlfw/h5iNHUgDNFjNhj4Zr6UKgy7mRDPW4PI/3qvFtEs2d1KZ1I5Oqgcv2M5VTaw8W0iIvlw5hRekE+jMV4uH5u3wh60bcgf8c5HfII8L2jQVkSBzXvybcN89PLH+vZBWQGjDD1803xkaEPMWoSz0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=DwCEea2W; arc=none smtp.client-ip=220.197.31.3 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="DwCEea2W" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=tC AvDF399KUDDlsRRvXTB3FeBcx5LWuoyIvPW26D+rA=; b=DwCEea2WZ5HljjrfYt aGWsT72c229yIttmDYQTd8hcCUk6JCkTUwyQCNSthHZ3ruWWeMjSsaQHdgMR6H6D c7EFbBte8s+hMpUkFY3B5OAuZ0k34xHsU3kDiMV6zdA/uDgcI+FGhlVam+x+xYu/ dru0TT5Bt8Y8n11L6pN01Uphw= Received: from czl-ubuntu-pc.. (unknown []) by gzga-smtp-mtada-g0-4 (Coremail) with SMTP id _____wBnVxek6wZq6RobBg--.38248S4; Fri, 15 May 2026 17:47:18 +0800 (CST) From: Chi Zhiling To: linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Hugh Dickins , Baolin Wang , Andrew Morton , David Hildenbrand , Lorenzo Stoakes , Zi Yan , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , "Matthew Wilcox (Oracle)" , Jan Kara , Chi Zhiling Subject: [RFC PATCH 2/4] mm/shmem: use SGP_GET in read operations Date: Fri, 15 May 2026 17:47:00 +0800 Message-ID: <20260515094702.1092355-3-chizhiling@163.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260515094702.1092355-1-chizhiling@163.com> References: <20260515094702.1092355-1-chizhiling@163.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: _____wBnVxek6wZq6RobBg--.38248S4 X-Coremail-Antispam: 1Uf129KBjvJXoW7tFWDKr1UJFyrCw13tF13urg_yoW8KFykpr W5Kwn7Xrs5J3yUuryfJayrZ34Ykas7XrWUta9xGw1fJFn5JFykKr47t348WFy3ArW7Aw1S g3WUGa1jv3Wjyr7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jOcTQUUUUU= X-CM-SenderInfo: hfkl6xxlol0wi6rwjhhfrp/xtbC+AZA3WoG66ZsIQAA3P Content-Type: text/plain; charset="utf-8" From: Chi Zhiling Replace SGP_READ with SGP_GET in shmem_file_read_iter(), shmem_file_splice_read(), and shmem_get_link(). These functions immediately unlock the folio after getting it, making the lock acquisition redundant. Even though folio_lock can protect folio data consistency or prevent truncate while holding the lock, these can still happen after unlock. Since these functions continue reading data after unlocking, the lock does not provide effective protection. The folio reference count is what actually prevents reclamation during access, making the lock unnecessary. Signed-off-by: Chi Zhiling --- mm/shmem.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index ef19968cc51c..767610f78d0d 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -3370,15 +3370,13 @@ static ssize_t shmem_file_read_iter(struct kiocb *i= ocb, struct iov_iter *to) break; =20 index =3D iocb->ki_pos >> PAGE_SHIFT; - error =3D shmem_get_folio(inode, index, 0, &folio, SGP_READ); + error =3D shmem_get_folio(inode, index, 0, &folio, SGP_GET); if (error) { if (error =3D=3D -EINVAL) error =3D 0; break; } if (folio) { - folio_unlock(folio); - page =3D folio_file_page(folio, index); if (PageHWPoison(page)) { folio_put(folio); @@ -3561,15 +3559,13 @@ static ssize_t shmem_file_splice_read(struct file *= in, loff_t *ppos, break; =20 index =3D *ppos >> PAGE_SHIFT; - error =3D shmem_get_folio(inode, index, 0, &folio, SGP_READ); + error =3D shmem_get_folio(inode, index, 0, &folio, SGP_GET); if (error) { if (error =3D=3D -EINVAL) error =3D 0; break; } if (folio) { - folio_unlock(folio); - page =3D folio_file_page(folio, index); if (PageHWPoison(page)) { error =3D -EIO; @@ -4170,17 +4166,15 @@ static const char *shmem_get_link(struct dentry *de= ntry, struct inode *inode, return ERR_PTR(-ECHILD); } } else { - error =3D shmem_get_folio(inode, 0, 0, &folio, SGP_READ); + error =3D shmem_get_folio(inode, 0, 0, &folio, SGP_GET); if (error) return ERR_PTR(error); if (!folio) return ERR_PTR(-ECHILD); if (PageHWPoison(folio_page(folio, 0))) { - folio_unlock(folio); folio_put(folio); return ERR_PTR(-ECHILD); } - folio_unlock(folio); } set_delayed_call(done, shmem_put_link, folio); return folio_address(folio); --=20 2.43.0 From nobody Fri Jun 12 11:37:34 2026 Received: from m16.mail.163.com (m16.mail.163.com [117.135.210.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AB42443D4EC; Fri, 15 May 2026 09:48:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=117.135.210.5 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778838520; cv=none; b=up0YhkviDC6UsX0EM38L3nq3+cX+nfk+HTDBuPk2uZm4Hn5SJGJWi1D1oSEKHO9HyzXKpNNVPjoW+TR9WNiewdHdYzjEqhM95IWxHoPUOX5goFjEJM0RTWxcpLYbLBTEpwzi//I9xMqjcSUa3GaAPMVYUflgqJKPSQXCoC+F4mw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778838520; c=relaxed/simple; bh=99KWN40sl8xDbpH9FMFsdsJAphPitUxrXoKeVFzgbhY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rkxIdy0KwozN+K6RRrAIgyLHSrfqjPwzHIvJzQDA0YQNqbFmbcjGstwcwKyKeaIfVn5ce7D/BU8rOdCJmRg7rw0DbaNAZwQuaTD3xbRibKHKuMSpulWT0/pLOrRuLrTv/eQt4S2CusOvKgcZ5kcj1DyLE3pvwkXXfLkwWWwPiuA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=G15ZBj01; arc=none smtp.client-ip=117.135.210.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="G15ZBj01" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=x1 ch1LMRZ8v16TQ/TGRFBp+464EezxW+tb87/9Sva4E=; b=G15ZBj01k81TcQUSs0 3m3R/MrQfVi/YrxYMDFwaXQ3HGMNWCJG2D9HuyzGaKEdO4LG59WXiZVLgz2wJ0cl f7GoyiIKBpZRqsS3hLdmf9JOv763sgYA9MA15aroJoU/bLkMTgO8oUzzavq2n2RB VYfSarMnHTyYRgBhW46HDGjEo= Received: from czl-ubuntu-pc.. (unknown []) by gzga-smtp-mtada-g0-4 (Coremail) with SMTP id _____wBnVxek6wZq6RobBg--.38248S5; Fri, 15 May 2026 17:47:19 +0800 (CST) From: Chi Zhiling To: linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Hugh Dickins , Baolin Wang , Andrew Morton , David Hildenbrand , Lorenzo Stoakes , Zi Yan , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , "Matthew Wilcox (Oracle)" , Jan Kara , Chi Zhiling Subject: [RFC PATCH 3/4] mm/shmem: optimize file read with folio batching Date: Fri, 15 May 2026 17:47:01 +0800 Message-ID: <20260515094702.1092355-4-chizhiling@163.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260515094702.1092355-1-chizhiling@163.com> References: <20260515094702.1092355-1-chizhiling@163.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: _____wBnVxek6wZq6RobBg--.38248S5 X-Coremail-Antispam: 1Uf129KBjvJXoWxZr1xtr18Gr4rAry5trWxXrb_yoWrtF15pF WUK3ZIqrWxGr43ur4fJFWkZa4rK392qrW5JayxG3W3Z3W5J3Z0kF18t34YvFW5ArW5C3WI g3WFk3W8W3Wjyr7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jLWrXUUUUU= X-CM-SenderInfo: hfkl6xxlol0wi6rwjhhfrp/xtbC9wdA3WoG66dn2wAA3x Content-Type: text/plain; charset="utf-8" From: Chi Zhiling Optimize shmem file read by implementing folio batching in the read iteration path. Only uptodate folios are added to the batch, ensuring all folios in the batch are valid and ready for use without additional checking. Signed-off-by: Chi Zhiling --- mm/shmem.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 93 insertions(+), 9 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 767610f78d0d..4bc4e463ca97 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -3348,16 +3348,100 @@ shmem_write_end(const struct kiocb *iocb, struct a= ddress_space *mapping, return copied; } =20 +static pgoff_t shmem_get_read_batch(struct address_space *mapping, + pgoff_t index, pgoff_t max, struct folio_batch *fbatch) +{ + XA_STATE(xas, &mapping->i_pages, index); + struct folio *folio; + pgoff_t end =3D max; + + rcu_read_lock(); + xas_for_each(&xas, folio, max) { + if (xas_retry(&xas, folio)) + continue; + if (xa_is_value(folio)) { + end =3D xas.xa_index; + break; + } + if (!folio_try_get(folio)) + goto retry; + + if (unlikely(folio !=3D xas_reload(&xas))) + goto put_folio; + + end =3D folio_next_index(folio); + + if (!folio_test_uptodate(folio)) { + xas_advance(&xas, end - 1); + folio_put(folio); + continue; + } + if (!folio_batch_add(fbatch, folio)) + break; + xas_advance(&xas, end - 1); + continue; +put_folio: + folio_put(folio); +retry: + xas_reset(&xas); + } + rcu_read_unlock(); + + return end; +} + +static inline int shmem_get_folio_from_batch(struct inode *inode, + pgoff_t index, pgoff_t last_index, struct folio **folio, + struct folio_batch *fbatch, pgoff_t *batch_end) +{ + struct folio *next; + int error; + + if (*batch_end > index) { +retry: + next =3D folio_batch_next(fbatch); + if (next) { + if (next->index > index) { + next =3D NULL; + fbatch->i--; /* revert folio_batch_next */ + } + } + *folio =3D next; + return 0; + } + + for (int i =3D 0; i < folio_batch_count(fbatch); i++) + folio_put(fbatch->folios[i]); + folio_batch_init(fbatch); + + *batch_end =3D shmem_get_read_batch(inode->i_mapping, index, + last_index, fbatch); + if (*batch_end > index) + goto retry; + + error =3D shmem_get_folio(inode, index, 0, folio, SGP_GET); + if (unlikely(error)) + return error; + if (*folio) { + folio_batch_add(fbatch, *folio); + goto retry; + } + return 0; +} + static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *t= o) { struct file *file =3D iocb->ki_filp; struct inode *inode =3D file_inode(file); struct address_space *mapping =3D inode->i_mapping; - pgoff_t index; + struct folio_batch fbatch; + pgoff_t index, last_index, fbatch_end =3D 0; unsigned long offset; int error =3D 0; ssize_t retval =3D 0; =20 + folio_batch_init(&fbatch); + for (;;) { struct folio *folio =3D NULL; struct page *page =3D NULL; @@ -3370,8 +3454,10 @@ static ssize_t shmem_file_read_iter(struct kiocb *io= cb, struct iov_iter *to) break; =20 index =3D iocb->ki_pos >> PAGE_SHIFT; - error =3D shmem_get_folio(inode, index, 0, &folio, SGP_GET); - if (error) { + last_index =3D (iocb->ki_pos + to->count - 1) >> PAGE_SHIFT; + error =3D shmem_get_folio_from_batch(inode, index, last_index, &folio, + &fbatch, &fbatch_end); + if (unlikely(error)) { if (error =3D=3D -EINVAL) error =3D 0; break; @@ -3379,7 +3465,6 @@ static ssize_t shmem_file_read_iter(struct kiocb *ioc= b, struct iov_iter *to) if (folio) { page =3D folio_file_page(folio, index); if (PageHWPoison(page)) { - folio_put(folio); error =3D -EIO; break; } @@ -3394,11 +3479,9 @@ static ssize_t shmem_file_read_iter(struct kiocb *io= cb, struct iov_iter *to) * are called without i_rwsem protection against truncate */ i_size =3D i_size_read(inode); - if (unlikely(iocb->ki_pos >=3D i_size)) { - if (folio) - folio_put(folio); + if (unlikely(iocb->ki_pos >=3D i_size)) break; - } + end_offset =3D min_t(loff_t, i_size, iocb->ki_pos + to->count); if (folio && likely(!fallback_page_copy)) fsize =3D folio_size(folio); @@ -3433,7 +3516,6 @@ static ssize_t shmem_file_read_iter(struct kiocb *ioc= b, struct iov_iter *to) ret =3D copy_folio_to_iter(folio, offset, nr, to); else ret =3D copy_page_to_iter(page, offset, nr, to); - folio_put(folio); } else if (user_backed_iter(to)) { /* * Copy to user tends to be so well optimized, but @@ -3462,6 +3544,8 @@ static ssize_t shmem_file_read_iter(struct kiocb *ioc= b, struct iov_iter *to) cond_resched(); } =20 + for (int i =3D 0; i < folio_batch_count(&fbatch); i++) + folio_put(fbatch.folios[i]); file_accessed(file); return retval ? retval : error; } --=20 2.43.0 From nobody Fri Jun 12 11:37:34 2026 Received: from m16.mail.163.com (m16.mail.163.com [117.135.210.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4F5CE44B675; Fri, 15 May 2026 09:48:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=117.135.210.4 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778838524; cv=none; b=ObVRPwLVui4dXNPsvsRkXVUOrs9Xt/YqIaaQDz2asKznWYUInVKLJwO3AfSJM/qYnDPym6cFYVGdV1j0JDjbgdwQudke16wXh/VjhffepTbzUpgIdnaaYImI4NqqFt32B2eI8h5RNWnqQpSbBZNJ3HnypYG8LfDiVPiESmufJNE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778838524; c=relaxed/simple; bh=QFGY4w9RovPA8CODuoSvEB6kQuqoXM61TrVG9cteS+I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=j406MhjMLH3/YO/8Zt+mDD/Wag9jUwH02tE2mLD41ec3Ujf9n5KX6a8QJbO3q21lb3h28BD1egF+5c/4+WzGmPtjG/0jZ6s1lmmLXDAW3opxF1/oe7+jE6+thtUdo49ysLHF962LgSGATVg0X+v7UeQi53tgG/Ox84ic/0nXtXY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=i7WLzVYf; arc=none smtp.client-ip=117.135.210.4 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="i7WLzVYf" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=JO pXXq0yhxeFCV5hNBrcOoYY/AYcpwADf6/pjp5Lkp0=; b=i7WLzVYf0vV3XZUCeZ 42Slhj+3H1wPtoGcQ/A6IS+HzEFrQ9IpkqShlJrKTuWvXb7yXAaOG6j2ZYMOaX2J gXKq8/i1lXOS1O/Gyjht2+wpKFRqQ7f0sl13POg1E9Ptlh/MaUvjHexZHGepQD5h 0nRvFAA8nhDeYkFhq1rRALm3Q= Received: from czl-ubuntu-pc.. (unknown []) by gzga-smtp-mtada-g0-4 (Coremail) with SMTP id _____wBnVxek6wZq6RobBg--.38248S6; Fri, 15 May 2026 17:47:19 +0800 (CST) From: Chi Zhiling To: linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Hugh Dickins , Baolin Wang , Andrew Morton , David Hildenbrand , Lorenzo Stoakes , Zi Yan , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , "Matthew Wilcox (Oracle)" , Jan Kara , Chi Zhiling Subject: [RFC PATCH 4/4] mm/shmem: make SGP_NOALLOC succeed on hole like SGP_READ Date: Fri, 15 May 2026 17:47:02 +0800 Message-ID: <20260515094702.1092355-5-chizhiling@163.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260515094702.1092355-1-chizhiling@163.com> References: <20260515094702.1092355-1-chizhiling@163.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: _____wBnVxek6wZq6RobBg--.38248S6 X-Coremail-Antispam: 1Uf129KBjvJXoWxXFW5Xr1rAry5Kr1UCr1xZrb_yoW5ZF1Dpr 97Gr97GrZ5W348Cr1Duws7Zr1UXan3KryUK34agw1vyF98AFy0kr1UAw1j9F18JrW7Ca1F qw4jkws8uF4UXrJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jxkucUUUUU= X-CM-SenderInfo: hfkl6xxlol0wi6rwjhhfrp/xtbC9wdA3WoG66dn5QAA3P Content-Type: text/plain; charset="utf-8" From: Chi Zhiling Change SGP_NOALLOC to return 0 with NULL folio on hole, matching SGP_READ/SGP_GET behavior. This simplifies the sgp_type handling by unifying hole semantics across these three types. Previously, SGP_NOALLOC returned -ENOENT on hole, while SGP_READ/SGP_GET returned 0. This inconsistency required special handling in callers like khugepaged and userfaultfd. After this change: - khugepaged: behavior unchanged (checks both error and NULL folio) - userfaultfd: behavior unchanged (both -ENOENT and NULL are converted to -EFAULT before returning to userspace) Signed-off-by: Chi Zhiling --- include/linux/shmem_fs.h | 2 +- mm/khugepaged.c | 2 +- mm/shmem.c | 9 +++------ 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h index 24698faea5a4..6f7f8b9e2a10 100644 --- a/include/linux/shmem_fs.h +++ b/include/linux/shmem_fs.h @@ -166,7 +166,7 @@ extern unsigned long shmem_partial_swap_usage(struct ad= dress_space *mapping, enum sgp_type { SGP_GET, /* don't exceed i_size, don't allocate page, don't lock */ SGP_READ, /* don't exceed i_size, don't allocate page, lock folio */ - SGP_NOALLOC, /* similar, but fail on hole or use fallocated page */ + SGP_NOALLOC, /* like SGP_READ, but accept fallocated page */ SGP_CACHE, /* don't exceed i_size, may allocate page */ SGP_WRITE, /* may exceed i_size, may allocate !Uptodate page */ SGP_FALLOC, /* like SGP_WRITE, but make existing page Uptodate */ diff --git a/mm/khugepaged.c b/mm/khugepaged.c index b8452dbdb043..3309d1c094df 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1950,7 +1950,7 @@ static enum scan_result collapse_file(struct mm_struc= t *mm, unsigned long addr, xas_unlock_irq(&xas); /* swap in or instantiate fallocated page */ if (shmem_get_folio(mapping->host, index, 0, - &folio, SGP_NOALLOC)) { + &folio, SGP_NOALLOC) || !folio) { result =3D SCAN_FAIL; goto xa_unlocked; } diff --git a/mm/shmem.c b/mm/shmem.c index 4bc4e463ca97..41f5e251f7ed 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2531,14 +2531,11 @@ static int shmem_get_folio_gfp(struct inode *inode,= pgoff_t index, } =20 /* - * SGP_READ/SGP_GET: succeed on hole, with NULL folio, letting caller zer= o. - * SGP_NOALLOC: fail on hole, with NULL folio, letting caller fail. + * SGP_READ/SGP_GET/SGP_NOALLOC: succeed on hole, with NULL folio. */ *foliop =3D NULL; - if (sgp =3D=3D SGP_READ || sgp =3D=3D SGP_GET) + if (sgp <=3D SGP_NOALLOC) return 0; - if (sgp =3D=3D SGP_NOALLOC) - return -ENOENT; =20 /* * Fast cache lookup and swap lookup did not find it: allocate. @@ -2666,7 +2663,7 @@ static int shmem_get_folio_gfp(struct inode *inode, p= goff_t index, * When no folio is found, the behavior depends on @sgp: * - for SGP_GET, *@foliop is %NULL and 0 is returned * - for SGP_READ, *@foliop is %NULL and 0 is returned - * - for SGP_NOALLOC, *@foliop is %NULL and -ENOENT is returned + * - for SGP_NOALLOC, *@foliop is %NULL and 0 is returned * - for all other flags a new folio is allocated, inserted into the * page cache and returned locked in @foliop. * --=20 2.43.0