From patchwork Wed May 15 05:57:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Gomez X-Patchwork-Id: 13664605 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 38559C25B75 for ; Wed, 15 May 2024 05:57:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CFA396B03D3; Wed, 15 May 2024 01:57:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C60A16B03D6; Wed, 15 May 2024 01:57:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8AE946B03D4; Wed, 15 May 2024 01:57:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 5EDD16B03D3 for ; Wed, 15 May 2024 01:57:38 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 16AA0A1455 for ; Wed, 15 May 2024 05:57:38 +0000 (UTC) X-FDA: 82119573396.10.6C3FC7C Received: from mailout2.w1.samsung.com (mailout2.w1.samsung.com [210.118.77.12]) by imf07.hostedemail.com (Postfix) with ESMTP id 2C2964000D for ; Wed, 15 May 2024 05:57:34 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=samsung.com header.s=mail20170921 header.b=bXJqDcB0; dmarc=pass (policy=none) header.from=samsung.com; spf=pass (imf07.hostedemail.com: domain of da.gomez@samsung.com designates 210.118.77.12 as permitted sender) smtp.mailfrom=da.gomez@samsung.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1715752655; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Yzf34yJLjjOZ5L6S/TOvcg7MvQWva+m/QvkKTJ1hVb4=; b=acIJMoWYHFbKJZAGIlqQdq7A2PakUERWvmXcqKvGHVF+Xm4yuHqR7GEP1PqcYJrlP6uo0n 64VLdZJkVJnuW0FbrCZhfPTnjBHI0pXey78JTpZUIjdH8KYc/Vb/uaIB02AUgJrKkHAGRU 9f1qrLT2n5X8vJNRDJ7rCtCT8RcR8Bo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1715752655; a=rsa-sha256; cv=none; b=nL4QhqDoGaelWm/fX3IK/upvT/6i17gckqatA7in/A+VxVlawLGIX0ItIA/A8LFdAl+zm7 Og48IanXkoqsVBAttFnWBY/Ola6EkBb+O+ZDPdKnkR1R6PMjPc58b7gKPAk0Ry+Za9dZm9 3VV6uEEi4ZdpqlV5To6JrH8sGmmRZC0= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=samsung.com header.s=mail20170921 header.b=bXJqDcB0; dmarc=pass (policy=none) header.from=samsung.com; spf=pass (imf07.hostedemail.com: domain of da.gomez@samsung.com designates 210.118.77.12 as permitted sender) smtp.mailfrom=da.gomez@samsung.com Received: from eucas1p2.samsung.com (unknown [182.198.249.207]) by mailout2.w1.samsung.com (KnoxPortal) with ESMTP id 20240515055733euoutp021ee63bbf6046f2155f5bc72aa0d8edc7~Pk-LFXIPk1630416304euoutp026 for ; Wed, 15 May 2024 05:57:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout2.w1.samsung.com 20240515055733euoutp021ee63bbf6046f2155f5bc72aa0d8edc7~Pk-LFXIPk1630416304euoutp026 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1715752653; bh=Yzf34yJLjjOZ5L6S/TOvcg7MvQWva+m/QvkKTJ1hVb4=; h=From:To:CC:Subject:Date:In-Reply-To:References:From; b=bXJqDcB0N9aUk1MGm91N6M1lKG8EaMISYNP47SSH3mR0BWBor5or/6AkM/1sz755Y IJZrTE9iiB/9h7xctUC2I7XihsHAOiTUvnMYUVLGyUiM9XUCT2kHWcvdGwXXeSZ7sM jsouEsfBa5ZUmuBW14uWD05hp9m1WcsjQ4L1/DgA= Received: from eusmges1new.samsung.com (unknown [203.254.199.242]) by eucas1p2.samsung.com (KnoxPortal) with ESMTP id 20240515055733eucas1p2a521a9e0623f0531a8272ecfb655f5b7~Pk-K2bw-B0810008100eucas1p2S; Wed, 15 May 2024 05:57:33 +0000 (GMT) Received: from eucas1p1.samsung.com ( [182.198.249.206]) by eusmges1new.samsung.com (EUCPMTA) with SMTP id FC.BC.09624.DCE44466; Wed, 15 May 2024 06:57:33 +0100 (BST) Received: from eusmtrp1.samsung.com (unknown [182.198.249.138]) by eucas1p2.samsung.com (KnoxPortal) with ESMTPA id 20240515055733eucas1p2804d2fb5f5bf7d6adb460054f6e9f4d8~Pk-KcM6iH1245212452eucas1p2C; Wed, 15 May 2024 05:57:33 +0000 (GMT) Received: from eusmgms2.samsung.com (unknown [182.198.249.180]) by eusmtrp1.samsung.com (KnoxPortal) with ESMTP id 20240515055733eusmtrp129539bea3fb8883a5d82c38a09701dcf~Pk-Kbl7An0411404114eusmtrp1B; Wed, 15 May 2024 05:57:33 +0000 (GMT) X-AuditID: cbfec7f2-bfbff70000002598-5c-66444ecd6288 Received: from eusmtip1.samsung.com ( [203.254.199.221]) by eusmgms2.samsung.com (EUCPMTA) with SMTP id FB.F1.09010.DCE44466; Wed, 15 May 2024 06:57:33 +0100 (BST) Received: from CAMSVWEXC01.scsc.local (unknown [106.1.227.71]) by eusmtip1.samsung.com (KnoxPortal) with ESMTPA id 20240515055733eusmtip1aa61160f14684c57a464d66ecebdd55b~Pk-KOVfKq0235902359eusmtip1T; Wed, 15 May 2024 05:57:33 +0000 (GMT) Received: from CAMSVWEXC02.scsc.local (2002:6a01:e348::6a01:e348) by CAMSVWEXC01.scsc.local (2002:6a01:e347::6a01:e347) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Wed, 15 May 2024 06:57:32 +0100 Received: from CAMSVWEXC02.scsc.local ([::1]) by CAMSVWEXC02.scsc.local ([fe80::3c08:6c51:fa0a:6384%13]) with mapi id 15.00.1497.012; Wed, 15 May 2024 06:57:32 +0100 From: Daniel Gomez To: "hughd@google.com" , "akpm@linux-foundation.org" , "willy@infradead.org" , "jack@suse.cz" , "mcgrof@kernel.org" CC: "linux-mm@kvack.org" , "linux-xfs@vger.kernel.org" , "djwong@kernel.org" , "Pankaj Raghav" , "dagmcr@gmail.com" , "yosryahmed@google.com" , "baolin.wang@linux.alibaba.com" , "ritesh.list@gmail.com" , "lsf-pc@lists.linux-foundation.org" , "david@redhat.com" , "chandan.babu@oracle.com" , "linux-kernel@vger.kernel.org" , "brauner@kernel.org" , Daniel Gomez Subject: [PATCH 08/12] shmem: clear uptodate blocks after PUNCH_HOLE Thread-Topic: [PATCH 08/12] shmem: clear uptodate blocks after PUNCH_HOLE Thread-Index: AQHapozGSURBX17ZzUyZscS7mtN1hQ== Date: Wed, 15 May 2024 05:57:32 +0000 Message-ID: <20240515055719.32577-9-da.gomez@samsung.com> In-Reply-To: <20240515055719.32577-1-da.gomez@samsung.com> Accept-Language: en-US, en-GB Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-messagesentrepresentingtype: 1 x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [106.210.248.161] MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFvrEKsWRmVeSWpSXmKPExsWy7djPc7pn/VzSDH6/UbaYs34Nm8X/vccY LV4f/sRocemonMXZvt9sFl/X/2K2uPyEz+Lppz4Wi9nTm5ksLu+aw2Zxb81/Votdf3awW+x7 vZfZ4saEp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VUsW71eRTXo56ep+pgnGgvsTmp7mNz0JEb6h5i3HMifwxfa7 a5jWC0z/HPt1o5LU2zfpsnL6T+r6/9+ytve5ofq116RCvE7lm/VTec62wAcpV0+W35fO+Pzz 1TbD6W+3Hvj+s1H8LkMXS2bTwkBfXjvjv8smra4992eR1P4Srud3cvSlmlfF1UR4Skh3H/vS ZbH48P1f0iJKLMUZiYZazEXFiQBTKjItAgQAAA== X-CMS-MailID: 20240515055733eucas1p2804d2fb5f5bf7d6adb460054f6e9f4d8 X-Msg-Generator: CA X-RootMTR: 20240515055733eucas1p2804d2fb5f5bf7d6adb460054f6e9f4d8 X-EPHeader: CA CMS-TYPE: 201P X-CMS-RootMailID: 20240515055733eucas1p2804d2fb5f5bf7d6adb460054f6e9f4d8 References: <20240515055719.32577-1-da.gomez@samsung.com> X-Stat-Signature: 5c4846hieihawphbunpnou4w34hrkjo4 X-Rspamd-Queue-Id: 2C2964000D X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1715752654-343610 X-HE-Meta: U2FsdGVkX1/LLs67RHXkfgMkKx/WI6tAugHDfJbSjNRfjj7PjcBjXjrnEZM2kKiXzaFTmYGtrzK/0rkexosj/x+Yyo585H0I6+6kjuz+oonlUmOUyS7Jaxa7/y+E/hGY2yOAHAqrUuP2Iifv4QVZed82AcEYV6pihqHG+HUD/OLaUBOiigvjsniGF4AIslppF5zIZFOTmbOGaHMKdwoWaUSBUGGn6Mk/4w16gX9BWiCsmhx8GHkuCOlzmt1c8u7SAe1hcN1GJDCxLD5e8K5RG8BPIGtWR600FZsNOw7QxBTn3musQpApnywy4C5uFg9viPxtG1F4n+Xc4E2mfOb3F8UfdWjvKEZ9z/7xn3NtDBm3Es/U/lt53uNOd6sCMmQjmbm9SYJaStq4aLDLhKgjlB7kpPvsZu780fvdQQITTWmSBknYPOU0DWsDuJwWDmQQdMJcop1/bY/7sTdZFpdVeiyugBqny5cvsxxSbg+orAEAirJ42eQ6JD7qxQds/1aFxEDMKq0FBvYAeNeVewLy5lFztDutSYgvqPA71TExJoJJG1liZxF3Gfs07ZEQ0/Xnn/c9OCGA5W3ncM5BkDt8QyJ4Vlsckhxni0t0LoN9wxHcnr/zfo2jmQYAj0Pfnh3OptXeWr8JulaAsHm2/T/v1onsU83TdMAv98IA9g75+WoMpy1LDjX1eU9u/4XUDi0BAQZ9fFfZVYG4MXWakhqJCNCdtQiWnIampHGGm4d9OqAd5F13QpZOKRaOBMOEPMFZCjNp35/2CFOXrbDGqvoLZnrEdHjIFo6spQDfw7mZ2p9b2i8k7s4rNKynjZs1qrm21ZE2wXxPFVL8k/PiGi4LCHbazUOAEdAmjovBrH/mwfhFdQ08NLXbv2fox4Kz4QPtQWJa0+GmzTTU2hH3fP7E3Zt5It+G8Ca2M4BFNGI+df2V89fnqcNVL8jBZU9Mm49xGO0O6axNeeBVHsb2vO+ E7lngCJV pG+ovyic0GE/xVCh4rCwUAF24zvqO2uUojudST0/YIWX0Q+ROcmt/S/79IyBE/vl+ukATrhDSMBNGO5yCyt3m+YRXZwkSpbK1fovxObe8FtK0XrnteeunOorXCvDl7vVsebS8BiNEbmR7MDvV70OW2NdUgURpVTK6V5hrLtYmLHe+hEyeTwgPu8ahTJDsDFWSOGmP4zzFlqCo0j4uR/2SPz5TT9CdjQC6lFOcoFQ2p3ShY1OLqZU4aTUV318QXkl77pmXqsRlzW2nur0R/FfJAJx7x3ORiq3OIzco 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: In the fallocate path with PUNCH_HOLE mode flag enabled, clear the uptodate flag for those blocks covered by the punch. Skip all partial blocks as they may still contain data. Signed-off-by: Daniel Gomez --- mm/shmem.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 72 insertions(+), 6 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index e06cb6438ef8..d5e6c8eba983 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -209,6 +209,28 @@ static void sfs_set_range_uptodate(struct folio *folio, spin_unlock_irqrestore(&sfs->state_lock, flags); } +static void sfs_clear_range_uptodate(struct folio *folio, + struct shmem_folio_state *sfs, size_t off, + size_t len) +{ + struct inode *inode = folio->mapping->host; + unsigned int first_blk, last_blk; + unsigned long flags; + + first_blk = DIV_ROUND_UP_ULL(off, 1 << inode->i_blkbits); + last_blk = DIV_ROUND_DOWN_ULL(off + len, 1 << inode->i_blkbits) - 1; + if (last_blk == UINT_MAX) + return; + + if (first_blk > last_blk) + return; + + spin_lock_irqsave(&sfs->state_lock, flags); + bitmap_clear(sfs->state, first_blk, last_blk - first_blk + 1); + folio_clear_uptodate(folio); + spin_unlock_irqrestore(&sfs->state_lock, flags); +} + static struct shmem_folio_state *sfs_alloc(struct inode *inode, struct folio *folio) { @@ -276,6 +298,19 @@ static void shmem_set_range_uptodate(struct folio *folio, size_t off, else folio_mark_uptodate(folio); } + +static void shmem_clear_range_uptodate(struct folio *folio, size_t off, + size_t len) +{ + struct shmem_folio_state *sfs = folio->private; + + if (sfs) + sfs_clear_range_uptodate(folio, sfs, off, len); + else + folio_clear_uptodate(folio); + +} + #ifdef CONFIG_TMPFS static unsigned long shmem_default_max_blocks(void) { @@ -1103,12 +1138,33 @@ static struct folio *shmem_get_partial_folio(struct inode *inode, pgoff_t index) return folio; } +static void shmem_clear(struct folio *folio, loff_t start, loff_t end, int mode) +{ + loff_t pos = folio_pos(folio); + unsigned int offset, length; + + if (!(mode & FALLOC_FL_PUNCH_HOLE) || !(folio_test_large(folio))) + return; + + if (pos < start) + offset = start - pos; + else + offset = 0; + length = folio_size(folio); + if (pos + length <= (u64)end) + length = length - offset; + else + length = end + 1 - pos - offset; + + shmem_clear_range_uptodate(folio, offset, length); +} + /* * Remove range of pages and swap entries from page cache, and free them. * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate. */ static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, - bool unfalloc) + bool unfalloc, int mode) { struct address_space *mapping = inode->i_mapping; struct shmem_inode_info *info = SHMEM_I(inode); @@ -1166,6 +1222,7 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, if (folio) { same_folio = lend < folio_pos(folio) + folio_size(folio); folio_mark_dirty(folio); + shmem_clear(folio, lstart, lend, mode); if (!truncate_inode_partial_folio(folio, lstart, lend)) { start = folio_next_index(folio); if (same_folio) @@ -1255,9 +1312,17 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, shmem_recalc_inode(inode, 0, -nr_swaps_freed); } +static void shmem_truncate_range_mode(struct inode *inode, loff_t lstart, + loff_t lend, int mode) +{ + shmem_undo_range(inode, lstart, lend, false, mode); + inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); + inode_inc_iversion(inode); +} + void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) { - shmem_undo_range(inode, lstart, lend, false); + shmem_undo_range(inode, lstart, lend, false, 0); inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); inode_inc_iversion(inode); } @@ -3342,7 +3407,7 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset, if ((u64)unmap_end > (u64)unmap_start) unmap_mapping_range(mapping, unmap_start, 1 + unmap_end - unmap_start, 0); - shmem_truncate_range(inode, offset, offset + len - 1); + shmem_truncate_range_mode(inode, offset, offset + len - 1, mode); /* No need to unmap again: hole-punching leaves COWed pages */ spin_lock(&inode->i_lock); @@ -3408,9 +3473,10 @@ static long shmem_fallocate(struct file *file, int mode, loff_t offset, info->fallocend = undo_fallocend; /* Remove the !uptodate folios we added */ if (index > start) { - shmem_undo_range(inode, - (loff_t)start << PAGE_SHIFT, - ((loff_t)index << PAGE_SHIFT) - 1, true); + shmem_undo_range( + inode, (loff_t)start << PAGE_SHIFT, + ((loff_t)index << PAGE_SHIFT) - 1, true, + 0); } goto undone; }