From: Masami Hiramatsu <mhiramat@kernel.org>
To: Josh Stone <jistone@redhat.com>
Cc: Ingo Molnar <mingo@kernel.org>,
Steven Rostedt <rostedt@goodmis.org>,
Ingo Molnar <mingo@redhat.com>,
Alban Crequy <alban.crequy@gmail.com>,
Alban Crequy <alban@kinvolk.io>,
Alexei Starovoitov <ast@kernel.org>,
Jonathan Corbet <corbet@lwn.net>,
Arnaldo Carvalho de Melo <acme@redhat.com>,
Omar Sandoval <osandov@fb.com>,
linux-doc@vger.kernel.org, netdev@vger.kernel.org,
linux-kernel@vger.kernel.org, iago@kinvolk.io,
michael@kinvolk.io, Dorau Lukasz <lukasz.dorau@intel.com>,
systemtap@sourceware.org
Subject: Re: [RFC PATCH tip/master 2/3] kprobes: Allocate kretprobe instance if its free list is empty
Date: Thu, 30 Mar 2017 00:39:00 -0000 [thread overview]
Message-ID: <20170330093908.70106bda02c5f5915b0375ba@kernel.org> (raw)
In-Reply-To: <943c03db-0bfb-d03f-5ebb-fc7e6a5b5519@redhat.com>
On Wed, 29 Mar 2017 10:18:48 -0700
Josh Stone <jistone@redhat.com> wrote:
> On 03/29/2017 01:25 AM, Masami Hiramatsu wrote:
> > On Wed, 29 Mar 2017 08:30:05 +0200
> > Ingo Molnar <mingo@kernel.org> wrote:
> >>
> >> * Masami Hiramatsu <mhiramat@kernel.org> wrote:
> >>
> >>> @@ -1824,6 +1823,30 @@ void unregister_jprobes(struct jprobe **jps, int num)
> >>> EXPORT_SYMBOL_GPL(unregister_jprobes);
> >>>
> >>> #ifdef CONFIG_KRETPROBES
> >>> +
> >>> +/* Try to use free instance first, if failed, try to allocate new instance */
> >>> +struct kretprobe_instance *kretprobe_alloc_instance(struct kretprobe *rp)
> >>> +{
> >>> + struct kretprobe_instance *ri = NULL;
> >>> + unsigned long flags = 0;
> >>> +
> >>> + raw_spin_lock_irqsave(&rp->lock, flags);
> >>> + if (!hlist_empty(&rp->free_instances)) {
> >>> + ri = hlist_entry(rp->free_instances.first,
> >>> + struct kretprobe_instance, hlist);
> >>> + hlist_del(&ri->hlist);
> >>> + }
> >>> + raw_spin_unlock_irqrestore(&rp->lock, flags);
> >>> +
> >>> + /* Populate max active instance if possible */
> >>> + if (!ri && rp->maxactive < KRETPROBE_MAXACTIVE_ALLOC) {
> >>> + ri = kmalloc(sizeof(*ri) + rp->data_size, GFP_ATOMIC);
> >>> + if (ri)
> >>> + rp->maxactive++;
> >>> + }
> >>> +
> >>> + return ri;
> >>> +}
> >>> /*
> >>> * This kprobe pre_handler is registered with every kretprobe. When probe
> >>> * hits it will set up the return probe.
> >>> @@ -1846,14 +1869,8 @@ static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
> >>> }
> >>>
> >>> /* TODO: consider to only swap the RA after the last pre_handler fired */
> >>> - hash = hash_ptr(current, KPROBE_HASH_BITS);
> >>> - raw_spin_lock_irqsave(&rp->lock, flags);
> >>> - if (!hlist_empty(&rp->free_instances)) {
> >>> - ri = hlist_entry(rp->free_instances.first,
> >>> - struct kretprobe_instance, hlist);
> >>> - hlist_del(&ri->hlist);
> >>> - raw_spin_unlock_irqrestore(&rp->lock, flags);
> >>> -
> >>> + ri = kretprobe_alloc_instance(rp);
> >>> + if (ri) {
> >>> ri->rp = rp;
> >>> ri->task = current;
> >>>
> >>> @@ -1868,13 +1885,13 @@ static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
> >>>
> >>> /* XXX(hch): why is there no hlist_move_head? */
> >>> INIT_HLIST_NODE(&ri->hlist);
> >>> + hash = hash_ptr(current, KPROBE_HASH_BITS);
> >>> kretprobe_table_lock(hash, &flags);
> >>> hlist_add_head(&ri->hlist, &kretprobe_inst_table[hash]);
> >>> kretprobe_table_unlock(hash, &flags);
> >>> - } else {
> >>> + } else
> >>> rp->nmissed++;
> >>> - raw_spin_unlock_irqrestore(&rp->lock, flags);
> >>> - }
> >>> +
> >>> return 0;
> >>> }
> >>> NOKPROBE_SYMBOL(pre_handler_kretprobe);
> >>
> >> So this is something I missed while the original code was merged, but the concept
> >> looks a bit weird: why do we do any "allocation" while a handler is executing?
> >>
> >> That's fundamentally fragile. What's the maximum number of parallel
> >> 'kretprobe_instance' required per kretprobe - one per CPU?
> >
> > It depends on the place where we put the probe. If the probed function will be
> > blocked (yield to other tasks), then we need a same number of threads on
> > the system which can invoke the function. So, ultimately, it is same
> > as function_graph tracer, we need it for each thread.
>
> Isn't it also possible that the function may be reentrant? Whether by
> plain recursion or an interrupt call, this leads to multiple live
> instances even for a given thread.
Yes, that's another possible case, but I don't think that's so serious in kernel
because we have very limited kernel stack, which means the recursion may not
so deep.
Thank you,
--
Masami Hiramatsu <mhiramat@kernel.org>
next prev parent reply other threads:[~2017-03-30 0:39 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-03-29 5:21 [RFC PATCH tip/master 0/3] kprobes: tracing: kretprobe_instance dynamic allocation Masami Hiramatsu
2017-03-29 5:23 ` [RFC PATCH tip/master 1/3] trace: kprobes: Show sum of probe/retprobe nmissed count Masami Hiramatsu
2017-03-31 9:45 ` Alban Crequy
2017-03-29 5:24 ` [RFC PATCH tip/master 2/3] kprobes: Allocate kretprobe instance if its free list is empty Masami Hiramatsu
2017-03-29 6:30 ` Ingo Molnar
2017-03-29 8:25 ` Masami Hiramatsu
2017-03-29 17:18 ` Josh Stone
2017-03-30 0:39 ` Masami Hiramatsu [this message]
2017-03-30 6:53 ` Ingo Molnar
2017-03-30 13:01 ` Masami Hiramatsu
2017-04-12 6:42 ` Ingo Molnar
2017-03-30 13:03 ` Alban Crequy
2017-03-29 5:25 ` [RFC PATCH tip/master 3/3] kprobes: Limit kretprobe maximum instances Masami Hiramatsu
2017-03-29 13:27 ` [RFC PATCH tip/master 0/3] kprobes: tracing: kretprobe_instance dynamic allocation Frank Ch. Eigler
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=20170330093908.70106bda02c5f5915b0375ba@kernel.org \
--to=mhiramat@kernel.org \
--cc=acme@redhat.com \
--cc=alban.crequy@gmail.com \
--cc=alban@kinvolk.io \
--cc=ast@kernel.org \
--cc=corbet@lwn.net \
--cc=iago@kinvolk.io \
--cc=jistone@redhat.com \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lukasz.dorau@intel.com \
--cc=michael@kinvolk.io \
--cc=mingo@kernel.org \
--cc=mingo@redhat.com \
--cc=netdev@vger.kernel.org \
--cc=osandov@fb.com \
--cc=rostedt@goodmis.org \
--cc=systemtap@sourceware.org \
/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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).