On Tue, 12 Jul 2022, Hyeonggon Yoo wrote: > __ksize() returns size of objects allocated from slab allocator. > When invalid object is passed to __ksize(), returning zero > prevents further memory corruption and makes caller be able to > check if there is an error. > > If address of large object is not beginning of folio or size of > the folio is too small, it must be invalid. Return zero in such cases. Why return 0 if there is an error and why bother the callers with these checks. BUG()? > diff --git a/mm/slab_common.c b/mm/slab_common.c > index 1f8db7959366..0d6cbe9d7ad0 100644 > --- a/mm/slab_common.c > +++ b/mm/slab_common.c > @@ -1013,8 +1013,12 @@ size_t __ksize(const void *object) > > folio = virt_to_folio(object); > > - if (unlikely(!folio_test_slab(folio))) > + if (unlikely(!folio_test_slab(folio))) { > + if (WARN_ON(object != folio_address(folio) || > + folio_size(folio) <= KMALLOC_MAX_CACHE_SIZE)) Hmmm... This may change things a bit. Before this patch it was possible to determine the storage size of order-0 pages using ksize(). Now this returns 0? I guess this is an error since the order-0 page cannot come from slab allocations.