From: Masami Hiramatsu <mhiramat@kernel.org>
To: JianKang Chen <chenjiankang1@huawei.com>
Cc: <ananth@linux.vnet.ibm.com>, <anil.s.keshavamurthy@intel.com>,
<davem@davemloft.net>, <linux-kernel@vger.kernel.org>,
<wangkefeng.wang@huawei.com>, <xieyisheng1@huawei.com>
Subject: Re: [PATCH v3] kernel/kprobes: add re-register safe check for register_kretprobe()
Date: Fri, 10 Nov 2017 20:10:32 +0900 [thread overview]
Message-ID: <20171110201032.cb690d81004c762d226bc0c8@kernel.org> (raw)
In-Reply-To: <1510306113-52337-1-git-send-email-chenjiankang1@huawei.com>
On Fri, 10 Nov 2017 17:28:33 +0800
JianKang Chen <chenjiankang1@huawei.com> wrote:
> From: Chen Jiankang <chenjiankang1@huawei.com>
>
> When there are two same struct kretprobe rp, the INIT_HLIST_HEAD()
> will result in a empty list table rp->free_instances. The memory leak
> will happen. So it needs to add re-register safe check by
> __get_valid_kprobe().
Under rcu read lock :) Please see below comment.
>
> However, current this is not safe for multi-threadings, because
> there is still a chance to re-register kretprobe concurrently.
> So I add a kretprobe_mutex lock to protect the INIT_LIST_HEAD;
>
> Signed-off-by: Chen Jiankang <chenjiankang1@huawei.com>
> ---
> kernel/kprobes.c | 12 +++++++++++-
> 1 file changed, 11 insertions(+), 1 deletion(-)
>
> diff --git a/kernel/kprobes.c b/kernel/kprobes.c
> index a1606a4..5ff8f69 100644
> --- a/kernel/kprobes.c
> +++ b/kernel/kprobes.c
> @@ -67,6 +67,7 @@
>
> /* This protects kprobe_table and optimizing_list */
> static DEFINE_MUTEX(kprobe_mutex);
> +static DEFINE_MUTEX(kretprobe_mutex);
> static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
> static struct {
> raw_spinlock_t lock ____cacheline_aligned_in_smp;
> @@ -1947,6 +1948,12 @@ int register_kretprobe(struct kretprobe *rp)
> rp->maxactive = num_possible_cpus();
> #endif
> }
> +
> + mutex_lock(&kretprobe_mutex);
> + if (__get_valid_kprobe(&rp->kp)) {
__get_valid_kprobe() must be called under rcu_read_lock or preempt disabled,
because it tracks down the rcu list.
Thank you.
> + ret = -EINVAL;
> + goto out;
> + }
> raw_spin_lock_init(&rp->lock);
> INIT_HLIST_HEAD(&rp->free_instances);
> for (i = 0; i < rp->maxactive; i++) {
> @@ -1954,7 +1961,8 @@ int register_kretprobe(struct kretprobe *rp)
> rp->data_size, GFP_KERNEL);
> if (inst == NULL) {
> free_rp_inst(rp);
> - return -ENOMEM;
> + ret = -ENOMEM;
> + goto out;
> }
> INIT_HLIST_NODE(&inst->hlist);
> hlist_add_head(&inst->hlist, &rp->free_instances);
> @@ -1965,6 +1973,8 @@ int register_kretprobe(struct kretprobe *rp)
> ret = register_kprobe(&rp->kp);
> if (ret != 0)
> free_rp_inst(rp);
> +out:
> + mutex_unlock(&kretprobe_mutex);
> return ret;
> }
> EXPORT_SYMBOL_GPL(register_kretprobe);
> --
> 1.7.12.4
>
--
Masami Hiramatsu <mhiramat@kernel.org>
prev parent reply other threads:[~2017-11-10 11:10 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-11-10 9:28 JianKang Chen
2017-11-10 11:10 ` Masami Hiramatsu [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20171110201032.cb690d81004c762d226bc0c8@kernel.org \
--to=mhiramat@kernel.org \
--cc=ananth@linux.vnet.ibm.com \
--cc=anil.s.keshavamurthy@intel.com \
--cc=chenjiankang1@huawei.com \
--cc=davem@davemloft.net \
--cc=linux-kernel@vger.kernel.org \
--cc=wangkefeng.wang@huawei.com \
--cc=xieyisheng1@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome