Skip to content

Commit 892d208

Browse files
committed
Merge tag 'kmemleak' of git://git.kernel.org/pub/scm/linux/kernel/git/cmarinas/linux
Kmemleak patches Main features: - Handle percpu memory allocations (only scanning them, not actually reporting). - Memory hotplug support. Usability improvements: - Show the origin of early allocations. - Report previously found leaks even if kmemleak has been disabled by some error. * tag 'kmemleak' of git://git.kernel.org/pub/scm/linux/kernel/git/cmarinas/linux: kmemleak: Add support for memory hotplug kmemleak: Handle percpu memory allocation kmemleak: Report previously found leaks even after an error kmemleak: When the early log buffer is exceeded, report the actual number kmemleak: Show where early_log issues come from
2 parents dca88ad + 029aeff commit 892d208

5 files changed

Lines changed: 155 additions & 28 deletions

File tree

‎Documentation/kmemleak.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ See the include/linux/kmemleak.h header for the functions prototype.
127127

128128
kmemleak_init - initialize kmemleak
129129
kmemleak_alloc - notify of a memory block allocation
130+
kmemleak_alloc_percpu - notify of a percpu memory block allocation
130131
kmemleak_free - notify of a memory block freeing
132+
kmemleak_free_part - notify of a partial memory block freeing
133+
kmemleak_free_percpu - notify of a percpu memory block freeing
131134
kmemleak_not_leak - mark an object as not a leak
132135
kmemleak_ignore - do not scan or report an object as leak
133136
kmemleak_scan_area - add scan areas inside a memory block

‎include/linux/kmemleak.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
extern void kmemleak_init(void) __ref;
2727
extern void kmemleak_alloc(const void *ptr, size_t size, int min_count,
2828
gfp_t gfp) __ref;
29+
extern void kmemleak_alloc_percpu(const void __percpu *ptr, size_t size) __ref;
2930
extern void kmemleak_free(const void *ptr) __ref;
3031
extern void kmemleak_free_part(const void *ptr, size_t size) __ref;
32+
extern void kmemleak_free_percpu(const void __percpu *ptr) __ref;
3133
extern void kmemleak_padding(const void *ptr, unsigned long offset,
3234
size_t size) __ref;
3335
extern void kmemleak_not_leak(const void *ptr) __ref;
@@ -68,6 +70,9 @@ static inline void kmemleak_alloc_recursive(const void *ptr, size_t size,
6870
gfp_t gfp)
6971
{
7072
}
73+
static inline void kmemleak_alloc_percpu(const void __percpu *ptr, size_t size)
74+
{
75+
}
7176
static inline void kmemleak_free(const void *ptr)
7277
{
7378
}
@@ -77,6 +82,9 @@ static inline void kmemleak_free_part(const void *ptr, size_t size)
7782
static inline void kmemleak_free_recursive(const void *ptr, unsigned long flags)
7883
{
7984
}
85+
static inline void kmemleak_free_percpu(const void __percpu *ptr)
86+
{
87+
}
8088
static inline void kmemleak_not_leak(const void *ptr)
8189
{
8290
}

‎lib/Kconfig.debug‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ config SLUB_STATS
414414

415415
config DEBUG_KMEMLEAK
416416
bool "Kernel memory leak detector"
417-
depends on DEBUG_KERNEL && EXPERIMENTAL && !MEMORY_HOTPLUG && \
417+
depends on DEBUG_KERNEL && EXPERIMENTAL && \
418418
(X86 || ARM || PPC || MIPS || S390 || SPARC64 || SUPERH || MICROBLAZE || TILE)
419419

420420
select DEBUG_FS

‎mm/kmemleak.c‎

Lines changed: 132 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100

101101
#include <linux/kmemcheck.h>
102102
#include <linux/kmemleak.h>
103+
#include <linux/memory_hotplug.h>
103104

104105
/*
105106
* Kmemleak configuration and common defines.
@@ -196,7 +197,9 @@ static atomic_t kmemleak_enabled = ATOMIC_INIT(0);
196197
static atomic_t kmemleak_initialized = ATOMIC_INIT(0);
197198
/* enables or disables early logging of the memory operations */
198199
static atomic_t kmemleak_early_log = ATOMIC_INIT(1);
199-
/* set if a fata kmemleak error has occurred */
200+
/* set if a kmemleak warning was issued */
201+
static atomic_t kmemleak_warning = ATOMIC_INIT(0);
202+
/* set if a fatal kmemleak error has occurred */
200203
static atomic_t kmemleak_error = ATOMIC_INIT(0);
201204

