mirror of
https://github.com/BobTheBlinker/android_kernel_motorola_sm6375.git
synced 2026-10-28 02:32:09 -04:00
FROMGIT: kfence: add option to use KFENCE without static keys
For certain usecases, specifically where the sample interval is always set to a very low value such as 1ms, it can make sense to use a dynamic branch instead of static branches due to the overhead of toggling a static branch. Therefore, add a new Kconfig option to remove the static branches and instead check kfence_allocation_gate if a KFENCE allocation should be set up. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Marco Elver <[email protected]> Suggested-by: Jörn Engel <[email protected]> Reviewed-by: Jörn Engel <[email protected]> Cc: Alexander Potapenko <[email protected]> Cc: Dmitry Vyukov <[email protected]> Cc: Jann Horn <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Bug: 177201466 (cherry picked from commit c01761611b325c1e4ec7d3e236cc9db003cb82fd https://github.com/hnaz/linux-mm v5.11-rc4-mmots-2021-01-21-20-10) Test: CONFIG_KFENCE_KUNIT_TEST=y passes on Cuttlefish Signed-off-by: Alexander Potapenko <[email protected]> Change-Id: I68a112a8ff68fa24742b198e036f130a9757c27f
This commit is contained in:
parent
fa80023f3c
commit
9339667c95
3 changed files with 39 additions and 16 deletions
|
|
@ -95,11 +95,13 @@ struct kfence_metadata kfence_metadata[CONFIG_KFENCE_NUM_OBJECTS];
|
|||
static struct list_head kfence_freelist = LIST_HEAD_INIT(kfence_freelist);
|
||||
static DEFINE_RAW_SPINLOCK(kfence_freelist_lock); /* Lock protecting freelist. */
|
||||
|
||||
#ifdef CONFIG_KFENCE_STATIC_KEYS
|
||||
/* The static key to set up a KFENCE allocation. */
|
||||
DEFINE_STATIC_KEY_FALSE(kfence_allocation_key);
|
||||
#endif
|
||||
|
||||
/* Gates the allocation, ensuring only one succeeds in a given period. */
|
||||
static atomic_t allocation_gate = ATOMIC_INIT(1);
|
||||
atomic_t kfence_allocation_gate = ATOMIC_INIT(1);
|
||||
|
||||
/* Statistics counters for debugfs. */
|
||||
enum kfence_counter_id {
|
||||
|
|
@ -579,29 +581,31 @@ late_initcall(kfence_debugfs_init);
|
|||
static struct delayed_work kfence_timer;
|
||||
static void toggle_allocation_gate(struct work_struct *work)
|
||||
{
|
||||
unsigned long end_wait;
|
||||
|
||||
if (!READ_ONCE(kfence_enabled))
|
||||
return;
|
||||
|
||||
/* Enable static key, and await allocation to happen. */
|
||||
atomic_set(&allocation_gate, 0);
|
||||
atomic_set(&kfence_allocation_gate, 0);
|
||||
#ifdef CONFIG_KFENCE_STATIC_KEYS
|
||||
static_branch_enable(&kfence_allocation_key);
|
||||
/*
|
||||
* Await an allocation. Timeout after 1 second, in case the kernel stops
|
||||
* doing allocations, to avoid stalling this worker task for too long.
|
||||
*/
|
||||
end_wait = jiffies + HZ;
|
||||
do {
|
||||
set_current_state(TASK_UNINTERRUPTIBLE);
|
||||
if (atomic_read(&allocation_gate) != 0)
|
||||
break;
|
||||
schedule_timeout(1);
|
||||
} while (time_before(jiffies, end_wait));
|
||||
__set_current_state(TASK_RUNNING);
|
||||
{
|
||||
unsigned long end_wait = jiffies + HZ;
|
||||
|
||||
do {
|
||||
set_current_state(TASK_UNINTERRUPTIBLE);
|
||||
if (atomic_read(&kfence_allocation_gate) != 0)
|
||||
break;
|
||||
schedule_timeout(1);
|
||||
} while (time_before(jiffies, end_wait));
|
||||
__set_current_state(TASK_RUNNING);
|
||||
}
|
||||
/* Disable static key and reset timer. */
|
||||
static_branch_disable(&kfence_allocation_key);
|
||||
#endif
|
||||
schedule_delayed_work(&kfence_timer, msecs_to_jiffies(kfence_sample_interval));
|
||||
}
|
||||
static DECLARE_DELAYED_WORK(kfence_timer, toggle_allocation_gate);
|
||||
|
|
@ -707,7 +711,7 @@ void *__kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags)
|
|||
* sense to continue writing to it and pay the associated contention
|
||||
* cost, in case we have a large number of concurrent allocations.
|
||||
*/
|
||||
if (atomic_read(&allocation_gate) || atomic_inc_return(&allocation_gate) > 1)
|
||||
if (atomic_read(&kfence_allocation_gate) || atomic_inc_return(&kfence_allocation_gate) > 1)
|
||||
return NULL;
|
||||
|
||||
if (!READ_ONCE(kfence_enabled))
|
||||
|
|
|
|||
Loading…
Reference in a new issue