From: Masami Hiramatsu <mhiramat@kernel.org>
To: Jessica Yu <jeyu@kernel.org>
Cc: Steven Rostedt <rostedt@goodmis.org>,
Masami Hiramatsu <mhiramat@kernel.org>,
Ananth N Mavinakayanahalli <ananth@linux.vnet.ibm.com>,
Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>,
"David S . Miller" <davem@davemloft.net>,
Ingo Molnar <mingo@kernel.org>, Petr Mladek <pmladek@suse.com>,
Josh Poimboeuf <jpoimboe@redhat.com>,
Joe Lawrence <joe.lawrence@redhat.com>,
Jiri Kosina <jikos@kernel.org>, Miroslav Benes <mbenes@suse.cz>,
live-patching@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: kprobes: propagate error from arm_kprobe_ftrace()
Date: Thu, 9 Nov 2017 09:35:50 +0900 [thread overview]
Message-ID: <20171109093550.30f9bc1f00d84db0f453e2dc@kernel.org> (raw)
In-Reply-To: <20171107171455.iegqpye6wpvhztd5@redbean>
On Tue, 7 Nov 2017 18:14:56 +0100
Jessica Yu <jeyu@kernel.org> wrote:
> +++ Steven Rostedt [03/11/17 10:03 -0400]:
> >On Thu, 2 Nov 2017 17:33:33 +0100
> >Jessica Yu <jeyu@kernel.org> wrote:
> >
> >> Improve error handling when arming ftrace-based kprobes. Specifically, if
> >> we fail to arm a ftrace-based kprobe, register_kprobe()/enable_kprobe()
> >> should report an error instead of success. Previously, this has lead to
> >> confusing situations where register_kprobe() would return 0 indicating
> >> success, but the kprobe would not be functional if ftrace registration
> >> during the kprobe arming process had failed. We should therefore take any
> >> errors returned by ftrace into account and propagate this error so that we
> >> do not register/enable kprobes that cannot be armed. This can happen if,
> >> for example, register_ftrace_function() finds an IPMODIFY conflict (since
> >> kprobe_ftrace_ops has this flag set) and returns an error. Such a conflict
> >> is possible since livepatches also set the IPMODIFY flag for their ftrace_ops.
> >>
> >> arm_all_kprobes() keeps its current behavior and attempts to arm all
> >> kprobes. It returns the last encountered error and gives a warning if
> >> not all kprobes could be armed.
> >>
> >> This patch is based on Petr Mladek's original patchset (patches 2 and 3)
> >> back in 2015, which improved kprobes error handling, found here:
> >>
> >> https://lkml.org/lkml/2015/2/26/452
> >>
> >> However, further work on this had been paused since then and the patches
> >> were not upstreamed.
> >>
> >> Based-on-patches-by: Petr Mladek <pmladek@suse.com>
> >> Signed-off-by: Jessica Yu <jeyu@kernel.org>
> >> ---
> >> kernel/kprobes.c | 88 ++++++++++++++++++++++++++++++++++++++++----------------
> >> 1 file changed, 63 insertions(+), 25 deletions(-)
> >>
> >> diff --git a/kernel/kprobes.c b/kernel/kprobes.c
> >> index da2ccf142358..f4a094007cb5 100644
> >> --- a/kernel/kprobes.c
> >> +++ b/kernel/kprobes.c
> >> @@ -978,18 +978,27 @@ static int prepare_kprobe(struct kprobe *p)
> >> }
> >>
> >> /* Caller must lock kprobe_mutex */
> >> -static void arm_kprobe_ftrace(struct kprobe *p)
> >> +static int arm_kprobe_ftrace(struct kprobe *p)
> >> {
> >> - int ret;
> >> + int ret = 0;
> >>
> >> ret = ftrace_set_filter_ip(&kprobe_ftrace_ops,
> >> (unsigned long)p->addr, 0, 0);
> >> - WARN(ret < 0, "Failed to arm kprobe-ftrace at %p (%d)\n", p->addr, ret);
> >> - kprobe_ftrace_enabled++;
> >> - if (kprobe_ftrace_enabled == 1) {
> >> + if (WARN(ret < 0, "Failed to arm kprobe-ftrace at %p (%d)\n", p->addr, ret))
> >> + return ret;
> >> +
> >> + if (kprobe_ftrace_enabled == 0) {
> >> ret = register_ftrace_function(&kprobe_ftrace_ops);
> >> - WARN(ret < 0, "Failed to init kprobe-ftrace (%d)\n", ret);
> >> + if (WARN(ret < 0, "Failed to init kprobe-ftrace (%d)\n", ret))
> >> + goto err_ftrace;
> >> }
> >> +
> >> + kprobe_ftrace_enabled++;
> >> + return ret;
> >> +
> >> +err_ftrace:
> >> + ftrace_set_filter_ip(&kprobe_ftrace_ops, (unsigned long)p->addr, 1, 0);
> >
> >Hmm, this could have a very nasty side effect. If you remove a function
> >from the ops, and it was the last function, an empty ops means to trace
> >*all* functions.
>
> Good point, and yes, normally this would be the (undesirable) outcome.
>
> However in this case, kprobes_ftrace_ops has the IPMODIFY flag set, so
> ftrace_set_filter_ip() will not allow the removal of a function if it
> is the very last function, and it will return an error in
> __ftrace_hash_update_ipmodify(). The comment there explains, if
> IPMODIFY is set, "return -EINVAL if the new_hash tries to trace all
> recs". So I think we are safe here...
>
> >Perhaps you want to add it to the "notrace" list. Which would require
> >implementing a ftrace_set_notrace_ip() function. Which I believe is
> >what you want. Any function in the notrace hash will have the same
> >functions in the filter hash be ignored.
>
> I think this would've been a good alternative if we wanted to protect
> against the empty ops case, but IPMODIFY is also incompatible with notrace..
> (See: commit f8b8be8a310 and this comment here:
> https://www.mail-archive.com/linux-kernel@vger.kernel.org/msg688632.html)
>
> Speaking of IPMODIFY, a question for Masami - is this flag still
> relevant/needed for kprobes, since jprobes has been deprecated
> recently? IIRC, IPMODIFY was needed in the first place because jprobes
> and livepatch were in direct conflict, but I recall some work being
> done in the past to remove the IPMODIFY flag from kprobes, but I don't
> think this was ever upstreamed. (See: https://patchwork.kernel.org/patch/5352481/)
Hmm, good point. I just want to make kprobes transparently using ftrace.
This means if someone writes a kernel module which uses kprobes to
change IP address, which should work with/without CONFIG_FTRACE enabled.
kprobes itself supports to modify regs->ip (under some special settings,
see Documentation/kprobes.txt:Note for geeks), so we can not remove it.
Thank you,
--
Masami Hiramatsu <mhiramat@kernel.org>
next prev parent reply other threads:[~2017-11-09 0:35 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-11-02 16:33 [PATCH v2 0/2] kprobes: improve error handling when arming/disarming kprobes Jessica Yu
2017-11-02 16:33 ` [PATCH v2 1/2] kprobes: propagate error from arm_kprobe_ftrace() Jessica Yu
2017-11-03 14:03 ` Steven Rostedt
2017-11-03 14:53 ` Josh Poimboeuf
2017-11-03 17:33 ` Steven Rostedt
2017-11-03 20:35 ` Masami Hiramatsu
2017-11-07 17:14 ` Jessica Yu
2017-11-09 0:35 ` Masami Hiramatsu [this message]
2017-11-21 14:47 ` Jessica Yu
2017-11-03 21:49 ` [PATCH v2 1/2] " Masami Hiramatsu
2017-11-02 16:33 ` [PATCH v2 2/2] kprobes: propagate error from disarm_kprobe_ftrace() Jessica Yu
-- strict thread matches above, loose matches on Subject: below --
2018-01-03 1:40 [PATCH v3 0/2] kprobes: improve error handling when arming/disarming kprobes Jessica Yu
2018-01-03 1:40 ` [PATCH v3 1/2] kprobes: propagate error from arm_kprobe_ftrace() Jessica Yu
2018-01-03 14:33 ` Steven Rostedt
2018-01-03 21:00 ` Jessica Yu
2018-01-04 14:42 ` Masami Hiramatsu
2017-10-04 19:14 [PATCH 0/2] kprobes: improve error handling when arming/disarming kprobes Jessica Yu
2017-10-04 19:14 ` [PATCH 1/2] kprobes: propagate error from arm_kprobe_ftrace() Jessica Yu
2017-10-05 6:23 ` Masami Hiramatsu
2017-10-07 10:52 ` Jessica Yu
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=20171109093550.30f9bc1f00d84db0f453e2dc@kernel.org \
--to=mhiramat@kernel.org \
--cc=ananth@linux.vnet.ibm.com \
--cc=anil.s.keshavamurthy@intel.com \
--cc=davem@davemloft.net \
--cc=jeyu@kernel.org \
--cc=jikos@kernel.org \
--cc=joe.lawrence@redhat.com \
--cc=jpoimboe@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=live-patching@vger.kernel.org \
--cc=mbenes@suse.cz \
--cc=mingo@kernel.org \
--cc=pmladek@suse.com \
--cc=rostedt@goodmis.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 NNTP newsgroup(s).