2022-05-10 08:53:41 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2008-04-30 07:55:01 +00:00
|
|
|
/*
|
|
|
|
* Generic infrastructure for lifetime debugging of objects.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008, Thomas Gleixner <tglx@linutronix.de>
|
|
|
|
*/
|
2014-06-04 23:06:04 +00:00
|
|
|
|
|
|
|
#define pr_fmt(fmt) "ODEBUG: " fmt
|
|
|
|
|
2024-10-07 16:50:13 +00:00
|
|
|
#include <linux/cpu.h>
|
2008-04-30 07:55:01 +00:00
|
|
|
#include <linux/debugobjects.h>
|
2024-10-07 16:50:13 +00:00
|
|
|
#include <linux/debugfs.h>
|
|
|
|
#include <linux/hash.h>
|
|
|
|
#include <linux/kmemleak.h>
|
2009-10-07 13:09:06 +00:00
|
|
|
#include <linux/sched.h>
|
2017-02-08 17:51:37 +00:00
|
|
|
#include <linux/sched/task_stack.h>
|
2008-04-30 07:55:01 +00:00
|
|
|
#include <linux/seq_file.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 08:04:11 +00:00
|
|
|
#include <linux/slab.h>
|
2024-10-07 16:50:13 +00:00
|
|
|
#include <linux/static_key.h>
|
2008-04-30 07:55:01 +00:00
|
|
|
|
|
|
|
#define ODEBUG_HASH_BITS 14
|
|
|
|
#define ODEBUG_HASH_SIZE (1 << ODEBUG_HASH_BITS)
|
|
|
|
|
2024-10-07 16:50:14 +00:00
|
|
|
/* Must be power of two */
|
2019-05-20 14:14:47 +00:00
|
|
|
#define ODEBUG_BATCH_SIZE 16
|
2008-04-30 07:55:01 +00:00
|
|
|
|
2024-10-07 16:50:14 +00:00
|
|
|
/* Initial values. Must all be a multiple of batch size */
|
|
|
|
#define ODEBUG_POOL_SIZE (64 * ODEBUG_BATCH_SIZE)
|
|
|
|
#define ODEBUG_POOL_MIN_LEVEL (ODEBUG_POOL_SIZE / 4)
|
|
|
|
|
|
|
|
#define ODEBUG_POOL_PERCPU_SIZE (4 * ODEBUG_BATCH_SIZE)
|
|
|
|
|
2008-04-30 07:55:01 +00:00
|
|
|
#define ODEBUG_CHUNK_SHIFT PAGE_SHIFT
|
|
|
|
#define ODEBUG_CHUNK_SIZE (1 << ODEBUG_CHUNK_SHIFT)
|
|
|
|
#define ODEBUG_CHUNK_MASK (~(ODEBUG_CHUNK_SIZE - 1))
|
|
|
|
|
2019-05-20 14:14:49 +00:00
|
|
|
/*
|
|
|
|
* We limit the freeing of debug objects via workqueue at a maximum
|
|
|
|
* frequency of 10Hz and about 1024 objects for each freeing operation.
|
|
|
|
* So it is freeing at most 10k debug objects per second.
|
|
|
|
*/
|
2024-10-07 16:50:12 +00:00
|
|
|
#define ODEBUG_FREE_WORK_MAX (1024 / ODEBUG_BATCH_SIZE)
|
2019-05-20 14:14:49 +00:00
|
|
|
#define ODEBUG_FREE_WORK_DELAY DIV_ROUND_UP(HZ, 10)
|
|
|
|
|
2008-04-30 07:55:01 +00:00
|
|
|
struct debug_bucket {
|
|
|
|
struct hlist_head list;
|
2009-11-17 17:11:28 +00:00
|
|
|
raw_spinlock_t lock;
|
2008-04-30 07:55:01 +00:00
|
|
|
};
|
|
|
|
|
2024-10-07 16:50:04 +00:00
|
|
|
struct obj_pool {
|
|
|
|
struct hlist_head objects;
|
|
|
|
unsigned int cnt;
|
2024-10-07 16:50:08 +00:00
|
|
|
unsigned int min_cnt;
|
|
|
|
unsigned int max_cnt;
|
2024-10-07 16:50:04 +00:00
|
|
|
} ____cacheline_aligned;
|
|
|
|
|
2024-10-07 16:50:08 +00:00
|
|
|
|
|
|
|
static DEFINE_PER_CPU_ALIGNED(struct obj_pool, pool_pcpu) = {
|
|
|
|
.max_cnt = ODEBUG_POOL_PERCPU_SIZE,
|
|
|
|
};
|
2019-05-20 14:14:46 +00:00
|
|
|
|
2008-04-30 07:55:01 +00:00
|
|
|
static struct debug_bucket obj_hash[ODEBUG_HASH_SIZE];
|
|
|
|
|
2009-03-16 17:53:18 +00:00
|
|
|
static struct debug_obj obj_static_pool[ODEBUG_POOL_SIZE] __initdata;
|
2008-04-30 07:55:01 +00:00
|
|
|
|
2009-11-17 17:11:28 +00:00
|
|
|
static DEFINE_RAW_SPINLOCK(pool_lock);
|
2008-04-30 07:55:01 +00:00
|
|
|
|
2024-10-07 16:50:08 +00:00
|
|
|
static struct obj_pool pool_global = {
|
|
|
|
.min_cnt = ODEBUG_POOL_MIN_LEVEL,
|
|
|
|
.max_cnt = ODEBUG_POOL_SIZE,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct obj_pool pool_to_free = {
|
|
|
|
.max_cnt = UINT_MAX,
|
|
|
|
};
|
2008-04-30 07:55:01 +00:00
|
|
|
|
2024-10-07 16:50:05 +00:00
|
|
|
static HLIST_HEAD(pool_boot);
|
|
|
|
|
2019-05-20 14:14:46 +00:00
|
|
|
/*
|
|
|
|
* Because of the presence of percpu free pools, obj_pool_free will
|
|
|
|
* under-count those in the percpu free pools. Similarly, obj_pool_used
|
|
|
|
* will over-count those in the percpu free pools. Adjustments will be
|
|
|
|
* made at debug_stats_show(). Both obj_pool_min_free and obj_pool_max_used
|
|
|
|
* can be off.
|
|
|
|
*/
|
2024-09-04 13:39:39 +00:00
|
|
|
static int __data_racy obj_pool_min_free = ODEBUG_POOL_SIZE;
|
2008-04-30 07:55:01 +00:00
|
|
|
static int obj_pool_used;
|
2024-09-04 13:39:39 +00:00
|
|
|
static int __data_racy obj_pool_max_used;
|
2019-05-20 14:14:49 +00:00
|
|
|
static bool obj_freeing;
|
2008-04-30 07:55:01 +00:00
|
|
|
|
2024-06-11 09:18:12 +00:00
|
|
|
static int __data_racy debug_objects_maxchain __read_mostly;
|
|
|
|
static int __data_racy __maybe_unused debug_objects_maxchecked __read_mostly;
|
|
|
|
static int __data_racy debug_objects_fixups __read_mostly;
|
|
|
|
static int __data_racy debug_objects_warnings __read_mostly;
|
2024-10-07 16:50:02 +00:00
|
|
|
static bool __data_racy debug_objects_enabled __read_mostly
|
2024-06-11 09:18:12 +00:00
|
|
|
= CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT;
|
|
|
|
|
2024-10-07 16:50:08 +00:00
|
|
|
static const struct debug_obj_descr *descr_test __read_mostly;
|
|
|
|
static struct kmem_cache *obj_cache __ro_after_init;
|
2008-04-30 07:55:01 +00:00
|
|
|
|
2017-01-05 20:17:03 +00:00
|
|
|
/*
|
2017-02-07 21:40:30 +00:00
|
|
|
* Track numbers of kmem_cache_alloc()/free() calls done.
|
2017-01-05 20:17:03 +00:00
|
|
|
*/
|
2024-09-04 13:39:39 +00:00
|
|
|
static int __data_racy debug_objects_allocated;
|
|
|
|
static int __data_racy debug_objects_freed;
|
2017-01-05 20:17:03 +00:00
|
|
|
|
2009-03-16 09:04:53 +00:00
|
|
|
static void free_obj_work(struct work_struct *work);
|
2019-05-20 14:14:49 +00:00
|
|
|
static DECLARE_DELAYED_WORK(debug_obj_work, free_obj_work);
|
2009-03-16 09:04:53 +00:00
|
|
|
|
2024-10-07 16:50:13 +00:00
|
|
|
static DEFINE_STATIC_KEY_FALSE(obj_cache_enabled);
|
|
|
|
|
2008-04-30 07:55:01 +00:00
|
|
|
static int __init enable_object_debug(char *str)
|
|
|
|
{
|
2024-10-07 16:50:02 +00:00
|
|
|
debug_objects_enabled = true;
|
2008-04-30 07:55:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2024-10-07 16:50:02 +00:00
|
|
|
early_param("debug_objects", enable_object_debug);
|
2009-03-02 01:41:41 +00:00
|
|
|
|
|
|
|
static int __init disable_object_debug(char *str)
|
|
|
|
{
|
2024-10-07 16:50:02 +00:00
|
|
|
debug_objects_enabled = false;
|
2009-03-02 01:41:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
early_param("no_debug_objects", disable_object_debug);
|
2008-04-30 07:55:01 +00:00
|
|
|
|
|
|
|
static const char *obj_states[ODEBUG_STATE_MAX] = {
|
|
|
|
[ODEBUG_STATE_NONE] = "none",
|
|
|
|
[ODEBUG_STATE_INIT] = "initialized",
|
|
|
|
[ODEBUG_STATE_INACTIVE] = "inactive",
|
|
|
|
[ODEBUG_STATE_ACTIVE] = "active",
|
|
|
|
[ODEBUG_STATE_DESTROYED] = "destroyed",
|
|
|
|
[ODEBUG_STATE_NOTAVAILABLE] = "not available",
|
|
|
|
};
|
|
|
|
|
2024-10-07 16:50:04 +00:00
|
|
|
static __always_inline unsigned int pool_count(struct obj_pool *pool)
|
|
|
|
{
|
|
|
|
return READ_ONCE(pool->cnt);
|
|
|
|
}
|
|
|
|
|
2024-10-07 16:50:08 +00:00
|
|
|
static __always_inline bool pool_should_refill(struct obj_pool *pool)
|
2024-10-07 16:50:04 +00:00
|
|
|
{
|
2024-10-07 16:50:08 +00:00
|
|
|
return pool_count(pool) < pool->min_cnt;
|
2024-10-07 16:50:04 +00:00
|
|
|
}
|
|
|
|
|
2024-10-07 16:50:08 +00:00
|
|
|
static __always_inline bool pool_must_refill(struct obj_pool *pool)
|
2024-10-07 16:50:04 +00:00
|
|
|
{
|
2024-10-07 16:50:08 +00:00
|
|
|
return pool_count(pool) < pool->min_cnt / 2;
|
2024-10-07 16:50:04 +00:00
|
|
|
}
|
|
|
|
|
2024-10-07 16:50:09 +00:00
|
|
|
static bool pool_move_batch(struct obj_pool *dst, struct obj_pool *src)
|
|
|
|
{
|
|
|
|
if (dst->cnt + ODEBUG_BATCH_SIZE > dst->max_cnt || !src->cnt)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (int i = 0; i < ODEBUG_BATCH_SIZE && src->cnt; i++) {
|
|
|
|
struct hlist_node *node = src->objects.first;
|
|
|
|
|
|
|
|
WRITE_ONCE(src->cnt, src->cnt - 1);
|
|
|
|
WRITE_ONCE(dst->cnt, dst->cnt + 1);
|
|
|
|
|
|
|
|
hlist_del(node);
|
|
|
|
hlist_add_head(node, &dst->objects);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-10-07 16:50:15 +00:00
|
|
|
static bool pool_push_batch(struct obj_pool *dst, struct hlist_head *head)
|
|
|
|
{
|
|
|
|
struct hlist_node *last;
|
|
|
|
struct debug_obj *obj;
|
|
|
|
|
|
|
|
if (dst->cnt >= dst->max_cnt)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
obj = hlist_entry(head->first, typeof(*obj), node);
|
|
|
|
last = obj->batch_last;
|
|
|
|
|
|
|
|
hlist_splice_init(head, last, &dst->objects);
|
|
|
|
WRITE_ONCE(dst->cnt, dst->cnt + ODEBUG_BATCH_SIZE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-10-07 16:50:12 +00:00
|
|
|
static bool pool_pop_batch(struct hlist_head *head, struct obj_pool *src)
|
|
|
|
{
|
|
|
|
if (!src->cnt)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (int i = 0; src->cnt && i < ODEBUG_BATCH_SIZE; i++) {
|
|
|
|
struct hlist_node *node = src->objects.first;
|
|
|
|
|
|
|
|
WRITE_ONCE(src->cnt, src->cnt - 1);
|
|
|
|
hlist_del(node);
|
|
|
|
hlist_add_head(node, head);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-10-07 16:50:09 +00:00
|
|
|
static struct debug_obj *__alloc_object(struct hlist_head *list)
|
|
|
|
{
|
|
|
|
struct debug_obj *obj;
|
|
|
|
|
|
|
|
if (unlikely(!list->first))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
obj = hlist_entry(list->first, typeof(*obj), node);
|
|
|
|
hlist_del(&obj->node);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct debug_obj *pcpu_alloc(void)
|
|
|
|
{
|
|
|
|
struct obj_pool *pcp = this_cpu_ptr(&pool_pcpu);
|
|
|
|
|
|
|
|
lockdep_assert_irqs_disabled();
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
struct debug_obj *obj = __alloc_object(&pcp->objects);
|
|
|
|
|
|
|
|
if (likely(obj)) {
|
|
|
|
pcp->cnt--;
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
guard(raw_spinlock)(&pool_lock);
|
|
|
|
if (!pool_move_batch(pcp, &pool_to_free)) {
|
|
|
|
if (!pool_move_batch(pcp, &pool_global))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
obj_pool_used += pcp->cnt;
|
|
|
|
|
|
|
|
if (obj_pool_used > obj_pool_max_used)
|
|
|
|
obj_pool_max_used = obj_pool_used;
|
|
|
|
|
|
|
|
if (pool_global.cnt < obj_pool_min_free)
|
|
|
|
obj_pool_min_free = pool_global.cnt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-07 16:50:10 +00:00
|
|
|
static void pcpu_free(struct debug_obj *obj)
|
|
|
|
{
|
|
|
|
struct obj_pool *pcp = this_cpu_ptr(&pool_pcpu);
|
|
|
|
|
|
|
|
lockdep_assert_irqs_disabled();
|
|
|
|
|
|
|
|
hlist_add_head(&obj->node, &pcp->objects);
|
|
|
|
pcp->cnt++;
|
|
|
|
|
|
|
|
/* Pool full ? */
|
|
|
|
if (pcp->cnt < ODEBUG_POOL_PERCPU_SIZE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Remove a batch from the per CPU pool */
|
|
|
|
guard(raw_spinlock)(&pool_lock);
|
|
|
|
/* Try to fit the batch into the pool_global first */
|
|
|
|
if (!pool_move_batch(&pool_global, pcp))
|
|
|
|
pool_move_batch(&pool_to_free, pcp);
|
|
|
|
obj_pool_used -= ODEBUG_BATCH_SIZE;
|
|
|
|
}
|
|
|
|
|
2024-10-07 16:50:01 +00:00
|
|
|
static void free_object_list(struct hlist_head *head)
|
|
|
|
{
|
|
|
|
struct hlist_node *tmp;
|
|
|
|
struct debug_obj *obj;
|
|
|
|
int cnt = 0;
|
|
|
|
|
|
|
|
hlist_for_each_entry_safe(obj, tmp, head, node) {
|
|
|
|
hlist_del(&obj->node);
|
|
|
|
kmem_cache_free(obj_cache, obj);
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
debug_objects_freed += cnt;
|
|
|
|
}
|
|
|
|
|
2024-10-07 16:50:03 +00:00
|
|
|
static void fill_pool_from_freelist(void)
|
2008-04-30 07:55:01 +00:00
|
|
|
{
|
2024-10-07 16:50:03 +00:00
|
|
|
static unsigned long state;
|
2008-04-30 07:55:01 +00:00
|
|
|
|
2018-02-05 23:18:26 +00:00
|
|
|
/*
|
2024-09-04 13:39:41 +00:00
|
|
|
* Reuse objs from the global obj_to_free list; they will be
|
|
|
|
* reinitialized when allocating.
|
2018-02-05 23:18:26 +00:00
|
|
|
*/
|
2024-10-07 16:50:04 +00:00
|
|
|
if (!pool_count(&pool_to_free))
|
2024-10-07 16:50:03 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prevent the context from being scheduled or interrupted after
|
|
|
|
* setting the state flag;
|
|
|
|
*/
|
|
|
|
guard(irqsave)();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Avoid lock contention on &pool_lock and avoid making the cache
|
|
|
|
* line exclusive by testing the bit before attempting to set it.
|
|
|
|
*/
|
|
|
|
if (test_bit(0, &state) || test_and_set_bit(0, &state))
|
|
|
|
return;
|
|
|
|
|
2024-10-07 16:50:09 +00:00
|
|
|
/* Avoid taking the lock when there is no work to do */
|
|
|
|
while (pool_should_refill(&pool_global) && pool_count(&pool_to_free)) {
|
|
|
|
guard(raw_spinlock)(&pool_lock);
|
|
|
|
/* Move a batch if possible */
|
|
|
|
pool_move_batch(&pool_global, &pool_to_free);
|
2018-02-05 23:18:26 +00:00
|
|
|
}
|
2024-10-07 16:50:03 +00:00
|
|
|
clear_bit(0, &state);
|
|
|
|
}
|
2018-02-05 23:18:26 +00:00
|
|
|
|
2024-10-07 16:50:15 +00:00
|
|
|
static bool kmem_alloc_batch(struct hlist_head *head, struct kmem_cache *cache, gfp_t gfp)
|
|
|
|
{
|
|
|
|
struct hlist_node *last = NULL;
|
|
|
|
struct debug_obj *obj;
|
|
|
|
|
|
|
|
for (int cnt = 0; cnt < ODEBUG_BATCH_SIZE; cnt++) {
|
|
|
|
obj = kmem_cache_zalloc(cache, gfp);
|
|
|
|
if (!obj) {
|
|
|
|
free_object_list(head);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
debug_objects_allocated++;
|
|
|
|
|
|
|
|
if (!last)
|
|
|
|
last = &obj->node;
|
|
|
|
obj->batch_last = last;
|
|
|
|
|
|
|
|
hlist_add_head(&obj->node, head);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-10-07 16:50:03 +00:00
|
|
|
static void fill_pool(void)
|
|
|
|
{
|
|
|
|
static atomic_t cpus_allocating;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Avoid allocation and lock contention when:
|
|
|
|
* - One other CPU is already allocating
|
|
|
|
* - the global pool has not reached the critical level yet
|
|
|
|
*/
|
2024-10-07 16:50:08 +00:00
|
|
|
if (!pool_must_refill(&pool_global) && atomic_read(&cpus_allocating))
|
2012-04-11 09:52:18 +00:00
|
|
|
return;
|
2008-04-30 07:55:01 +00:00
|
|
|
|
2024-10-07 16:50:03 +00:00
|
|
|
atomic_inc(&cpus_allocating);
|
2024-10-07 16:50:08 +00:00
|
|
|
while (pool_should_refill(&pool_global)) {
|
2024-10-07 16:49:53 +00:00
|
|
|
HLIST_HEAD(head);
|
2008-04-30 07:55:01 +00:00
|
|
|
|
2024-10-07 16:50:15 +00:00
|
|
|
if (!kmem_alloc_batch(&head, obj_cache, __GFP_HIGH | __GFP_NOWARN))
|
2024-10-07 16:50:03 +00:00
|
|
|
break;
|
2008-04-30 07:55:01 +00:00
|
|
|
|
2024-10-07 16:50:03 +00:00
|
|
|
guard(raw_spinlock_irqsave)(&pool_lock);
|
2024-10-07 16:50:15 +00:00
|
|
|
if (!pool_push_batch(&pool_global, &head))
|
|
|
|
pool_push_batch(&pool_to_free, &head);
|
2008-04-30 07:55:01 +00:00
|
|
|
}
|
2024-10-07 16:50:03 +00:00
|
|
|
atomic_dec(&cpus_allocating);
|
2008-04-30 07:55:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lookup an object in the hash bucket.
|
|
|
|
*/
|
|
|
|
static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
|
|
|
|
{
|
|
|
|
struct debug_obj *obj;
|
|
|
|
int cnt = 0;
|
|
|
|
|
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 01:06:00 +00:00
|
|
|
hlist_for_each_entry(obj, &b->list, node) {
|
2008-04-30 07:55:01 +00:00
|
|
|
cnt++;
|
|
|
|
if (obj->object == addr)
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
if (cnt > debug_objects_maxchain)
|
|
|
|
debug_objects_maxchain = cnt;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2024-10-07 16:50:09 +00:00
|
|
|
static struct debug_obj *alloc_object(void *addr, struct debug_bucket *b,
|
|
|
|
const struct debug_obj_descr *descr)
|
2019-05-20 14:14:46 +00:00
|
|
|
{
|
|
|
|
struct debug_obj *obj;
|
2008-04-30 07:55:01 +00:00
|
|
|
|
2024-10-07 16:50:13 +00:00
|
|
|
if (static_branch_likely(&obj_cache_enabled))
|
2024-10-07 16:50:09 +00:00
|
|
|
obj = pcpu_alloc();
|
|
|
|
else
|
2024-10-07 16:50:05 +00:00
|
|
|
obj = __alloc_object(&pool_boot);
|
2008-04-30 07:55:01 +00:00
|
|
|
|
2024-10-07 16:50:09 +00:00
|
|
|
if (likely(obj)) {
|
2019-05-20 14:14:46 +00:00
|
|
|
obj->object = addr;
|
|
|
|
obj->descr = descr;
|
|
|
|
obj->state = ODEBUG_STATE_NONE;
|
|
|
|
obj->astate = 0;
|
|
|
|
hlist_add_head(&obj->node, &b->list);
|
|
|
|
}
|
2008-04-30 07:55:01 +00:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2024-10-07 16:50:12 +00:00
|
|
|
/* workqueue function to free objects. */
|
2009-03-16 09:04:53 +00:00
|
|
|
static void free_obj_work(struct work_struct *work)
|
2008-04-30 07:55:01 +00:00
|
|
|
{
|
2024-10-07 16:50:12 +00:00
|
|
|
bool free = true;
|
2008-04-30 07:55:01 +00:00
|
|
|
|
2019-05-20 14:14:49 +00:00
|
|
|
WRITE_ONCE(obj_freeing, false);
|
2018-02-05 23:18:26 +00:00
|
|
|
|
2024-10-07 16:50:12 +00:00
|
|
|
if (!pool_count(&pool_to_free))
|
|
|
|
return;
|
2018-02-05 23:18:26 +00:00
|
|
|
|
2024-10-07 16:50:12 +00:00
|
|
|
for (unsigned int cnt = 0; cnt < ODEBUG_FREE_WORK_MAX; cnt++) {
|
|
|
|
HLIST_HEAD(tofree);
|
|
|
|
|
|
|
|
/* Acquire and drop the lock for each batch */
|
|
|
|
scoped_guard(raw_spinlock_irqsave, &pool_lock) {
|
|
|
|
if (!pool_to_free.cnt)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Refill the global pool if possible */
|
|
|
|
if (pool_move_batch(&pool_global, &pool_to_free)) {
|
|
|
|
/* Don't free as there seems to be demand */
|
|
|
|
free = false;
|
|
|
|
} else if (free) {
|
|
|
|
pool_pop_batch(&tofree, &pool_to_free);
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free_object_list(&tofree);
|
2018-02-05 23:18:26 +00:00
|
|
|
}
|
2009-03-16 09:04:53 +00:00
|
|
|
}
|
|
|
|
|
2019-05-20 14:14:49 +00:00
|
|
|
static void __free_object(struct debug_obj *obj)
|
2009-03-16 09:04:53 +00:00
|
|
|
{
|
2024-10-07 16:50:05 +00:00
|
|
|
guard(irqsave)();
|
2024-10-07 16:50:13 +00:00
|
|
|
if (static_branch_likely(&obj_cache_enabled))
|
2024-10-07 16:50:10 +00:00
|
|
|
pcpu_free(obj);
|
|
|
|
else
|
2024-10-07 16:50:05 +00:00
|
|
|
hlist_add_head(&obj->node, &pool_boot);
|
2018-02-05 23:18:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Put the object back into the pool and schedule work to free objects
|
|
|
|
* if necessary.
|
|
|
|
*/
|
|
|
|
static void free_object(struct debug_obj *obj)
|
|
|
|
{
|
2019-05-20 14:14:49 +00:00
|
|
|
__free_object(obj);
|
2024-10-07 16:50:04 +00:00
|
|
|
if (!READ_ONCE(obj_freeing) && pool_count(&pool_to_free)) {
|
2019-05-20 14:14:49 +00:00
|
|
|
WRITE_ONCE(obj_freeing, true);
|
|
|
|
schedule_delayed_work(&debug_obj_work, ODEBUG_FREE_WORK_DELAY);
|
|
|
|
}
|
2008-04-30 07:55:01 +00:00
|
|
|
}
|
|
|
|
|
2024-10-07 16:49:57 +00:00
|
|
|
static void put_objects(struct hlist_head *list)
|
2020-09-08 06:27:09 +00:00
|
|
|
{
|
|
|
|
struct hlist_node *tmp;
|
|
|
|
struct debug_obj *obj;
|
|
|
|
|
2024-10-07 16:49:57 +00:00
|
|
|
/*
|
|
|
|
* Using free_object() puts the objects into reuse or schedules
|
|
|
|
* them for freeing and it get's all the accounting correct.
|
|
|
|
*/
|
|
|
|
hlist_for_each_entry_safe(obj, tmp, list, node) {
|
2020-09-08 06:27:09 +00:00
|
|
|
hlist_del(&obj->node);
|
2024-10-07 16:49:57 +00:00
|
|
|
free_object(obj);
|
2020-09-08 06:27:09 +00:00
|
|
|
}
|
2024-10-07 16:49:57 +00:00
|
|
|
}
|
2022-06-11 13:06:34 +00:00
|
|
|
|
2024-10-07 16:49:58 +00:00
|
|
|
#ifdef CONFIG_HOTPLUG_CPU
|
2024-10-07 16:49:57 +00:00
|
|
|
static int object_cpu_offline(unsigned int cpu)
|
|
|
|
{
|
|
|
|
/* Remote access is safe as the CPU is dead already */
|
2024-10-07 16:50:07 +00:00
|
|
|
struct obj_pool *pcp = per_cpu_ptr(&pool_pcpu, cpu);
|
2020-09-08 06:27:09 +00:00
|
|
|
|
2024-10-07 16:50:07 +00:00
|
|
|
put_objects(&pcp->objects);
|
|
|
|
pcp->cnt = 0;
|
2020-09-08 06:27:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2024-10-07 16:49:58 +00:00
|
|
|
/* Out of memory. Free all objects from hash */
|
2008-04-30 07:55:01 +00:00
|
|
|
static void debug_objects_oom(void)
|
|
|
|
{
|
|
|
|
struct debug_bucket *db = obj_hash;
|
2008-08-31 21:39:21 +00:00
|
|
|
HLIST_HEAD(freelist);
|
2008-04-30 07:55:01 +00:00
|
|
|
|
2014-06-04 23:06:04 +00:00
|
|
|
pr_warn("Out of memory. ODEBUG disabled\n");
|
2008-04-30 07:55:01 +00:00
|
|
|
|
2024-10-07 16:49:58 +00:00
|
|
|
for (int i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
|
|
|
|
scoped_guard(raw_spinlock_irqsave, &db->lock)
|
|
|
|
hlist_move_list(&db->list, &freelist);
|
2008-08-31 21:39:21 +00:00
|
|
|
|
2024-10-07 16:49:58 +00:00
|
|
|
put_objects(&freelist);
|
2008-04-30 07:55:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We use the pfn of the address for the hash. That way we can check
|
|
|
|
* for freed objects simply by checking the affected bucket.
|
|
|
|
*/
|
|
|
|
static struct debug_bucket *get_bucket(unsigned long addr)
|
|
|
|
{
|
|
|
|
unsigned long hash;
|
|
|
|
|
|
|
|
hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
|
|
|
|
return &obj_hash[hash];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void debug_print_object(struct debug_obj *obj, char *msg)
|
|
|
|
{
|
2020-08-15 00:40:26 +00:00
|
|
|
const struct debug_obj_descr *descr = obj->descr;
|
2008-04-30 07:55:01 +00:00
|
|
|
static int limit;
|
|
|
|
|
2023-06-07 10:19:02 +00:00
|
|
|
/*
|
|
|
|
* Don't report if lookup_object_or_alloc() by the current thread
|
|
|
|
* failed because lookup_object_or_alloc()/debug_objects_oom() by a
|
|
|
|
* concurrent thread turned off debug_objects_enabled and cleared
|
|
|
|
* the hash buckets.
|
|
|
|
*/
|
|
|
|
if (!debug_objects_enabled)
|
|
|
|
return;
|
|
|
|
|
2011-03-07 08:58:33 +00:00
|
|
|
if (limit < 5 && descr != descr_test) {
|
|
|
|
void *hint = descr->debug_hint ?
|
|
|
|
descr->debug_hint(obj->object) : NULL;
|
2008-04-30 07:55:01 +00:00
|
|
|
limit++;
|
2010-04-17 12:48:38 +00:00
|
|
|
WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
|
2022-05-19 20:22:01 +00:00
|
|
|
"object: %p object type: %s hint: %pS\n",
|
2010-04-17 12:48:38 +00:00
|
|
|
msg, obj_states[obj->state], obj->astate,
|
2022-05-19 20:22:01 +00:00
|
|
|
obj->object, descr->name, hint);
|
2008-04-30 07:55:01 +00:00
|
|
|
}
|
|
|
|
debug_objects_warnings++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to repair the damage, so we have a better chance to get useful
|
|
|
|
* debug output.
|
|
|
|
*/
|
2016-05-20 00:09:20 +00:00
|
|
|
static bool
|
|
|
|
debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state),
|
2008-04-30 07:55:01 +00:00
|
|
|
void * addr, enum debug_obj_state state)
|
|
|
|
{
|
2016-05-20 00:09:20 +00:00
|
|
|
if (fixup && fixup(addr, state)) {
|
|
|
|
debug_objects_fixups++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2008-04-30 07:55:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void debug_object_is_on_stack(void *addr, int onstack)
|
|
|
|
{
|
|
|
|
int is_on_stack;
|
|
|
|
static int limit;
|
|
|
|
|
|
|
|
if (limit > 4)
|
|
|
|
return;
|
|
|
|
|
2008-07-24 04:26:53 +00:00
|
|
|
is_on_stack = object_is_on_stack(addr);
|
2008-04-30 07:55:01 +00:00
|
|
|
if (is_on_stack == onstack)
|
|
|
|
return;
|
|
|
|
|
|
|
|
limit++;
|
|
|
|
if (is_on_stack)
|
2018-07-23 21:25:31 +00:00
|
|
|
pr_warn("object %p is on stack %p, but NOT annotated.\n", addr,
|
|
|
|
task_stack_page(current));
|
2008-04-30 07:55:01 +00:00
|
|
|
else
|
2018-07-23 21:25:31 +00:00
|
|
|
pr_warn("object %p is NOT on stack %p, but annotated.\n", addr,
|
|
|
|
task_stack_page(current));
|
|
|
|
|
2008-04-30 07:55:01 +00:00
|
|
|
WARN_ON(1);
|
|
|
|
}
|
|
|
|
|
2023-04-12 07:54:39 +00:00
|
|
|
static struct debug_obj *lookup_object_or_alloc(void *addr, struct debug_bucket *b,
|
|
|
|
const struct debug_obj_descr *descr,
|
|
|
|
bool onstack, bool alloc_ifstatic)
|
|
|
|
{
|
|
|
|
struct debug_obj *obj = lookup_object(addr, b);
|
|
|
|
enum debug_obj_state state = ODEBUG_STATE_NONE;
|
|
|
|
|
|
|
|
if (likely(obj))
|
|
|
|
return obj;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* debug_object_init() unconditionally allocates untracked
|
|
|
|
* objects. It does not matter whether it is a static object or
|
|
|
|
* not.
|
|
|
|
*
|
|
|
|
* debug_object_assert_init() and debug_object_activate() allow
|
|
|
|
* allocation only if the descriptor callback confirms that the
|
|
|
|
* object is static and considered initialized. For non-static
|
|
|
|
* objects the allocation needs to be done from the fixup callback.
|
|
|
|
*/
|
|
|
|
if (unlikely(alloc_ifstatic)) {
|
|
|
|
if (!descr->is_static_object || !descr->is_static_object(addr))
|
|
|
|
return ERR_PTR(-ENOENT);
|
|
|
|
/* Statically allocated objects are considered initialized */
|
|
|
|
state = ODEBUG_STATE_INIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
obj = alloc_object(addr, b, descr);
|
|
|
|
if (likely(obj)) {
|
|
|
|
obj->state = state;
|
|
|
|
debug_object_is_on_stack(addr, onstack);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Out of memory. Do the cleanup outside of the locked region */
|
2024-10-07 16:50:02 +00:00
|
|
|
debug_objects_enabled = false;
|
2023-04-12 07:54:39 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-05-01 15:42:06 +00:00
|
|
|
static void debug_objects_fill_pool(void)
|
|
|
|
{
|
2024-10-07 16:50:13 +00:00
|
|
|
if (!static_branch_likely(&obj_cache_enabled))
|
2024-10-07 16:50:03 +00:00
|
|
|
return;
|
|
|
|
|
2024-10-07 16:50:08 +00:00
|
|
|
if (likely(!pool_should_refill(&pool_global)))
|
2024-10-07 16:50:03 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* Try reusing objects from obj_to_free_list */
|
|
|
|
fill_pool_from_freelist();
|
|
|
|
|
2024-10-07 16:50:08 +00:00
|
|
|
if (likely(!pool_should_refill(&pool_global)))
|
2024-10-07 16:50:03 +00:00
|
|
|
return;
|
|
|
|
|
2023-05-01 15:42:06 +00:00
|
|
|
/*
|
|
|
|
* On RT enabled kernels the pool refill must happen in preemptible
|
2023-04-25 15:03:13 +00:00
|
|
|
* context -- for !RT kernels we rely on the fact that spinlock_t and
|
|
|
|
* raw_spinlock_t are basically the same type and this lock-type
|
|
|
|
* inversion works just fine.
|
2023-05-01 15:42:06 +00:00
|
|
|
*/
|
2023-04-25 15:03:13 +00:00
|
|
|
if (!IS_ENABLED(CONFIG_PREEMPT_RT) || preemptible()) {
|
|
|
|
/*
|
|
|
|
* Annotate away the spinlock_t inside raw_spinlock_t warning
|
|
|
|
* by temporarily raising the wait-type to WAIT_SLEEP, matching
|
|
|
|
* the preemptible() condition above.
|
|
|
|
*/
|
|
|
|
static DEFINE_WAIT_OVERRIDE_MAP(fill_pool_map, LD_WAIT_SLEEP);
|
|
|
|
lock_map_acquire_try(&fill_pool_map);
|
2023-05-01 15:42:06 +00:00
|
|
|
fill_pool();
|
2023-04-25 15:03:13 +00:00
|
|
|
lock_map_release(&fill_pool_map);
|
|
|
|
}
|
2023-05-01 15:42:06 +00:00
|
|
|
}
|
|
|
|
|
2008-04-30 07:55:01 +00:00
|
|
|
static void
|
2020-08-15 00:40:26 +00:00
|
|
|
__debug_object_init(void *addr, const struct debug_obj_descr *descr, int onstack)
|
2008-04-30 07:55:01 +00:00
|
|
|
{
|
2023-10-25 21:39:07 +00:00
|
|
|
struct debug_obj *obj, o;
|
2008-04-30 07:55:01 +00:00
|
|
|
struct debug_bucket *db;
|
|
|
|
unsigned long flags;
|
|
|
|
|
2023-05-01 15:42:06 +00:00
|
|
|
debug_objects_fill_pool();
|
2008-06-14 22:47:36 +00:00
|
|
|
|
2008-04-30 07:55:01 +00:00
|
|
|
db = get_bucket((unsigned long) addr);
|
|
|
|
|
2009-11-17 17:11:28 +00:00
|
|
|
raw_spin_lock_irqsave(&db->lock, flags);
|
2008-04-30 07:55:01 +00:00
|
|
|
|
2023-04-12 07:54:39 +00:00
|
|
|
obj = lookup_object_or_alloc(addr, db, descr, onstack, false);
|
|
|
|
if (unlikely(!obj)) {
|
|
|
|
raw_spin_unlock_irqrestore(&db->lock, flags);
|
|
|
|
debug_objects_oom();
|
|
|
|
return;
|
2008-04-30 07:55:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (obj->state) {
|
|
|
|
case ODEBUG_STATE_NONE:
|
|
|
|
case ODEBUG_STATE_INIT:
|
|
|
|
case ODEBUG_STATE_INACTIVE:
|
|
|
|
obj->state = ODEBUG_STATE_INIT;
|
2019-05-20 14:14:50 +00:00
|
|
|
raw_spin_unlock_irqrestore(&db->lock, flags);
|
|
|
|
return;
|
2008-04-30 07:55:01 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-10-25 21:39:07 +00:00
|
|
|
o = *obj;
|
2009-11-17 17:11:28 +00:00
|
|
|
raw_spin_unlock_irqrestore(&db->lock, flags);
|
2023-10-25 21:39:07 +00:00
|
|
|
debug_print_object(&o, "init");
|
|
|
|
|
|
|
|
if (o.state == ODEBUG_STATE_ACTIVE)
|
|
|
|
debug_object_fixup(descr->fixup_init, addr, o.state);
|
2008-04-30 07:55:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* debug_object_init - debug checks when an object is initialized
|
|
|
|
* @addr: address of the object
|
|
|
|
* @descr: pointer to an object specific debug description structure
|
|
|
|
*/
|
2020-08-15 00:40:26 +00:00
|
|
|
void debug_object_init(void *addr, const struct debug_obj_descr *descr)
|
2008-04-30 07:55:01 +00:00
|
|
|
{
|
|
|
|
if (!debug_objects_enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
__debug_object_init(addr, descr, 0);
|
|
|
|
}
|
2016-11-30 23:54:10 +00:00
|
|
|
EXPORT_SYMBOL_GPL(debug_object_init);
|
2008-04-30 07:55:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* debug_object_init_on_stack - debug checks when an object on stack is
|
|
|
|
* initialized
|
|
|
|
* @addr: address of the object
|
|
|
|
* @descr: pointer to an object specific debug description structure
|
|
|
|
*/
|
2020-08-15 00:40:26 +00:00
|
|
|
void debug_object_init_on_stack(void *addr, const struct debug_obj_descr *descr)
|
2008-04-30 07:55:01 +00:00
|
|
|
{
|
|
|
|
if (!debug_objects_enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
__debug_object_init(addr, descr, 1);
|
|
|
|
}
|
2016-11-30 23:54:10 +00:00
|
|
|
EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
|
2008-04-30 07:55:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* debug_object_activate - debug checks when an object is activated
|
|
|
|
* @addr: address of the object
|
|
|
|
* @descr: pointer to an object specific debug description structure
|
2013-04-23 19:51:11 +00:00
|
|
|
* Returns 0 for success, -EINVAL for check failed.
|
2008-04-30 07:55:01 +00:00
|
|
|
*/
|
2020-08-15 00:40:26 +00:00
|
|
|
int debug_object_activate(void *addr, const struct debug_obj_descr *descr)
|
2008-04-30 07:55:01 +00:00
|
|
|
{
|
2023-04-12 07:54:39 +00:00
|
|
|
struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
|
2008-04-30 07:55:01 +00:00
|
|
|
struct debug_bucket *db;
|
|
|
|
struct debug_obj *obj;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
if (!debug_objects_enabled)
|
2013-04-23 19:51:11 +00:00
|
|
|
return 0;
|
2008-04-30 07:55:01 +00:00
|
|
|
|
2023-05-01 15:42:06 +00:00
|
|
|
debug_objects_fill_pool();
|
|
|
|
|
2008-04-30 07:55:01 +00:00
|
|
|
db = get_bucket((unsigned long) addr);
|
|
|
|
|
2009-11-17 17:11:28 +00:00
|
|
|
raw_spin_lock_irqsave(&db->lock, flags);
|
2008-04-30 07:55:01 +00:00
|
|
|
|
2023-04-12 07:54:39 +00:00
|
|
|
obj = lookup_object_or_alloc(addr, db, descr, false, true);
|
2023-10-25 21:39:07 +00:00
|
|
|
if (unlikely(!obj)) {
|
|
|
|
raw_spin_unlock_irqrestore(&db->lock, flags);
|
|
|
|
debug_objects_oom();
|
|
|
|
return 0;
|
|
|
|
} else if (likely(!IS_ERR(obj))) {
|
2008-04-30 07:55:01 +00:00
|
|
|
switch (obj->state) {
|
|
|
|
case ODEBUG_STATE_ACTIVE:
|
|
|
|
case ODEBUG_STATE_DESTROYED:
|
2023-10-25 21:39:07 +00:00
|
|
|
o = *obj;
|
2008-04-30 07:55:01 +00:00
|
|
|
break;
|
2023-10-25 21:39:07 +00:00
|
|
|
case ODEBUG_STATE_INIT:
|
|
|
|
case ODEBUG_STATE_INACTIVE:
|
|
|
|
obj->state = ODEBUG_STATE_ACTIVE;
|
|
|
|
fallthrough;
|
2008-04-30 07:55:01 +00:00
|
|
|
default:
|
2023-10-25 21:39:07 +00:00
|
|
|
raw_spin_unlock_irqrestore(&db->lock, flags);
|
|
|
|
return 0;
|
2008-04-30 07:55:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-17 17:11:28 +00:00
|
|
|
raw_spin_unlock_irqrestore(&db->lock, flags);
|
2023-10-25 21:39:07 +00:00
|
|
|
debug_print_object(&o, "activate");
|
2019-05-20 14:14:50 +00:00
|
|
|
|
2023-10-25 21:39:07 +00:00
|
|
|
switch (o.state) {
|
|
|
|
case ODEBUG_STATE_ACTIVE:
|
|
|
|
case ODEBUG_STATE_NOTAVAILABLE:
|
|
|
|
if (debug_object_fixup(descr->fixup_activate, addr, o.state))
|
|
|
|
return 0;
|
|
|
|
fallthrough;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
2013-04-23 19:51:11 +00:00
|
|
|
}
|
2008-04-30 07:55:01 +00:00
|
|
|
}
|
2016-11-30 23:54:10 +00:00
|
|
|
EXPORT_SYMBOL_GPL(debug_object_activate);
|
2008-04-30 07:55:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* debug_object_deactivate - debug checks when an object is deactivated
|
|
|
|
* @addr: address of the object
|
|
|
|
* @descr: pointer to an object specific debug description structure
|
|
|
|
*/
|
2020-08-15 00:40:26 +00:00
|
|
|
void debug_object_deactivate(void *addr, const struct debug_obj_descr *descr)
|
2008-04-30 07:55:01 +00:00
|
|
|
{
|
2023-10-25 21:39:07 +00:00
|
|
|
struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
|
2008-04-30 07:55:01 +00:00
|
|
|
struct debug_bucket *db;
|
|
|
|
struct debug_obj *obj;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
if (!debug_objects_enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
db = get_bucket((unsigned long) addr);
|
|
|
|
|
2009-11-17 17:11:28 +00:00
|
|
|
raw_spin_lock_irqsave(&db->lock, flags);
|
2008-04-30 07:55:01 +00:00
|
|
|
|
|
|
|
obj = lookup_object(addr, db);
|
|
|
|
if (obj) {
|
|
|
|
switch (obj->state) {
|
2023-10-25 21:39:07 +00:00
|
|
|
case ODEBUG_STATE_DESTROYED:
|
|
|
|
break;
|
2008-04-30 07:55:01 +00:00
|
|
|
case ODEBUG_STATE_INIT:
|
|
|
|
case ODEBUG_STATE_INACTIVE:
|
|
|
|
case ODEBUG_STATE_ACTIVE:
|
2023-10-25 21:39:07 +00:00
|
|
|
if (obj->astate)
|
|
|
|
break;
|
|
|
|
obj->state = ODEBUG_STATE_INACTIVE;
|
|
|
|
fallthrough;
|
2008-04-30 07:55:01 +00:00
|
|
|
default:
|
2023-10-25 21:39:07 +00:00
|
|
|
raw_spin_unlock_irqrestore(&db->lock, flags);
|
|
|
|
return;
|
2008-04-30 07:55:01 +00:00
|
|
|
}
|
2023-10-25 21:39:07 +00:00
|
|
|
o = *obj;
|
2019-05-20 14:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
raw_spin_unlock_irqrestore(&db->lock, flags);
|
2023-10-25 21:39:07 +00:00
|
|
|
debug_print_object(&o, "deactivate");
|
2008-04-30 07:55:01 +00:00
|
|
|
}
|
2016-11-30 23:54:10 +00:00
|
|
|
EXPORT_SYMBOL_GPL(debug_object_deactivate);
|
2008-04-30 07:55:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* debug_object_destroy - debug checks when an object is destroyed
|
|
|
|
* @addr: address of the object
|
|
|
|
* @descr: pointer to an object specific debug description structure
|
|
|
|
*/
|
2020-08-15 00:40:26 +00:00
|
|
|
void debug_object_destroy(void *addr, const struct debug_obj_descr *descr)
|
2008-04-30 07:55:01 +00:00
|
|
|
{
|
2023-10-25 21:39:07 +00:00
|
|
|
struct debug_obj *obj, o;
|
2008-04-30 07:55:01 +00:00
|
|
|
struct debug_bucket *db;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
if (!debug_objects_enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
db = get_bucket((unsigned long) addr);
|
|
|
|
|
2009-11-17 17:11:28 +00:00
|
|
|
raw_spin_lock_irqsave(&db->lock, flags);
|
2008-04-30 07:55:01 +00:00
|
|
|
|
|
|
|
obj = lookup_object(addr, db);
|
2023-10-25 21:39:07 +00:00
|
|
|
if (!obj) {
|
|
|
|
raw_spin_unlock_irqrestore(&db->lock, flags);
|
|
|
|
return;
|
|
|
|
}
|
2008-04-30 07:55:01 +00:00
|
|
|
|
|
|
|
switch (obj->state) {
|
2023-10-25 21:39:07 +00:00
|
|
|
case ODEBUG_STATE_ACTIVE:
|
|
|
|
case ODEBUG_STATE_DESTROYED:
|
|
|
|
break;
|
2008-04-30 07:55:01 +00:00
|
|
|
case ODEBUG_STATE_NONE:
|
|
|
|
case ODEBUG_STATE_INIT:
|
|
|
|
case ODEBUG_STATE_INACTIVE:
|
|
|
|
obj->state = ODEBUG_STATE_DESTROYED;
|
2023-10-25 21:39:07 +00:00
|
|
|
fallthrough;
|
|
|
|
default:
|
2009-11-17 17:11:28 +00:00
|
|
|
raw_spin_unlock_irqrestore(&db->lock, flags);
|
2008-04-30 07:55:01 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-10-25 21:39:07 +00:00
|
|
|
|
|
|
|
o = *obj;
|
2009-11-17 17:11:28 +00:00
|
|
|
raw_spin_unlock_irqrestore(&db->lock, flags);
|
2023-10-25 21:39:07 +00:00
|
|
|
debug_print_object(&o, "destroy");
|
|
|
|
|
|
|
|
if (o.state == ODEBUG_STATE_ACTIVE)
|
|
|
|
debug_object_fixup(descr->fixup_destroy, addr, o.state);
|
2008-04-30 07:55:01 +00:00
|
|
|
}
|
2016-11-30 23:54:10 +00:00
|
|
|
EXPORT_SYMBOL_GPL(debug_object_destroy);
|
2008-04-30 07:55:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* debug_object_free - debug checks when an object is freed
|
|
|
|
* @addr: address of the object
|
|
|
|
* @descr: pointer to an object specific debug description structure
|
|
|
|
*/
|
2020-08-15 00:40:26 +00:00
|
|
|
void debug_object_free(void *addr, const struct debug_obj_descr *descr)
|
2008-04-30 07:55:01 +00:00
|
|
|
{
|
2023-10-25 21:39:07 +00:00
|
|
|
struct debug_obj *obj, o;
|
2008-04-30 07:55:01 +00:00
|
|
|
struct debug_bucket *db;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
if (!debug_objects_enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
db = get_bucket((unsigned long) addr);
|
|
|
|
|
2009-11-17 17:11:28 +00:00
|
|
|
raw_spin_lock_irqsave(&db->lock, flags);
|
2008-04-30 07:55:01 +00:00
|
|
|
|
|
|
|
obj = lookup_object(addr, db);
|
2023-10-25 21:39:07 +00:00
|
|
|
if (!obj) {
|
|
|
|
raw_spin_unlock_irqrestore(&db->lock, flags);
|
|
|
|
return;
|
|
|
|
}
|
2008-04-30 07:55:01 +00:00
|
|
|
|
|
|
|
switch (obj->state) {
|
|
|
|
case ODEBUG_STATE_ACTIVE:
|
2023-10-25 21:39:07 +00:00
|
|
|
break;
|
2008-04-30 07:55:01 +00:00
|
|
|
default:
|
|
|
|
hlist_del(&obj->node);
|
2009-11-17 17:11:28 +00:00
|
|
|
raw_spin_unlock_irqrestore(&db->lock, flags);
|
2008-04-30 07:55:01 +00:00
|
|
|
free_object(obj);
|
2008-08-31 21:39:21 +00:00
|
|
|
return;
|
2008-04-30 07:55:01 +00:00
|
|
|
}
|
2023-10-25 21:39:07 +00:00
|
|
|
|
|
|
|
o = *obj;
|
2009-11-17 17:11:28 +00:00
|
|
|
raw_spin_unlock_irqrestore(&db->lock, flags);
|
2023-10-25 21:39:07 +00:00
|
|
|
debug_print_object(&o, "free");
|
|
|
|
|
|
|
|
debug_object_fixup(descr->fixup_free, addr, o.state);
|
2008-04-30 07:55:01 +00:00
|
|
|
}
|
2016-11-30 23:54:10 +00:00
|
|
|
EXPORT_SYMBOL_GPL(debug_object_free);
|
2008-04-30 07:55:01 +00:00
|
|
|
|
2011-11-08 03:48:27 +00:00
|
|
|
/**
|
|
|
|
* debug_object_assert_init - debug checks when object should be init-ed
|
|
|
|
* @addr: address of the object
|
|
|
|
* @descr: pointer to an object specific debug description structure
|
|
|
|
*/
|
2020-08-15 00:40:26 +00:00
|
|
|
void debug_object_assert_init(void *addr, const struct debug_obj_descr *descr)
|
2011-11-08 03:48:27 +00:00
|
|
|
{
|
2023-04-12 07:54:39 +00:00
|
|
|
struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
|
2011-11-08 03:48:27 +00:00
|
|
|
struct debug_bucket *db;
|
|
|
|
struct debug_obj *obj;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
if (!debug_objects_enabled)
|
|
|
|
return;
|
|
|
|
|
2023-05-01 15:42:06 +00:00
|
|
|
debug_objects_fill_pool();
|
|
|
|
|
2011-11-08 03:48:27 +00:00
|
|
|
db = get_bucket((unsigned long) addr);
|
|
|
|
|
|
|
|
raw_spin_lock_irqsave(&db->lock, flags);
|
2023-04-12 07:54:39 +00:00
|
|
|
obj = lookup_object_or_alloc(addr, db, descr, false, true);
|
|
|
|
raw_spin_unlock_irqrestore(&db->lock, flags);
|
|
|
|
if (likely(!IS_ERR_OR_NULL(obj)))
|
|
|
|
return;
|
2011-11-08 03:48:27 +00:00
|
|
|
|
2023-04-12 07:54:39 +00:00
|
|
|
/* If NULL the allocation has hit OOM */
|
2011-11-08 03:48:27 +00:00
|
|
|
if (!obj) {
|
2023-04-12 07:54:39 +00:00
|
|
|
debug_objects_oom();
|
2011-11-08 03:48:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-12 07:54:39 +00:00
|
|
|
/* Object is neither tracked nor static. It's not initialized. */
|
|
|
|
debug_print_object(&o, "assert_init");
|
|
|
|
debug_object_fixup(descr->fixup_assert_init, addr, ODEBUG_STATE_NOTAVAILABLE);
|
2011-11-08 03:48:27 +00:00
|
|
|
}
|
2016-11-30 23:54:10 +00:00
|
|
|
EXPORT_SYMBOL_GPL(debug_object_assert_init);
|
2011-11-08 03:48:27 +00:00
|
|
|
|
2010-04-17 12:48:38 +00:00
|
|
|
/**
|
|
|
|
* debug_object_active_state - debug checks object usage state machine
|
|
|
|
* @addr: address of the object
|
|
|
|
* @descr: pointer to an object specific debug description structure
|
|
|
|
* @expect: expected state
|
|
|
|
* @next: state to move to if expected state is found
|
|
|
|
*/
|
|
|
|
void
|
2020-08-15 00:40:26 +00:00
|
|
|
debug_object_active_state(void *addr, const struct debug_obj_descr *descr,
|
2010-04-17 12:48:38 +00:00
|
|
|
unsigned int expect, unsigned int next)
|
|
|
|
{
|
2023-10-25 21:39:07 +00:00
|
|
|
struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
|
2010-04-17 12:48:38 +00:00
|
|
|
struct debug_bucket *db;
|
|
|
|
struct debug_obj *obj;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
if (!debug_objects_enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
db = get_bucket((unsigned long) addr);
|
|
|
|
|
|
|
|
raw_spin_lock_irqsave(&db->lock, flags);
|
|
|
|
|
|
|
|
obj = lookup_object(addr, db);
|
|
|
|
if (obj) {
|
|
|
|
switch (obj->state) {
|
|
|
|
case ODEBUG_STATE_ACTIVE:
|
2023-10-25 21:39:07 +00:00
|
|
|
if (obj->astate != expect)
|
|
|
|
break;
|
|
|
|
obj->astate = next;
|
|
|
|
raw_spin_unlock_irqrestore(&db->lock, flags);
|
|
|
|
return;
|
2010-04-17 12:48:38 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2023-10-25 21:39:07 +00:00
|
|
|
o = *obj;
|
2019-05-20 14:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
raw_spin_unlock_irqrestore(&db->lock, flags);
|
2023-10-25 21:39:07 +00:00
|
|
|
debug_print_object(&o, "active_state");
|
2010-04-17 12:48:38 +00:00
|
|
|
}
|
2016-11-30 23:54:10 +00:00
|
|
|
EXPORT_SYMBOL_GPL(debug_object_active_state);
|
2010-04-17 12:48:38 +00:00
|
|
|
|
2008-04-30 07:55:01 +00:00
|
|
|
#ifdef CONFIG_DEBUG_OBJECTS_FREE
|
|
|
|
static void __debug_check_no_obj_freed(const void *address, unsigned long size)
|
|
|
|
{
|
|
|
|
unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
|
2023-10-25 21:39:07 +00:00
|
|
|
int cnt, objs_checked = 0;
|
|
|
|
struct debug_obj *obj, o;
|
2008-04-30 07:55:01 +00:00
|
|
|
struct debug_bucket *db;
|
2018-02-05 23:18:28 +00:00
|
|
|
struct hlist_node *tmp;
|
2008-04-30 07:55:01 +00:00
|
|
|
|
|
|
|
saddr = (unsigned long) address;
|
|
|
|
eaddr = saddr + size;
|
|
|
|
paddr = saddr & ODEBUG_CHUNK_MASK;
|
|
|
|
chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
|
|
|
|
chunks >>= ODEBUG_CHUNK_SHIFT;
|
|
|
|
|
|
|
|
for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
|
|
|
|
db = get_bucket(paddr);
|
|
|
|
|
|
|
|
repeat:
|
|
|
|
cnt = 0;
|
2009-11-17 17:11:28 +00:00
|
|
|
raw_spin_lock_irqsave(&db->lock, flags);
|
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 01:06:00 +00:00
|
|
|
hlist_for_each_entry_safe(obj, tmp, &db->list, node) {
|
2008-04-30 07:55:01 +00:00
|
|
|
cnt++;
|
|
|
|
oaddr = (unsigned long) obj->object;
|
|
|
|
if (oaddr < saddr || oaddr >= eaddr)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch (obj->state) {
|
|
|
|
case ODEBUG_STATE_ACTIVE:
|
2023-10-25 21:39:07 +00:00
|
|
|
o = *obj;
|
2009-11-17 17:11:28 +00:00
|
|
|
raw_spin_unlock_irqrestore(&db->lock, flags);
|
2023-10-25 21:39:07 +00:00
|
|
|
debug_print_object(&o, "free");
|
|
|
|
debug_object_fixup(o.descr->fixup_free, (void *)oaddr, o.state);
|
2008-04-30 07:55:01 +00:00
|
|
|
goto repeat;
|
|
|
|
default:
|
|
|
|
hlist_del(&obj->node);
|
2019-05-20 14:14:49 +00:00
|
|
|
__free_object(obj);
|
2008-04-30 07:55:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-11-17 17:11:28 +00:00
|
|
|
raw_spin_unlock_irqrestore(&db->lock, flags);
|
2008-08-31 21:39:21 +00:00
|
|
|
|
2008-04-30 07:55:01 +00:00
|
|
|
if (cnt > debug_objects_maxchain)
|
|
|
|
debug_objects_maxchain = cnt;
|
2018-02-05 23:18:25 +00:00
|
|
|
|
|
|
|
objs_checked += cnt;
|
2008-04-30 07:55:01 +00:00
|
|
|
}
|
2018-02-05 23:18:25 +00:00
|
|
|
|
|
|
|
if (objs_checked > debug_objects_maxchecked)
|
|
|
|
debug_objects_maxchecked = objs_checked;
|
2018-02-05 23:18:28 +00:00
|
|
|
|
|
|
|
/* Schedule work to actually kmem_cache_free() objects */
|
2024-10-07 16:50:04 +00:00
|
|
|
if (!READ_ONCE(obj_freeing) && pool_count(&pool_to_free)) {
|
2019-05-20 14:14:49 +00:00
|
|
|
WRITE_ONCE(obj_freeing, true);
|
|
|
|
schedule_delayed_work(&debug_obj_work, ODEBUG_FREE_WORK_DELAY);
|
|
|
|
}
|
2008-04-30 07:55:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void debug_check_no_obj_freed(const void *address, unsigned long size)
|
|
|
|
{
|
|
|
|
if (debug_objects_enabled)
|
|
|
|
__debug_check_no_obj_freed(address, size);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_FS
|
|
|
|
|
|
|
|
static int debug_stats_show(struct seq_file *m, void *v)
|
|
|
|
{
|
2019-05-20 14:14:46 +00:00
|
|
|
int cpu, obj_percpu_free = 0;
|
|
|
|
|
|
|
|
for_each_possible_cpu(cpu)
|
2024-10-07 16:50:07 +00:00
|
|
|
obj_percpu_free += per_cpu(pool_pcpu.cnt, cpu);
|
2019-05-20 14:14:46 +00:00
|
|
|
|
2008-04-30 07:55:01 +00:00
|
|
|
seq_printf(m, "max_chain :%d\n", debug_objects_maxchain);
|
2018-02-05 23:18:25 +00:00
|
|
|
seq_printf(m, "max_checked :%d\n", debug_objects_maxchecked);
|
2008-04-30 07:55:01 +00:00
|
|
|
seq_printf(m, "warnings :%d\n", debug_objects_warnings);
|
|
|
|
seq_printf(m, "fixups :%d\n", debug_objects_fixups);
|
2024-10-07 16:50:04 +00:00
|
|
|
seq_printf(m, "pool_free :%d\n", pool_count(&pool_global) + obj_percpu_free);
|
2019-05-20 14:14:46 +00:00
|
|
|
seq_printf(m, "pool_pcp_free :%d\n", obj_percpu_free);
|
2008-04-30 07:55:01 +00:00
|
|
|
seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
|
2019-05-20 14:14:46 +00:00
|
|
|
seq_printf(m, "pool_used :%d\n", obj_pool_used - obj_percpu_free);
|
2008-04-30 07:55:01 +00:00
|
|
|
seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
|
2024-10-07 16:50:04 +00:00
|
|
|
seq_printf(m, "on_free_list :%d\n", pool_count(&pool_to_free));
|
2017-02-07 21:40:30 +00:00
|
|
|
seq_printf(m, "objs_allocated:%d\n", debug_objects_allocated);
|
|
|
|
seq_printf(m, "objs_freed :%d\n", debug_objects_freed);
|
2008-04-30 07:55:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2020-07-16 08:47:47 +00:00
|
|
|
DEFINE_SHOW_ATTRIBUTE(debug_stats);
|
2008-04-30 07:55:01 +00:00
|
|
|
|
|
|
|
static int __init debug_objects_init_debugfs(void)
|
|
|
|
{
|
2019-06-12 15:35:13 +00:00
|
|
|
struct dentry *dbgdir;
|
2008-04-30 07:55:01 +00:00
|
|
|
|
|
|
|
if (!debug_objects_enabled)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
dbgdir = debugfs_create_dir("debug_objects", NULL);
|
|
|
|
|
2019-06-12 15:35:13 +00:00
|
|
|
debugfs_create_file("stats", 0444, dbgdir, NULL, &debug_stats_fops);
|
2008-04-30 07:55:01 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
__initcall(debug_objects_init_debugfs);
|
|
|
|
|
|
|
|
#else
|
|
|
|
static inline void debug_objects_init_debugfs(void) { }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
|
|
|
|
|
|
|
|
/* Random data structure for the self test */
|
|
|
|
struct self_test {
|
|
|
|
unsigned long dummy1[6];
|
|
|
|
int static_init;
|
|
|
|
unsigned long dummy2[3];
|
|
|
|
};
|
|
|
|
|
2020-08-15 00:40:26 +00:00
|
|
|
static __initconst const struct debug_obj_descr descr_type_test;
|
2008-04-30 07:55:01 +00:00
|
|
|
|
2016-05-20 00:09:41 +00:00
|
|
|
static bool __init is_static_object(void *addr)
|
|
|
|
{
|
|
|
|
struct self_test *obj = addr;
|
|
|
|
|
|
|
|
return obj->static_init;
|
|
|
|
}
|
|
|
|
|
2008-04-30 07:55:01 +00:00
|
|
|
/*
|
|
|
|
* fixup_init is called when:
|
|
|
|
* - an active object is initialized
|
|
|
|
*/
|
2016-05-20 00:09:20 +00:00
|
|
|
static bool __init fixup_init(void *addr, enum debug_obj_state state)
|
2008-04-30 07:55:01 +00:00
|
|
|
{
|
|
|
|
struct self_test *obj = addr;
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case ODEBUG_STATE_ACTIVE:
|
|
|
|
debug_object_deactivate(obj, &descr_type_test);
|
|
|
|
debug_object_init(obj, &descr_type_test);
|
2016-05-20 00:09:20 +00:00
|
|
|
return true;
|
2008-04-30 07:55:01 +00:00
|
|
|
default:
|
2016-05-20 00:09:20 +00:00
|
|
|
return false;
|
2008-04-30 07:55:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* fixup_activate is called when:
|
|
|
|
* - an active object is activated
|
2016-05-20 00:09:41 +00:00
|
|
|
* - an unknown non-static object is activated
|
2008-04-30 07:55:01 +00:00
|
|
|
*/
|
2016-05-20 00:09:20 +00:00
|
|
|
static bool __init fixup_activate(void *addr, enum debug_obj_state state)
|
2008-04-30 07:55:01 +00:00
|
|
|
{
|
|
|
|
struct self_test *obj = addr;
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case ODEBUG_STATE_NOTAVAILABLE:
|
2016-05-20 00:09:20 +00:00
|
|
|
return true;
|
2008-04-30 07:55:01 +00:00
|
|
|
case ODEBUG_STATE_ACTIVE:
|
|
|
|
debug_object_deactivate(obj, &descr_type_test);
|
|
|
|
debug_object_activate(obj, &descr_type_test);
|
2016-05-20 00:09:20 +00:00
|
|
|
return true;
|
2008-04-30 07:55:01 +00:00
|
|
|
|
|
|
|
default:
|
2016-05-20 00:09:20 +00:00
|
|
|
return false;
|
2008-04-30 07:55:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* fixup_destroy is called when:
|
|
|
|
* - an active object is destroyed
|
|
|
|
*/
|
2016-05-20 00:09:20 +00:00
|
|
|
static bool __init fixup_destroy(void *addr, enum debug_obj_state state)
|
2008-04-30 07:55:01 +00:00
|
|
|
{
|
|
|
|
struct self_test *obj = addr;
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case ODEBUG_STATE_ACTIVE:
|
|
|
|
debug_object_deactivate(obj, &descr_type_test);
|
|
|
|
debug_object_destroy(obj, &descr_type_test);
|
2016-05-20 00:09:20 +00:00
|
|
|
return true;
|
2008-04-30 07:55:01 +00:00
|
|
|
default:
|
2016-05-20 00:09:20 +00:00
|
|
|
return false;
|
2008-04-30 07:55:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* fixup_free is called when:
|
|
|
|
* - an active object is freed
|
|
|
|
*/
|
2016-05-20 00:09:20 +00:00
|
|
|
static bool __init fixup_free(void *addr, enum debug_obj_state state)
|
2008-04-30 07:55:01 +00:00
|
|
|
{
|
|
|
|
struct self_test *obj = addr;
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case ODEBUG_STATE_ACTIVE:
|
|
|
|
debug_object_deactivate(obj, &descr_type_test);
|
|
|
|
debug_object_free(obj, &descr_type_test);
|
2016-05-20 00:09:20 +00:00
|
|
|
return true;
|
2008-04-30 07:55:01 +00:00
|
|
|
default:
|
2016-05-20 00:09:20 +00:00
|
|
|
return false;
|
2008-04-30 07:55:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-26 19:38:35 +00:00
|
|
|
static int __init
|
2008-04-30 07:55:01 +00:00
|
|
|
check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
|
|
|
|
{
|
|
|
|
struct debug_bucket *db;
|
|
|
|
struct debug_obj *obj;
|
|
|
|
unsigned long flags;
|
|
|
|
int res = -EINVAL;
|
|
|
|
|
|
|
|
db = get_bucket((unsigned long) addr);
|
|
|
|
|
2009-11-17 17:11:28 +00:00
|
|
|
raw_spin_lock_irqsave(&db->lock, flags);
|
2008-04-30 07:55:01 +00:00
|
|
|
|
|
|
|
obj = lookup_object(addr, db);
|
|
|
|
if (!obj && state != ODEBUG_STATE_NONE) {
|
2008-07-26 02:45:39 +00:00
|
|
|
WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
|
2008-04-30 07:55:01 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (obj && obj->state != state) {
|
2008-07-26 02:45:39 +00:00
|
|
|
WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
|
2008-04-30 07:55:01 +00:00
|
|
|
obj->state, state);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (fixups != debug_objects_fixups) {
|
2008-07-26 02:45:39 +00:00
|
|
|
WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
|
2008-04-30 07:55:01 +00:00
|
|
|
fixups, debug_objects_fixups);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (warnings != debug_objects_warnings) {
|
2008-07-26 02:45:39 +00:00
|
|
|
WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
|
2008-04-30 07:55:01 +00:00
|
|
|
warnings, debug_objects_warnings);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
res = 0;
|
|
|
|
out:
|
2009-11-17 17:11:28 +00:00
|
|
|
raw_spin_unlock_irqrestore(&db->lock, flags);
|
2008-04-30 07:55:01 +00:00
|
|
|
if (res)
|
2024-10-07 16:50:02 +00:00
|
|
|
debug_objects_enabled = false;
|
2008-04-30 07:55:01 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-08-15 00:40:26 +00:00
|
|
|
static __initconst const struct debug_obj_descr descr_type_test = {
|
2008-04-30 07:55:01 +00:00
|
|
|
.name = "selftest",
|
2016-05-20 00:09:41 +00:00
|
|
|
.is_static_object = is_static_object,
|
2008-04-30 07:55:01 +00:00
|
|
|
.fixup_init = fixup_init,
|
|
|
|
.fixup_activate = fixup_activate,
|
|
|
|
.fixup_destroy = fixup_destroy,
|
|
|
|
.fixup_free = fixup_free,
|
|
|
|
};
|
|
|
|
|
|
|
|
static __initdata struct self_test obj = { .static_init = 0 };
|
|
|
|
|
2024-10-07 16:49:54 +00:00
|
|
|
static bool __init debug_objects_selftest(void)
|
2008-04-30 07:55:01 +00:00
|
|
|
{
|
|
|
|
int fixups, oldfixups, warnings, oldwarnings;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
local_irq_save(flags);
|
|
|
|
|
|
|
|
fixups = oldfixups = debug_objects_fixups;
|
|
|
|
warnings = oldwarnings = debug_objects_warnings;
|
|
|
|
descr_test = &descr_type_test;
|
|
|
|
|
|
|
|
debug_object_init(&obj, &descr_type_test);
|
|
|
|
if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
|
|
|
|
goto out;
|
|
|
|
debug_object_activate(&obj, &descr_type_test);
|
|
|
|
if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
|
|
|
|
goto out;
|
|
|
|
debug_object_activate(&obj, &descr_type_test);
|
|
|
|
if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
|
|
|
|
goto out;
|
|
|
|
debug_object_deactivate(&obj, &descr_type_test);
|
|
|
|
if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
|
|
|
|
goto out;
|
|
|
|
debug_object_destroy(&obj, &descr_type_test);
|
|
|
|
if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
|
|
|
|
goto out;
|
|
|
|
debug_object_init(&obj, &descr_type_test);
|
|
|
|
if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
|
|
|
|
goto out;
|
|
|
|
debug_object_activate(&obj, &descr_type_test);
|
|
|
|
if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
|
|
|
|
goto out;
|
|
|
|
debug_object_deactivate(&obj, &descr_type_test);
|
|
|
|
if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
|
|
|
|
goto out;
|
|
|
|
debug_object_free(&obj, &descr_type_test);
|
|
|
|
if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
obj.static_init = 1;
|
|
|
|
debug_object_activate(&obj, &descr_type_test);
|
2012-03-05 22:59:17 +00:00
|
|
|
if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
|
2008-04-30 07:55:01 +00:00
|
|
|
goto out;
|
|
|
|
debug_object_init(&obj, &descr_type_test);
|
|
|
|
if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
|
|
|
|
goto out;
|
|
|
|
debug_object_free(&obj, &descr_type_test);
|
|
|
|
if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_OBJECTS_FREE
|
|
|
|
debug_object_init(&obj, &descr_type_test);
|
|
|
|
if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
|
|
|
|
goto out;
|
|
|
|
debug_object_activate(&obj, &descr_type_test);
|
|
|
|
if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
|
|
|
|
goto out;
|
|
|
|
__debug_check_no_obj_freed(&obj, sizeof(obj));
|
|
|
|
if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
|
|
|
|
goto out;
|
|
|
|
#endif
|
2014-06-04 23:06:04 +00:00
|
|
|
pr_info("selftest passed\n");
|
2008-04-30 07:55:01 +00:00
|
|
|
|
|
|
|
out:
|
|
|
|
debug_objects_fixups = oldfixups;
|
|
|
|
debug_objects_warnings = oldwarnings;
|
|
|
|
descr_test = NULL;
|
|
|
|
|
|
|
|
local_irq_restore(flags);
|
2024-10-07 16:50:02 +00:00
|
|
|
return debug_objects_enabled;
|
2008-04-30 07:55:01 +00:00
|
|
|
}
|
|
|
|
#else
|
2024-10-07 16:49:54 +00:00
|
|
|
static inline bool debug_objects_selftest(void) { return true; }
|
2008-04-30 07:55:01 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called during early boot to initialize the hash buckets and link
|
|
|
|
* the static object pool objects into the poll list. After this call
|
|
|
|
* the object tracker is fully operational.
|
|
|
|
*/
|
|
|
|
void __init debug_objects_early_init(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ODEBUG_HASH_SIZE; i++)
|
2009-11-17 17:11:28 +00:00
|
|
|
raw_spin_lock_init(&obj_hash[i].lock);
|
2008-04-30 07:55:01 +00:00
|
|
|
|
2024-10-07 16:50:05 +00:00
|
|
|
/* Keep early boot simple and add everything to the boot list */
|
2008-04-30 07:55:01 +00:00
|
|
|
for (i = 0; i < ODEBUG_POOL_SIZE; i++)
|
2024-10-07 16:50:05 +00:00
|
|
|
hlist_add_head(&obj_static_pool[i].node, &pool_boot);
|
2008-04-30 07:55:01 +00:00
|
|
|
}
|
|
|
|
|
2009-03-16 17:53:18 +00:00
|
|
|
/*
|
2024-10-07 16:49:54 +00:00
|
|
|
* Convert the statically allocated objects to dynamic ones.
|
|
|
|
* debug_objects_mem_init() is called early so only one CPU is up and
|
|
|
|
* interrupts are disabled, which means it is safe to replace the active
|
|
|
|
* object references.
|
2009-03-16 17:53:18 +00:00
|
|
|
*/
|
2024-10-07 16:49:54 +00:00
|
|
|
static bool __init debug_objects_replace_static_objects(struct kmem_cache *cache)
|
2009-03-16 17:53:18 +00:00
|
|
|
{
|
|
|
|
struct debug_bucket *db = obj_hash;
|
2024-10-07 16:49:54 +00:00
|
|
|
struct hlist_node *tmp;
|
2024-10-07 16:50:15 +00:00
|
|
|
struct debug_obj *obj;
|
2009-03-16 17:53:18 +00:00
|
|
|
HLIST_HEAD(objects);
|
2024-10-07 16:49:59 +00:00
|
|
|
int i;
|
2009-03-16 17:53:18 +00:00
|
|
|
|
2024-10-07 16:50:15 +00:00
|
|
|
for (i = 0; i < ODEBUG_POOL_SIZE; i += ODEBUG_BATCH_SIZE) {
|
|
|
|
if (!kmem_alloc_batch(&objects, cache, GFP_KERNEL))
|
2009-03-16 17:53:18 +00:00
|
|
|
goto free;
|
2024-10-07 16:50:15 +00:00
|
|
|
pool_push_batch(&pool_global, &objects);
|
2009-03-16 17:53:18 +00:00
|
|
|
}
|
|
|
|
|
2024-10-07 16:50:15 +00:00
|
|
|
/* Disconnect the boot pool. */
|
2024-10-07 16:50:05 +00:00
|
|
|
pool_boot.first = NULL;
|
2009-03-16 17:53:18 +00:00
|
|
|
|
|
|
|
/* Replace the active object references */
|
|
|
|
for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
|
|
|
|
hlist_move_list(&db->list, &objects);
|
|
|
|
|
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 01:06:00 +00:00
|
|
|
hlist_for_each_entry(obj, &objects, node) {
|
2024-10-07 16:50:15 +00:00
|
|
|
struct debug_obj *new = pcpu_alloc();
|
|
|
|
|
2009-03-16 17:53:18 +00:00
|
|
|
/* copy object data */
|
|
|
|
*new = *obj;
|
|
|
|
hlist_add_head(&new->node, &db->list);
|
|
|
|
}
|
|
|
|
}
|
2024-10-07 16:49:54 +00:00
|
|
|
return true;
|
2009-03-16 17:53:18 +00:00
|
|
|
free:
|
2024-10-07 16:50:01 +00:00
|
|
|
/* Can't use free_object_list() as the cache is not populated yet */
|
2024-10-07 16:50:15 +00:00
|
|
|
hlist_for_each_entry_safe(obj, tmp, &pool_global.objects, node) {
|
2009-03-16 17:53:18 +00:00
|
|
|
hlist_del(&obj->node);
|
2024-10-07 16:49:54 +00:00
|
|
|
kmem_cache_free(cache, obj);
|
2009-03-16 17:53:18 +00:00
|
|
|
}
|
2024-10-07 16:49:54 +00:00
|
|
|
return false;
|
2009-03-16 17:53:18 +00:00
|
|
|
}
|
|
|
|
|
2008-04-30 07:55:01 +00:00
|
|
|
/*
|
|
|
|
* Called after the kmem_caches are functional to setup a dedicated
|
|
|
|
* cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
|
|
|
|
* prevents that the debug code is called on kmem_cache_free() for the
|
|
|
|
* debug tracker objects to avoid recursive calls.
|
|
|
|
*/
|
|
|
|
void __init debug_objects_mem_init(void)
|
|
|
|
{
|
2024-10-07 16:49:54 +00:00
|
|
|
struct kmem_cache *cache;
|
2024-10-07 16:49:56 +00:00
|
|
|
int extras;
|
2019-05-20 14:14:46 +00:00
|
|
|
|
2008-04-30 07:55:01 +00:00
|
|
|
if (!debug_objects_enabled)
|
|
|
|
return;
|
|
|
|
|
2024-10-07 16:49:54 +00:00
|
|
|
if (!debug_objects_selftest())
|
|
|
|
return;
|
2008-04-30 07:55:01 +00:00
|
|
|
|
2024-10-07 16:49:54 +00:00
|
|
|
cache = kmem_cache_create("debug_objects_cache", sizeof (struct debug_obj), 0,
|
|
|
|
SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE, NULL);
|
|
|
|
|
|
|
|
if (!cache || !debug_objects_replace_static_objects(cache)) {
|
2024-10-07 16:50:02 +00:00
|
|
|
debug_objects_enabled = false;
|
2024-10-07 16:49:54 +00:00
|
|
|
pr_warn("Out of memory.\n");
|
2022-06-11 13:06:34 +00:00
|
|
|
return;
|
2024-10-07 16:49:54 +00:00
|
|
|
}
|
2020-09-08 06:27:09 +00:00
|
|
|
|
2019-05-20 14:14:47 +00:00
|
|
|
/*
|
2024-10-07 16:49:54 +00:00
|
|
|
* Adjust the thresholds for allocating and freeing objects
|
|
|
|
* according to the number of possible CPUs available in the
|
|
|
|
* system.
|
2019-05-20 14:14:47 +00:00
|
|
|
*/
|
|
|
|
extras = num_possible_cpus() * ODEBUG_BATCH_SIZE;
|
2024-10-07 16:50:08 +00:00
|
|
|
pool_global.max_cnt += extras;
|
|
|
|
pool_global.min_cnt += extras;
|
2024-10-07 16:49:54 +00:00
|
|
|
|
|
|
|
/* Everything worked. Expose the cache */
|
|
|
|
obj_cache = cache;
|
2024-10-07 16:50:13 +00:00
|
|
|
static_branch_enable(&obj_cache_enabled);
|
2024-10-07 16:49:54 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_HOTPLUG_CPU
|
|
|
|
cpuhp_setup_state_nocalls(CPUHP_DEBUG_OBJ_DEAD, "object:offline", NULL,
|
|
|
|
object_cpu_offline);
|
|
|
|
#endif
|
|
|
|
return;
|
2008-04-30 07:55:01 +00:00
|
|
|
}
|