From patchwork Thu Apr 4 13:36:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Huacai Chen X-Patchwork-Id: 13617865 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 86409CD1292 for ; Thu, 4 Apr 2024 13:37:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 201C46B0088; Thu, 4 Apr 2024 09:37:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1B20D6B0089; Thu, 4 Apr 2024 09:37:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0A0C46B008A; Thu, 4 Apr 2024 09:37:29 -0400 (EDT) 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 E3DE26B0088 for ; Thu, 4 Apr 2024 09:37:28 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 70ACE160E7B for ; Thu, 4 Apr 2024 13:37:28 +0000 (UTC) X-FDA: 81971951376.19.AAB7CCB Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf21.hostedemail.com (Postfix) with ESMTP id 9A2671C0026 for ; Thu, 4 Apr 2024 13:37:26 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=none; spf=pass (imf21.hostedemail.com: domain of "SRS0=Mf8M=LJ=loongson.cn=chenhuacai@kernel.org" designates 145.40.73.55 as permitted sender) smtp.mailfrom="SRS0=Mf8M=LJ=loongson.cn=chenhuacai@kernel.org"; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1712237847; 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=pTgEUmRV29boQvDiaGRCgV4iQjAkeKOWdTafpyI81Kg=; b=Y5AnCsUkfgueJRuRQ1jYIAbXsIODMHGVS/6E8K4fVPFwHObFkzDKCJUGAyTrKNVVocXW/m 1HP+Vxu8q0qckfVygI6BIeuTddA4atH12Rd2vBJfLkaoCdrvkS5BS1YdJJwiskC0itzn5q pacNXuyFMX3KGMXYXDtSDRHboBJxOto= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712237847; a=rsa-sha256; cv=none; b=e970L2Z/Y5IrS4ooCfIPecF2Wo77vDZKbC+4Wdhwv/aV5e3HLu4J8/gpLIKrYQ8P71JaVN mkZSDFm/vy5TglINeHGA1XYuYcpslxzthWZZikBfcG7EqQlRkXNZZwSGDQEBXyI49p3i97 H99rrkbAFrFSSSEWlmCqRxKlfdsUYLg= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=none; spf=pass (imf21.hostedemail.com: domain of "SRS0=Mf8M=LJ=loongson.cn=chenhuacai@kernel.org" designates 145.40.73.55 as permitted sender) smtp.mailfrom="SRS0=Mf8M=LJ=loongson.cn=chenhuacai@kernel.org"; dmarc=none Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id A8A04CE31B4; Thu, 4 Apr 2024 13:37:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1989BC433F1; Thu, 4 Apr 2024 13:37:19 +0000 (UTC) From: Huacai Chen To: Andrew Morton , Huacai Chen Cc: loongarch@lists.linux.dev, linux-mm@kvack.org, Xuefeng Li , Guo Ren , Xuerui Wang , Jiaxun Yang , linux-kernel@vger.kernel.org, loongson-kernel@lists.loongnix.cn, Huacai Chen , Guenter Roeck Subject: [PATCH 3/3] LoongArch: Make virt_addr_valid()/__virt_addr_valid() work with KFENCE Date: Thu, 4 Apr 2024 21:36:36 +0800 Message-ID: <20240404133642.971583-4-chenhuacai@loongson.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240404133642.971583-1-chenhuacai@loongson.cn> References: <20240404133642.971583-1-chenhuacai@loongson.cn> MIME-Version: 1.0 X-Rspamd-Queue-Id: 9A2671C0026 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 7837y3yu4zrf4gbsfy6h8995ch7q8ii1 X-HE-Tag: 1712237846-302815 X-HE-Meta: U2FsdGVkX19I3J8NqF6b0ta4swsYZPKoteACwM045dkB5+sOe4hFRe2rOY8GHq0WgDd8A96jBW36NgcF9ThQMwzgCZPSwFBDhb9CMiPkz+79YX1Izkp3PmDbjC8G/spOb84NfpM0/BX90KrKYO5FkiGiVll7CDODrivvmZj/WLYHRPpkkzrji91l54WGPaHzyU2L12yHAfF/OO5tCmr+9rdyqj9ufSTIHBHpdpn4ncE06KzZEMOd/PfIIAmpA09CTcsej17RsFWkfh1+0ZHP6cZ4bHrCtQ7DFx0zEfZO/ZmIvXo+YVePYKnYTrz9ddvaVA9BEOxFKj1FCssstv327iqDkjbKyhTC0kV9utcRqrGN17atl9HoIQ9fATipKwZwpt62Dp9S6YW1o9Ya7s/55Yz9sQpw3uGxWlYcAQ4aKJ5VzacYfSZ3KaRDjyTDAR/3BLx/3nXpl6U7dpA5xYUOY8SXsgNT5yeRlLNIKFP8vZTk8WG+V1wmiqM1gk4eI++1swWcfNxdfPPYGYwW0zi9i/NRF+lfXBMZqrnaFVZt5ZtFlMGABLWtoS9UIj1knsLY2tKBODLDxHhAWwvYTjeEGwHkE1iPKHUXdzlMo877Iv5+kx2aR4Ce1wXuDsQ4GOiWc2Z/WybPEdGYUZyM6dM0ucRwG1k+Kn2ZfcDw94+hsi2Xcc8MB5yx821jKSkNK5xuYAG4pf3kxBHl6Tv3VwKWpOGKKA0Z4vOVqLQg/45XC0srJ+IAUCwIeVZsyncV6ZvaZYUL5hHRHB+Zw0zS6Gx+BcxcQyYqUxFwVbHawDO8apoa8PLuuQHHLzC5RyL2LFEqdNDO3YI4ho9Ci50U+dLD5dh+h8ny57m3r92bJQK5IHJuAnvP8erhyeBP0RVhHg+e9/9nKeaLfSck8z+tOyugDcwB/boFpawxMfkEPA8KXVJyipegNCplZ2L5/RnhcSBVh1SodonaEm3rEJ4U068 46tMQHrW MLsuO1Qsm6VC1XIU54QzbckUJA4wbeJ+9Dzbd/lsSF38sZYmfLFLuQxZfddg+LbnawWsO5uD8GIGz5wO0ZRCcQAPWLGQKWw8KIidZIrvJtuOwx6VAKTFVRi1YwA== 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: When enabling both CONFIG_KFENCE and CONFIG_DEBUG_SG, I get the following backtraces when running LongArch kernels. [ 2.496257] kernel BUG at include/linux/scatterlist.h:187! ... [ 2.501925] Call Trace: [ 2.501950] [<9000000004ad59c4>] sg_init_one+0xac/0xc0 [ 2.502204] [<9000000004a438f8>] do_test_kpp+0x278/0x6e4 [ 2.502353] [<9000000004a43dd4>] alg_test_kpp+0x70/0xf4 [ 2.502494] [<9000000004a41b48>] alg_test+0x128/0x690 [ 2.502631] [<9000000004a3d898>] cryptomgr_test+0x20/0x40 [ 2.502775] [<90000000041b4508>] kthread+0x138/0x158 [ 2.502912] [<9000000004161c48>] ret_from_kernel_thread+0xc/0xa4 The backtrace is always similar but not exactly the same. It is always triggered from cryptomgr_test, but not always from the same test. Analysis shows that with CONFIG_KFENCE active, the address returned from kmalloc() and friends is not always below vm_map_base. It is allocated by kfence_alloc() which at least sometimes seems to get its memory from an address space above vm_map_base. This causes __virt_addr_valid() to return false for the affected objects. Let __virt_addr_valid() return 1 for kfence pool addresses, this make virt_addr_valid()/__virt_addr_valid() work with KFENCE. Reported-by: Guenter Roeck Suggested-by: Guenter Roeck Signed-off-by: Huacai Chen --- arch/loongarch/mm/mmap.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/loongarch/mm/mmap.c b/arch/loongarch/mm/mmap.c index a9630a81b38a..89af7c12e8c0 100644 --- a/arch/loongarch/mm/mmap.c +++ b/arch/loongarch/mm/mmap.c @@ -4,6 +4,7 @@ */ #include #include +#include #include #include #include @@ -111,6 +112,9 @@ int __virt_addr_valid(volatile void *kaddr) { unsigned long vaddr = (unsigned long)kaddr; + if (is_kfence_address((void *)kaddr)) + return 1; + if ((vaddr < PAGE_OFFSET) || (vaddr >= vm_map_base)) return 0;