Commit d5dc831
committed
slab: replace cache_from_obj() with inline checks
Eric Dumazet has noticed cache_from_obj() is not inlined with clang and
suggested splitting it into two functions, where the smaller inlined one
assumes the fastpath is !CONFIG_SLAB_FREELIST_HARDENED. However most
distros enable it these days and so this would likely add a function
call to the object free fastpaths.
Instead take a step back and consider that cache_from_obj() is a relict
from when memcgs created their separate kmem_cache copies, as the
outdated comment in build_detached_freelist() reminds us.
Meanwhile hardening/debugging had reused cache_from_obj() to validate
that the freed object really belongs to a slab from the cache we think
we are freeing from.
In build_detached_freelist() simply remove this, because it did not
handle the NULL result from cache_from_obj() failure properly, nor
validate objects (for the NULL slab->slab_cache pointer) when called via
kfree_bulk(). If anyone is motivated to implement it properly, it should
be possible in a similar way to kmem_cache_free().
In kmem_cache_free(), do the hardening/debugging checks directly so they
are inlined by definition and virt_to_slab(obj) is performed just once.
In case they failed, call a newly introduced warn_free_bad_obj() that
performs the warnings outside of the fastpath, and leak the object.
As an intentional change, leak the object when slab->slab_cache differs
from the cache given to kmem_cache_free(). Previously we would only leak
when the object is not in a valid slab page or the slab->slab_cache
pointer is NULL, and otherwise trust the slab->slab_cache over the
kmem_cache_free() argument. But if those differ, it means something went
wrong enough that it's best not to continue freeing.
As a result the fastpath should be inlined in all configs and the
warnings are moved away.
Reported-by: Eric Dumazet <edumazet@google.com>
Closes: https://lore.kernel.org/all/20260115130642.3419324-1-edumazet@google.com/
Reviewed-by: Harry Yoo <harry.yoo@oracle.com>
Reviewed-by: Hao Li <hao.li@linux.dev>
Acked-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: Vlastimil Babka <vbabka@suse.cz>1 parent 99a3e3a commit d5dc831
1 file changed
Lines changed: 33 additions & 23 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6742 | 6742 | | |
6743 | 6743 | | |
6744 | 6744 | | |
6745 | | - | |
| 6745 | + | |
6746 | 6746 | | |
| 6747 | + | |
6747 | 6748 | | |
6748 | 6749 | | |
6749 | 6750 | | |
6750 | | - | |
6751 | | - | |
6752 | | - | |
6753 | | - | |
6754 | | - | |
6755 | | - | |
6756 | | - | |
6757 | | - | |
| 6751 | + | |
| 6752 | + | |
| 6753 | + | |
| 6754 | + | |
6758 | 6755 | | |
6759 | | - | |
6760 | | - | |
6761 | | - | |
| 6756 | + | |
6762 | 6757 | | |
6763 | | - | |
6764 | | - | |
6765 | | - | |
6766 | | - | |
6767 | | - | |
6768 | | - | |
| 6758 | + | |
| 6759 | + | |
| 6760 | + | |
| 6761 | + | |
| 6762 | + | |
| 6763 | + | |
| 6764 | + | |
6769 | 6765 | | |
6770 | 6766 | | |
6771 | 6767 | | |
| |||
6778 | 6774 | | |
6779 | 6775 | | |
6780 | 6776 | | |
6781 | | - | |
6782 | | - | |
6783 | | - | |
| 6777 | + | |
| 6778 | + | |
| 6779 | + | |
| 6780 | + | |
| 6781 | + | |
| 6782 | + | |
| 6783 | + | |
| 6784 | + | |
| 6785 | + | |
| 6786 | + | |
| 6787 | + | |
| 6788 | + | |
| 6789 | + | |
| 6790 | + | |
| 6791 | + | |
| 6792 | + | |
| 6793 | + | |
6784 | 6794 | | |
6785 | | - | |
| 6795 | + | |
6786 | 6796 | | |
6787 | 6797 | | |
6788 | 6798 | | |
| |||
7309 | 7319 | | |
7310 | 7320 | | |
7311 | 7321 | | |
7312 | | - | |
| 7322 | + | |
7313 | 7323 | | |
7314 | 7324 | | |
7315 | 7325 | | |
| |||
0 commit comments