Unverified Commit 73c55d5a authored by openeuler-ci-bot's avatar openeuler-ci-bot Committed by Gitee
Browse files

!3897 Some simple extensions of the kfence feature

Merge Pull Request from: @ci-robot 
 
PR sync from: Ze Zuo <zuoze1@huawei.com>
https://mailweb.openeuler.org/hyperkitty/list/kernel@openeuler.org/message/WMUY7ZB2JWAGSPZWTUW4K3756JNX3LD7/ 
This patchset supports the following two features:

1. The number of kfence objects is a key factor to balance kfence bug
detection capabilities and performance loss, and often needs to be
optimized according to actual business scenarios. The number of
kfence objects can be dynamically configured kfence.num_objects
cmdline, so as to reduce the release of kernel versions in actual use,
thereby reducing costs.

2. The kfence feature under arm64 supports dynamic opening after system
startup, which will turn all the kernel memblock memory into page-mapped
mapping and increase the memory overhead, in order to save the page
table memory usage, only the kfence pool supports page-mapped mapping,
which can be enabled at kernel startup by setting
kfence.sample _interval=-1 at kernel startup to enable the dynamic on
capability, at this time it will allocate memory for kfence by default.
The kfence will be allocated memory by default. Subsequent dynamic
enablement and disabling will no longer consume additional memory.

This feature has been tested under ARM64, x86_64 and ARM architectures,
and the Kfence Kunit test results are consistent with the native ones.

ChangeLog:
- Add config kfence_must_early_init to perform code isolation for
  non-functional purposes.
- Some minor changes about document, no functional changes.

Ze Zuo (2):
  kfence: Add a module parameter to adjust kfence objects
  arm64: kfence: scale sample_interval to support early init for kfence.


-- 
2.25.1
 
https://gitee.com/openeuler/kernel/issues/I8Q3P9 
 
Link:https://gitee.com/openeuler/kernel/pulls/3897

 

Reviewed-by: default avatarKefeng Wang <wangkefeng.wang@huawei.com>
Signed-off-by: default avatarZheng Zengkai <zhengzengkai@huawei.com>
parents 2f8f5a20 2ef3a018
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -53,13 +53,19 @@ configurable via the Kconfig option ``CONFIG_KFENCE_DEFERRABLE``.
   The KUnit test suite is very likely to fail when using a deferrable timer
   since it currently causes very unpredictable sample intervals.

The KFENCE memory pool is of fixed size, and if the pool is exhausted, no
If ``CONFIG_KFENCE_DYNAMIC_OBJECTS`` is disabled,
the KFENCE memory pool is of fixed size, and if the pool is exhausted, no
further KFENCE allocations occur. With ``CONFIG_KFENCE_NUM_OBJECTS`` (default
255), the number of available guarded objects can be controlled. Each object
requires 2 pages, one for the object itself and the other one used as a guard
page; object pages are interleaved with guard pages, and every object page is
therefore surrounded by two guard pages.

If ``CONFIG_KFENCE_DYNAMIC_OBJECTS`` is enabled,
the KFENCE memory pool size could be set via the kernel boot parameter
``kfence.num_objects``. Note, the performance will degrade due to additional
instructions(eg, load) added to the fast path of the memory allocation.

The total memory dedicated to the KFENCE memory pool can be computed as::

    ( #objects + 1 ) * 2 * PAGE_SIZE
+3 −0
Original line number Diff line number Diff line
@@ -23,6 +23,9 @@ static inline bool kfence_protect_page(unsigned long addr, bool protect)
extern bool kfence_early_init;
static inline bool arm64_kfence_can_set_direct_map(void)
{
	if (IS_ENABLED(CONFIG_KFENCE_MUST_EARLY_INIT))
		return false;

	return !kfence_early_init;
}
#else /* CONFIG_KFENCE */
+5 −0
Original line number Diff line number Diff line
@@ -520,6 +520,11 @@ static int __init parse_kfence_early_init(char *arg)

	if (get_option(&arg, &val))
		kfence_early_init = !!val;

#if IS_ENABLED(CONFIG_KFENCE_MUST_EARLY_INIT)
	kfence_must_early_init = (val == -1) ? true : false;
#endif

	return 0;
}
early_param("kfence.sample_interval", parse_kfence_early_init);
+12 −1
Original line number Diff line number Diff line
@@ -19,12 +19,23 @@

extern unsigned long kfence_sample_interval;

#if IS_ENABLED(CONFIG_KFENCE_MUST_EARLY_INIT)
extern bool __ro_after_init kfence_must_early_init;
#endif

#ifdef CONFIG_KFENCE_DYNAMIC_OBJECTS
extern int kfence_num_objects;
#define KFENCE_NR_OBJECTS kfence_num_objects
#else
#define KFENCE_NR_OBJECTS CONFIG_KFENCE_NUM_OBJECTS
#endif

/*
 * We allocate an even number of pages, as it simplifies calculations to map
 * address to metadata indices; effectively, the very first page serves as an
 * extended guard page, but otherwise has no special purpose.
 */
#define KFENCE_POOL_SIZE ((CONFIG_KFENCE_NUM_OBJECTS + 1) * 2 * PAGE_SIZE)
#define KFENCE_POOL_SIZE ((KFENCE_NR_OBJECTS + 1) * 2 * PAGE_SIZE)
extern char *__kfence_pool;

DECLARE_STATIC_KEY_FALSE(kfence_allocation_key);
+28 −0
Original line number Diff line number Diff line
@@ -57,6 +57,34 @@ config KFENCE_DEFERRABLE

	  Say N if you are unsure.

config KFENCE_DYNAMIC_OBJECTS
	bool "Support dynamic configuration of the number of guarded objects"
	default n
	help
	  Enable dynamic configuration of the number of KFENCE guarded objects.
	  If this config is enabled, the number of KFENCE guarded objects could
	  be overridden via boot parameter "kfence.num_objects". Note that the
	  performance will degrade due to additional instructions(eg, load)
	  added to the fast path of the memory allocation.

	  Say N if you are unsure.

config KFENCE_MUST_EARLY_INIT
	bool "Require kfence_pool to be pre-allocated on arm64."
	depends on ARM64
	help
	  To support KFENCE late init, arm64 will convert block mapping to
	  page-level mappings, which leads to performance degradation and
	  increased memory consumption.

	  If this config is enabled, only KFENCE memory early init for arm64
	  is supported, extending sample_interval to implement late enable. When
	  "kfence.sample_interval" is set to -1 or 0, KFENCE will not be enabled.
	  Only when "kfence.sample_interval" is set to -1, it can be enabled by
	  setting it to a non-zero value.

	  Say N if you are unsure.

config KFENCE_STATIC_KEYS
	bool "Use static keys to set up allocations" if EXPERT
	depends on JUMP_LABEL
Loading