From nobody Fri Dec 27 15:00:31 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0137119C54D; Fri, 29 Nov 2024 16:48:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732898884; cv=none; b=eturyf7HypEulJfrvkrUWgGNicxgV6wkTZDzEr2mOTrAoh8wuT+fFEw4/SVgEwUCkBBMQYK5bUb6NWPrbowguN9GJl3leno64fx8p6+ypBaXs1kl/124FoaARSMf5Us3mrxXax2603lVRVm6EnIg/2bc+95U4oIYulTmhBeeWCs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732898884; c=relaxed/simple; bh=cLxIbSGBoRaPw3169/Ujnl5h46Pq1YmSYLjEwUEjjI4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=X3alxcuV8JpTRmNxIQD3ayIdq0CWuZeUTw0P4+wl4pVQ451YsSb1Ait1iHVtXOl8uvDATQ7s6pGtxTq601DCH/jAVkcF7EWVktaQoKEvZ4jIkNYiGpu1wfkZcBW43aqPFtkDhwXmYPQ6VKhJPIMdjueNLtY4BBP9XxzfJ2Epbh4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=p0ENWV5f; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="p0ENWV5f" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A178AC4CECF; Fri, 29 Nov 2024 16:48:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1732898883; bh=cLxIbSGBoRaPw3169/Ujnl5h46Pq1YmSYLjEwUEjjI4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=p0ENWV5f8UnLZ1wJ0txhZnsWVicVguA5Jn3cj+/RzUcLKS1hiaUQ6ImdlXyss3KO5 5PXbvgb9rfNPJKqqybXOfhzmZ7gSO5HD1qpxU+LiO00PRrofxj5GSkqqlObxX0dnjm yTV6R4WWBXEo28FzYVjqOChMnLaIvmnPXdPoltdIFfzDjs8bs7l7CyCf73U98RdZMh CYj/SHoSxYAoQtzj1rDoctyiCfApEgeu1o0H1x/LKD8cQKra96MBAs4m5MPcMXg3TJ 0R/xtG0LXOOoMzMK1pPjJZy9InUAA+u8PVBhVwvFUjDX2p/NyLmEu2UdKNjeDd9Ynj d2EBXASXvPT+Q== From: "Masami Hiramatsu (Google)" To: Steven Rostedt , Naveen N Rao Cc: Anil S Keshavamurthy , Masami Hiramatsu , "David S . Miller" , Mathieu Desnoyers , Oleg Nesterov , Tzvetomir Stoyanov , linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [PATCH v2 2/6] kprobes: Adopt guard() and scoped_guard() Date: Sat, 30 Nov 2024 01:47:58 +0900 Message-ID: <173289887835.73724.608223217359025939.stgit@devnote2> X-Mailer: git-send-email 2.43.0 In-Reply-To: <173289885627.73724.13632317993038475335.stgit@devnote2> References: <173289885627.73724.13632317993038475335.stgit@devnote2> User-Agent: StGit/0.19 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Masami Hiramatsu (Google) Use guard() or scoped_guard() for critical sections rather than discrete lock/unlock pairs. Signed-off-by: Masami Hiramatsu (Google) --- kernel/kprobes.c | 205 +++++++++++++++++++++++---------------------------= ---- 1 file changed, 89 insertions(+), 116 deletions(-) diff --git a/kernel/kprobes.c b/kernel/kprobes.c index cb9dbdafbbcf..62b5b08d809d 100644 --- a/kernel/kprobes.c +++ b/kernel/kprobes.c @@ -141,10 +141,9 @@ static int collect_garbage_slots(struct kprobe_insn_ca= che *c); kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c) { struct kprobe_insn_page *kip; - kprobe_opcode_t *slot =3D NULL; =20 /* Since the slot array is not protected by rcu, we need a mutex */ - mutex_lock(&c->mutex); + guard(mutex)(&c->mutex); retry: rcu_read_lock(); list_for_each_entry_rcu(kip, &c->pages, list) { @@ -155,9 +154,8 @@ kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cac= he *c) if (kip->slot_used[i] =3D=3D SLOT_CLEAN) { kip->slot_used[i] =3D SLOT_USED; kip->nused++; - slot =3D kip->insns + (i * c->insn_size); rcu_read_unlock(); - goto out; + return kip->insns + (i * c->insn_size); } } /* kip->nused is broken. Fix it. */ @@ -174,12 +172,12 @@ kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_c= ache *c) /* All out of space. Need to allocate a new page. */ kip =3D kmalloc(struct_size(kip, slot_used, slots_per_page(c)), GFP_KERNE= L); if (!kip) - goto out; + return NULL; =20 kip->insns =3D c->alloc(); if (!kip->insns) { kfree(kip); - goto out; + return NULL; } INIT_LIST_HEAD(&kip->list); memset(kip->slot_used, SLOT_CLEAN, slots_per_page(c)); @@ -188,14 +186,12 @@ kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_c= ache *c) kip->ngarbage =3D 0; kip->cache =3D c; list_add_rcu(&kip->list, &c->pages); - slot =3D kip->insns; =20 /* Record the perf ksymbol register event after adding the page */ perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL, (unsigned long)kip->insn= s, PAGE_SIZE, false, c->sym); -out: - mutex_unlock(&c->mutex); - return slot; + + return kip->insns; } =20 /* Return true if all garbages are collected, otherwise false. */ @@ -256,7 +252,7 @@ void __free_insn_slot(struct kprobe_insn_cache *c, struct kprobe_insn_page *kip; long idx; =20 - mutex_lock(&c->mutex); + guard(mutex)(&c->mutex); rcu_read_lock(); list_for_each_entry_rcu(kip, &c->pages, list) { idx =3D ((long)slot - (long)kip->insns) / @@ -282,7 +278,6 @@ void __free_insn_slot(struct kprobe_insn_cache *c, collect_one_slot(kip, idx); } } - mutex_unlock(&c->mutex); } =20 /* @@ -638,10 +633,9 @@ static void kprobe_optimizer(struct work_struct *work) mutex_unlock(&kprobe_mutex); } =20 -/* Wait for completing optimization and unoptimization */ -void wait_for_kprobe_optimizer(void) +static void wait_for_kprobe_optimizer_locked(void) { - mutex_lock(&kprobe_mutex); + lockdep_assert_held(&kprobe_mutex); =20 while (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list)) { mutex_unlock(&kprobe_mutex); @@ -653,8 +647,14 @@ void wait_for_kprobe_optimizer(void) =20 mutex_lock(&kprobe_mutex); } +} =20 - mutex_unlock(&kprobe_mutex); +/* Wait for completing optimization and unoptimization */ +void wait_for_kprobe_optimizer(void) +{ + guard(mutex)(&kprobe_mutex); + + wait_for_kprobe_optimizer_locked(); } =20 bool optprobe_queued_unopt(struct optimized_kprobe *op) @@ -884,10 +884,10 @@ static void optimize_all_kprobes(void) struct kprobe *p; unsigned int i; =20 - mutex_lock(&kprobe_mutex); + guard(mutex)(&kprobe_mutex); /* If optimization is already allowed, just return. */ if (kprobes_allow_optimization) - goto out; + return; =20 cpus_read_lock(); kprobes_allow_optimization =3D true; @@ -899,8 +899,6 @@ static void optimize_all_kprobes(void) } cpus_read_unlock(); pr_info("kprobe jump-optimization is enabled. All kprobes are optimized i= f possible.\n"); -out: - mutex_unlock(&kprobe_mutex); } =20 #ifdef CONFIG_SYSCTL @@ -910,12 +908,10 @@ static void unoptimize_all_kprobes(void) struct kprobe *p; unsigned int i; =20 - mutex_lock(&kprobe_mutex); + guard(mutex)(&kprobe_mutex); /* If optimization is already prohibited, just return. */ - if (!kprobes_allow_optimization) { - mutex_unlock(&kprobe_mutex); + if (!kprobes_allow_optimization) return; - } =20 cpus_read_lock(); kprobes_allow_optimization =3D false; @@ -927,10 +923,8 @@ static void unoptimize_all_kprobes(void) } } cpus_read_unlock(); - mutex_unlock(&kprobe_mutex); - /* Wait for unoptimizing completion. */ - wait_for_kprobe_optimizer(); + wait_for_kprobe_optimizer_locked(); pr_info("kprobe jump-optimization is disabled. All kprobes are based on s= oftware breakpoint.\n"); } =20 @@ -942,7 +936,7 @@ static int proc_kprobes_optimization_handler(const stru= ct ctl_table *table, { int ret; =20 - mutex_lock(&kprobe_sysctl_mutex); + guard(mutex)(&kprobe_sysctl_mutex); sysctl_kprobes_optimization =3D kprobes_allow_optimization ? 1 : 0; ret =3D proc_dointvec_minmax(table, write, buffer, length, ppos); =20 @@ -950,7 +944,6 @@ static int proc_kprobes_optimization_handler(const stru= ct ctl_table *table, optimize_all_kprobes(); else unoptimize_all_kprobes(); - mutex_unlock(&kprobe_sysctl_mutex); =20 return ret; } @@ -1025,7 +1018,8 @@ static void __disarm_kprobe(struct kprobe *p, bool re= opt) #define __arm_kprobe(p) arch_arm_kprobe(p) #define __disarm_kprobe(p, o) arch_disarm_kprobe(p) #define kprobe_disarmed(p) kprobe_disabled(p) -#define wait_for_kprobe_optimizer() do {} while (0) +#define wait_for_kprobe_optimizer_locked() \ + lockdep_assert_held(&kprobe_mutex) =20 static int reuse_unused_kprobe(struct kprobe *ap) { @@ -1489,6 +1483,7 @@ _kprobe_addr(kprobe_opcode_t *addr, const char *symbo= l_name, static kprobe_opcode_t *kprobe_addr(struct kprobe *p) { bool on_func_entry; + return _kprobe_addr(p->addr, p->symbol_name, p->offset, &on_func_entry); } =20 @@ -1523,14 +1518,12 @@ static struct kprobe *__get_valid_kprobe(struct kpr= obe *p) */ static inline int warn_kprobe_rereg(struct kprobe *p) { - int ret =3D 0; + guard(mutex)(&kprobe_mutex); =20 - mutex_lock(&kprobe_mutex); if (WARN_ON_ONCE(__get_valid_kprobe(p))) - ret =3D -EINVAL; - mutex_unlock(&kprobe_mutex); + return -EINVAL; =20 - return ret; + return 0; } =20 static int check_ftrace_location(struct kprobe *p) @@ -1618,44 +1611,17 @@ static int check_kprobe_address_safe(struct kprobe = *p, return ret; } =20 -int register_kprobe(struct kprobe *p) +static int __register_kprobe(struct kprobe *p) { int ret; struct kprobe *old_p; - struct module *probed_mod; - kprobe_opcode_t *addr; - bool on_func_entry; - - /* Adjust probe address from symbol */ - addr =3D _kprobe_addr(p->addr, p->symbol_name, p->offset, &on_func_entry); - if (IS_ERR(addr)) - return PTR_ERR(addr); - p->addr =3D addr; - - ret =3D warn_kprobe_rereg(p); - if (ret) - return ret; =20 - /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */ - p->flags &=3D KPROBE_FLAG_DISABLED; - p->nmissed =3D 0; - INIT_LIST_HEAD(&p->list); - - ret =3D check_kprobe_address_safe(p, &probed_mod); - if (ret) - return ret; - - mutex_lock(&kprobe_mutex); - - if (on_func_entry) - p->flags |=3D KPROBE_FLAG_ON_FUNC_ENTRY; + guard(mutex)(&kprobe_mutex); =20 old_p =3D get_kprobe(p->addr); - if (old_p) { + if (old_p) /* Since this may unoptimize 'old_p', locking 'text_mutex'. */ - ret =3D register_aggr_kprobe(old_p, p); - goto out; - } + return register_aggr_kprobe(old_p, p); =20 cpus_read_lock(); /* Prevent text modification */ @@ -1664,7 +1630,7 @@ int register_kprobe(struct kprobe *p) mutex_unlock(&text_mutex); cpus_read_unlock(); if (ret) - goto out; + return ret; =20 INIT_HLIST_NODE(&p->hlist); hlist_add_head_rcu(&p->hlist, @@ -1675,14 +1641,43 @@ int register_kprobe(struct kprobe *p) if (ret) { hlist_del_rcu(&p->hlist); synchronize_rcu(); - goto out; } } =20 /* Try to optimize kprobe */ try_to_optimize_kprobe(p); -out: - mutex_unlock(&kprobe_mutex); + return 0; +} + +int register_kprobe(struct kprobe *p) +{ + int ret; + struct module *probed_mod; + kprobe_opcode_t *addr; + bool on_func_entry; + + /* Canonicalize probe address from symbol */ + addr =3D _kprobe_addr(p->addr, p->symbol_name, p->offset, &on_func_entry); + if (IS_ERR(addr)) + return PTR_ERR(addr); + p->addr =3D addr; + + ret =3D warn_kprobe_rereg(p); + if (ret) + return ret; + + /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */ + p->flags &=3D KPROBE_FLAG_DISABLED; + if (on_func_entry) + p->flags |=3D KPROBE_FLAG_ON_FUNC_ENTRY; + p->nmissed =3D 0; + INIT_LIST_HEAD(&p->list); + + ret =3D check_kprobe_address_safe(p, &probed_mod); + if (ret) + return ret; + + ret =3D __register_kprobe(p); =20 if (probed_mod) module_put(probed_mod); @@ -1858,12 +1853,11 @@ void unregister_kprobes(struct kprobe **kps, int nu= m) =20 if (num <=3D 0) return; - mutex_lock(&kprobe_mutex); - for (i =3D 0; i < num; i++) - if (__unregister_kprobe_top(kps[i]) < 0) - kps[i]->addr =3D NULL; - mutex_unlock(&kprobe_mutex); - + scoped_guard(mutex, &kprobe_mutex) { + for (i =3D 0; i < num; i++) + if (__unregister_kprobe_top(kps[i]) < 0) + kps[i]->addr =3D NULL; + } synchronize_rcu(); for (i =3D 0; i < num; i++) if (kps[i]->addr) @@ -2302,8 +2296,9 @@ void unregister_kretprobes(struct kretprobe **rps, in= t num) =20 if (num <=3D 0) return; - mutex_lock(&kprobe_mutex); for (i =3D 0; i < num; i++) { + guard(mutex)(&kprobe_mutex); + if (__unregister_kprobe_top(&rps[i]->kp) < 0) rps[i]->kp.addr =3D NULL; #ifdef CONFIG_KRETPROBE_ON_RETHOOK @@ -2312,7 +2307,6 @@ void unregister_kretprobes(struct kretprobe **rps, in= t num) rcu_assign_pointer(rps[i]->rph->rp, NULL); #endif } - mutex_unlock(&kprobe_mutex); =20 synchronize_rcu(); for (i =3D 0; i < num; i++) { @@ -2393,18 +2387,14 @@ static void kill_kprobe(struct kprobe *p) /* Disable one kprobe */ int disable_kprobe(struct kprobe *kp) { - int ret =3D 0; struct kprobe *p; =20 - mutex_lock(&kprobe_mutex); + guard(mutex)(&kprobe_mutex); =20 /* Disable this kprobe */ p =3D __disable_kprobe(kp); - if (IS_ERR(p)) - ret =3D PTR_ERR(p); =20 - mutex_unlock(&kprobe_mutex); - return ret; + return IS_ERR(p) ? PTR_ERR(p) : 0; } EXPORT_SYMBOL_GPL(disable_kprobe); =20 @@ -2414,20 +2404,16 @@ int enable_kprobe(struct kprobe *kp) int ret =3D 0; struct kprobe *p; =20 - mutex_lock(&kprobe_mutex); + guard(mutex)(&kprobe_mutex); =20 /* Check whether specified probe is valid. */ p =3D __get_valid_kprobe(kp); - if (unlikely(p =3D=3D NULL)) { - ret =3D -EINVAL; - goto out; - } + if (unlikely(p =3D=3D NULL)) + return -EINVAL; =20 - if (kprobe_gone(kp)) { + if (kprobe_gone(kp)) /* This kprobe has gone, we couldn't enable it. */ - ret =3D -EINVAL; - goto out; - } + return -EINVAL; =20 if (p !=3D kp) kp->flags &=3D ~KPROBE_FLAG_DISABLED; @@ -2441,8 +2427,6 @@ int enable_kprobe(struct kprobe *kp) kp->flags |=3D KPROBE_FLAG_DISABLED; } } -out: - mutex_unlock(&kprobe_mutex); return ret; } EXPORT_SYMBOL_GPL(enable_kprobe); @@ -2630,11 +2614,11 @@ static int kprobes_module_callback(struct notifier_= block *nb, unsigned int i; int checkcore =3D (val =3D=3D MODULE_STATE_GOING); =20 - if (val =3D=3D MODULE_STATE_COMING) { - mutex_lock(&kprobe_mutex); + guard(mutex)(&kprobe_mutex); + + if (val =3D=3D MODULE_STATE_COMING) add_module_kprobe_blacklist(mod); - mutex_unlock(&kprobe_mutex); - } + if (val !=3D MODULE_STATE_GOING && val !=3D MODULE_STATE_LIVE) return NOTIFY_DONE; =20 @@ -2644,7 +2628,6 @@ static int kprobes_module_callback(struct notifier_bl= ock *nb, * notified, only '.init.text' section would be freed. We need to * disable kprobes which have been inserted in the sections. */ - mutex_lock(&kprobe_mutex); for (i =3D 0; i < KPROBE_TABLE_SIZE; i++) { head =3D &kprobe_table[i]; hlist_for_each_entry(p, head, hlist) @@ -2667,7 +2650,6 @@ static int kprobes_module_callback(struct notifier_bl= ock *nb, } if (val =3D=3D MODULE_STATE_GOING) remove_module_kprobe_blacklist(mod); - mutex_unlock(&kprobe_mutex); return NOTIFY_DONE; } =20 @@ -2695,7 +2677,7 @@ void kprobe_free_init_mem(void) struct kprobe *p; int i; =20 - mutex_lock(&kprobe_mutex); + guard(mutex)(&kprobe_mutex); =20 /* Kill all kprobes on initmem because the target code has been freed. */ for (i =3D 0; i < KPROBE_TABLE_SIZE; i++) { @@ -2705,8 +2687,6 @@ void kprobe_free_init_mem(void) kill_kprobe(p); } } - - mutex_unlock(&kprobe_mutex); } =20 static int __init init_kprobes(void) @@ -2902,11 +2882,11 @@ static int arm_all_kprobes(void) unsigned int i, total =3D 0, errors =3D 0; int err, ret =3D 0; =20 - mutex_lock(&kprobe_mutex); + guard(mutex)(&kprobe_mutex); =20 /* If kprobes are armed, just return */ if (!kprobes_all_disarmed) - goto already_enabled; + return 0; =20 /* * optimize_kprobe() called by arm_kprobe() checks @@ -2936,8 +2916,6 @@ static int arm_all_kprobes(void) else pr_info("Kprobes globally enabled\n"); =20 -already_enabled: - mutex_unlock(&kprobe_mutex); return ret; } =20 @@ -2948,13 +2926,11 @@ static int disarm_all_kprobes(void) unsigned int i, total =3D 0, errors =3D 0; int err, ret =3D 0; =20 - mutex_lock(&kprobe_mutex); + guard(mutex)(&kprobe_mutex); =20 /* If kprobes are already disarmed, just return */ - if (kprobes_all_disarmed) { - mutex_unlock(&kprobe_mutex); + if (kprobes_all_disarmed) return 0; - } =20 kprobes_all_disarmed =3D true; =20 @@ -2979,11 +2955,8 @@ static int disarm_all_kprobes(void) else pr_info("Kprobes globally disabled\n"); =20 - mutex_unlock(&kprobe_mutex); - /* Wait for disarming all kprobes by optimizer */ - wait_for_kprobe_optimizer(); - + wait_for_kprobe_optimizer_locked(); return ret; }