Skip to content

Commit 6e284c5

Browse files
Zhen LeiFrederic Weisbecker
authored andcommitted
mm: Remove kmem_valid_obj()
Function kmem_dump_obj() will splat if passed a pointer to a non-slab object. So nothing calls it directly, instead calling kmem_valid_obj() first to determine whether the passed pointer to a valid slab object. This means that merging kmem_valid_obj() into kmem_dump_obj() will make the code more concise. Therefore, convert kmem_dump_obj() to work the same way as vmalloc_dump_obj(), removing the need for the kmem_dump_obj() caller to check kmem_valid_obj(). After this, there are no remaining calls to kmem_valid_obj() anymore, and it can be safely removed. Suggested-by: Matthew Wilcox <[email protected]> Signed-off-by: Zhen Lei <[email protected]> Reviewed-by: Matthew Wilcox (Oracle) <[email protected]> Acked-by: Vlastimil Babka <[email protected]> Signed-off-by: Paul E. McKenney <[email protected]> Signed-off-by: Frederic Weisbecker <[email protected]>
1 parent b93c5fe commit 6e284c5

File tree

3 files changed

+15
-35
lines changed

3 files changed

+15
-35
lines changed

include/linux/slab.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,9 @@ DEFINE_FREE(kfree, void *, if (_T) kfree(_T))
245245
size_t ksize(const void *objp);
246246

247247
#ifdef CONFIG_PRINTK
248-
bool kmem_valid_obj(void *object);
249-
void kmem_dump_obj(void *object);
248+
bool kmem_dump_obj(void *object);
249+
#else
250+
static inline bool kmem_dump_obj(void *object) { return false; }
250251
#endif
251252

252253
/*

mm/slab_common.c

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -528,26 +528,6 @@ bool slab_is_available(void)
528528
}
529529

530530
#ifdef CONFIG_PRINTK
531-
/**
532-
* kmem_valid_obj - does the pointer reference a valid slab object?
533-
* @object: pointer to query.
534-
*
535-
* Return: %true if the pointer is to a not-yet-freed object from
536-
* kmalloc() or kmem_cache_alloc(), either %true or %false if the pointer
537-
* is to an already-freed object, and %false otherwise.
538-
*/
539-
bool kmem_valid_obj(void *object)
540-
{
541-
struct folio *folio;
542-
543-
/* Some arches consider ZERO_SIZE_PTR to be a valid address. */
544-
if (object < (void *)PAGE_SIZE || !virt_addr_valid(object))
545-
return false;
546-
folio = virt_to_folio(object);
547-
return folio_test_slab(folio);
548-
}
549-
EXPORT_SYMBOL_GPL(kmem_valid_obj);
550-
551531
static void kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab)
552532
{
553533
if (__kfence_obj_info(kpp, object, slab))
@@ -566,25 +546,25 @@ static void kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *
566546
* and, if available, the slab name, return address, and stack trace from
567547
* the allocation and last free path of that object.
568548
*
569-
* This function will splat if passed a pointer to a non-slab object.
570-
* If you are not sure what type of object you have, you should instead
571-
* use mem_dump_obj().
549+
* Return: %true if the pointer is to a not-yet-freed object from
550+
* kmalloc() or kmem_cache_alloc(), either %true or %false if the pointer
551+
* is to an already-freed object, and %false otherwise.
572552
*/
573-
void kmem_dump_obj(void *object)
553+
bool kmem_dump_obj(void *object)
574554
{
575555
char *cp = IS_ENABLED(CONFIG_MMU) ? "" : "/vmalloc";
576556
int i;
577557
struct slab *slab;
578558
unsigned long ptroffset;
579559
struct kmem_obj_info kp = { };
580560

581-
if (WARN_ON_ONCE(!virt_addr_valid(object)))
582-
return;
561+
/* Some arches consider ZERO_SIZE_PTR to be a valid address. */
562+
if (object < (void *)PAGE_SIZE || !virt_addr_valid(object))
563+
return false;
583564
slab = virt_to_slab(object);
584-
if (WARN_ON_ONCE(!slab)) {
585-
pr_cont(" non-slab memory.\n");
586-
return;
587-
}
565+
if (!slab)
566+
return false;
567+
588568
kmem_obj_info(&kp, object, slab);
589569
if (kp.kp_slab_cache)
590570
pr_cont(" slab%s %s", cp, kp.kp_slab_cache->name);
@@ -621,6 +601,7 @@ void kmem_dump_obj(void *object)
621601
pr_info(" %pS\n", kp.kp_free_stack[i]);
622602
}
623603

604+
return true;
624605
}
625606
EXPORT_SYMBOL_GPL(kmem_dump_obj);
626607
#endif

mm/util.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,10 +1060,8 @@ void mem_dump_obj(void *object)
10601060
{
10611061
const char *type;
10621062

1063-
if (kmem_valid_obj(object)) {
1064-
kmem_dump_obj(object);
1063+
if (kmem_dump_obj(object))
10651064
return;
1066-
}
10671065

10681066
if (vmalloc_dump_obj(object))
10691067
return;

0 commit comments

Comments
 (0)