Skip to contentSkip to conversation

mm: Fix possible NULL pointer dereference in __swap_duplicate

PATCH Superseded gaoxu 5 participants 9 messages Feb 12, 2025 – Feb 14, 2025

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

gaoxu

author
#0 lore raw

PATCH 1/1 mm: Fix possible NULL pointer dereference in __swap_duplicate

swp_swap_info() may return null; it is necessary to check the return value to avoid NULL pointer dereference. The code for other calls to swp_swap_info() includes checks, and __swap_duplicate() should also include checks.

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:

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 = 0x0000000096000005
EC = 0x25: DABT (current EL), IL = 32 bits
SET = 0, FnV = 0
EA = 0, S1PTW = 0
FSC = 0x05: level 1 translation fault
Data abort info:
ISV = 0, ISS = 0x00000005, ISS2 = 0x00000000
CM = 0, WnR = 0, TnD = 0, TagAccess = 0
GCS = 0, Overlay = 0, DirtyBit = 0, Xs = 0
user pgtable: 4k pages, 39-bit VAs, pgdp=00000008a80e5000 [0000000000000058] pgd=0000000000000000, p4d=0000000000000000, pud=0000000000000000

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)
---[ 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.

---
 mm/swapfile.c | 2 ++
 1 file changed, 2 insertions(+)
mm/swapfile.c +2 -0
index 7448a3876..0b253b8ca 100644
@@ -3521,6 +3521,8 @@ static int __swap_duplicate(swp_entry_t entry, unsigned char usage, int nr)
 	int err, i;
 
 	si = swp_swap_info(entry);
+	if (unlikely(!si))
+		return -EINVAL;
 
 	offset = swp_offset(entry);
 	VM_WARN_ON(nr > SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER);
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 4 lines · On Wed, 12 Feb 2025 03:13:46 +0000 gaoxu <[email protected]> wrote:

swp_swap_info() may return null; it is necessary to check the return value to avoid NULL pointer dereference. The code for other calls to swp_swap_info() includes checks, and __swap_duplicate() should also include checks.

Actually very few of the swp_swap_info() callers check for a NULL return.

Quoted 2 lines

The reason why swp_swap_info() returns NULL is unclear; it may be due to CPU cache issues or DDR bit flips.

Quite possibly it's a kernel bug.

Quoted 10 lines

The probability of this issue is very
small, and the stack info we encountered is as follows:

Unable to handle kernel NULL pointer dereference at virtual address

0000000000000058

...

mm/swapfile.c static int __swap_duplicate(swp_entry_t entry, unsigned char usage, int nr) · line 3528

Quoted 8 lines
mm/swapfile.c +2 -0
@@ -3521,6 +3521,8 @@ static int __swap_duplicate(swp_entry_t entry, unsigned char usage, int nr)
 	int err, i;
 
 	si = swp_swap_info(entry);
+	if (unlikely(!si))
+		return -EINVAL;
 
 	offset = swp_offset(entry);
 	VM_WARN_ON(nr > SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER);

OK, I guess avoiding the crash is good. But please let's include a WARN so that we can perhaps fix the bug, if one is there.

author
#2 lore raw
Quoted 9 lines · On Wed, 12 Feb 2025 03:13:46 +0000 gaoxu <[email protected]> wrote:
Quoted 5 lines

swp_swap_info() may return null; it is necessary to check the return value to avoid NULL pointer dereference. The code for other calls to swp_swap_info() includes checks, and __swap_duplicate() should also include checks.

Actually very few of the swp_swap_info() callers check for a NULL return.

The swapfile.c file contains three instances where the return value of swp_swap_info() is checked for a NULL return. In other files that call swp_swap_info(), I have confirmed that there are no such checks. The description in the patch is inaccurate, and I have made modifications in patch v2.

Quoted 23 lines
Quoted 3 lines

The reason why swp_swap_info() returns NULL is unclear; it may be due to CPU cache issues or DDR bit flips.

Quite possibly it's a kernel bug.

mm/swapfile.c static int __swap_duplicate(swp_entry_t entry,

Quoted 10 lines

The probability of this issue is very
small, and the stack info we encountered is as follows:

Unable to handle kernel NULL pointer dereference at virtual address

0000000000000058

...

mm/swapfile.c +0 -0
@@ -3521,6 +3521,8 @@ static int __swap_duplicate(swp_entry_t entry,

unsigned char usage, int nr)

Quoted 6 lines
mm/swapfile.c +2 -0
 	int err, i;
 
 	si = swp_swap_info(entry);
+	if (unlikely(!si))
+		return -EINVAL;
 
Quoted 5 lines

mm/swapfile.c static int __swap_duplicate(swp_entry_t entry, unsigned char usage, int nr) · line 3528

Quoted 2 lines
mm/swapfile.c +0 -0
 	offset = swp_offset(entry);
 	VM_WARN_ON(nr > SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER);

OK, I guess avoiding the crash is good. But please let's include a WARN so that we can perhaps fix the bug, if one is there.

Good. I'll change it as mentioned and send a new patch.

	si = swp_swap_info(entry);

+ if (unlikely(!si)) {
+ WARN(1, KERN_ERR "%s: %s%08lx\n", __func__, Bad_file, entry.val);
+ return -EINVAL;
+ }

#3 lore raw
Quoted 44 lines · gaoxu
Quoted 9 lines · On Wed, 12 Feb 2025 03:13:46 +0000 gaoxu <[email protected]> wrote:
Quoted 5 lines

swp_swap_info() may return null; it is necessary to check the return value to avoid NULL pointer dereference. The code for other calls to swp_swap_info() includes checks, and __swap_duplicate() should also include checks.

Actually very few of the swp_swap_info() callers check for a NULL return.

The swapfile.c file contains three instances where the return value of swp_swap_info() is checked for a NULL return. In other files that call swp_swap_info(), I have confirmed that there are no such checks. The description in the patch is inaccurate, and I have made modifications in patch v2.

Quoted 23 lines
Quoted 3 lines

The reason why swp_swap_info() returns NULL is unclear; it may be due to CPU cache issues or DDR bit flips.

Quite possibly it's a kernel bug.

mm/swapfile.c static int __swap_duplicate(swp_entry_t entry,

Quoted 10 lines

The probability of this issue is very
small, and the stack info we encountered is as follows:

Unable to handle kernel NULL pointer dereference at virtual address

0000000000000058

...

mm/swapfile.c +0 -0
@@ -3521,6 +3521,8 @@ static int __swap_duplicate(swp_entry_t entry,

unsigned char usage, int nr)

Quoted 6 lines
mm/swapfile.c +2 -0
 	int err, i;
 
 	si = swp_swap_info(entry);
+	if (unlikely(!si))
+		return -EINVAL;
 
Quoted 5 lines

mm/swapfile.c static int __swap_duplicate(swp_entry_t entry, unsigned char usage, int nr) · line 3528

Quoted 2 lines
mm/swapfile.c +0 -0
 	offset = swp_offset(entry);
 	VM_WARN_ON(nr > SWAPFILE_CLUSTER - offset % SWAPFILE_CLUSTER);

OK, I guess avoiding the crash is good. But please let's include a WARN so that we can perhaps fix the bug, if one is there.

Good. I'll change it as mentioned and send a new patch.

	si = swp_swap_info(entry);

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (unlikely(!si)) {
+		WARN(1, KERN_ERR "%s: %s%08lx\n", __func__, Bad_file, entry.val);

WARN() already contains unlikely(). Also, no need to print the function name it's already in the stack trace.

We should probably just do if (WARN_ON_ONCE(!si)).

Quoted 4 lines
(quoted diff) +2 -0
+		return -EINVAL;
+	}
author
#4 lore raw
Quoted 52 lines
Quoted 32 lines
Quoted 8 lines
Quoted 6 lines

swp_swap_info() may return null; it is necessary to check the return value to avoid NULL pointer dereference. The code for other calls to
swp_swap_info() includes checks, and __swap_duplicate() should also include checks.

Actually very few of the swp_swap_info() callers check for a NULL return.

The swapfile.c file contains three instances where the return value of swp_swap_info() is checked for a NULL return. In other files that call swp_swap_info(), I have confirmed that there are no such checks. The description in the patch is inaccurate, and I have made modifications in patch v2.

Quoted 19 lines
Quoted 3 lines

The reason why swp_swap_info() returns NULL is unclear; it may be due to CPU cache issues or DDR bit flips.

Quite possibly it's a kernel bug.

mm/swapfile.c static int __swap_duplicate(swp_entry_t · line 3521

Quoted 12 lines

The probability of this issue is very small, and the stack info we encountered is as follows:

Unable to handle kernel NULL pointer dereference at virtual

address
0000000000000058

...

mm/swapfile.c +0 -0
@@ -3521,6 +3521,8 @@ static int __swap_duplicate(swp_entry_t
 entry,

unsigned char usage, int nr)

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
mm/swapfile.c +2 -0
 	int err, i;
 
 	si = swp_swap_info(entry);
+	if (unlikely(!si))
+		return -EINVAL;
 
 	offset = swp_offset(entry);

VM_WARN_ON(nr > SWAPFILE_CLUSTER - offset %

SWAPFILE_CLUSTER);

Review

Quoted 7 lines
Quoted 3 lines

OK, I guess avoiding the crash is good. But please let's include a WARN so that we can perhaps fix the bug, if one is there.

Good. I'll change it as mentioned and send a new patch.

	si = swp_swap_info(entry);
(quoted diff) +2 -0
+	if (unlikely(!si)) {
+		WARN(1, KERN_ERR "%s: %s%08lx\n", __func__, Bad_file, entry.val);

WARN() already contains unlikely(). Also, no need to print the function name it's already in the stack trace.

Quoted 4 lines

We should probably just do if (WARN_ON_ONCE(!si)).

Review

Quoted 2 lines
(quoted diff) +2 -0
+		return -EINVAL;
+	}

Yes, thank you for your suggestion. This modification makes the code simpler.

	si = swp_swap_info(entry);

+ if (WARN_ON_ONCE(!si))
+ return -EINVAL;

Quoted 2 lines
Quoted 2 lines
reviewer
#5 lore raw
In reply to gaoxu
Quoted 55 lines · gaoxu

swp_swap_info() may return null; it is necessary to check the return value to avoid NULL pointer dereference. The code for other calls to swp_swap_info() includes checks, and __swap_duplicate() should also include checks.

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:

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 = 0x0000000096000005
EC = 0x25: DABT (current EL), IL = 32 bits
SET = 0, FnV = 0
EA = 0, S1PTW = 0
FSC = 0x05: level 1 translation fault
Data abort info:
ISV = 0, ISS = 0x00000005, ISS2 = 0x00000000
CM = 0, WnR = 0, TnD = 0, TagAccess = 0
GCS = 0, Overlay = 0, DirtyBit = 0, Xs = 0
user pgtable: 4k pages, 39-bit VAs, pgdp=00000008a80e5000 [0000000000000058] pgd=0000000000000000, p4d=0000000000000000, pud=0000000000000000

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)
---[ end trace 0000000000000000 ]---
Kernel panic - not syncing: Oops: Fatal exception
Quoted 8 lines

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.

Yeah this smells like a bug. A bit strange though - I have eyeballed the code, and we (should have?) locked the PTE before resolving it into the swap entry format. Which should have been enough to prevent the swap entry from being unmapped and freed up. Which should have been enough to prevent swapoff...?

(are you even doing concurrent swapoff?)

Can you provide more context? What kernel version is this, what kind of workload is this, any reproducer, etc.?

author
#6 lore raw
Quoted 61 lines · gaoxu
Quoted 59 lines

swp_swap_info() may return null; it is necessary to check the return value to avoid NULL pointer dereference. The code for other calls to swp_swap_info() includes checks, and __swap_duplicate() should also include checks.

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:

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 = 0x0000000096000005
EC = 0x25: DABT (current EL), IL = 32 bits
SET = 0, FnV = 0
EA = 0, S1PTW = 0
FSC = 0x05: level 1 translation fault Data abort info:
ISV = 0, ISS = 0x00000005, ISS2 = 0x00000000
CM = 0, WnR = 0, TnD = 0, TagAccess = 0
GCS = 0, Overlay = 0, DirtyBit = 0, Xs = 0 user pgtable: 4k pages,
39-bit VAs, pgdp=00000008a80e5000 [0000000000000058] pgd=0000000000000000, p4d=0000000000000000, pud=0000000000000000

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) ---[ 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.

Quoted 8 lines
Quoted 1 line

Yeah this smells like a bug. A bit strange though - I have eyeballed the code, and we (should have?) locked the PTE before resolving it into the swap entry format. Which should have been enough to prevent the swap entry from being unmapped and freed up. Which should have been enough to prevent swapoff...?

(are you even doing concurrent swapoff?)

No, the swapoff operation was not executed.

Quoted 3 lines

Can you provide more context? What kernel version is this, what kind of workload is this, any reproducer, etc.?

kernel version is linux 6.6, Android15 - linux6.6.30.

The issues encountered by mobile users during usage.
The system load should not be high, as there is no info related to low memory found in the logs.
The probability of this issue occurring is very low and irregular. We cannot reproduce the problem during stress testing in the laboratory.

I found someone reporting a similar issue on the web, see:
https://lkml.indiana.edu/hypermail/linux/kernel/2406.0/02380.html
https://forum.proxmox.com/threads/get_swap_device-bad-swap-file-entry.155581/
https://forums.unraid.net/topic/145497-server-crashes-with-repeated-get_swap_device-bad-swap-file-entry-3ffffffffffff/

#7 lore raw
Quoted 82 lines · gaoxu
Quoted 62 lines · gaoxu
Quoted 59 lines

swp_swap_info() may return null; it is necessary to check the return value to avoid NULL pointer dereference. The code for other calls to swp_swap_info() includes checks, and __swap_duplicate() should also include checks.

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:

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 = 0x0000000096000005
EC = 0x25: DABT (current EL), IL = 32 bits
SET = 0, FnV = 0
EA = 0, S1PTW = 0
FSC = 0x05: level 1 translation fault Data abort info:
ISV = 0, ISS = 0x00000005, ISS2 = 0x00000000
CM = 0, WnR = 0, TnD = 0, TagAccess = 0
GCS = 0, Overlay = 0, DirtyBit = 0, Xs = 0 user pgtable: 4k pages,
39-bit VAs, pgdp=00000008a80e5000 [0000000000000058] pgd=0000000000000000, p4d=0000000000000000, pud=0000000000000000

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) ---[ 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.

Quoted 8 lines
Quoted 1 line

Yeah this smells like a bug. A bit strange though - I have eyeballed the code, and we (should have?) locked the PTE before resolving it into the swap entry format. Which should have been enough to prevent the swap entry from being unmapped and freed up. Which should have been enough to prevent swapoff...?

(are you even doing concurrent swapoff?)

No, the swapoff operation was not executed.

Quoted 3 lines

Can you provide more context? What kernel version is this, what kind of workload is this, any reproducer, etc.?

kernel version is linux 6.6, Android15 - linux6.6.30.

The issues encountered by mobile users during usage.
The system load should not be high, as there is no info related to low memory found in the logs.
The probability of this issue occurring is very low and irregular. We cannot reproduce the problem during stress testing in the laboratory.

Quoted 4 lines

I found someone reporting a similar issue on the web, see:
https://lkml.indiana.edu/hypermail/linux/kernel/2406.0/02380.html
https://forum.proxmox.com/threads/get_swap_device-bad-swap-file-entry.155581/
https://forums.unraid.net/topic/145497-server-crashes-with-repeated-get_swap_device-bad-swap-file-entry-3ffffffffffff/

It might be a non-swap entry mistakenly passed to swap functions. I remember fixing a similar issue in the Android Common Kernel 6.6:

https://android.googlesource.com/kernel/common/+/119351fe20bc73b71c6

where a migration entry is mistakenly passed to swap APIs.

In any case, we need to identify and fix the actual bug.

Quoted 3 lines

Thanks
Barry

author
#8 lore raw
Quoted 98 lines
Quoted 29 lines
Quoted 29 lines · gaoxu
Quoted 26 lines

swp_swap_info() may return null; it is necessary to check the return value to avoid NULL pointer dereference. The code for other calls to
swp_swap_info() includes checks, and __swap_duplicate() should also include checks.

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:

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 = 0x0000000096000005
EC = 0x25: DABT (current EL), IL = 32 bits
SET = 0, FnV = 0
EA = 0, S1PTW = 0
FSC = 0x05: level 1 translation fault Data abort info:
ISV = 0, ISS = 0x00000005, ISS2 = 0x00000000
CM = 0, WnR = 0, TnD = 0, TagAccess = 0
GCS = 0, Overlay = 0, DirtyBit = 0, Xs = 0 user pgtable: 4k
pages, 39-bit VAs, pgdp=00000008a80e5000 [0000000000000058] pgd=0000000000000000, p4d=0000000000000000, pud=0000000000000000

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
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

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

x20: 00000000002d2d2f x19: 18000000002d2d2f x18: ffffffdf726faec0
x17: 0000000000000000 x16: 0010000000000001 x15:

0040000000000001

Quoted 28 lines
Quoted 28 lines
Quoted 28 lines

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) ---[ 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.

Quoted 4 lines
Quoted 4 lines
Quoted 1 line

Yeah this smells like a bug. A bit strange though - I have eyeballed the code, and we (should have?) locked the PTE before resolving it into the

swap entry format.

Quoted 2 lines
Quoted 2 lines

Which should have been enough to prevent the swap entry from being unmapped and freed up. Which should have been enough to prevent

swapoff...?

Quoted 14 lines
Quoted 2 lines

(are you even doing concurrent swapoff?)

No, the swapoff operation was not executed.

Quoted 3 lines

Can you provide more context? What kernel version is this, what kind of workload is this, any reproducer, etc.?

kernel version is linux 6.6, Android15 - linux6.6.30.

The issues encountered by mobile users during usage.
The system load should not be high, as there is no info related to low memory found in the logs.
The probability of this issue occurring is very low and irregular. We cannot reproduce the problem during stress testing in the laboratory.

Quoted 6 lines

I found someone reporting a similar issue on the web, see:
https://lkml.indiana.edu/hypermail/linux/kernel/2406.0/02380.html
https://forum.proxmox.com/threads/get_swap_device-bad-swap-file-entry.
155581/
https://forums.unraid.net/topic/145497-server-crashes-with-repeated-ge
t_swap_device-bad-swap-file-entry-3ffffffffffff/

Quoted 8 lines

It might be a non-swap entry mistakenly passed to swap functions. I remember fixing a similar issue in the Android Common Kernel 6.6:

https://android.googlesource.com/kernel/common/+/119351fe20bc73b71c6

where a migration entry is mistakenly passed to swap APIs.

In any case, we need to identify and fix the actual bug.

As I mentioned in my previous email, the root cause of this issue is unclear, and we do not know why this entry is behaving abnormally. The probability of this issue occurring is very low, and it cannot be reproduced locally. We are now looking to transform a null pointer crash issue into an individual process issue, aiming to reduce the impact of the problem on actual users. I believe everyone understands that this approach is also valuable.

Of course, we can also refer to the link you provided to make the corresponding modifications, This modification can also help avoid the null pointer issue. such as:
- if (swap_duplicate(entry) < 0) {

+  if (!non_swap_entry(entry) && swap_duplicate(entry) < 0) {
	   set_pte_at(mm, address, pvmw.pte, pteval);
	   ret = false;
	   page_vma_mapped_walk_done(&pvmw);
	   break;

}

Quoted 7 lines
Quoted 4 lines

Thanks
Barry