202205
/* minimum and maximum address that may be valid pointers */
@@ -228,8 +231,10 @@ static int kmemleak_skip_disable;
228231
/* kmemleak operation type for early logging */
229232
enum {
230233
KMEMLEAK_ALLOC,
234+
KMEMLEAK_ALLOC_PERCPU,
231235
KMEMLEAK_FREE,
232236
KMEMLEAK_FREE_PART,
237+
KMEMLEAK_FREE_PERCPU,
233238
KMEMLEAK_NOT_LEAK,
234239
KMEMLEAK_IGNORE,
235240
KMEMLEAK_SCAN_AREA,
@@ -259,9 +264,10 @@ static void kmemleak_disable(void);
259264
/*
260265
* Print a warning and dump the stack trace.
261266
*/
262-
#define kmemleak_warn(x...) do { \
263-
pr_warning(x); \
264-
dump_stack(); \
267+
#define kmemleak_warn(x...) do { \
268+
pr_warning(x); \
269+
dump_stack(); \
270+
atomic_set(&kmemleak_warning, 1); \
265271
} while (0)
266272

267273
/*
@@ -403,8 +409,8 @@ static struct kmemleak_object *lookup_object(unsigned long ptr, int alias)
403409
object = prio_tree_entry(node, struct kmemleak_object,
404410
tree_node);
405411
if (!alias && object->pointer != ptr) {
406-
pr_warning("Found object by alias at 0x%08lx\n", ptr);
407-
dump_stack();
412+
kmemleak_warn("Found object by alias at 0x%08lx\n",
413+
ptr);
408414
dump_object_info(object);
409415
object = NULL;
410416
}
@@ -794,9 +800,13 @@ static void __init log_early(int op_type, const void *ptr, size_t size,
794800
unsigned long flags;
795801
struct early_log *log;
796802

803+
if (atomic_read(&kmemleak_error)) {
804+
/* kmemleak stopped recording, just count the requests */
805+
crt_early_log++;
806+
return;
807+
}
808+
797809
if (crt_early_log >= ARRAY_SIZE(early_log)) {
798-
pr_warning("Early log buffer exceeded, "
799-
"please increase DEBUG_KMEMLEAK_EARLY_LOG_SIZE\n");
800810
kmemleak_disable();
801811
return;
802812
}
@@ -811,8 +821,7 @@ static void __init log_early(int op_type, const void *ptr, size_t size,
811821
log->ptr = ptr;
812822
log->size = size;
813823
log->min_count = min_count;
814-
if (op_type == KMEMLEAK_ALLOC)
815-
log->trace_len = __save_stack_trace(log->trace);
824+
log->trace_len = __save_stack_trace(log->trace);
816825
crt_early_log++;
817826
local_irq_restore(flags);
818827
}
@@ -846,6 +855,20 @@ static void early_alloc(struct early_log *log)
846855
rcu_read_unlock();
847856
}
848857

