From nobody Thu Dec 18 10:00:10 2025 Received: from mta21.hihonor.com (mta21.hihonor.com [81.70.160.142]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 86A8314F70 for ; Wed, 19 Feb 2025 01:56:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=81.70.160.142 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739930193; cv=none; b=p8440ujxnhP7zCrBb4oWn2PmldYgTqqldCHPXLGG+Q8hWNuPzBNcfKP8sa52Xdg2aF/3LmCfltscxu6ro6dlsqoLa6C7Z4JP0G/23gBCxXQYgR+RcytoFnLAxOCELyYpZW2hbY7QiZAzLwjVJEMBfgfkiptbGvE4XF94WSbGIi4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739930193; c=relaxed/simple; bh=Mc+kH8GEon/PVfC0HKf5NV/vmITYzF+FwowKOyfCJMk=; h=From:To:CC:Subject:Date:Message-ID:Content-Type:MIME-Version; b=hUQeej+J+PKeJGJqKb4ifL31a4V6aa7RPcj9oY4mWB18htxAf/iIl0Cv5yQqNOpxOIbqNqQI7B8KkuYNl7PTwJXEbkFtVkbYUYW3v15r72KU5JUYW6dv3jqk57pb9oSjrpJBhCwPJlbkGOdjoQ5FMnWudGh+HOPn9KhIz8bqbvU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=honor.com; spf=pass smtp.mailfrom=honor.com; arc=none smtp.client-ip=81.70.160.142 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=honor.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=honor.com Received: from w011.hihonor.com (unknown [10.68.20.122]) by mta21.hihonor.com (SkyGuard) with ESMTPS id 4YyKGw4XmzzYky7j; Wed, 19 Feb 2025 09:55:36 +0800 (CST) Received: from a005.hihonor.com (10.68.18.24) by w011.hihonor.com (10.68.20.122) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Wed, 19 Feb 2025 09:56:29 +0800 Received: from a007.hihonor.com (10.68.22.31) by a005.hihonor.com (10.68.18.24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Wed, 19 Feb 2025 09:56:28 +0800 Received: from a007.hihonor.com ([fe80::e866:83ac:f23b:c25c]) by a007.hihonor.com ([fe80::e866:83ac:f23b:c25c%10]) with mapi id 15.02.1544.011; Wed, 19 Feb 2025 09:56:28 +0800 From: gaoxu To: Andrew Morton , "linux-mm@kvack.org" CC: "linux-kernel@vger.kernel.org" , "Suren Baghdasaryan" , Barry Song <21cnbao@gmail.com>, "Yosry Ahmed" , Yosry Ahmed Subject: [PATCH v4] mm: Fix possible NULL pointer dereference in __swap_duplicate Thread-Topic: [PATCH v4] mm: Fix possible NULL pointer dereference in __swap_duplicate Thread-Index: AduCcQRTKSEr86o5RfKgi/48SSQ2Jw== Date: Wed, 19 Feb 2025 01:56:28 +0000 Message-ID: Accept-Language: zh-CN, en-US Content-Language: zh-CN X-MS-Has-Attach: X-MS-TNEF-Correlator: Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add a NULL check on the return value of swp_swap_info in __swap_duplicate to prevent crashes caused by NULL pointer dereference. The reason why swp_swap_info() returns NULL is unclear; it may be due to CPU cache issues or DDR bit flips. The probability of this issue is very small, and the stack info we encountered is as follows=EF=BC=9A Unable to handle kernel NULL pointer dereference at virtual address 0000000000000058 [RB/E]rb_sreason_str_set: sreason_str set null_pointer Mem abort info: ESR =3D 0x0000000096000005 EC =3D 0x25: DABT (current EL), IL =3D 32 bits SET =3D 0, FnV =3D 0 EA =3D 0, S1PTW =3D 0 FSC =3D 0x05: level 1 translation fault Data abort info: ISV =3D 0, ISS =3D 0x00000005, ISS2 =3D 0x00000000 CM =3D 0, WnR =3D 0, TnD =3D 0, TagAccess =3D 0 GCS =3D 0, Overlay =3D 0, DirtyBit =3D 0, Xs =3D 0 user pgtable: 4k pages, 39-bit VAs, pgdp=3D00000008a80e5000 [0000000000000058] pgd=3D0000000000000000, p4d=3D0000000000000000, pud=3D0000000000000000 Internal error: Oops: 0000000096000005 [#1] PREEMPT SMP Skip md ftrace buffer dump for: 0x1609e0 ... pc : swap_duplicate+0x44/0x164 lr : copy_page_range+0x508/0x1e78 sp : ffffffc0f2a699e0 x29: ffffffc0f2a699e0 x28: ffffff8a5b28d388 x27: ffffff8b06603388 x26: ffffffdf7291fe70 x25: 0000000000000006 x24: 0000000000100073 x23: 00000000002d2d2f x22: 0000000000000008 x21: 0000000000000000 x20: 00000000002d2d2f x19: 18000000002d2d2f x18: ffffffdf726faec0 x17: 0000000000000000 x16: 0010000000000001 x15: 0040000000000001 x14: 0400000000000001 x13: ff7ffffffffffb7f x12: ffeffffffffffbff x11: ffffff8a5c7e1898 x10: 0000000000000018 x9 : 0000000000000006 x8 : 1800000000000000 x7 : 0000000000000000 x6 : ffffff8057c01f10 x5 : 000000000000a318 x4 : 0000000000000000 x3 : 0000000000000000 x2 : 0000006daf200000 x1 : 0000000000000001 x0 : 18000000002d2d2f Call trace: swap_duplicate+0x44/0x164 copy_page_range+0x508/0x1e78 copy_process+0x1278/0x21cc kernel_clone+0x90/0x438 __arm64_sys_clone+0x5c/0x8c invoke_syscall+0x58/0x110 do_el0_svc+0x8c/0xe0 el0_svc+0x38/0x9c el0t_64_sync_handler+0x44/0xec el0t_64_sync+0x1a8/0x1ac Code: 9139c35a 71006f3f 54000568 f8797b55 (f9402ea8) Reviewed-by: Barry Song ---[ end trace 0000000000000000 ]--- Kernel panic - not syncing: Oops: Fatal exception SMP: stopping secondary CPUs The patch seems to only provide a workaround, but there are no more effective software solutions to handle the bit flips problem. This path will change the issue from a system crash to a process exception, thereby reducing the impact on the entire machine. Signed-off-by: gao xu --- v1 -> v2:=20 - Add WARN_ON_ONCE. - update the commit info. v2 -> v3: Delete the review tags (This is my issue, and I apologize). V3 -> v4: Add swap entry logging per Barry Song's suggestion. --- mm/swapfile.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mm/swapfile.c b/mm/swapfile.c index 7448a3876..403df1817 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -3521,6 +3521,10 @@ static int __swap_duplicate(swp_entry_t entry, unsig= ned char usage, int nr) int err, i; =20 si =3D swp_swap_info(entry); + if (WARN_ON_ONCE(!si)) { + pr_err("%s%08lx\n", Bad_file, entry.val); + return -EINVAL; + } =20 offset =3D swp_offset(entry); VM_WARN_ON(nr > SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER); --=20 2.17.1