mm: Remove kmem_valid_obj()

commit 6e284c55fc0bef7d25fd34d29db11f483da60ea4 upstream.

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 <willy@infradead.org>
Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
Reviewed-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Acked-by: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
Signed-off-by: Frederic Weisbecker <frederic@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
Zhen Lei 2023-08-05 11:17:25 +08:00 committed by Greg Kroah-Hartman
parent 62b6ce5d87
commit a34268fefb
3 changed files with 15 additions and 35 deletions

View File

@ -215,8 +215,9 @@ DEFINE_FREE(kfree, void *, if (!IS_ERR_OR_NULL(_T)) kfree(_T))
size_t ksize(const void *objp); size_t ksize(const void *objp);
#ifdef CONFIG_PRINTK #ifdef CONFIG_PRINTK
bool kmem_valid_obj(void *object); bool kmem_dump_obj(void *object);
void kmem_dump_obj(void *object); #else
static inline bool kmem_dump_obj(void *object) { return false; }
#endif #endif
/* /*

View File

@ -523,26 +523,6 @@ bool slab_is_available(void)
} }
#ifdef CONFIG_PRINTK #ifdef CONFIG_PRINTK
/**
* kmem_valid_obj - does the pointer reference a valid slab object?
* @object: pointer to query.
*
* Return: %true if the pointer is to a not-yet-freed object from
* kmalloc() or kmem_cache_alloc(), either %true or %false if the pointer
* is to an already-freed object, and %false otherwise.
*/
bool kmem_valid_obj(void *object)
{
struct folio *folio;
/* Some arches consider ZERO_SIZE_PTR to be a valid address. */
if (object < (void *)PAGE_SIZE || !virt_addr_valid(object))
return false;
folio = virt_to_folio(object);
return folio_test_slab(folio);
}
EXPORT_SYMBOL_GPL(kmem_valid_obj);
static void kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab) static void kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *slab)
{ {
if (__kfence_obj_info(kpp, object, slab)) if (__kfence_obj_info(kpp, object, slab))
@ -561,11 +541,11 @@ static void kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *
* and, if available, the slab name, return address, and stack trace from * and, if available, the slab name, return address, and stack trace from
* the allocation and last free path of that object. * the allocation and last free path of that object.
* *
* This function will splat if passed a pointer to a non-slab object. * Return: %true if the pointer is to a not-yet-freed object from
* If you are not sure what type of object you have, you should instead * kmalloc() or kmem_cache_alloc(), either %true or %false if the pointer
* use mem_dump_obj(). * is to an already-freed object, and %false otherwise.
*/ */
void kmem_dump_obj(void *object) bool kmem_dump_obj(void *object)
{ {
char *cp = IS_ENABLED(CONFIG_MMU) ? "" : "/vmalloc"; char *cp = IS_ENABLED(CONFIG_MMU) ? "" : "/vmalloc";
int i; int i;
@ -573,13 +553,13 @@ void kmem_dump_obj(void *object)
unsigned long ptroffset; unsigned long ptroffset;
struct kmem_obj_info kp = { }; struct kmem_obj_info kp = { };
if (WARN_ON_ONCE(!virt_addr_valid(object))) /* Some arches consider ZERO_SIZE_PTR to be a valid address. */
return; if (object < (void *)PAGE_SIZE || !virt_addr_valid(object))
return false;
slab = virt_to_slab(object); slab = virt_to_slab(object);
if (WARN_ON_ONCE(!slab)) { if (!slab)
pr_cont(" non-slab memory.\n"); return false;
return;
}
kmem_obj_info(&kp, object, slab); kmem_obj_info(&kp, object, slab);
if (kp.kp_slab_cache) if (kp.kp_slab_cache)
pr_cont(" slab%s %s", cp, kp.kp_slab_cache->name); pr_cont(" slab%s %s", cp, kp.kp_slab_cache->name);
@ -616,6 +596,7 @@ void kmem_dump_obj(void *object)
pr_info(" %pS\n", kp.kp_free_stack[i]); pr_info(" %pS\n", kp.kp_free_stack[i]);
} }
return true;
} }
EXPORT_SYMBOL_GPL(kmem_dump_obj); EXPORT_SYMBOL_GPL(kmem_dump_obj);
#endif #endif

View File

@ -1119,10 +1119,8 @@ void mem_dump_obj(void *object)
{ {
const char *type; const char *type;
if (kmem_valid_obj(object)) { if (kmem_dump_obj(object))
kmem_dump_obj(object);
return; return;
}
if (vmalloc_dump_obj(object)) if (vmalloc_dump_obj(object))
return; return;