From mboxrd@z Thu Jan 1 00:00:00 1970 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 45926E7717F for ; Thu, 12 Dec 2024 06:48:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CA0EE6B0083; Thu, 12 Dec 2024 01:48:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C519A6B0085; Thu, 12 Dec 2024 01:48:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B18BC6B0088; Thu, 12 Dec 2024 01:48:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 91BDF6B0083 for ; Thu, 12 Dec 2024 01:48:54 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 2DAEF141688 for ; Thu, 12 Dec 2024 06:48:54 +0000 (UTC) X-FDA: 82885378464.07.4487224 Received: from mail-qt1-f175.google.com (mail-qt1-f175.google.com [209.85.160.175]) by imf21.hostedemail.com (Postfix) with ESMTP id 711961C0010 for ; Thu, 12 Dec 2024 06:48:06 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=PqX3C5aH; spf=pass (imf21.hostedemail.com: domain of surenb@google.com designates 209.85.160.175 as permitted sender) smtp.mailfrom=surenb@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1733986116; 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=kkJY3geoX0M1zaTua7B16V9O+RTw/DvF3frv9xA0mIU=; b=ZsPi2NeXM3DAwny+21MnsmgCaE7L9Ox2/DP7z/czR38kUTzx4NJYSWQ6U9KGR0J/iWaaL7 hvS/twnugnfnRIuvGPZr0fjkkYPyhGJ4Cwyxb6/dxm44IEVnBxsF5L7XHsnteLf7p2B02W WMA3TnjuFhjcInvbB/KGQG6QhQr5pQU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733986116; a=rsa-sha256; cv=none; b=q0z+VspHqmC9wNKd2O1rjbK87rbQCYQmWFGqXIbjOP/k/wOvYGXIeuDx4Ywvi0keMRyc8h w8vbbVRgk5nSZxodWB6F+9dJCLzKpTlmeXDaKN/jujM6XcG7rb92yapGJZb+pXfyu1ALbj 7fTblDiSPNZxklrNl9mAXDVRloEWBWM= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=PqX3C5aH; spf=pass (imf21.hostedemail.com: domain of surenb@google.com designates 209.85.160.175 as permitted sender) smtp.mailfrom=surenb@google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-qt1-f175.google.com with SMTP id d75a77b69052e-4675936f333so108291cf.0 for ; Wed, 11 Dec 2024 22:48:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733986131; x=1734590931; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=kkJY3geoX0M1zaTua7B16V9O+RTw/DvF3frv9xA0mIU=; b=PqX3C5aHUZC3KDLPka9rnC9aWrOP6B/L2OLnfoxIORY1zc9UPDEJtj56CKi9werggc DoyS5k5TUg28NQ0Kzlu9FU/1PjcWVyYlqRWRHnYPylbwVSJLKD9ESi2a+wazQSpMHeWw DRmduhP8VinjOfoT+7rtHPywE7zH8/BuGuu77SUIEy2jv7X6YP0rnr0EeisWprF0sgtR tSjog0CMAa/wcgU915T4hN58BsQz82R4kEhJQUFzHReEEnZhRB1IubEVFwh3k0KqOZEM rVcYiIdfkyJsK3tWiRKSgNsSvo69lK06a/QlEtAAp5UQ5zLAgHRi5LJtSjJZcyB5yGGx iY/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733986131; x=1734590931; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kkJY3geoX0M1zaTua7B16V9O+RTw/DvF3frv9xA0mIU=; b=JkDqrCDrP6MaBDO1xZbCF33ky6e6g1iAuZBsBwqVDD4u8SyCqwfkwNgNjD+c1FSPHB dr2PYGiDcKoJeI3e75xFXQHDhHNKZbS0Grwpe+iT1yPnEhy/+7TbdbTUnzDmDO5ttzuM 2DU7vJj9oXCtIo7/i06GhNp5B4riLcvVN6i70C4rM23z+hMaDX7x1WxwOKU5j7bs8Ah4 y17fcCJzgwQAbV2SrcxWGgSGyIYDTz9om5J5iYT01r3amB/axVppKcQCQd1BofM3YhJe k+3EVuJ9MiUQ4/l4AbyALpzpLvJhMFHa/PyF4p84vFabtBGpJPxEkyCb8SQkqIcvOmEt rf6g== X-Forwarded-Encrypted: i=1; AJvYcCW3Hj7ZXTG/op3CWs8M+Fpc1BzRDY3p9edQj7Mfq3Hq7KwsDKu5rgzVZK+RSl7lkw6xBjGebKVbyg==@kvack.org X-Gm-Message-State: AOJu0YyePCyM3Dx0YO2MqD0o7OVfl9sgln83+Rs2mYWn03p1XfV5MBVr WEfMiyM4QPtfpbTKgedmKH6oBlo6XqPApZrtShG4BaORxB8nQ3SJ8Q+0vbu/n86Gou8iH3oAs5k ZeKxQjNuPzKP9yH+TKg7lqbNig+YSx8N7kI6b X-Gm-Gg: ASbGncsf6GIZT428SmcTsSjOkOInx1cPJcAquiIw4uV94EXjV+2GZGxc7We3tdNDRl0 yyxZPiXYeLWx9+SGCH+X1omcp7+2+R+7udg0fgdrSmLY4Vi4WoW2WgqNUxI/oeXJ8F1Ts X-Google-Smtp-Source: AGHT+IF7OcsWjiCJWyQIZu4uDTQO9WOQ1Bgwd6KubdmTsqssR+D0yuVSAOf6N6OT40WJlIXDufXXmkYiDAqbEh/ZMkY= X-Received: by 2002:a05:622a:4c16:b0:467:8416:d99e with SMTP id d75a77b69052e-46798201583mr1668951cf.21.1733986131121; Wed, 11 Dec 2024 22:48:51 -0800 (PST) MIME-Version: 1.0 References: <20241212013723.113408-1-hao.ge@linux.dev> In-Reply-To: <20241212013723.113408-1-hao.ge@linux.dev> From: Suren Baghdasaryan Date: Wed, 11 Dec 2024 22:48:40 -0800 Message-ID: Subject: Re: [PATCH v4] mm/alloc_tag: Fix panic when CONFIG_KASAN enabled and CONFIG_KASAN_VMALLOC not enabled To: Hao Ge Cc: kent.overstreet@linux.dev, akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Hao Ge , Ben Greear Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 711961C0010 X-Stat-Signature: yb8bsrrfqgczz11y76dya9y8w59t7fjm X-Rspam-User: X-HE-Tag: 1733986086-692959 X-HE-Meta: U2FsdGVkX19zy7XWOTWPKaeEh70Zh75SCFYpl6vv8hrzrL3wNZGq/lu+i6qdF90YVHwqM4fWTXOcheid359rB4J0Y/7bX/AETUdHTxKET7xPXwhY45RwRnMMUFCH6kffXZTXdCesaVOVsmNARYLmERUaiNc8S2Zkr5SsbxuehexUDoZhzMEI/Yk+0RrWJWGwF1oYx/0zhmAS3rniZZ+wl09nZs3DJDxITna/dhk0HUsKzFk/HtdHCGU8E2TRh9ARxvqTRyGqWEJkj/GcHA2wYJlHFNYXaSHBXeTKtP9no2mnCEx0ZQJbbDESatU8F+HeITLUmV+qqt3g3BwL/76UAsuwO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icity=0.000127, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Wed, Dec 11, 2024 at 5:38=E2=80=AFPM Hao Ge wrote: > > From: Hao Ge > > When CONFIG_KASAN is enabled but CONFIG_KASAN_VMALLOC > is not enabled, we may encounter a panic during system boot. > > Because we haven't allocated pages and created mappings > for the shadow memory corresponding to module_tags region, > similar to how it is done for execmem_vmalloc. > > The difference is that our module_tags are allocated on demand, > so similarly,we also need to allocate shadow memory regions on demand. > However, we still need to adhere to the MODULE_ALIGN principle. nit: the above wording is a bit unclear. Instead of module_tags I would call it "memory for module allocation tags". So, I would change the above paragraph to: The memory for module allocation tags is allocated on demand, therefore we need to allocate shadow memory on demand as well in MODULE_ALIGN blocks. > > Here is the log for panic: > > [ 18.349421] BUG: unable to handle page fault for address: fffffbfff809= 2000 > [ 18.350016] #PF: supervisor read access in kernel mode > [ 18.350459] #PF: error_code(0x0000) - not-present page > [ 18.350904] PGD 20fe52067 P4D 219dc8067 PUD 219dc4067 PMD 102495067 PT= E 0 > [ 18.351484] Oops: Oops: 0000 [#1] PREEMPT SMP KASAN NOPTI > [ 18.351961] CPU: 5 UID: 0 PID: 1 Comm: systemd Not tainted 6.13.0-rc1+= #3 > [ 18.352533] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIO= S rel-1.16.3-0-ga6ed6b701f0a-prebuilt.qemu.org 04/01/2014 > [ 18.353494] RIP: 0010:kasan_check_range+0xba/0x1b0 > [ 18.353931] Code: 8d 5a 07 4c 0f 49 da 49 c1 fb 03 45 85 db 0f 84 dd 0= 0 00 00 45 89 db 4a 8d 14 d8 eb 0d 48 83 c0 08 48 39 c2 0f 84 c1 00 00 00 <= 48> 83 38 00 74 ed 48 8d 50 08 eb 0d 48 83 c0 01 48 39 d0 0f 84 90 > [ 18.355484] RSP: 0018:ff11000101877958 EFLAGS: 00010206 > [ 18.355937] RAX: fffffbfff8092000 RBX: fffffbfff809201e RCX: ffffffff8= 2a7ceac > [ 18.356542] RDX: fffffbfff8092018 RSI: 00000000000000f0 RDI: ffffffffc= 0490000 > [ 18.357153] RBP: fffffbfff8092000 R08: 0000000000000001 R09: fffffbfff= 809201d > [ 18.357756] R10: ffffffffc04900ef R11: 0000000000000003 R12: ffffffffc= 0490000 > [ 18.358365] R13: ff11000101877b48 R14: ffffffffc0490000 R15: 000000000= 000002c > [ 18.358968] FS: 00007f9bd13c5940(0000) GS:ff110001eb480000(0000) knlG= S:0000000000000000 > [ 18.359648] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 > [ 18.360178] CR2: fffffbfff8092000 CR3: 0000000109214004 CR4: 000000000= 0771ef0 > [ 18.360790] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 000000000= 0000000 > [ 18.361404] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 000000000= 0000400 > [ 18.362020] PKRU: 55555554 > [ 18.362261] Call Trace: > [ 18.362481] > [ 18.362671] ? __die+0x23/0x70 > [ 18.362964] ? page_fault_oops+0xc2/0x160 > [ 18.363318] ? exc_page_fault+0xad/0xc0 > [ 18.363680] ? asm_exc_page_fault+0x26/0x30 > [ 18.364056] ? move_module+0x3cc/0x8a0 > [ 18.364398] ? kasan_check_range+0xba/0x1b0 > [ 18.364755] __asan_memcpy+0x3c/0x60 > [ 18.365074] move_module+0x3cc/0x8a0 > [ 18.365386] layout_and_allocate.constprop.0+0x3d5/0x720 > [ 18.365841] ? early_mod_check+0x3dc/0x510 > [ 18.366195] load_module+0x72/0x1850 > [ 18.366509] ? __pfx_kernel_read_file+0x10/0x10 > [ 18.366918] ? vm_mmap_pgoff+0x21c/0x2d0 > [ 18.367262] init_module_from_file+0xd1/0x130 > [ 18.367638] ? __pfx_init_module_from_file+0x10/0x10 > [ 18.368073] ? __pfx__raw_spin_lock+0x10/0x10 > [ 18.368456] ? __pfx_cred_has_capability.isra.0+0x10/0x10 > [ 18.368938] idempotent_init_module+0x22c/0x790 > [ 18.369332] ? simple_getattr+0x6f/0x120 > [ 18.369676] ? __pfx_idempotent_init_module+0x10/0x10 > [ 18.370110] ? fdget+0x58/0x3a0 > [ 18.370393] ? security_capable+0x64/0xf0 > [ 18.370745] __x64_sys_finit_module+0xc2/0x140 > [ 18.371136] do_syscall_64+0x7d/0x160 > [ 18.371459] ? fdget_pos+0x1c8/0x4c0 > [ 18.371784] ? ksys_read+0xfd/0x1d0 > [ 18.372106] ? syscall_exit_to_user_mode+0x10/0x1f0 > [ 18.372525] ? do_syscall_64+0x89/0x160 > [ 18.372860] ? do_syscall_64+0x89/0x160 > [ 18.373194] ? do_syscall_64+0x89/0x160 > [ 18.373527] ? syscall_exit_to_user_mode+0x10/0x1f0 > [ 18.373952] ? do_syscall_64+0x89/0x160 > [ 18.374283] ? syscall_exit_to_user_mode+0x10/0x1f0 > [ 18.374701] ? do_syscall_64+0x89/0x160 > [ 18.375037] ? do_user_addr_fault+0x4a8/0xa40 > [ 18.375416] ? clear_bhb_loop+0x25/0x80 > [ 18.375748] ? clear_bhb_loop+0x25/0x80 > [ 18.376119] ? clear_bhb_loop+0x25/0x80 > [ 18.376450] entry_SYSCALL_64_after_hwframe+0x76/0x7e > > Fixes: 233e89322cbe ("alloc_tag: fix module allocation tags populated are= a calculation") > Reported-by: Ben Greear > Closes: https://lore.kernel.org/all/1ba0cc57-e2ed-caa2-1241-aa5615bee01f@= candelatech.com/ > Suggested-by: Suren Baghdasaryan > Signed-off-by: Hao Ge Other than that nit, LGTM. Acked-by: Suren Baghdasaryan > --- > v4: Based on Suren's suggestion for modification (to make the code simple= r), > modify the code. > Update the comments in the code due to the modifications made to the = code. > Add Suggested-by: Suren Baghdasaryan > > v3: Adjusting the title because the previous one was a bit unclear. > Suren has pointed out that our condition for determining whether > to allocate shadow memory is unreasonable.We have adjusted our method > to use every 8 pages as an index (idx), and we will make decisions ba= sed > on this idx when determining whether to allocate shadow memory. > > v2: Add comments to facilitate understanding of the code. > Add align nr << PAGE_SHIFT to MODULE_ALIGN,even though kasan_alloc_mo= dule_shadow > already handles this internally,but to make the code more readable an= d user-friendly > > commit 233e89322cbe ("alloc_tag: fix module allocation > tags populated area calculation") is currently in the > mm-hotfixes-unstable branch, so this patch is > developed based on the mm-hotfixes-unstable branch. > --- > lib/alloc_tag.c | 14 ++++++++++++++ > 1 file changed, 14 insertions(+) > > diff --git a/lib/alloc_tag.c b/lib/alloc_tag.c > index f942408b53ef..c5bdfa297a35 100644 > --- a/lib/alloc_tag.c > +++ b/lib/alloc_tag.c > @@ -407,6 +407,8 @@ static int vm_module_tags_populate(void) > > if (phys_end < new_end) { > struct page **next_page =3D vm_module_tags->pages + vm_mo= dule_tags->nr_pages; > + unsigned long old_shadow_end =3D ALIGN(phys_end, MODULE_A= LIGN); > + unsigned long new_shadow_end =3D ALIGN(new_end, MODULE_AL= IGN); > unsigned long more_pages; > unsigned long nr; > > @@ -421,7 +423,19 @@ static int vm_module_tags_populate(void) > __free_page(next_page[i]); > return -ENOMEM; > } > + > vm_module_tags->nr_pages +=3D nr; > + > + /* > + * Kasan allocates 1 byte of shadow for every 8 bytes of = data. > + * When kasan_alloc_module_shadow allocates shadow memory= , > + * its unit of allocation is a page. > + * Therefore, here we need to align to MODULE_ALIGN. > + */ > + if (old_shadow_end < new_shadow_end) > + kasan_alloc_module_shadow((void *)old_shadow_end, > + new_shadow_end - old_sh= adow_end, > + GFP_KERNEL); > } > > /* > -- > 2.25.1 >