Skip to content

Commit 0edd7b5

Browse files
Liu Shixinakpm00
authored andcommitted
mm: kmemleak: split __create_object into two functions
__create_object() consists of two part, the first part allocate a kmemleak object and initialize it, the second part insert it into object tree. This function need kmemleak_lock but actually only the second part need lock. Split it into two functions, the first function __alloc_object only allocate a kmemleak object, and the second function __link_object() will initialize the object and insert it into object tree, use the kmemleak_lock to protect __link_object() only. [akpm@linux-foundation.org: coding-style cleanups] Link: https://lkml.kernel.org/r/20231018102952.3339837-5-liushixin2@huawei.com Signed-off-by: Liu Shixin <liushixin2@huawei.com> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Kefeng Wang <wangkefeng.wang@huawei.com> Cc: Patrick Wang <patrick.wang.shcn@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 62047e0 commit 0edd7b5

1 file changed

Lines changed: 40 additions & 21 deletions

File tree

mm/kmemleak.c

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -623,39 +623,24 @@ static noinline depot_stack_handle_t set_track_prepare(void)
623623
return trace_handle;
624624
}
625625

626-
/*
627-
* Create the metadata (struct kmemleak_object) corresponding to an allocated
628-
* memory block and add it to the object_list and object_tree_root (or
629-
* object_phys_tree_root).
630-
*/
631-
static void __create_object(unsigned long ptr, size_t size,
632-
int min_count, gfp_t gfp, bool is_phys)
626+
static struct kmemleak_object *__alloc_object(gfp_t gfp)
633627
{
634-
unsigned long flags;
635-
struct kmemleak_object *object, *parent;
636-
struct rb_node **link, *rb_parent;
637-
unsigned long untagged_ptr;
638-
unsigned long untagged_objp;
628+
struct kmemleak_object *object;
639629

640630
object = mem_pool_alloc(gfp);
641631
if (!object) {
642632
pr_warn("Cannot allocate a kmemleak_object structure\n");
643633
kmemleak_disable();
644-
return;
634+
return NULL;
645635
}
646636

647637
INIT_LIST_HEAD(&object->object_list);
648638
INIT_LIST_HEAD(&object->gray_list);
649639
INIT_HLIST_HEAD(&object->area_list);
650640
raw_spin_lock_init(&object->lock);
651641
atomic_set(&object->use_count, 1);
652-
object->flags = OBJECT_ALLOCATED | (is_phys ? OBJECT_PHYS : 0);
653-
object->pointer = ptr;
654-
object->size = kfence_ksize((void *)ptr) ?: size;
655642
object->excess_ref = 0;
656-
object->min_count = min_count;
657643
object->count = 0; /* white color initially */
658-
object->jiffies = jiffies;
659644
object->checksum = 0;
660645
object->del_state = 0;
661646

@@ -680,7 +665,23 @@ static void __create_object(unsigned long ptr, size_t size,
680665
/* kernel backtrace */
681666
object->trace_handle = set_track_prepare();
682667

683-
raw_spin_lock_irqsave(&kmemleak_lock, flags);
668+
return object;
669+
}
670+
671+
static void __link_object(struct kmemleak_object *object, unsigned long ptr,
672+
size_t size, int min_count, bool is_phys)
673+
{
674+
675+
struct kmemleak_object *parent;
676+
struct rb_node **link, *rb_parent;
677+
unsigned long untagged_ptr;
678+
unsigned long untagged_objp;
679+
680+
object->flags = OBJECT_ALLOCATED | (is_phys ? OBJECT_PHYS : 0);
681+
object->pointer = ptr;
682+
object->size = kfence_ksize((void *)ptr) ?: size;
683+
object->min_count = min_count;
684+
object->jiffies = jiffies;
684685

685686
untagged_ptr = (unsigned long)kasan_reset_tag((void *)ptr);
686687
/*
@@ -711,14 +712,32 @@ static void __create_object(unsigned long ptr, size_t size,
711712
*/
712713
dump_object_info(parent);
713714
kmem_cache_free(object_cache, object);
714-
goto out;
715+
return;
715716
}
716717
}
717718
rb_link_node(&object->rb_node, rb_parent, link);
718719
rb_insert_color(&object->rb_node, is_phys ? &object_phys_tree_root :
719720
&object_tree_root);
720721
list_add_tail_rcu(&object->object_list, &object_list);
721-
out:
722+
}
723+
724+
/*
725+
* Create the metadata (struct kmemleak_object) corresponding to an allocated
726+
* memory block and add it to the object_list and object_tree_root (or
727+
* object_phys_tree_root).
728+
*/
729+
static void __create_object(unsigned long ptr, size_t size,
730+
int min_count, gfp_t gfp, bool is_phys)
731+
{
732+
struct kmemleak_object *object;
733+
unsigned long flags;
734+
735+
object = __alloc_object(gfp);
736+
if (!object)
737+
return;
738+
739+
raw_spin_lock_irqsave(&kmemleak_lock, flags);
740+
__link_object(object, ptr, size, min_count, is_phys);
722741
raw_spin_unlock_irqrestore(&kmemleak_lock, flags);
723742
}
724743

0 commit comments

Comments
 (0)