From patchwork Wed Dec 18 15:46:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kemeng Shi X-Patchwork-Id: 13913109 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 C2DB3E77183 for ; Wed, 18 Dec 2024 06:48:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 91FB66B0092; Wed, 18 Dec 2024 01:48:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 76A3F6B009A; Wed, 18 Dec 2024 01:48:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 439AA6B0098; Wed, 18 Dec 2024 01:48:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id E8D9E6B0096 for ; Wed, 18 Dec 2024 01:48:17 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id A1AC1160B31 for ; Wed, 18 Dec 2024 06:48:17 +0000 (UTC) X-FDA: 82907149164.19.ED6601F Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) by imf25.hostedemail.com (Postfix) with ESMTP id CDEC7A0002 for ; Wed, 18 Dec 2024 06:47:50 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=none; spf=pass (imf25.hostedemail.com: domain of shikemeng@huaweicloud.com designates 45.249.212.51 as permitted sender) smtp.mailfrom=shikemeng@huaweicloud.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734504475; 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; bh=iu0wCSvHGOTioJPqw7DFuRYH/gM4yKHJPZZS9oXegso=; b=sPCCxnvttyrIwwfThFk0vaRkknYBzwHH9olBRqm+DzL6ikoC9IjRpF1oJPAwsQcOFQKGnb FkVZIWWzsGuas0g7jtO3Eko26M+cvpvCaBokv69/bflNnATIffiwfBkWlVKJ1Thw50Y3fa g5ZBmOKqEhkIhSYsRMMTSZ6IoeJNu6g= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734504475; a=rsa-sha256; cv=none; b=W81WGTj45qCbxX+8L42jKJJQyD07srk2SPsdHDuLLFtpz0DGM9Moj9lPhwRJyb2Nr/F0Sc 8yUb2AliBTHdOucThY6NjvzUQmVuELOqyKeeJrR1kvoieCuVw6fN50U5sdz8qUDezdRXY2 njgXZwoTji7/0B7plpXH1e+exIswyX4= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=none; spf=pass (imf25.hostedemail.com: domain of shikemeng@huaweicloud.com designates 45.249.212.51 as permitted sender) smtp.mailfrom=shikemeng@huaweicloud.com; dmarc=none Received: from mail.maildlp.com (unknown [172.19.93.142]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4YCklF4YPdz4f3jqP for ; Wed, 18 Dec 2024 14:47:53 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id 1AB211A018D for ; Wed, 18 Dec 2024 14:48:08 +0800 (CST) Received: from huaweicloud.com (unknown [10.175.101.6]) by APP1 (Coremail) with SMTP id cCh0CgA33a4mcGJnxKU7Ew--.4083S3; Wed, 18 Dec 2024 14:48:08 +0800 (CST) From: Kemeng Shi To: akpm@linux-foundation.org, willy@infradead.org Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-nfs@vger.kernel.org Subject: [PATCH v4 1/5] Xarray: Do not return sibling entries from xas_find_marked() Date: Wed, 18 Dec 2024 23:46:09 +0800 Message-Id: <20241218154613.58754-2-shikemeng@huaweicloud.com> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20241218154613.58754-1-shikemeng@huaweicloud.com> References: <20241218154613.58754-1-shikemeng@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: cCh0CgA33a4mcGJnxKU7Ew--.4083S3 X-Coremail-Antispam: 1UD129KBjvJXoWxZFyfJw18Wr15Cw43Kr15Arb_yoWrCF45pF W8GFykKr4xtr4UArykAayUXFWru34UXFWayFWrG34av3Z8J3WjkF1j9FyjqF9rZrWDZF4f ZF40v3yUZ3WDC3DanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPYb4IE77IF4wAFF20E14v26r4j6ryUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M280x2IEY4vEnII2IxkI6r1a6r45M2 8IrcIa0xkI8VA2jI8067AKxVWUGwA2048vs2IY020Ec7CjxVAFwI0_JFI_Gr1l8cAvFVAK 0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJwA2z4 x0Y4vE2Ix0cI8IcVCY1x0267AKxVWxJVW8Jr1l84ACjcxK6I8E87Iv67AKxVW0oVCq3wA2 z4x0Y4vEx4A2jsIEc7CjxVAFwI0_GcCE3s1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4 xG64xvF2IEw4CE5I8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v2 6r1j6r4UMcvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvY0x0EwIxGrwCY1x0262kKe7AKxV WUAVWUtwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E 14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_JF0_Jw1lIx kGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxVAF wI0_Gr0_Cr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r 4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf9x07jfAwsU UUUU= X-CM-SenderInfo: 5vklyvpphqwq5kxd4v5lfo033gof0z/ X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: CDEC7A0002 X-Stat-Signature: ds7pwf83pw13pqkxrpctt53tgk31a8hm X-Rspam-User: X-HE-Tag: 1734504470-978643 X-HE-Meta: U2FsdGVkX1/p+trJ8H/0YEj0hbuOb2qX8bxjski3tRkHtu0oBv6xTUMRM5HuUdlcmqbaUz4txOnl8bnMdD0ObA2TK8eImcfWMziAh9mcGmPNNVhzmbz+xGrShFJYqQwTs+9wyfjN09wFqRWYik5eFMhzInGMtomPu0ZRgGhAUZ92A0UJ0UCRYs54XRhiIXKZlpqqfZKXTOXuypeldHk5ZIiQWXVjhZkTxkq0sF9sJ6mwFtx9LO5aqgsIKU2wiLpenG6ebxmQSN+Y00tE3N+odBct5RuQwEDTX7fHIas5TaGZsEepxI2DXKb0n5wOh8ww8xddZ79e9QeTvStVnwT06YI5ZOWvilKgAkHahZabHCUnrwNMzSvIOcfXgdhWH4QQZARj82n42jdjA9xe8f6udqoHenvQzdxr4HIlpqH81BMNozkETHXB2cAlV2hYra9LYObt8aYjcDy66KurFu7Sei56KxAAvBiszTBoX8jisrYWXs52wlFjF/sFLpSM6TR0t7A+Td/Y8xwHuC1zHL+/kxFMqmo9qxwGYzxbJwfhZLw5a8SSgJejhXxhmeRn9+rzfK6DDvozp2s+N4k+kKeEQ1/zkWEy5B5eGuh5NCZDKZ2olNfB5+n/RS0IqfTBu0PiIJmy83B1wA3HoQlx6Xu/u++qagJOxxZ4PXgFkfp3ueXu6gqX8zYSkBt0FLyqh4rO+E/4dPqlOogWvhyczQM8kXXuJOa969UYFRxCECF3Z65LJiwAfg4dfNbTwvZ1zmRh1yegkKQ/7QrqZibkYcIRV5Y+2hGRU5nh49SZqEpgSmqppJg96blqTD6Ts+YSDX3dButGf0ewGAisBCUWq7kDy61exz/nFx/m7hW7vHalc93Y6wAer6KVqzwkOvuj+9t7A+glQxE6CXmDGbAxALVt04iZSK3DkGfmf0ugvr65N6PEMFWpzcnFefSUPs9LtfmbFl4vSRRrbU3tK7qkWG5 5A3uJ+b8 +MFPcCEAQx4SR0v7NxOoe/6inabSdPrxvMI1xUAuRz9f2BeLNfZ/ehDHmocAz/DWC7WtquTa5751elG9AV8rt4M4oNH765p/CYpiXVm/H1ObArC4e8ajz47Rp8YyAJh8xVkYU9PEz/LeO4izHUpcjHflbfp5dWoXdhBQSca4kCtIxLvwnzt7a/puvABtgEO7B78udeF51XrDA+/8BzfV/xpU9Qw== 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: Similar to issue fixed in commit cbc02854331ed ("XArray: Do not return sibling entries from xa_load()"), we may return sibling entries from xas_find_marked as following: Thread A: Thread B: xa_store_range(xa, entry, 6, 7, gfp); xa_set_mark(xa, 6, mark) XA_STATE(xas, xa, 6); xas_find_marked(&xas, 7, mark); offset = xas_find_chunk(xas, advance, mark); [offset is 6 which points to a valid entry] xa_store_range(xa, entry, 4, 7, gfp); entry = xa_entry(xa, node, 6); [entry is a sibling of 4] if (!xa_is_node(entry)) return entry; Skip sibling entry like xas_find() does to protect caller from seeing sibling entry from xas_find_marked() or caller may use sibling entry as a valid entry and crash the kernel. Besides, load_race() test is modified to catch mentioned issue and modified load_race() only passes after this fix is merged. Here is an example how this bug could be triggerred in theory in nfs which enables large folio in mapping: Let's take a look at involved racer: 1. How pages could be created and dirtied in nfs. write ksys_write vfs_write new_sync_write nfs_file_write generic_perform_write nfs_write_begin fgf_set_order __filemap_get_folio nfs_write_end nfs_update_folio nfs_writepage_setup nfs_mark_request_dirty filemap_dirty_folio __folio_mark_dirty __xa_set_mark 2. How dirty pages could be deleted in nfs. ioctl do_vfs_ioctl file_ioctl ioctl_preallocate vfs_fallocate nfs42_fallocate nfs42_proc_deallocate truncate_pagecache_range truncate_inode_pages_range truncate_inode_folio filemap_remove_folio page_cache_delete xas_store(&xas, NULL); 3. How dirty pages could be lockless searched sync_file_range ksys_sync_file_range __filemap_fdatawrite_range filemap_fdatawrite_wbc do_writepages writeback_use_writepage writeback_iter writeback_get_folio filemap_get_folios_tag find_get_entry folio = xas_find_marked() folio_try_get(folio) In theory, kernel will crash as following: 1.Create 2.Search 3.Delete /* write page 2,3 */ write ... nfs_write_begin fgf_set_order __filemap_get_folio ... /* index = 2, order = 1 */ xa_store(&xas, folio) nfs_write_end ... __folio_mark_dirty /* sync page 2 and page 3 */ sync_file_range ... find_get_entry folio = xas_find_marked() /* offset will be 2 */ offset = xas_find_chunk() /* delete page 2 and page 3 */ ioctl ... xas_store(&xas, NULL); /* write page 0-3 */ write ... nfs_write_begin fgf_set_order __filemap_get_folio ... /* index = 0, order = 2 */ xa_store(&xas, folio) nfs_write_end ... __folio_mark_dirty /* get sibling entry from offset 2 */ entry = xa_entry(.., 2) /* use sibling entry as folio and crash kernel */ folio_try_get(folio) Signed-off-by: Kemeng Shi --- lib/xarray.c | 2 ++ tools/testing/radix-tree/multiorder.c | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/lib/xarray.c b/lib/xarray.c index 32d4bac8c94c..fa87949719a0 100644 --- a/lib/xarray.c +++ b/lib/xarray.c @@ -1382,6 +1382,8 @@ void *xas_find_marked(struct xa_state *xas, unsigned long max, xa_mark_t mark) entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset); if (!entry && !(xa_track_free(xas->xa) && mark == XA_FREE_MARK)) continue; + if (xa_is_sibling(entry)) + continue; if (!xa_is_node(entry)) return entry; xas->xa_node = xa_to_node(entry); diff --git a/tools/testing/radix-tree/multiorder.c b/tools/testing/radix-tree/multiorder.c index cffaf2245d4f..eaff1b036989 100644 --- a/tools/testing/radix-tree/multiorder.c +++ b/tools/testing/radix-tree/multiorder.c @@ -227,6 +227,7 @@ static void *load_creator(void *ptr) unsigned long index = (3 << RADIX_TREE_MAP_SHIFT) - (1 << order); item_insert_order(tree, index, order); + xa_set_mark(tree, index, XA_MARK_1); item_delete_rcu(tree, index); } } @@ -242,8 +243,11 @@ static void *load_worker(void *ptr) rcu_register_thread(); while (!stop_iteration) { + unsigned long find_index = (2 << RADIX_TREE_MAP_SHIFT) + 1; struct item *item = xa_load(ptr, index); assert(!xa_is_internal(item)); + item = xa_find(ptr, &find_index, index, XA_MARK_1); + assert(!xa_is_internal(item)); } rcu_unregister_thread();