mm/kmemleak: Fix sleeping function called from invalid context at print message
Subsystems (3)
RFC 1/1 mm/kmemleak: Fix sleeping function called from invalid context at print message
Address a bug in the kernel that triggers a "sleeping function called from invalid context" warning when /sys/kernel/debug/kmemleak is printed under specific conditions:
- CONFIG_PREEMPT_RT=y
- Set SELinux as the LSM for the system
- Set kptr_restrict to 1
- kmemleak buffer contains at least one item
Ensure the kmemleak buffer contains at least one item
Commit 8c96f1bc6fc49c724c4cdd22d3e99260263b7384 ("mm/kmemleak: turn kmemleak_lock and object->lock to raw_spinlock_t") introduced a change where kmemleak_seq_show is executed in atomic context within the RT kernel. However, the SELinux capability check in this function flow still relies on regular spinlocks, leading to potential race conditions that trigger an error when printing the kmemleak backtrace.
Move the backtrace printing out of the critical section. Use a stack variable to store the backtrace pointers, avoiding spinlocks in the atomic context.
Implement delta encoding to minimize the stack memory footprint, addressing the potentially large memory demands for storing these pointers on 64-bit systems.
---
```
[ 159.247069] BUG: sleeping function called from invalid context at kernel/locking/spinlock_rt.c:48
[ 159.247193] in_atomic(): 1, irqs_disabled(): 1, non_block: 0, pid: 136, name: cat
[ 159.247241] preempt_count: 1, expected: 0
[ 159.247277] RCU nest depth: 2, expected: 2
[ 159.247388] 6 locks held by cat/136:
[ 159.247438] #0: ffff32e64bcbf950 (&p->lock){+.+.}-{3:3}, at: seq_read_iter+0xb8/0xe30
[ 159.248835] #1: ffffafe6aaa9dea0 (scan_mutex){+.+.}-{3:3}, at: kmemleak_seq_start+0x34/0x128
[ 159.249053] #3: ffff32e6546b1cd0 (&object->lock){....}-{2:2}, at: kmemleak_seq_show+0x3c/0x1e0
[ 159.249127] #4: ffffafe6aa8d8560 (rcu_read_lock){....}-{1:2}, at: has_ns_capability_noaudit+0x8/0x1b0
[ 159.249205] #5: ffffafe6aabbc0f8 (notif_lock){+.+.}-{2:2}, at: avc_compute_av+0xc4/0x3d0
[ 159.249364] irq event stamp: 136660
[ 159.249407] hardirqs last enabled at (136659): [<ffffafe6a80fd7a0>] _raw_spin_unlock_irqrestore+0xa8/0xd8
[ 159.249465] hardirqs last disabled at (136660): [<ffffafe6a80fd85c>] _raw_spin_lock_irqsave+0x8c/0xb0
[ 159.249518] softirqs last enabled at (0): [<ffffafe6a5d50b28>] copy_process+0x11d8/0x3df8
[ 159.249571] softirqs last disabled at (0): [<0000000000000000>] 0x0
[ 159.249970] Preemption disabled at:
[ 159.249988] [<ffffafe6a6598a4c>] kmemleak_seq_show+0x3c/0x1e0
[ 159.250609] CPU: 1 UID: 0 PID: 136 Comm: cat Tainted: G E 6.11.0-rt7+ #34
[ 159.250797] Tainted: [E]=UNSIGNED_MODULE
[ 159.250822] Hardware name: linux,dummy-virt (DT)
[ 159.251050] Call trace:
[ 159.251079] dump_backtrace+0xa0/0x128
[ 159.251132] show_stack+0x1c/0x30
[ 159.251156] dump_stack_lvl+0xe8/0x198
[ 159.251180] dump_stack+0x18/0x20
[ 159.251227] rt_spin_lock+0x8c/0x1a8
[ 159.251273] avc_perm_nonode+0xa0/0x150
[ 159.251316] cred_has_capability.isra.0+0x118/0x218
[ 159.251340] selinux_capable+0x50/0x80
[ 159.251363] security_capable+0x7c/0xd0
[ 159.251388] has_ns_capability_noaudit+0x94/0x1b0
[ 159.251412] has_capability_noaudit+0x20/0x30
[ 159.251437] restricted_pointer+0x21c/0x4b0
[ 159.251461] pointer+0x298/0x760
[ 159.251482] vsnprintf+0x330/0xf70
[ 159.251504] seq_printf+0x178/0x218
[ 159.251526] print_unreferenced+0x1a4/0x2d0
[ 159.251551] kmemleak_seq_show+0xd0/0x1e0
[ 159.251576] seq_read_iter+0x354/0xe30
[ 159.251599] seq_read+0x250/0x378
[ 159.251622] full_proxy_read+0xd8/0x148
[ 159.251649] vfs_read+0x190/0x918
[ 159.251672] ksys_read+0xf0/0x1e0
[ 159.251693] __arm64_sys_read+0x70/0xa8
[ 159.251716] invoke_syscall.constprop.0+0xd4/0x1d8
[ 159.251767] el0_svc+0x50/0x158
[ 159.251813] el0t_64_sync+0x17c/0x180
```I have considered three potential approaches to address this matter:
1. Remove Raw Pointer Printing
The simplest solution is to eliminate raw pointer printing from the report. This approach involves minimal changes to the kernel code and is straightforward to implement.
While I am confident that omitting the raw address would result in negligible information loss in most scenarios, some may perceive it as a feature regression. Below is an example of the modification:
``` - warn_or_seq_printf(seq, " [<%pK>] %pS\n", ptr, ptr); + warn_or_seq_printf(seq, " %pS\n", ptr); ```
This change may be acceptable since the %pS format outputs a hex string if no kallsyms are available. However, it modifies the original behavior, and in the kallsyms scenario, the raw pointer would no longer be present.
2. Modify SELinux to Avoid Sleeping Spinlocks
Another option is to alter the SELinux capability check to use non-sleeping spinlocks.
However, this approach is not advisable. The SELinux capability check is extensively used across the kernel and is far more critical than the kmemleak reporting feature.
Adapting it to address this rare issue could unnecessarily introduce latency across the entire kernel, particularly as kmemleak is rarely used in production environments.
3. Move Stack Trace Printing Outside the Atomic Section
The third and preferred approach is to move the stack trace printing outside the atomic section. This would preserve the current functionality without modifying SELinux.
The primary challenge here lies in making the backtrace pointers available after exiting the critical section, as they are captured within it. To address this, the backtrace pointers can be copied to a safe location, enabling access once the raw_spinlock is released.
Options for Creating a Safe Location for Backtrace Pointers Several strategies have been considered for storing the backtrace pointers safely:
* Dynamic Allocation
* Allocating memory with kmalloc cannot be done within a raw_spinlock area. Using GFP_ATOMIC is also infeasible.
* Since the code that prints the message is inside a loop, executed potentially multiple times, it is only within the raw_spinlock section that we can determine whether allocation is needed.
* Allocating and deallocating memory on every loop iteration would be prohibitively expensive.
* Global Data Section
* In this strategy, the message would be printed after exiting the raw_spinlock protected section.
* However, this approach risks data corruption if another occurrence of the issue arises before the first operation completes.
* Per-CPU Data
* The same concerns as with global data apply here. While data corruption is less likely, it is not impossible.
* Stack
* Using the stack is the best option since each thread has its own stack, ensuring data isolation. However, the size of the data poses a challenge.
* Exporting a full stack trace pointer list requires considerable space. A 32-level stack trace in a 64-bit system would require 256 bytes, which is contrary to best practices for stack size management.
To mitigate this, I propose using delta encoding to store the addresses. This method reduces the size of each address from 8 bytes to 4 bytes on 64-bit systems. While this introduces some complexity, it significantly reduces memory usage and allows us to preserve the kmemleak reports in their current form.
mm/kmemleak.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 71 insertions(+), 7 deletions(-)
mm/kmemleak.c +71 -7
@@ -274,6 +274,44 @@ static void kmemleak_disable(void); pr_warn(fmt, ##__VA_ARGS__); \ } while (0) +#define PTR_STORAGE_OP_OK -1 +#define PTR_STORAGE_OP_FAIL 0 +#define PTR_STORAGE_CAPACITY 32 + +struct ptr_storage { +unsigned long base; +u32 data[PTR_STORAGE_CAPACITY ]; +int nr_entries; +}; + +static int ptr_storage_insert (unsigned long p,struct ptr_storage *s) +{ +unsigned long diff_data; + +if (s->nr_entries !=0 ) { + diff_data = s->base - p; +if (s->nr_entries <PTR_STORAGE_CAPACITY ) { + s->data[((s->nr_entries -1 ))] = diff_data &0xffffffff ; + s->nr_entries++; +return PTR_STORAGE_OP_OK ; + } +return PTR_STORAGE_OP_FAIL ; + } + s->base = p; + s->nr_entries++; +return PTR_STORAGE_OP_OK ; +} + +static void *ptr_storage_get (struct ptr_storage *s,int item_no) +{ +if (item_no < s->nr_entries && item_no >0 ) +return (void *)s->base - (s32 )s->data[(item_no -1 )]; + +if (item_no ==0 ) +return (void *)s->base; +return NULL ; +} +static void warn_or_seq_hex_dump (struct seq_file *seq,int prefix_type,int rowsize,int groupsize,const void *buf,size_t len,bool ascii)
@@ -357,11 +395,13 @@ static bool unreferenced_object(struct kmemleak_object *object)* print_unreferenced function must be called with the object->lock held. */ static void print_unreferenced (struct seq_file *seq, -struct kmemleak_object *object) +struct kmemleak_object *object, +struct ptr_storage *s) {int i;unsigned long *entries;unsigned int nr_entries; +unsigned long tmp; nr_entries =stack_depot_fetch (object->trace_handle, &entries); warn_or_seq_printf(seq, "unreferenced object 0x%08lx (size %zu):\n",
@@ -372,8 +412,8 @@ static void print_unreferenced(struct seq_file *seq,warn_or_seq_printf (seq," backtrace (crc %x):\n" , object->checksum);for (i =0 ; i < nr_entries; i++) { -void *ptr = (void *)entries[i]; -warn_or_seq_printf (seq," [<%pK>] %pS\n" , ptr, ptr); + tmp = (unsigned long )entries[i]; +ptr_storage_insert (tmp, s); } }
@@ -1625,6 +1665,10 @@ static void kmemleak_scan(void)struct zone *zone;int __maybe_unused i;int new_leaks =0 ; +struct ptr_storage s = {0 }; +bool do_print = false; +void *tmp; +int inx; jiffies_last_scan = jiffies;
@@ -1783,12 +1827,20 @@ static void kmemleak_scan(void) !(object->flags & OBJECT_REPORTED)) { object->flags |=OBJECT_REPORTED ; -if (kmemleak_verbose) -print_unreferenced (NULL , object); +if (kmemleak_verbose) { +print_unreferenced (NULL , object, &s); + do_print = true; + } new_leaks++; }raw_spin_unlock_irq (&object->lock); +if (kmemleak_verbose && do_print) { +for (inx =0 ; inx < s.nr_entries; inx++) { + tmp =ptr_storage_get (&s, i); +warn_or_seq_printf (NULL ," [<%pK>] %pS\n" , tmp, tmp); + } + } }rcu_read_unlock ();
@@ -1939,11 +1991,23 @@ static int kmemleak_seq_show(struct seq_file *seq, void *v) {struct kmemleak_object *object = v;unsigned long flags; +struct ptr_storage s = {0 }; +void *tmp; +int i; +bool do_print = false;raw_spin_lock_irqsave (&object->lock, flags); -if ((object->flags &OBJECT_REPORTED ) &&unreferenced_object (object)) -print_unreferenced (seq, object); +if ((object->flags &OBJECT_REPORTED ) &&unreferenced_object (object)) { +print_unreferenced (seq, object, &s); + do_print = true; + }raw_spin_unlock_irqrestore (&object->lock, flags); +if (do_print) { +for (i =0 ; i < s.nr_entries; i++) { + tmp =ptr_storage_get (&s, i); +warn_or_seq_printf (seq," [<%pK>] %pS\n" , tmp, tmp); + } + }return 0 ; }
Signature · 1 line
2.34.1
Quoted 18 lines · Alessandro Carminati
Address a bug in the kernel that triggers a "sleeping function called from invalid context" warning when
/sys/kernel/debug/kmemleakis printed under specific conditions:
-CONFIG_PREEMPT_RT=y
- Set SELinux as the LSM for the system
- Setkptr_restrictto 1
- kmemleak buffer contains at least one item
Ensure the kmemleak buffer contains at least one item
Commit8c96f1bc6fc49c724c4cdd22d3e99260263b7384("mm/kmemleak: turnkmemleak_lockandobject->locktoraw_spinlock_t") introduced a change wherekmemleak_seq_showis executed in atomic context within the RT kernel. However, the SELinux capability check in this function flow still relies on regular spinlocks, leading to potential race conditions that trigger an error when printing the kmemleak backtrace.
Move the backtrace printing out of the critical section. Use a stack variable to store the backtrace pointers, avoiding spinlocks in the atomic context.
Quoted 3 lines
Implement delta encoding to minimize the stack memory footprint, addressing the potentially large memory demands for storing these pointers on 64-bit systems.
The stacktrace is already stored in the stackdepot. Shouldn't it be possible to take a reference to the stackdepot entry inside the critical section and then use that reference outside of the critical section for printing?
Quoted 262 lines
Signed-off-by Alessandro Carminati <[email protected]>---
``` [ 159.247069] BUG: sleeping function called from invalid context at kernel/locking/spinlock_rt.c:48 [ 159.247193] in_atomic(): 1, irqs_disabled(): 1, non_block: 0, pid: 136, name: cat [ 159.247241] preempt_count: 1, expected: 0 [ 159.247277] RCU nest depth: 2, expected: 2 [ 159.247388] 6 locks held by cat/136: [ 159.247438] #0: ffff32e64bcbf950 (&p->lock){+.+.}-{3:3}, at: seq_read_iter+0xb8/0xe30 [ 159.248835] #1: ffffafe6aaa9dea0 (scan_mutex){+.+.}-{3:3}, at: kmemleak_seq_start+0x34/0x128 [ 159.249053] #3: ffff32e6546b1cd0 (&object->lock){....}-{2:2}, at: kmemleak_seq_show+0x3c/0x1e0 [ 159.249127] #4: ffffafe6aa8d8560 (rcu_read_lock){....}-{1:2}, at: has_ns_capability_noaudit+0x8/0x1b0 [ 159.249205] #5: ffffafe6aabbc0f8 (notif_lock){+.+.}-{2:2}, at: avc_compute_av+0xc4/0x3d0 [ 159.249364] irq event stamp: 136660 [ 159.249407] hardirqs last enabled at (136659): [<ffffafe6a80fd7a0>] _raw_spin_unlock_irqrestore+0xa8/0xd8 [ 159.249465] hardirqs last disabled at (136660): [<ffffafe6a80fd85c>] _raw_spin_lock_irqsave+0x8c/0xb0 [ 159.249518] softirqs last enabled at (0): [<ffffafe6a5d50b28>] copy_process+0x11d8/0x3df8 [ 159.249571] softirqs last disabled at (0): [<0000000000000000>] 0x0 [ 159.249970] Preemption disabled at: [ 159.249988] [<ffffafe6a6598a4c>] kmemleak_seq_show+0x3c/0x1e0 [ 159.250609] CPU: 1 UID: 0 PID: 136 Comm: cat Tainted: G E 6.11.0-rt7+ #34 [ 159.250797] Tainted: [E]=UNSIGNED_MODULE [ 159.250822] Hardware name: linux,dummy-virt (DT) [ 159.251050] Call trace: [ 159.251079] dump_backtrace+0xa0/0x128 [ 159.251132] show_stack+0x1c/0x30 [ 159.251156] dump_stack_lvl+0xe8/0x198 [ 159.251180] dump_stack+0x18/0x20 [ 159.251227] rt_spin_lock+0x8c/0x1a8 [ 159.251273] avc_perm_nonode+0xa0/0x150 [ 159.251316] cred_has_capability.isra.0+0x118/0x218 [ 159.251340] selinux_capable+0x50/0x80 [ 159.251363] security_capable+0x7c/0xd0 [ 159.251388] has_ns_capability_noaudit+0x94/0x1b0 [ 159.251412] has_capability_noaudit+0x20/0x30 [ 159.251437] restricted_pointer+0x21c/0x4b0 [ 159.251461] pointer+0x298/0x760 [ 159.251482] vsnprintf+0x330/0xf70 [ 159.251504] seq_printf+0x178/0x218 [ 159.251526] print_unreferenced+0x1a4/0x2d0 [ 159.251551] kmemleak_seq_show+0xd0/0x1e0 [ 159.251576] seq_read_iter+0x354/0xe30 [ 159.251599] seq_read+0x250/0x378 [ 159.251622] full_proxy_read+0xd8/0x148 [ 159.251649] vfs_read+0x190/0x918 [ 159.251672] ksys_read+0xf0/0x1e0 [ 159.251693] __arm64_sys_read+0x70/0xa8 [ 159.251716] invoke_syscall.constprop.0+0xd4/0x1d8 [ 159.251767] el0_svc+0x50/0x158 [ 159.251813] el0t_64_sync+0x17c/0x180 ```I have considered three potential approaches to address this matter:
1. Remove Raw Pointer Printing
The simplest solution is to eliminate raw pointer printing from the report. This approach involves minimal changes to the kernel code and is straightforward to implement.While I am confident that omitting the raw address would result in negligible information loss in most scenarios, some may perceive it as a feature regression. Below is an example of the modification:
``` - warn_or_seq_printf(seq, " [<%pK>] %pS\n", ptr, ptr); + warn_or_seq_printf(seq, " %pS\n", ptr); ```This change may be acceptable since the %pS format outputs a hex string if no kallsyms are available. However, it modifies the original behavior, and in the kallsyms scenario, the raw pointer would no longer be present.
2. Modify SELinux to Avoid Sleeping Spinlocks
Another option is to alter the SELinux capability check to use non-sleeping spinlocks.
However, this approach is not advisable. The SELinux capability check is extensively used across the kernel and is far more critical than the kmemleak reporting feature.
Adapting it to address this rare issue could unnecessarily introduce latency across the entire kernel, particularly as kmemleak is rarely used in production environments.3. Move Stack Trace Printing Outside the Atomic Section
The third and preferred approach is to move the stack trace printing outside the atomic section. This would preserve the current functionality without modifying SELinux.The primary challenge here lies in making the backtrace pointers available after exiting the critical section, as they are captured within it. To address this, the backtrace pointers can be copied to a safe location, enabling access once the
raw_spinlockis released.Options for Creating a Safe Location for Backtrace Pointers Several strategies have been considered for storing the backtrace pointers safely:
* Dynamic Allocation
* Allocating memory with kmalloc cannot be done within araw_spinlockarea. UsingGFP_ATOMICis also infeasible.
* Since the code that prints the message is inside a loop, executed potentially multiple times, it is only within theraw_spinlocksection that we can determine whether allocation is needed.
* Allocating and deallocating memory on every loop iteration would be prohibitively expensive.
* Global Data Section
* In this strategy, the message would be printed after exiting theraw_spinlockprotected section.
* However, this approach risks data corruption if another occurrence of the issue arises before the first operation completes.
* Per-CPU Data
* The same concerns as with global data apply here. While data corruption is less likely, it is not impossible.
* Stack
* Using the stack is the best option since each thread has its own stack, ensuring data isolation. However, the size of the data poses a challenge.
* Exporting a full stack trace pointer list requires considerable space. A 32-level stack trace in a 64-bit system would require 256 bytes, which is contrary to best practices for stack size management.To mitigate this, I propose using delta encoding to store the addresses. This method reduces the size of each address from 8 bytes to 4 bytes on 64-bit systems. While this introduces some complexity, it significantly reduces memory usage and allows us to preserve the kmemleak reports in their current form.
mm/kmemleak.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 71 insertions(+), 7 deletions(-)mm/kmemleak.c +71
-7@@ -274,6 +274,44 @@ static void kmemleak_disable(void); pr_warn(fmt, ##__VA_ARGS__); \ } while (0) +#define PTR_STORAGE_OP_OK -1 +#define PTR_STORAGE_OP_FAIL 0 +#define PTR_STORAGE_CAPACITY 32 + +struct ptr_storage { +unsigned long base; +u32 data[PTR_STORAGE_CAPACITY ]; +int nr_entries; +}; + +static int ptr_storage_insert (unsigned long p,struct ptr_storage *s) +{ +unsigned long diff_data; + +if (s->nr_entries !=0 ) { + diff_data = s->base - p; +if (s->nr_entries <PTR_STORAGE_CAPACITY ) { + s->data[((s->nr_entries -1 ))] = diff_data &0xffffffff ; + s->nr_entries++; +return PTR_STORAGE_OP_OK ; + } +return PTR_STORAGE_OP_FAIL ; + } + s->base = p; + s->nr_entries++; +return PTR_STORAGE_OP_OK ; +} + +static void *ptr_storage_get (struct ptr_storage *s,int item_no) +{ +if (item_no < s->nr_entries && item_no >0 ) +return (void *)s->base - (s32 )s->data[(item_no -1 )]; + +if (item_no ==0 ) +return (void *)s->base; +return NULL ; +} +static void warn_or_seq_hex_dump (struct seq_file *seq,int prefix_type,int rowsize,int groupsize,const void *buf,size_t len,bool ascii)@@ -357,11 +395,13 @@ static bool unreferenced_object(struct kmemleak_object *object)* print_unreferenced function must be called with the object->lock held. */ static void print_unreferenced (struct seq_file *seq, -struct kmemleak_object *object) +struct kmemleak_object *object, +struct ptr_storage *s) {int i;unsigned long *entries;unsigned int nr_entries; +unsigned long tmp; nr_entries =stack_depot_fetch (object->trace_handle, &entries); warn_or_seq_printf(seq, "unreferenced object 0x%08lx (size %zu):\n",@@ -372,8 +412,8 @@ static void print_unreferenced(struct seq_file *seq,warn_or_seq_printf (seq," backtrace (crc %x):\n" , object->checksum);for (i =0 ; i < nr_entries; i++) { -void *ptr = (void *)entries[i]; -warn_or_seq_printf (seq," [<%pK>] %pS\n" , ptr, ptr); + tmp = (unsigned long )entries[i]; +ptr_storage_insert (tmp, s); } }@@ -1625,6 +1665,10 @@ static void kmemleak_scan(void)struct zone *zone;int __maybe_unused i;int new_leaks =0 ; +struct ptr_storage s = {0 }; +bool do_print = false; +void *tmp; +int inx; jiffies_last_scan = jiffies;@@ -1783,12 +1827,20 @@ static void kmemleak_scan(void) !(object->flags & OBJECT_REPORTED)) { object->flags |=OBJECT_REPORTED ; -if (kmemleak_verbose) -print_unreferenced (NULL , object); +if (kmemleak_verbose) { +print_unreferenced (NULL , object, &s); + do_print = true; + } new_leaks++; }raw_spin_unlock_irq (&object->lock); +if (kmemleak_verbose && do_print) { +for (inx =0 ; inx < s.nr_entries; inx++) { + tmp =ptr_storage_get (&s, i); +warn_or_seq_printf (NULL ," [<%pK>] %pS\n" , tmp, tmp); + } + } }rcu_read_unlock ();@@ -1939,11 +1991,23 @@ static int kmemleak_seq_show(struct seq_file *seq, void *v) {struct kmemleak_object *object = v;unsigned long flags; +struct ptr_storage s = {0 }; +void *tmp; +int i; +bool do_print = false;raw_spin_lock_irqsave (&object->lock, flags); -if ((object->flags &OBJECT_REPORTED ) &&unreferenced_object (object)) -print_unreferenced (seq, object); +if ((object->flags &OBJECT_REPORTED ) &&unreferenced_object (object)) { +print_unreferenced (seq, object, &s); + do_print = true; + }raw_spin_unlock_irqrestore (&object->lock, flags); +if (do_print) { +for (i =0 ; i < s.nr_entries; i++) { + tmp =ptr_storage_get (&s, i); +warn_or_seq_printf (seq," [<%pK>] %pS\n" , tmp, tmp); + } + }return 0 ; }Signature · 1 line
2.34.1
Hello Thomas,
Thanks for your suggestion.
Quoted 25 lines · Thomas Weißschuh
Quoted 1 line · Alessandro Carminati
Address a bug in the kernel that triggers a "sleeping function called
from
Quoted 10 lines
invalid context" warning when
/sys/kernel/debug/kmemleakis printed under specific conditions:
-CONFIG_PREEMPT_RT=y
- Set SELinux as the LSM for the system
- Setkptr_restrictto 1
- kmemleak buffer contains at least one item
Ensure the kmemleak buffer contains at least one item
Commit8c96f1bc6fc49c724c4cdd22d3e99260263b7384("mm/kmemleak: turnkmemleak_lockandobject->locktoraw_spinlock_t") introduced a change wherekmemleak_seq_showis executed in atomic context within the RTkernel.
Quoted 1 line
However, the SELinux capability check in this function flow still relies
on
Quoted 4 lines
regular spinlocks, leading to potential race conditions that trigger an error when printing the kmemleak backtrace.
Move the backtrace printing out of the critical section. Use a stack variable to store the backtrace pointers, avoiding spinlocks in theatomic
Quoted 4 lines
context.
Implement delta encoding to minimize the stack memory footprint, addressing the potentially large memory demands for storing these
Quoted 8 lines
pointers
Quoted 1 line
on 64-bit systems.
The stacktrace is already stored in the stackdepot. Shouldn't it be possible to take a reference to the stackdepot entry inside the critical section and then use that reference outside of the critical section for printing?
Yes, it is indeed possible to do that.
However, kmemleak operates in such a way that entries can be deleted, for example, if they are found to be false positives.
My concern was that using a reference to a potentially deleted entry could cause problems. But after considering your suggestion, I verified that stack_depot_put, which is used to delete a stack depot entry, does not appear to be called when a kmemleak object is deleted.
This makes me question whether that is the intended behavior.
As things currently stand, it seems possible to remove all the code I added to store the trace in the stack and instead directly use the stack_depot handle.
I would appreciate feedback from kmemleak and stackdepot experts regarding this approach.
Quoted 292 lines
Quoted 5 lines
Signed-off-by Alessandro Carminati <[email protected]>---
```[ 159.247069] BUG: sleeping function called from invalid context at
kernel/locking/spinlock_rt.c:48Quoted 1 line
[ 159.247193] in_atomic(): 1, irqs_disabled(): 1, non_block: 0, pid:136, name: cat
Quoted 4 lines
[ 159.247241] preempt_count: 1, expected: 0 [ 159.247277] RCU nest depth: 2, expected: 2 [ 159.247388] 6 locks held by cat/136: [ 159.247438] #0: ffff32e64bcbf950 (&p->lock){+.+.}-{3:3}, at:
seq_read_iter+0xb8/0xe30Quoted 1 line
[ 159.248835] #1: ffffafe6aaa9dea0 (scan_mutex){+.+.}-{3:3}, at:
kmemleak_seq_start+0x34/0x128Quoted 1 line
[ 159.249053] #3: ffff32e6546b1cd0 (&object->lock){....}-{2:2}, at:
kmemleak_seq_show+0x3c/0x1e0Quoted 1 line
[ 159.249127] #4: ffffafe6aa8d8560 (rcu_read_lock){....}-{1:2}, at:
has_ns_capability_noaudit+0x8/0x1b0Quoted 1 line
[ 159.249205] #5: ffffafe6aabbc0f8 (notif_lock){+.+.}-{2:2}, at:
avc_compute_av+0xc4/0x3d0Quoted 2 lines
[ 159.249364] irq event stamp: 136660 [ 159.249407] hardirqs last enabled at (136659): [<ffffafe6a80fd7a0>]
_raw_spin_unlock_irqrestore+0xa8/0xd8Quoted 1 line
[ 159.249465] hardirqs last disabled at (136660): [<ffffafe6a80fd85c>]
_raw_spin_lock_irqsave+0x8c/0xb0Quoted 1 line
[ 159.249518] softirqs last enabled at (0): [<ffffafe6a5d50b28>]
copy_process+0x11d8/0x3df8Quoted 4 lines
[ 159.249571] softirqs last disabled at (0): [<0000000000000000>] 0x0 [ 159.249970] Preemption disabled at: [ 159.249988] [<ffffafe6a6598a4c>] kmemleak_seq_show+0x3c/0x1e0 [ 159.250609] CPU: 1 UID: 0 PID: 136 Comm: cat Tainted: G E6.11.0-rt7+ #34Quoted 29 lines
[ 159.250797] Tainted: [E]=UNSIGNED_MODULE [ 159.250822] Hardware name: linux,dummy-virt (DT) [ 159.251050] Call trace: [ 159.251079] dump_backtrace+0xa0/0x128 [ 159.251132] show_stack+0x1c/0x30 [ 159.251156] dump_stack_lvl+0xe8/0x198 [ 159.251180] dump_stack+0x18/0x20 [ 159.251227] rt_spin_lock+0x8c/0x1a8 [ 159.251273] avc_perm_nonode+0xa0/0x150 [ 159.251316] cred_has_capability.isra.0+0x118/0x218 [ 159.251340] selinux_capable+0x50/0x80 [ 159.251363] security_capable+0x7c/0xd0 [ 159.251388] has_ns_capability_noaudit+0x94/0x1b0 [ 159.251412] has_capability_noaudit+0x20/0x30 [ 159.251437] restricted_pointer+0x21c/0x4b0 [ 159.251461] pointer+0x298/0x760 [ 159.251482] vsnprintf+0x330/0xf70 [ 159.251504] seq_printf+0x178/0x218 [ 159.251526] print_unreferenced+0x1a4/0x2d0 [ 159.251551] kmemleak_seq_show+0xd0/0x1e0 [ 159.251576] seq_read_iter+0x354/0xe30 [ 159.251599] seq_read+0x250/0x378 [ 159.251622] full_proxy_read+0xd8/0x148 [ 159.251649] vfs_read+0x190/0x918 [ 159.251672] ksys_read+0xf0/0x1e0 [ 159.251693] __arm64_sys_read+0x70/0xa8 [ 159.251716] invoke_syscall.constprop.0+0xd4/0x1d8 [ 159.251767] el0_svc+0x50/0x158 [ 159.251813] el0t_64_sync+0x17c/0x180Quoted 5 lines
```
I have considered three potential approaches to address this matter:1. Remove Raw Pointer Printing
The simplest solution is to eliminate raw pointer printing from thereport.
Quoted 24 lines
This approach involves minimal changes to the kernel code and is straightforward to implement.
While I am confident that omitting the raw address would result in negligible information loss in most scenarios, some may perceive it as a feature regression. Below is an example of the modification:
``` - warn_or_seq_printf(seq, " [<%pK>] %pS\n", ptr, ptr); + warn_or_seq_printf(seq, " %pS\n", ptr); ```This change may be acceptable since the %pS format outputs a hex string if no kallsyms are available. However, it modifies the original behavior, and in the kallsyms scenario, the raw pointer would no longer be present.
2. Modify SELinux to Avoid Sleeping Spinlocks
Another option is to alter the SELinux capability check to use non-sleeping spinlocks.
However, this approach is not advisable. The SELinux capability check is extensively used across the kernel and is far more critical than the kmemleak reporting feature.
Adapting it to address this rare issue could unnecessarily introduce latency across the entire kernel, particularly as kmemleak is rarely used in production environments.Quoted 6 lines
3. Move Stack Trace Printing Outside the Atomic Section
The third and preferred approach is to move the stack trace printing outside the atomic section. This would preserve the current functionality without modifying SELinux.The primary challenge here lies in making the backtrace pointers
available
Quoted 6 lines
after exiting the critical section, as they are captured within it. To address this, the backtrace pointers can be copied to a safe location, enabling access once the
raw_spinlockis released.Options for Creating a Safe Location for Backtrace Pointers Several strategies have been considered for storing the backtrace
pointers
Quoted 14 lines
safely:
* Dynamic Allocation
* Allocating memory with kmalloc cannot be done within araw_spinlockarea. UsingGFP_ATOMICis also infeasible.
* Since the code that prints the message is inside a loop, executed potentially multiple times, it is only within theraw_spinlocksection that we can determine whether allocation is needed.
* Allocating and deallocating memory on every loop iteration would be prohibitively expensive.
* Global Data Section
* In this strategy, the message would be printed after exiting theraw_spinlockprotected section.
* However, this approach risks data corruption if another occurrence of the issue arises before the first operation completes.Quoted 8 lines
* Per-CPU Data
* The same concerns as with global data apply here. While data corruption is less likely, it is not impossible.
* Stack
* Using the stack is the best option since each thread has its own stack, ensuring data isolation. However, the size of the data poses a challenge.
* Exporting a full stack trace pointer list requires considerablespace.
Quoted 7 lines
A 32-level stack trace in a 64-bit system would require 256 bytes, which is contrary to best practices for stack size management.
To mitigate this, I propose using delta encoding to store the addresses. This method reduces the size of each address from 8 bytes to 4 bytes on 64-bit systems. While this introduces some complexity, it significantly reduces memory usage and allows us to preserve the kmemleak reports in
their
Quoted 20 lines
current form.
mm/kmemleak.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 71 insertions(+), 7 deletions(-)mm/kmemleak.c +8
-0@@ -274,6 +274,44 @@ static void kmemleak_disable(void); pr_warn(fmt, ##__VA_ARGS__); \ } while (0) +#define PTR_STORAGE_OP_OK -1 +#define PTR_STORAGE_OP_FAIL 0 +#define PTR_STORAGE_CAPACITY 32 + +struct ptr_storage { +unsigned long base; +u32 data[PTR_STORAGE_CAPACITY ]; +int nr_entries;mm/kmemleak.c static void kmemleak_disable(void); · line 294
Quoted 10 lines
mm/kmemleak.c +10
-0+}; + +static int ptr_storage_insert (unsigned long p,struct ptr_storage *s) +{ +unsigned long diff_data; + +if (s->nr_entries !=0 ) { + diff_data = s->base - p; +if (s->nr_entries <PTR_STORAGE_CAPACITY ) { + s->data[((s->nr_entries - 1))] = diff_data &
0xffffffff;Quoted 12 lines
mm/kmemleak.c +12
-0+ s->nr_entries++; +return PTR_STORAGE_OP_OK ; + } +return PTR_STORAGE_OP_FAIL ; + } + s->base = p; + s->nr_entries++; +return PTR_STORAGE_OP_OK ; +} + +static void *ptr_storage_get (struct ptr_storage *s,int item_no) +{mm/kmemleak.c static void kmemleak_disable(void);
Quoted 10 lines
mm/kmemleak.c +8
-0+if (item_no < s->nr_entries && item_no >0 ) +return (void *)s->base - (s32 )s->data[(item_no -1 )]; + +if (item_no ==0 ) +return (void *)s->base; +return NULL ; +} +static void warn_or_seq_hex_dump (struct seq_file *seq,int prefix_type, int rowsize, int groupsize, const void*buf,
mm/kmemleak.c static bool unreferenced_object(struct
Quoted 2 lines
mm/kmemleak.c +0
-0size_t len, bool ascii)@@ -357,11 +395,13 @@ static bool unreferenced_object(struct
kmemleak_object*object)mm/kmemleak.c static bool unreferenced_object(struct · line 395
Quoted 1 line
mm/kmemleak.c +0
-0* print_unreferenced function must be called with the object->lock held.
mm/kmemleak.c static bool unreferenced_object(struct kmemleak_object *object)
Quoted 13 lines
mm/kmemleak.c +3
-1*/ static void print_unreferenced (struct seq_file *seq, -struct kmemleak_object *object) +struct kmemleak_object *object, +struct ptr_storage *s) {int i;unsigned long *entries;unsigned int nr_entries; +unsigned long tmp; nr_entries =stack_depot_fetch (object->trace_handle, &entries); warn_or_seq_printf(seq, "unreferenced object 0x%08lx (size%zu):\n",
mm/kmemleak.c static void print_unreferenced(struct seq_file *seq, · line 412
Quoted 2 lines
mm/kmemleak.c +0
-0@@ -372,8 +412,8 @@ static void print_unreferenced(struct seq_file *seq, warn_or_seq_printf(seq, " backtrace (crc %x):\n",
object->checksum);Quoted 28 lines
mm/kmemleak.c +8
-4for (i =0 ; i < nr_entries; i++) { -void *ptr = (void *)entries[i]; -warn_or_seq_printf (seq," [<%pK>] %pS\n" , ptr, ptr); + tmp = (unsigned long )entries[i]; +ptr_storage_insert (tmp, s); } }@@ -1625,6 +1665,10 @@ static void kmemleak_scan(void)struct zone *zone;int __maybe_unused i;int new_leaks =0 ; +struct ptr_storage s = {0 }; +bool do_print = false; +void *tmp; +int inx; jiffies_last_scan = jiffies;@@ -1783,12 +1827,20 @@ static void kmemleak_scan(void) !(object->flags & OBJECT_REPORTED)) { object->flags |=OBJECT_REPORTED ; -if (kmemleak_verbose) -print_unreferenced (NULL , object); +if (kmemleak_verbose) { +print_unreferenced (NULL , object, &s);mm/kmemleak.c static void kmemleak_scan(void) · line 1841
Quoted 10 lines
mm/kmemleak.c +6
-0+ do_print = true; + } new_leaks++; }raw_spin_unlock_irq (&object->lock); +if (kmemleak_verbose && do_print) { +for (inx =0 ; inx < s.nr_entries; inx++) { + tmp =ptr_storage_get (&s, i); + warn_or_seq_printf(NULL, " [<%pK>]%pS\n", tmp, tmp);
mm/kmemleak.c static int kmemleak_seq_show(struct seq_file
Quoted 6 lines
mm/kmemleak.c +2
-0+ } + } }rcu_read_unlock ();@@ -1939,11 +1991,23 @@ static int kmemleak_seq_show(struct seq_file*seq, void *v)
mm/kmemleak.c static int kmemleak_seq_show(struct seq_file *seq, void *v)
Quoted 10 lines
mm/kmemleak.c +4
-1{struct kmemleak_object *object = v;unsigned long flags; +struct ptr_storage s = {0 }; +void *tmp; +int i; +bool do_print = false;raw_spin_lock_irqsave (&object->lock, flags); - if ((object->flags & OBJECT_REPORTED) &&
unreferenced_object(object))mm/kmemleak.c static int kmemleak_seq_show(struct seq_file · line 2000
Quoted 2 lines
mm/kmemleak.c +1
-1-print_unreferenced (seq, object); + if ((object->flags & OBJECT_REPORTED) &&
unreferenced_object(object)) {mm/kmemleak.c static int kmemleak_seq_show(struct seq_file *seq, void *v)
Quoted 8 lines
mm/kmemleak.c +7
-0+print_unreferenced (seq, object, &s); + do_print = true; + }raw_spin_unlock_irqrestore (&object->lock, flags); +if (do_print) { +for (i =0 ; i < s.nr_entries; i++) { + tmp =ptr_storage_get (&s, i); + warn_or_seq_printf(seq, " [<%pK>] %pS\n", tmp,tmp);
Quoted 8 lines
mm/kmemleak.c +2
-1+ } + }return 0 ; } --2.34.1
Signature · 2 lines
--- 172
Hi Alessandro,
Quoted 42 lines · Alessandro Carminati
Hello Thomas,
Thanks for your suggestion.Quoted 25 lines · Thomas Weißschuh
Quoted 1 line · Alessandro Carminati
Address a bug in the kernel that triggers a "sleeping function called
from
Quoted 10 lines
invalid context" warning when
/sys/kernel/debug/kmemleakis printed under specific conditions:
-CONFIG_PREEMPT_RT=y
- Set SELinux as the LSM for the system
- Setkptr_restrictto 1
- kmemleak buffer contains at least one item
Ensure the kmemleak buffer contains at least one item
Commit8c96f1bc6fc49c724c4cdd22d3e99260263b7384("mm/kmemleak: turnkmemleak_lockandobject->locktoraw_spinlock_t") introduced a change wherekmemleak_seq_showis executed in atomic context within the RTkernel.
Quoted 1 line
However, the SELinux capability check in this function flow still relies
on
Quoted 4 lines
regular spinlocks, leading to potential race conditions that trigger an error when printing the kmemleak backtrace.
Move the backtrace printing out of the critical section. Use a stack variable to store the backtrace pointers, avoiding spinlocks in theatomic
Quoted 4 lines
context.
Implement delta encoding to minimize the stack memory footprint, addressing the potentially large memory demands for storing these
Quoted 8 lines
pointers
Quoted 1 line
on 64-bit systems.
The stacktrace is already stored in the stackdepot. Shouldn't it be possible to take a reference to the stackdepot entry inside the critical section and then use that reference outside of the critical section for printing?
Yes, it is indeed possible to do that.
Quoted 7 lines
However, kmemleak operates in such a way that entries can be deleted, for example, if they are found to be false positives.
My concern was that using a reference to a potentially deleted entry could cause problems. But after considering your suggestion, I verified thatstack_depot_put, which is used to delete a stack depot entry, does not appear to be called when a kmemleak object is deleted.
This makes me question whether that is the intended behavior.
I also looked a bit more into stackdepot.
It seems that it is intentional that by default no stacks are ever evicted. If actual refcounting and eviction is desired then stack_depot_save_flags(STACK_DEPOT_FLAG_GET) and stack_depot_put() have to be used.
If kmemleak wants to switch to that API, there would be a slight problem when printing the stacktrace outside of the locked section, as currently there is no API available to increment the refcount of an existing depot_stack_handle_t.
But that's not relevant for now anyways and such a function could be implemented, even now in a roundabout way through stack_depot_save_flags(stack_depot_fetch(handle)).
Quoted 304 lines
As things currently stand, it seems possible to remove all the code I added to store the trace in the stack and instead directly use the
stack_depothandle.I would appreciate feedback from kmemleak and stackdepot experts regarding this approach.
Quoted 292 lines
Quoted 5 lines
Signed-off-by Alessandro Carminati <[email protected]>---
```[ 159.247069] BUG: sleeping function called from invalid context at
kernel/locking/spinlock_rt.c:48Quoted 1 line
[ 159.247193] in_atomic(): 1, irqs_disabled(): 1, non_block: 0, pid:136, name: cat
Quoted 4 lines
[ 159.247241] preempt_count: 1, expected: 0 [ 159.247277] RCU nest depth: 2, expected: 2 [ 159.247388] 6 locks held by cat/136: [ 159.247438] #0: ffff32e64bcbf950 (&p->lock){+.+.}-{3:3}, at:
seq_read_iter+0xb8/0xe30Quoted 1 line
[ 159.248835] #1: ffffafe6aaa9dea0 (scan_mutex){+.+.}-{3:3}, at:
kmemleak_seq_start+0x34/0x128Quoted 1 line
[ 159.249053] #3: ffff32e6546b1cd0 (&object->lock){....}-{2:2}, at:
kmemleak_seq_show+0x3c/0x1e0Quoted 1 line
[ 159.249127] #4: ffffafe6aa8d8560 (rcu_read_lock){....}-{1:2}, at:
has_ns_capability_noaudit+0x8/0x1b0Quoted 1 line
[ 159.249205] #5: ffffafe6aabbc0f8 (notif_lock){+.+.}-{2:2}, at:
avc_compute_av+0xc4/0x3d0Quoted 2 lines
[ 159.249364] irq event stamp: 136660 [ 159.249407] hardirqs last enabled at (136659): [<ffffafe6a80fd7a0>]
_raw_spin_unlock_irqrestore+0xa8/0xd8Quoted 1 line
[ 159.249465] hardirqs last disabled at (136660): [<ffffafe6a80fd85c>]
_raw_spin_lock_irqsave+0x8c/0xb0Quoted 1 line
[ 159.249518] softirqs last enabled at (0): [<ffffafe6a5d50b28>]
copy_process+0x11d8/0x3df8Quoted 4 lines
[ 159.249571] softirqs last disabled at (0): [<0000000000000000>] 0x0 [ 159.249970] Preemption disabled at: [ 159.249988] [<ffffafe6a6598a4c>] kmemleak_seq_show+0x3c/0x1e0 [ 159.250609] CPU: 1 UID: 0 PID: 136 Comm: cat Tainted: G E6.11.0-rt7+ #34Quoted 29 lines
[ 159.250797] Tainted: [E]=UNSIGNED_MODULE [ 159.250822] Hardware name: linux,dummy-virt (DT) [ 159.251050] Call trace: [ 159.251079] dump_backtrace+0xa0/0x128 [ 159.251132] show_stack+0x1c/0x30 [ 159.251156] dump_stack_lvl+0xe8/0x198 [ 159.251180] dump_stack+0x18/0x20 [ 159.251227] rt_spin_lock+0x8c/0x1a8 [ 159.251273] avc_perm_nonode+0xa0/0x150 [ 159.251316] cred_has_capability.isra.0+0x118/0x218 [ 159.251340] selinux_capable+0x50/0x80 [ 159.251363] security_capable+0x7c/0xd0 [ 159.251388] has_ns_capability_noaudit+0x94/0x1b0 [ 159.251412] has_capability_noaudit+0x20/0x30 [ 159.251437] restricted_pointer+0x21c/0x4b0 [ 159.251461] pointer+0x298/0x760 [ 159.251482] vsnprintf+0x330/0xf70 [ 159.251504] seq_printf+0x178/0x218 [ 159.251526] print_unreferenced+0x1a4/0x2d0 [ 159.251551] kmemleak_seq_show+0xd0/0x1e0 [ 159.251576] seq_read_iter+0x354/0xe30 [ 159.251599] seq_read+0x250/0x378 [ 159.251622] full_proxy_read+0xd8/0x148 [ 159.251649] vfs_read+0x190/0x918 [ 159.251672] ksys_read+0xf0/0x1e0 [ 159.251693] __arm64_sys_read+0x70/0xa8 [ 159.251716] invoke_syscall.constprop.0+0xd4/0x1d8 [ 159.251767] el0_svc+0x50/0x158 [ 159.251813] el0t_64_sync+0x17c/0x180Quoted 5 lines
```
I have considered three potential approaches to address this matter:1. Remove Raw Pointer Printing
The simplest solution is to eliminate raw pointer printing from thereport.
Quoted 24 lines
This approach involves minimal changes to the kernel code and is straightforward to implement.
While I am confident that omitting the raw address would result in negligible information loss in most scenarios, some may perceive it as a feature regression. Below is an example of the modification:
``` - warn_or_seq_printf(seq, " [<%pK>] %pS\n", ptr, ptr); + warn_or_seq_printf(seq, " %pS\n", ptr); ```This change may be acceptable since the %pS format outputs a hex string if no kallsyms are available. However, it modifies the original behavior, and in the kallsyms scenario, the raw pointer would no longer be present.
2. Modify SELinux to Avoid Sleeping Spinlocks
Another option is to alter the SELinux capability check to use non-sleeping spinlocks.
However, this approach is not advisable. The SELinux capability check is extensively used across the kernel and is far more critical than the kmemleak reporting feature.
Adapting it to address this rare issue could unnecessarily introduce latency across the entire kernel, particularly as kmemleak is rarely used in production environments.Quoted 6 lines
3. Move Stack Trace Printing Outside the Atomic Section
The third and preferred approach is to move the stack trace printing outside the atomic section. This would preserve the current functionality without modifying SELinux.The primary challenge here lies in making the backtrace pointers
available
Quoted 6 lines
after exiting the critical section, as they are captured within it. To address this, the backtrace pointers can be copied to a safe location, enabling access once the
raw_spinlockis released.Options for Creating a Safe Location for Backtrace Pointers Several strategies have been considered for storing the backtrace
pointers
Quoted 14 lines
safely:
* Dynamic Allocation
* Allocating memory with kmalloc cannot be done within araw_spinlockarea. UsingGFP_ATOMICis also infeasible.
* Since the code that prints the message is inside a loop, executed potentially multiple times, it is only within theraw_spinlocksection that we can determine whether allocation is needed.
* Allocating and deallocating memory on every loop iteration would be prohibitively expensive.
* Global Data Section
* In this strategy, the message would be printed after exiting theraw_spinlockprotected section.
* However, this approach risks data corruption if another occurrence of the issue arises before the first operation completes.Quoted 8 lines
* Per-CPU Data
* The same concerns as with global data apply here. While data corruption is less likely, it is not impossible.
* Stack
* Using the stack is the best option since each thread has its own stack, ensuring data isolation. However, the size of the data poses a challenge.
* Exporting a full stack trace pointer list requires considerablespace.
Quoted 7 lines
A 32-level stack trace in a 64-bit system would require 256 bytes, which is contrary to best practices for stack size management.
To mitigate this, I propose using delta encoding to store the addresses. This method reduces the size of each address from 8 bytes to 4 bytes on 64-bit systems. While this introduces some complexity, it significantly reduces memory usage and allows us to preserve the kmemleak reports in
their
Quoted 20 lines
current form.
mm/kmemleak.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 71 insertions(+), 7 deletions(-)mm/kmemleak.c +8
-0@@ -274,6 +274,44 @@ static void kmemleak_disable(void); pr_warn(fmt, ##__VA_ARGS__); \ } while (0) +#define PTR_STORAGE_OP_OK -1 +#define PTR_STORAGE_OP_FAIL 0 +#define PTR_STORAGE_CAPACITY 32 + +struct ptr_storage { +unsigned long base; +u32 data[PTR_STORAGE_CAPACITY ]; +int nr_entries;mm/kmemleak.c static void kmemleak_disable(void); · line 294
Quoted 10 lines
mm/kmemleak.c +10
-0+}; + +static int ptr_storage_insert (unsigned long p,struct ptr_storage *s) +{ +unsigned long diff_data; + +if (s->nr_entries !=0 ) { + diff_data = s->base - p; +if (s->nr_entries <PTR_STORAGE_CAPACITY ) { + s->data[((s->nr_entries - 1))] = diff_data &
0xffffffff;Quoted 12 lines
mm/kmemleak.c +12
-0+ s->nr_entries++; +return PTR_STORAGE_OP_OK ; + } +return PTR_STORAGE_OP_FAIL ; + } + s->base = p; + s->nr_entries++; +return PTR_STORAGE_OP_OK ; +} + +static void *ptr_storage_get (struct ptr_storage *s,int item_no) +{mm/kmemleak.c static void kmemleak_disable(void);
Quoted 10 lines
mm/kmemleak.c +8
-0+if (item_no < s->nr_entries && item_no >0 ) +return (void *)s->base - (s32 )s->data[(item_no -1 )]; + +if (item_no ==0 ) +return (void *)s->base; +return NULL ; +} +static void warn_or_seq_hex_dump (struct seq_file *seq,int prefix_type, int rowsize, int groupsize, const void*buf,
mm/kmemleak.c static bool unreferenced_object(struct
Quoted 2 lines
mm/kmemleak.c +0
-0size_t len, bool ascii)@@ -357,11 +395,13 @@ static bool unreferenced_object(struct
kmemleak_object*object)mm/kmemleak.c static bool unreferenced_object(struct · line 395
Quoted 1 line
mm/kmemleak.c +0
-0* print_unreferenced function must be called with the object->lock held.
mm/kmemleak.c static bool unreferenced_object(struct kmemleak_object *object)
Quoted 13 lines
mm/kmemleak.c +3
-1*/ static void print_unreferenced (struct seq_file *seq, -struct kmemleak_object *object) +struct kmemleak_object *object, +struct ptr_storage *s) {int i;unsigned long *entries;unsigned int nr_entries; +unsigned long tmp; nr_entries =stack_depot_fetch (object->trace_handle, &entries); warn_or_seq_printf(seq, "unreferenced object 0x%08lx (size%zu):\n",
mm/kmemleak.c static void print_unreferenced(struct seq_file *seq, · line 412
Quoted 2 lines
mm/kmemleak.c +0
-0@@ -372,8 +412,8 @@ static void print_unreferenced(struct seq_file *seq, warn_or_seq_printf(seq, " backtrace (crc %x):\n",
object->checksum);Quoted 28 lines
mm/kmemleak.c +8
-4for (i =0 ; i < nr_entries; i++) { -void *ptr = (void *)entries[i]; -warn_or_seq_printf (seq," [<%pK>] %pS\n" , ptr, ptr); + tmp = (unsigned long )entries[i]; +ptr_storage_insert (tmp, s); } }@@ -1625,6 +1665,10 @@ static void kmemleak_scan(void)struct zone *zone;int __maybe_unused i;int new_leaks =0 ; +struct ptr_storage s = {0 }; +bool do_print = false; +void *tmp; +int inx; jiffies_last_scan = jiffies;@@ -1783,12 +1827,20 @@ static void kmemleak_scan(void) !(object->flags & OBJECT_REPORTED)) { object->flags |=OBJECT_REPORTED ; -if (kmemleak_verbose) -print_unreferenced (NULL , object); +if (kmemleak_verbose) { +print_unreferenced (NULL , object, &s);mm/kmemleak.c static void kmemleak_scan(void) · line 1841
Quoted 10 lines
mm/kmemleak.c +6
-0+ do_print = true; + } new_leaks++; }raw_spin_unlock_irq (&object->lock); +if (kmemleak_verbose && do_print) { +for (inx =0 ; inx < s.nr_entries; inx++) { + tmp =ptr_storage_get (&s, i); + warn_or_seq_printf(NULL, " [<%pK>]%pS\n", tmp, tmp);
mm/kmemleak.c static int kmemleak_seq_show(struct seq_file
Quoted 6 lines
mm/kmemleak.c +2
-0+ } + } }rcu_read_unlock ();@@ -1939,11 +1991,23 @@ static int kmemleak_seq_show(struct seq_file*seq, void *v)
mm/kmemleak.c static int kmemleak_seq_show(struct seq_file *seq, void *v)
Quoted 10 lines
mm/kmemleak.c +4
-1{struct kmemleak_object *object = v;unsigned long flags; +struct ptr_storage s = {0 }; +void *tmp; +int i; +bool do_print = false;raw_spin_lock_irqsave (&object->lock, flags); - if ((object->flags & OBJECT_REPORTED) &&
unreferenced_object(object))mm/kmemleak.c static int kmemleak_seq_show(struct seq_file · line 2000
Quoted 2 lines
mm/kmemleak.c +1
-1-print_unreferenced (seq, object); + if ((object->flags & OBJECT_REPORTED) &&
unreferenced_object(object)) {mm/kmemleak.c static int kmemleak_seq_show(struct seq_file *seq, void *v)
Quoted 8 lines
mm/kmemleak.c +7
-0+print_unreferenced (seq, object, &s); + do_print = true; + }raw_spin_unlock_irqrestore (&object->lock, flags); +if (do_print) { +for (i =0 ; i < s.nr_entries; i++) { + tmp =ptr_storage_get (&s, i); + warn_or_seq_printf(seq, " [<%pK>] %pS\n", tmp,tmp);
Quoted 8 lines
mm/kmemleak.c +2
-1+ } + }return 0 ; } --2.34.1
Signature · 2 lines
--- 172
kernel test robot
build botkernel test robot noticed the following build warnings:
Hi Alessandro,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:
[auto build test WARNING on akpm-mm/mm-everything]
url: https://github.com/intel-lab-lkp/linux/commits/Alessandro-Carminati/mm-kmemleak-Fix-sleeping-function-called-from-invalid-context-at-print-message/20241117-004112 base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything patch link: https://lore.kernel.org/r/20241115145410.114376-1-acarmina%40redhat.com patch subject: [RFC] mm/kmemleak: Fix sleeping function called from invalid context at print message config: um-allmodconfig (https://download.01.org/0day-ci/archive/20241120/[email protected]/config) compiler: clang version 20.0.0git (https://github.com/llvm/llvm-project 592c0fe55f6d9a811028b5f3507be91458ab2713) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241120/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
In file included from mm/kmemleak.c:84:
In file included from include/linux/memblock.h:12:
In file included from include/linux/mm.h:2211:
include/linux/vmstat.h:518:36: warning: arithmetic between different enumeration types ('enum node_stat_item' and 'enum lru_list') [-Wenum-enum-conversion]
518 | return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
| ~~~~~~~~~~~ ^ ~~~
In file included from mm/kmemleak.c:84:
In file included from include/linux/memblock.h:13:
In file included from arch/um/include/asm/dma.h:5:
In file included from arch/um/include/asm/io.h:24:
include/asm-generic/io.h:548:31: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
548 | val = __raw_readb(PCI_IOBASE + addr);
| ~~~~~~~~~~ ^
include/asm-generic/io.h:561:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
561 | val = __le16_to_cpu((__le16 __force)__raw_readw(PCI_IOBASE + addr));
| ~~~~~~~~~~ ^
include/uapi/linux/byteorder/little_endian.h:37:51: note: expanded from macro '__le16_to_cpu'
37 | #define __le16_to_cpu(x) ((__force __u16)(__le16)(x))
| ^
In file included from mm/kmemleak.c:84:
In file included from include/linux/memblock.h:13:
In file included from arch/um/include/asm/dma.h:5:
In file included from arch/um/include/asm/io.h:24:
include/asm-generic/io.h:574:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
574 | val = __le32_to_cpu((__le32 __force)__raw_readl(PCI_IOBASE + addr));
| ~~~~~~~~~~ ^
include/uapi/linux/byteorder/little_endian.h:35:51: note: expanded from macro '__le32_to_cpu'
35 | #define __le32_to_cpu(x) ((__force __u32)(__le32)(x))
| ^
In file included from mm/kmemleak.c:84:
In file included from include/linux/memblock.h:13:
In file included from arch/um/include/asm/dma.h:5:
In file included from arch/um/include/asm/io.h:24:
include/asm-generic/io.h:585:33: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
585 | __raw_writeb(value, PCI_IOBASE + addr);
| ~~~~~~~~~~ ^
include/asm-generic/io.h:595:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
595 | __raw_writew((u16 __force)cpu_to_le16(value), PCI_IOBASE + addr);
| ~~~~~~~~~~ ^
include/asm-generic/io.h:605:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
605 | __raw_writel((u32 __force)cpu_to_le32(value), PCI_IOBASE + addr);
| ~~~~~~~~~~ ^
include/asm-generic/io.h:693:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
693 | readsb(PCI_IOBASE + addr, buffer, count);
| ~~~~~~~~~~ ^
include/asm-generic/io.h:701:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
701 | readsw(PCI_IOBASE + addr, buffer, count);
| ~~~~~~~~~~ ^
include/asm-generic/io.h:709:20: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
709 | readsl(PCI_IOBASE + addr, buffer, count);
| ~~~~~~~~~~ ^
include/asm-generic/io.h:718:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
718 | writesb(PCI_IOBASE + addr, buffer, count);
| ~~~~~~~~~~ ^
include/asm-generic/io.h:727:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
727 | writesw(PCI_IOBASE + addr, buffer, count);
| ~~~~~~~~~~ ^
include/asm-generic/io.h:736:21: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
736 | writesl(PCI_IOBASE + addr, buffer, count);
| ~~~~~~~~~~ ^
>> mm/kmemleak.c:1879:31: warning: variable 'i' is uninitialized when used here [-Wuninitialized]
1879 | tmp = ptr_storage_get(&s, i);
| ^
mm/kmemleak.c:1705:22: note: initialize the variable 'i' to silence this warning
1705 | int __maybe_unused i;
| ^
| = 014 warnings generated.
vim +/i +1879 mm/kmemleak.c
1758
1759 /*
1760 * Struct page scanning for each node.
1761 */
1762 get_online_mems();
1763 for_each_populated_zone(zone) {
1764 unsigned long start_pfn = zone->zone_start_pfn;
1765 unsigned long end_pfn = zone_end_pfn(zone);
1766 unsigned long pfn;
1767
1768 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
1769 struct page *page = pfn_to_online_page(pfn);
1770
1771 if (!(pfn & 63))
1772 cond_resched();
1773
1774 if (!page)
1775 continue;
1776
1777 /* only scan pages belonging to this zone */
1778 if (page_zone(page) != zone)
1779 continue;
1780 /* only scan if page is in use */
1781 if (page_count(page) == 0)
1782 continue;
1783 scan_block(page, page + 1, NULL);
1784 }
1785 }
1786 put_online_mems();
1787
1788 /*
1789 * Scanning the task stacks (may introduce false negatives).
1790 */
1791 if (kmemleak_stack_scan) {
1792 struct task_struct *p, *g;
1793
1794 rcu_read_lock();
1795 for_each_process_thread(g, p) {
1796 void *stack = try_get_task_stack(p);
1797 if (stack) {
1798 scan_block(stack, stack + THREAD_SIZE, NULL);
1799 put_task_stack(p);
1800 }
1801 }
1802 rcu_read_unlock();
1803 }
1804
1805 /*
1806 * Scan the objects already referenced from the sections scanned
1807 * above.
1808 */
1809 scan_gray_list();
1810
1811 /*
1812 * Check for new or unreferenced objects modified since the previous
1813 * scan and color them gray until the next scan.
1814 */
1815 rcu_read_lock();
1816 list_for_each_entry_rcu(object, &object_list, object_list) {
1817 if (need_resched())
1818 kmemleak_cond_resched(object);
1819
1820 /*
1821 * This is racy but we can save the overhead of lock/unlock
1822 * calls. The missed objects, if any, should be caught in
1823 * the next scan.
1824 */
1825 if (!color_white(object))
1826 continue;
1827 raw_spin_lock_irq(&object->lock);
1828 if (color_white(object) && (object->flags & OBJECT_ALLOCATED)
1829 && update_checksum(object) && get_object(object)) {
1830 /* color it gray temporarily */
1831 object->count = object->min_count;
1832 list_add_tail(&object->gray_list, &gray_list);
1833 }
1834 raw_spin_unlock_irq(&object->lock);
1835 }
1836 rcu_read_unlock();
1837
1838 /*
1839 * Re-scan the gray list for modified unreferenced objects.
1840 */
1841 scan_gray_list();
1842
1843 /*
1844 * If scanning was stopped do not report any new unreferenced objects.
1845 */
1846 if (scan_should_stop())
1847 return;
1848
1849 /*
1850 * Scanning result reporting.
1851 */
1852 rcu_read_lock();
1853 list_for_each_entry_rcu(object, &object_list, object_list) {
1854 if (need_resched())
1855 kmemleak_cond_resched(object);
1856
1857 /*
1858 * This is racy but we can save the overhead of lock/unlock
1859 * calls. The missed objects, if any, should be caught in
1860 * the next scan.
1861 */
1862 if (!color_white(object))
1863 continue;
1864 raw_spin_lock_irq(&object->lock);
1865 if (unreferenced_object(object) &&
1866 !(object->flags & OBJECT_REPORTED)) {
1867 object->flags |= OBJECT_REPORTED;
1868
1869 if (kmemleak_verbose) {
1870 print_unreferenced(NULL, object, &s);
1871 do_print = true;
1872 }
1873
1874 new_leaks++;
1875 }
1876 raw_spin_unlock_irq(&object->lock);
1877 if (kmemleak_verbose && do_print) {
1878 for (inx = 0; inx < s.nr_entries; inx++) {
> 1879 tmp = ptr_storage_get(&s, i);
1880 warn_or_seq_printf(NULL, " [<%pK>] %pS\n", tmp, tmp);
1881 }
1882 }
1883 }
1884 rcu_read_unlock();
1885
1886 if (new_leaks) {
1887 kmemleak_found_leaks = true;
1888
1889 pr_info("%d new suspected memory leaks (see /sys/kernel/debug/kmemleak)\n",
1890 new_leaks);
1891 }
1892
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki