RCU barrier in the module unload path is needed to ensure all kfree_rcu operations which modify tag counters are complete before we free the tags. However this is not necessary if memory allocation profiling was never enabled on the system. Introduce a sticky flag that records whether memory profiling was ever enabled and make the RCU barrier conditional. Suggested-by: Vlastimil Babka Signed-off-by: Suren Baghdasaryan --- Applies over mm-new include/linux/codetag.h | 2 ++ lib/alloc_tag.c | 11 ++++++++++- lib/codetag.c | 14 ++++++++++++-- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/include/linux/codetag.h b/include/linux/codetag.h index 8ea2a5f7c98a..8a07a83cabdd 100644 --- a/include/linux/codetag.h +++ b/include/linux/codetag.h @@ -95,6 +95,7 @@ void codetag_free_module_sections(struct module *mod); void codetag_module_replaced(struct module *mod, struct module *new_mod); int codetag_load_module(struct module *mod); void codetag_unload_module(struct module *mod); +void codetag_flush_rcu_on_module_unload(void); #else /* defined(CONFIG_CODE_TAGGING) && defined(CONFIG_MODULES) */ @@ -109,6 +110,7 @@ static inline void codetag_free_module_sections(struct module *mod) {} static inline void codetag_module_replaced(struct module *mod, struct module *new_mod) {} static inline int codetag_load_module(struct module *mod) { return 0; } static inline void codetag_unload_module(struct module *mod) {} +static inline void codetag_flush_rcu_on_module_unload(void) {} #endif /* defined(CONFIG_CODE_TAGGING) && defined(CONFIG_MODULES) */ diff --git a/lib/alloc_tag.c b/lib/alloc_tag.c index 27fee57a5c91..173eebfacb1e 100644 --- a/lib/alloc_tag.c +++ b/lib/alloc_tag.c @@ -776,10 +776,16 @@ EXPORT_SYMBOL(page_alloc_tagging_ops); static int proc_mem_profiling_handler(const struct ctl_table *table, int write, void *buffer, size_t *lenp, loff_t *ppos) { + int ret; + if (!mem_profiling_support && write) return -EINVAL; - return proc_do_static_key(table, write, buffer, lenp, ppos); + ret = proc_do_static_key(table, write, buffer, lenp, ppos); + if (!ret && write && mem_alloc_profiling_enabled()) + codetag_flush_rcu_on_module_unload(); + + return ret; } @@ -829,6 +835,9 @@ static int __init alloc_tag_init(void) return 0; } + if (mem_alloc_profiling_enabled()) + codetag_flush_rcu_on_module_unload(); + if (!proc_create_seq_private(ALLOCINFO_FILE_NAME, 0400, NULL, &allocinfo_seq_op, sizeof(struct allocinfo_private), NULL)) { pr_err("Failed to create %s file\n", ALLOCINFO_FILE_NAME); diff --git a/lib/codetag.c b/lib/codetag.c index 545911cebd25..ed6d6425b62a 100644 --- a/lib/codetag.c +++ b/lib/codetag.c @@ -34,6 +34,7 @@ struct codetag_module { static DEFINE_MUTEX(codetag_lock); static LIST_HEAD(codetag_types); +static bool flush_rcu_on_module_unload; void codetag_lock_module_list(struct codetag_type *cttype, bool lock) { @@ -335,6 +336,11 @@ int codetag_load_module(struct module *mod) return ret; } +void codetag_flush_rcu_on_module_unload(void) +{ + flush_rcu_on_module_unload = true; +} + void codetag_unload_module(struct module *mod) { struct codetag_type *cttype; @@ -342,8 +348,12 @@ void codetag_unload_module(struct module *mod) if (!mod) return; - /* await any module's kfree_rcu() operations to complete */ - kvfree_rcu_barrier(); + /* + * Await any module's kfree_rcu() operations to complete + * if profiling was ever enabled. + */ + if (flush_rcu_on_module_unload) + kvfree_rcu_barrier(); mutex_lock(&codetag_lock); list_for_each_entry(cttype, &codetag_types, link) { base-commit: 3f43be96f919cc611dcb2a4e38dd464831f4513e -- 2.52.0.223.gf5cc29aaa4-goog