Skip to content

Commit da93dd9

Browse files
ruanjinjie-engmhiramat
authored andcommitted
kprobes: Cleanup collect_one_slot() and __disable_kprobe()
If kip->nused is not zero, collect_one_slot() return false, otherwise do a lot of linked list operations, reverse the processing order to make the code if nesting more concise. __disable_kprobe() is the same as well. Link: https://lore.kernel.org/all/20240813115334.3922580-4-ruanjinjie@huawei.com/ Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com> Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
1 parent ce7f27d commit da93dd9

1 file changed

Lines changed: 40 additions & 39 deletions

File tree

kernel/kprobes.c

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -206,29 +206,29 @@ static bool collect_one_slot(struct kprobe_insn_page *kip, int idx)
206206
{
207207
kip->slot_used[idx] = SLOT_CLEAN;
208208
kip->nused--;
209-
if (kip->nused == 0) {
209+
if (kip->nused != 0)
210+
return false;
211+
212+
/*
213+
* Page is no longer in use. Free it unless
214+
* it's the last one. We keep the last one
215+
* so as not to have to set it up again the
216+
* next time somebody inserts a probe.
217+
*/
218+
if (!list_is_singular(&kip->list)) {
210219
/*
211-
* Page is no longer in use. Free it unless
212-
* it's the last one. We keep the last one
213-
* so as not to have to set it up again the
214-
* next time somebody inserts a probe.
220+
* Record perf ksymbol unregister event before removing
221+
* the page.
215222
*/
216-
if (!list_is_singular(&kip->list)) {
217-
/*
218-
* Record perf ksymbol unregister event before removing
219-
* the page.
220-
*/
221-
perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL,
222-
(unsigned long)kip->insns, PAGE_SIZE, true,
223-
kip->cache->sym);
224-
list_del_rcu(&kip->list);
225-
synchronize_rcu();
226-
kip->cache->free(kip->insns);
227-
kfree(kip);
228-
}
229-
return true;
223+
perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL,
224+
(unsigned long)kip->insns, PAGE_SIZE, true,
225+
kip->cache->sym);
226+
list_del_rcu(&kip->list);
227+
synchronize_rcu();
228+
kip->cache->free(kip->insns);
229+
kfree(kip);
230230
}
231-
return false;
231+
return true;
232232
}
233233

234234
static int collect_garbage_slots(struct kprobe_insn_cache *c)
@@ -1725,28 +1725,29 @@ static struct kprobe *__disable_kprobe(struct kprobe *p)
17251725
if (unlikely(orig_p == NULL))
17261726
return ERR_PTR(-EINVAL);
17271727

1728-
if (!kprobe_disabled(p)) {
1729-
/* Disable probe if it is a child probe */
1730-
if (p != orig_p)
1731-
p->flags |= KPROBE_FLAG_DISABLED;
1728+
if (kprobe_disabled(p))
1729+
return orig_p;
17321730

1733-
/* Try to disarm and disable this/parent probe */
1734-
if (p == orig_p || aggr_kprobe_disabled(orig_p)) {
1735-
/*
1736-
* Don't be lazy here. Even if 'kprobes_all_disarmed'
1737-
* is false, 'orig_p' might not have been armed yet.
1738-
* Note arm_all_kprobes() __tries__ to arm all kprobes
1739-
* on the best effort basis.
1740-
*/
1741-
if (!kprobes_all_disarmed && !kprobe_disabled(orig_p)) {
1742-
ret = disarm_kprobe(orig_p, true);
1743-
if (ret) {
1744-
p->flags &= ~KPROBE_FLAG_DISABLED;
1745-
return ERR_PTR(ret);
1746-
}
1731+
/* Disable probe if it is a child probe */
1732+
if (p != orig_p)
1733+
p->flags |= KPROBE_FLAG_DISABLED;
1734+
1735+
/* Try to disarm and disable this/parent probe */
1736+
if (p == orig_p || aggr_kprobe_disabled(orig_p)) {
1737+
/*
1738+
* Don't be lazy here. Even if 'kprobes_all_disarmed'
1739+
* is false, 'orig_p' might not have been armed yet.
1740+
* Note arm_all_kprobes() __tries__ to arm all kprobes
1741+
* on the best effort basis.
1742+
*/
1743+
if (!kprobes_all_disarmed && !kprobe_disabled(orig_p)) {
1744+
ret = disarm_kprobe(orig_p, true);
1745+
if (ret) {
1746+
p->flags &= ~KPROBE_FLAG_DISABLED;
1747+
return ERR_PTR(ret);
17471748
}
1748-
orig_p->flags |= KPROBE_FLAG_DISABLED;
17491749
}
1750+
orig_p->flags |= KPROBE_FLAG_DISABLED;
17501751
}
17511752

17521753
return orig_p;

0 commit comments

Comments
 (0)