From patchwork Fri Feb 9 14:29:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Gomez X-Patchwork-Id: 13551419 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 B023FC48297 for ; Fri, 9 Feb 2024 14:29:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5FE706B0088; Fri, 9 Feb 2024 09:29:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 570D06B0099; Fri, 9 Feb 2024 09:29:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 283A66B0093; Fri, 9 Feb 2024 09:29:13 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id E850D6B0099 for ; Fri, 9 Feb 2024 09:29:12 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id C60A81405C3 for ; Fri, 9 Feb 2024 14:29:12 +0000 (UTC) X-FDA: 81772497744.22.FA19C9E Received: from mailout2.w1.samsung.com (mailout2.w1.samsung.com [210.118.77.12]) by imf04.hostedemail.com (Postfix) with ESMTP id C6EA44000B for ; Fri, 9 Feb 2024 14:29:09 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=samsung.com header.s=mail20170921 header.b=tjFYJKAe; dmarc=pass (policy=none) header.from=samsung.com; spf=pass (imf04.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=1707488950; 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=a88W1Bbg0jxB2ngLMkFDtFwflxkCFGav4/gOzgelMLM=; b=QNHF/09zWsOKuEIqb6UhJIXvSWHe+hBYcIIqjvBx805/GGkio60W7lw0aaUkkNjxvK8M2X Gbuww3rFXOPVLPD5Gl8mVrcBV2jr6Cj8wl8TgX8/17gqXUFV3HgrXcBFqe1/q87PvmuEDP Irh7/+C5Owc+nshfF5c/1mlWg7QEvS0= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=samsung.com header.s=mail20170921 header.b=tjFYJKAe; dmarc=pass (policy=none) header.from=samsung.com; spf=pass (imf04.hostedemail.com: domain of da.gomez@samsung.com designates 210.118.77.12 as permitted sender) smtp.mailfrom=da.gomez@samsung.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707488950; a=rsa-sha256; cv=none; b=PBLEWlAmwWSPAHIqGmnNkV5Zg0CSpNoKE+QMlvZwpiNhDxE03aQe5j//Fgy4DuV//xLAS4 Q5yCDAhTnfrjlwn3OrHw/V6j0dYFa1ElTA743Ti+hfX/9KY/rrfbnCFOYlkoMcNiiLeLF9 LKGU7n2JeYva1i3pnMWQ1nN1M8ZGR/M= Received: from eucas1p2.samsung.com (unknown [182.198.249.207]) by mailout2.w1.samsung.com (KnoxPortal) with ESMTP id 20240209142908euoutp02c0dd40786008434e64adb0841c12100b~yOCbjCirx2144221442euoutp02k for ; Fri, 9 Feb 2024 14:29:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout2.w1.samsung.com 20240209142908euoutp02c0dd40786008434e64adb0841c12100b~yOCbjCirx2144221442euoutp02k DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1707488948; bh=a88W1Bbg0jxB2ngLMkFDtFwflxkCFGav4/gOzgelMLM=; h=From:To:CC:Subject:Date:In-Reply-To:References:From; b=tjFYJKAebkiJIwpM50vhlXOLbPn0zREBKWRH/LD5cD5jan0ZiYFAg0HXrgf2WLmT+ 4J0RCX0UfX9sCyYkie2JirnYLxF74XihbO/qQ6de9Qxb5XZ4yxE+zw+c0hn6j4HAxs ASETTTjD5AfGyHF8E0CxWzVIEW020EKZorD5gM1E= Received: from eusmges1new.samsung.com (unknown [203.254.199.242]) by eucas1p1.samsung.com (KnoxPortal) with ESMTP id 20240209142908eucas1p16a6b31632e8d9865f952e959c973c8e5~yOCbPMlkz0281802818eucas1p1s; Fri, 9 Feb 2024 14:29:08 +0000 (GMT) Received: from eucas1p1.samsung.com ( [182.198.249.206]) by eusmges1new.samsung.com (EUCPMTA) with SMTP id B6.0E.09539.3B636C56; Fri, 9 Feb 2024 14:29:07 +0000 (GMT) Received: from eusmtrp1.samsung.com (unknown [182.198.249.138]) by eucas1p2.samsung.com (KnoxPortal) with ESMTPA id 20240209142907eucas1p2c61ae37b2a1ca2caeccc48b2169226f2~yOCatdJaJ2253522535eucas1p2p; Fri, 9 Feb 2024 14:29:07 +0000 (GMT) Received: from eusmgms2.samsung.com (unknown [182.198.249.180]) by eusmtrp1.samsung.com (KnoxPortal) with ESMTP id 20240209142907eusmtrp11d733555d1468f5959c30cb5a5a11e05~yOCasoSCp0528405284eusmtrp1H; Fri, 9 Feb 2024 14:29:07 +0000 (GMT) X-AuditID: cbfec7f2-52bff70000002543-1c-65c636b3da7b Received: from eusmtip1.samsung.com ( [203.254.199.221]) by eusmgms2.samsung.com (EUCPMTA) with SMTP id F5.B3.10702.3B636C56; Fri, 9 Feb 2024 14:29:07 +0000 (GMT) Received: from CAMSVWEXC02.scsc.local (unknown [106.1.227.72]) by eusmtip1.samsung.com (KnoxPortal) with ESMTPA id 20240209142907eusmtip1170d8fecd9e85f89a441b27f26df0489~yOCaisoha0140701407eusmtip1F; Fri, 9 Feb 2024 14:29:07 +0000 (GMT) Received: from CAMSVWEXC02.scsc.local (2002:6a01:e348::6a01:e348) by CAMSVWEXC02.scsc.local (2002:6a01:e348::6a01:e348) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Fri, 9 Feb 2024 14:29:06 +0000 Received: from CAMSVWEXC02.scsc.local ([::1]) by CAMSVWEXC02.scsc.local ([fe80::3c08:6c51:fa0a:6384%13]) with mapi id 15.00.1497.012; Fri, 9 Feb 2024 14:29:06 +0000 From: Daniel Gomez To: "viro@zeniv.linux.org.uk" , "brauner@kernel.org" , "jack@suse.cz" , "hughd@google.com" , "akpm@linux-foundation.org" CC: "dagmcr@gmail.com" , "linux-fsdevel@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "linux-mm@kvack.org" , "willy@infradead.org" , "hch@infradead.org" , "mcgrof@kernel.org" , Pankaj Raghav , "gost.dev@samsung.com" , Daniel Gomez Subject: [RFC PATCH 8/9] shmem: clear uptodate blocks after PUNCH_HOLE Thread-Topic: [RFC PATCH 8/9] shmem: clear uptodate blocks after PUNCH_HOLE Thread-Index: AQHaW2RVqhumUoqlFUi8PRO0fiHHFQ== Date: Fri, 9 Feb 2024 14:29:04 +0000 Message-ID: <20240209142901.126894-9-da.gomez@samsung.com> In-Reply-To: <20240209142901.126894-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.110.32.67] MIME-Version: 1.0 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrJKsWRmVeSWpSXmKPExsWy7djPc7qbzY6lGnhYzFm/hs3i9eFPjBZn +36zWZyesIjJ4umnPhaL2dObmSz27D3JYnF51xw2i3tr/rNa3JjwlNHi/N/jrBa/f8xhc+Dx 2DnrLrvHgk2lHptXaHlsWtXJ5rHp0yR2jxMzfrN4nFlwhN3j8yY5j01P3jIFcEZx2aSk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S57mXR iX0xRy7dUrsq0Cq02mSuejtbv6tLf++laUosxRmJhlrMRcWJABo8BnvZAwAA X-CMS-MailID: 20240209142907eucas1p2c61ae37b2a1ca2caeccc48b2169226f2 X-Msg-Generator: CA X-RootMTR: 20240209142907eucas1p2c61ae37b2a1ca2caeccc48b2169226f2 X-EPHeader: CA CMS-TYPE: 201P X-CMS-RootMailID: 20240209142907eucas1p2c61ae37b2a1ca2caeccc48b2169226f2 References: <20240209142901.126894-1-da.gomez@samsung.com> X-Rspam-User: X-Stat-Signature: 7nb6qw3pts49ujrbexiyhe1oh6eyt6cw X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: C6EA44000B X-HE-Tag: 1707488949-991146 X-HE-Meta: U2FsdGVkX19cVA2QrvIM83HxhId3IA4F4k28autTwv5F73U+qWBUS3AQmUh13mKXVTMG0m5Mj6h7I7BE3sdwDoXhz2nGTLuzAwPjlmgsdtG2T0DzaJg7f9hJDnzdmicpCLCNmOqHQCqNAAyPKiF43JzqL1trxsKEPQz7edfPHxKbUuMdQCgAzuPHOpC1iVwfvxMaGOcqnzQFsjP+wo0/qH7dWGQ46s5TF2Zx85np4uMBsgCV2kufgazf2KK6259f00S+hFf/wT77NkcAMOV65ml1sZd8OdOVHANOf4PsQ74F3OXldl8MsKDjtPCeSub1egW9LpVcVACymZNAP3zT+so0sko3+ZgQDE3FPeaCHp3tVLoPR9t2MQ5/fh7QtbxkvIB5CF4b3x+kszNMhfA4JtueyVG4dRGRvpwvCg4Vy8hLxT/He1rFj3yt1fvWQoL1tmv/hASNPwRrnoC8uqrekaRtYeGu8suvHKzthfP/T4xCb0vDN7CT8MV+yjiXXZQX4GAIXk5eQtTlE4K/cOyxLdSBTiUJzWGB5wM2cSn8Ke1sOeNcAGBkVVNrd4sENcdmQpryNSnq/1rVFRXvlaHhOgUT/gZyvq6nSoEzhS6p2I/EC7Jdo449EKWG/uPvDtHQ/oeScR7kQ27DC/0jcmKYHQ/ErOeOtGOo4GpCrIr9sxUMGUxwMSfRkUJDGDmUfy/BdWnOdEPO81cz0wLL3Gtvl1gHfyyoF2OnuYHVhrFvMToBA+AweIi4uJYvQtv2OaY0HEz9CuH6aGUX6zBhRwSYPvEYnI5sOhpwj29PttyeuVKVlWg44OXomZ30c2s/295TXP8ui51SJq8YNJBHFfBmKcfq9TEAr6hECuxrOl8gBhT7/KCY5m+4woh/6NUYmscmTGBN9+4wFMV6hCFe1E2BINKourEQLTnH4/G4mzbg1AuTZlIVJ8QGo8zVf2aLu51dFUnOXUi6kGkp8clwavC XodVacww RDVmoljURV6V76al2+DmjMs8kQl4PWV06Pm9cV+EIY5iS8Nfymdbon6LVh5lfJAtVsi9dPGaLwCFFi+XdYfGg8HlwwKky3yGEqaiPHWtRaRz2ShnP/EotJ7VJy+DoAKCmFRQW//kDsV2kbj/BWDSyTusfwgNnJ2NlYq71/zlT87Qkj7TBe/9na2581/kXcJogMtIoAOULO44yHjv8AJW6JMmeig== 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 2d2eeb40f19b..2157a87b2e4b 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); } @@ -3315,7 +3380,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); @@ -3381,9 +3446,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; }