858+
/*
859+
* Log an early allocated block and populate the stack trace.
860+
*/
861+
static void early_alloc_percpu(struct early_log *log)
862+
{
863+
unsigned int cpu;
864+
const void __percpu *ptr = log->ptr;
865+
866+
for_each_possible_cpu(cpu) {
867+
log->ptr = per_cpu_ptr(ptr, cpu);
868+
early_alloc(log);
869+
}
870+
}
871+
849872
/**
850873
* kmemleak_alloc - register a newly allocated object
851874
* @ptr: pointer to beginning of the object
@@ -872,6 +895,34 @@ void __ref kmemleak_alloc(const void *ptr, size_t size, int min_count,
872895
}
873896
EXPORT_SYMBOL_GPL(kmemleak_alloc);
874897

898+
/**
899+
* kmemleak_alloc_percpu - register a newly allocated __percpu object
900+
* @ptr: __percpu pointer to beginning of the object
901+
* @size: size of the object
902+
*
903+
* This function is called from the kernel percpu allocator when a new object
904+
* (memory block) is allocated (alloc_percpu). It assumes GFP_KERNEL
905+
* allocation.
906+
*/
907+
void __ref kmemleak_alloc_percpu(const void __percpu *ptr, size_t size)
908+
{
909+
unsigned int cpu;
910+
911+
pr_debug("%s(0x%p, %zu)\n", __func__, ptr, size);
912+
913+
/*
914+
* Percpu allocations are only scanned and not reported as leaks
915+
* (min_count is set to 0).
916+
*/
917+
if (atomic_read(&kmemleak_enabled) && ptr && !IS_ERR(ptr))
918+
for_each_possible_cpu(cpu)
919+
create_object((unsigned long)per_cpu_ptr(ptr, cpu),
920+
size, 0, GFP_KERNEL);
921+
else if (atomic_read(&kmemleak_early_log))
922+
log_early(KMEMLEAK_ALLOC_PERCPU, ptr, size, 0);
923+
}
924+
EXPORT_SYMBOL_GPL(kmemleak_alloc_percpu);
925+
875926
/**
876927
* kmemleak_free - unregister a previously registered object
877928
* @ptr: pointer to beginning of the object
@@ -910,6 +961,28 @@ void __ref kmemleak_free_part(const void *ptr, size_t size)
910961
}
911962
EXPORT_SYMBOL_GPL(kmemleak_free_part);
912963

964+
/**
965+
* kmemleak_free_percpu - unregister a previously registered __percpu object
966+
* @ptr: __percpu pointer to beginning of the object
967+
*
968+
* This function is called from the kernel percpu allocator when an object
969+
* (memory block) is freed (free_percpu).
970+
*/
971+
void __ref kmemleak_free_percpu(const void __percpu *ptr)
972+
{
973+
unsigned int cpu;
974+
975+
pr_debug("%s(0x%p)\n", __func__, ptr);
976+
977+
if (atomic_read(&kmemleak_enabled) && ptr && !IS_ERR(ptr))
978+
for_each_possible_cpu(cpu)
979+
delete_object_full((unsigned long)per_cpu_ptr(ptr,
980+
cpu));
981+
else if (atomic_read(&kmemleak_early_log))
982+
log_early(KMEMLEAK_FREE_PERCPU, ptr, 0, 0);
983+
}
984+
EXPORT_SYMBOL_GPL(kmemleak_free_percpu);
985+
913986
/**
914987
* kmemleak_not_leak - mark an allocated object as false positive
915988
* @ptr: pointer to beginning of the object
@@ -1220,9 +1293,9 @@ static void kmemleak_scan(void)
12201293
#endif
12211294

12221295
/*
1223-
* Struct page scanning for each node. The code below is not yet safe
1224-
* with MEMORY_HOTPLUG.
1296+
* Struct page scanning for each node.
12251297
*/
1298+
lock_memory_hotplug();
12261299
for_each_online_node(i) {
12271300
pg_data_t *pgdat = NODE_DATA(i);
12281301
unsigned long start_pfn = pgdat->node_start_pfn;
@@ -1241,6 +1314,7 @@ static void kmemleak_scan(void)
12411314
scan_block(page, page + 1, NULL, 1);
12421315
}
12431316
}
1317+
unlock_memory_hotplug();
12441318

12451319
/*
12461320
* Scanning the task stacks (may introduce false negatives).
@@ -1467,9 +1541,6 @@ static const struct seq_operations kmemleak_seq_ops = {
14671541

14681542
static int kmemleak_open(struct inode *inode, struct file *file)
14691543
{
1470-
if (!atomic_read(&kmemleak_enabled))
1471-
return -EBUSY;
1472-
14731544
return seq_open(file, &kmemleak_seq_ops);
14741545
}
14751546

@@ -1543,6 +1614,9 @@ static ssize_t kmemleak_write(struct file *file, const char __user *user_buf,
15431614
int buf_size;
15441615
int ret;
15451616

1617+
if (!atomic_read(&kmemleak_enabled))
1618+
return -EBUSY;
1619+
15461620
buf_size = min(size, (sizeof(buf) - 1));
15471621
if (strncpy_from_user(buf, user_buf, buf_size) < 0)
15481622
return -EFAULT;
@@ -1602,20 +1676,24 @@ static const struct file_operations kmemleak_fops = {
16021676
};
16031677

16041678
/*
1605-
* Perform the freeing of the kmemleak internal objects after waiting for any
1606-
* current memory scan to complete.
1679+
* Stop the memory scanning thread and free the kmemleak internal objects if
1680+
* no previous scan thread (otherwise, kmemleak may still have some useful
1681+
* information on memory leaks).
16071682
*/
16081683
static void kmemleak_do_cleanup(struct work_struct *work)
16091684
{
16101685
struct kmemleak_object *object;
1686+
bool cleanup = scan_thread == NULL;
16111687

16121688
mutex_lock(&scan_mutex);
16131689
stop_scan_thread();
16141690

1615-
rcu_read_lock();
1616-
list_for_each_entry_rcu(object, &object_list, object_list)
1617-
delete_object_full(object->pointer);
1618-
rcu_read_unlock();
1691+
if (cleanup) {
1692+
rcu_read_lock();
1693+
list_for_each_entry_rcu(object, &object_list, object_list)
1694+
delete_object_full(object->pointer);
1695+
rcu_read_unlock();
1696+
}
16191697
mutex_unlock(&scan_mutex);
16201698
}
16211699

