Skip to content

Commit 4da81ea

Browse files
Liu Shixinopsiff
authored andcommitted
mm/kmemleak: fix partially freeing unknown object warning
mainline inclusion from mainline-v6.7-rc1 category: bugfix delete_object_part() can be called by multiple callers in the same time. If an object is found and removed by a caller, and then another caller try to find it too, it failed and return directly. It still be recorded by kmemleak even if it has already been freed to buddy. With DEBUG on, kmemleak will report the following warning, kmemleak: Partially freeing unknown object at 0xa1af86000 (size 4096) CPU: 0 PID: 742 Comm: test_huge Not tainted 6.6.0-rc3kmemleak+ deepin-community#54 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1ubuntu1.1 04/01/2014 Call Trace: <TASK> dump_stack_lvl+0x37/0x50 kmemleak_free_part_phys+0x50/0x60 hugetlb_vmemmap_optimize+0x172/0x290 ? __pfx_vmemmap_remap_pte+0x10/0x10 __prep_new_hugetlb_folio+0xe/0x30 prep_new_hugetlb_folio.isra.0+0xe/0x40 alloc_fresh_hugetlb_folio+0xc3/0xd0 alloc_surplus_hugetlb_folio.constprop.0+0x6e/0xd0 hugetlb_acct_memory.part.0+0xe6/0x2a0 hugetlb_reserve_pages+0x110/0x2c0 hugetlbfs_file_mmap+0x11d/0x1b0 mmap_region+0x248/0x9a0 ? hugetlb_get_unmapped_area+0x15c/0x2d0 do_mmap+0x38b/0x580 vm_mmap_pgoff+0xe6/0x190 ksys_mmap_pgoff+0x18a/0x1f0 do_syscall_64+0x3f/0x90 entry_SYSCALL_64_after_hwframe+0x6e/0xd8 Expand __create_object() and move __alloc_object() to the beginning. Then use kmemleak_lock to protect __find_and_remove_object() and __link_object() as a whole, which can guarantee all objects are processed sequentialally. Link: https://lkml.kernel.org/r/20231018102952.3339837-8-liushixin2@huawei.com Fixes: 53238a6 ("kmemleak: Allow partial freeing of memory blocks") Signed-off-by: Liu Shixin <liushixin2@huawei.com> Reviewed-by: 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> (cherry picked from commit 5e4fc57) Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
1 parent b86c5d8 commit 4da81ea

1 file changed

Lines changed: 31 additions & 11 deletions

File tree

mm/kmemleak.c

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -819,16 +819,25 @@ static void delete_object_full(unsigned long ptr)
819819
*/
820820
static void delete_object_part(unsigned long ptr, size_t size, bool is_phys)
821821
{
822-
struct kmemleak_object *object;
823-
unsigned long start, end;
822+
struct kmemleak_object *object, *object_l, *object_r;
823+
unsigned long start, end, flags;
824+
825+
object_l = __alloc_object(GFP_KERNEL);
826+
if (!object_l)
827+
return;
824828

825-
object = find_and_remove_object(ptr, 1, is_phys);
829+
object_r = __alloc_object(GFP_KERNEL);
830+
if (!object_r)
831+
goto out;
832+
833+
raw_spin_lock_irqsave(&kmemleak_lock, flags);
834+
object = __find_and_remove_object(ptr, 1, is_phys);
826835
if (!object) {
827836
#ifdef DEBUG
828837
kmemleak_warn("Partially freeing unknown object at 0x%08lx (size %zu)\n",
829838
ptr, size);
830839
#endif
831-
return;
840+
goto unlock;
832841
}
833842

834843
/*
@@ -838,14 +847,25 @@ static void delete_object_part(unsigned long ptr, size_t size, bool is_phys)
838847
*/
839848
start = object->pointer;
840849
end = object->pointer + object->size;
841-
if (ptr > start)
842-
__create_object(start, ptr - start, object->min_count,
843-
GFP_KERNEL, is_phys);
844-
if (ptr + size < end)
845-
__create_object(ptr + size, end - ptr - size, object->min_count,
846-
GFP_KERNEL, is_phys);
850+
if ((ptr > start) &&
851+
!__link_object(object_l, start, ptr - start,
852+
object->min_count, is_phys))
853+
object_l = NULL;
854+
if ((ptr + size < end) &&
855+
!__link_object(object_r, ptr + size, end - ptr - size,
856+
object->min_count, is_phys))
857+
object_r = NULL;
858+
859+
unlock:
860+
raw_spin_unlock_irqrestore(&kmemleak_lock, flags);
861+
if (object)
862+
__delete_object(object);
847863

848-
__delete_object(object);
864+
out:
865+
if (object_l)
866+
mem_pool_free(object_l);
867+
if (object_r)
868+
mem_pool_free(object_r);
849869
}
850870

851871
static void __paint_it(struct kmemleak_object *object, int color)

0 commit comments

Comments
 (0)