anolis: kfence: improve performance on is_kfence_address()

ANBZ: #27

Since is_kfence_address() is a hot path, add a static branch to skip the
following steps such as get page and nid. This static branch is also
used to speedup kfence_ksize() and fix error when KFENCE is not inited
yet but kfence_ksize() is called by kmemleak.

Signed-off-by: Tianchen Ding <dtcccc@linux.alibaba.com>
Reviewed-by: Xunlei Pang <xlpang@linux.alibaba.com>
This commit is contained in:
Tianchen Ding 2021-11-25 10:03:37 +08:00 committed by Qiao Ma
parent beb36c58b8
commit 2385e92686
2 changed files with 12 additions and 2 deletions

View File

@ -31,6 +31,7 @@ DECLARE_STATIC_KEY_FALSE(kfence_allocation_key);
extern atomic_t kfence_allocation_gate;
#endif
DECLARE_STATIC_KEY_FALSE(kfence_skip_interval);
DECLARE_STATIC_KEY_FALSE(kfence_once_inited);
#define GFP_KFENCE_NOT_ALLOC ((GFP_ZONEMASK & ~__GFP_HIGHMEM) | __GFP_NOKFENCE | __GFP_THISNODE)
/**
@ -69,7 +70,7 @@ static __always_inline bool is_kfence_address_node(const void *addr, const int n
*/
static __always_inline bool is_kfence_address(const void *addr)
{
if (unlikely(!virt_addr_valid(addr)))
if (!static_branch_unlikely(&kfence_once_inited) || unlikely(!virt_addr_valid(addr)))
return false;
return unlikely(is_kfence_address_node(addr, page_to_nid(virt_to_page(addr))));

View File

@ -59,6 +59,8 @@ EXPORT_SYMBOL(kfence_pool_size);
DEFINE_STATIC_KEY_FALSE(kfence_allocation_key);
#endif
DEFINE_STATIC_KEY_FALSE(kfence_skip_interval);
DEFINE_STATIC_KEY_FALSE(kfence_once_inited);
EXPORT_SYMBOL(kfence_once_inited);
static int param_set_sample_interval(const char *val, const struct kernel_param *kp)
{
@ -1093,6 +1095,8 @@ void __init kfence_init(void)
pr_cont("\n");
}
static_branch_enable(&kfence_once_inited);
return;
fail:
@ -1273,7 +1277,12 @@ alloc:
size_t kfence_ksize(const void *addr)
{
const struct kfence_metadata *meta = addr_to_metadata((unsigned long)addr);
struct kfence_metadata *meta;
if (!static_branch_unlikely(&kfence_once_inited))
return 0;
meta = addr_to_metadata((unsigned long)addr);
/*
* Read locklessly -- if there is a race with __kfence_alloc(), this is