@@ -1632,7 +1710,6 @@ static void kmemleak_disable(void)
16321710
return;
16331711

16341712
/* stop any memory operation tracing */
1635-
atomic_set(&kmemleak_early_log, 0);
16361713
atomic_set(&kmemleak_enabled, 0);
16371714

16381715
/* check whether it is too early for a kernel thread */
@@ -1659,6 +1736,17 @@ static int kmemleak_boot_config(char *str)
16591736
}
16601737
early_param("kmemleak", kmemleak_boot_config);
16611738

1739+
static void __init print_log_trace(struct early_log *log)
1740+
{
1741+
struct stack_trace trace;
1742+
1743+
trace.nr_entries = log->trace_len;
1744+
trace.entries = log->trace;
1745+
1746+
pr_notice("Early log backtrace:\n");
1747+
print_stack_trace(&trace, 2);
1748+
}
1749+
16621750
/*
16631751
* Kmemleak initialization.
16641752
*/
@@ -1681,12 +1769,18 @@ void __init kmemleak_init(void)
16811769
scan_area_cache = KMEM_CACHE(kmemleak_scan_area, SLAB_NOLEAKTRACE);
16821770
INIT_PRIO_TREE_ROOT(&object_tree_root);
16831771

1772+
if (crt_early_log >= ARRAY_SIZE(early_log))
1773+
pr_warning("Early log buffer exceeded (%d), please increase "
1774+
"DEBUG_KMEMLEAK_EARLY_LOG_SIZE\n", crt_early_log);
1775+
16841776
/* the kernel is still in UP mode, so disabling the IRQs is enough */
16851777
local_irq_save(flags);
1686-
if (!atomic_read(&kmemleak_error)) {
1778+
atomic_set(&kmemleak_early_log, 0);
1779+
if (atomic_read(&kmemleak_error)) {
1780+
local_irq_restore(flags);
1781+
return;
1782+
} else
16871783
atomic_set(&kmemleak_enabled, 1);
1688-
atomic_set(&kmemleak_early_log, 0);
1689-
}
16901784
local_irq_restore(flags);
16911785

16921786
/*
@@ -1701,12 +1795,18 @@ void __init kmemleak_init(void)
17011795
case KMEMLEAK_ALLOC:
17021796
early_alloc(log);
17031797
break;
1798+
case KMEMLEAK_ALLOC_PERCPU:
1799+
early_alloc_percpu(log);
1800+
break;
17041801
case KMEMLEAK_FREE:
17051802
kmemleak_free(log->ptr);
17061803
break;
17071804
case KMEMLEAK_FREE_PART:
17081805
kmemleak_free_part(log->ptr, log->size);
17091806
break;
1807+
case KMEMLEAK_FREE_PERCPU:
1808+
kmemleak_free_percpu(log->ptr);
1809+
break;
17101810
case KMEMLEAK_NOT_LEAK:
17111811
kmemleak_not_leak(log->ptr);
17121812
break;
@@ -1720,7 +1820,13 @@ void __init kmemleak_init(void)
17201820
kmemleak_no_scan(log->ptr);
17211821
break;
17221822
default:
1723-
WARN_ON(1);
1823+
kmemleak_warn("Unknown early log operation: %d\n",
1824+
log->op_type);
1825+
}
1826+
1827+
if (atomic_read(&kmemleak_warning)) {
1828+
print_log_trace(log);
1829+
atomic_set(&kmemleak_warning, 0);
17241830
}
17251831
}
17261832
}

0 commit comments

Comments
 (0)