mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [BUG] Incorrect ordering of klp and ftrace module notifier callbacks
@ 2016-01-28 20:40 Jessica Yu
  2016-01-28 20:59 ` Jiri Kosina
  0 siblings, 1 reply; 3+ messages in thread
From: Jessica Yu @ 2016-01-28 20:40 UTC (permalink / raw)
  To: Josh Poimboeuf, Seth Jennings, Jiri Kosina, Vojtech Pavlik,
	Miroslav Benes, Steven Rostedt
  Cc: live-patching, linux-kernel

Hi all,

I recently found that livepatch is broken for patches to modules on the latest
for-next branch [1]. In its current state, livepatch is able to patch a module
if the module is *already* loaded, but will fail if the target module loads
after the patch module loads. The patched, new functions are not getting
called, and the old functions are running instead.

I ran a git bisect with the last good commit as -
[b56b36e] livepatch: Cleanup module page permission changes
and the first bad commit identified was -
[5156dca] ftrace: Fix the race between ftrace and insmod

In short, that commit moves the call to ftrace_module_enable() out of
ftrace_module_init() to the ftrace_module_notify() callback, and is called when
the module state is COMING. The problem is, livepatch also has a module
notifier callback that enables a patch for COMING modules, but makes the
assumption that ftrace_module_enable() has already been called. So ordering is
important for livepatch to get this right, but with the current notifier
priorities, we get the ordering wrong.

So we currently get the following callback ordering when a target module loads:
1) klp_module_notify() [priority: INT_MIN+1, so this runs first] -
Sees target module is COMING, tries to enable the corresponding patch
(but ftrace_module_enable() hasn't been called yet)
2) ftrace_module_notify() [priority: INT_MIN, runs last] -
Sees module is COMING, calls ftrace_module_enable(), but it's too late for
livepatch

And the following ordering when the target module unloads:
1) klp_module_notify() [priority: INT_MIN+1] -
Sees target module GOING, disables patch
2) ftrace_module_notify [priority: INT_MIN] -
Sees module GOING, calls ftrace_release_mod()

The unload ordering is correct, but for the load case the calls need to be
flipped around (ftrace first).

So it doesn't look like a simple priority tweak will work, since we want a
ftrace->klp->klp->ftrace ordering from module load->module unload. Perhaps we
can split the ftrace and klp notifiers into separate coming and going handlers
with the appropriate priorities set to enforce correct ordering (not terribly
pretty, but it works). Is there a better solution?

[1] git://git.kernel.org/pub/scm/linux/kernel/git/jikos/livepatching.git for-next

Thanks,
Jessica

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [BUG] Incorrect ordering of klp and ftrace module notifier callbacks
  2016-01-28 20:40 [BUG] Incorrect ordering of klp and ftrace module notifier callbacks Jessica Yu
@ 2016-01-28 20:59 ` Jiri Kosina
  2016-01-28 21:06   ` Jiri Kosina
  0 siblings, 1 reply; 3+ messages in thread
From: Jiri Kosina @ 2016-01-28 20:59 UTC (permalink / raw)
  To: Jessica Yu
  Cc: Josh Poimboeuf, Seth Jennings, Vojtech Pavlik, Miroslav Benes,
	Steven Rostedt, live-patching, linux-kernel

On Thu, 28 Jan 2016, Jessica Yu wrote:

> So it doesn't look like a simple priority tweak will work, since we want a
> ftrace->klp->klp->ftrace ordering from module load->module unload. Perhaps we
> can split the ftrace and klp notifiers into separate coming and going handlers
> with the appropriate priorities set to enforce correct ordering (not terribly
> pretty, but it works). Is there a better solution?

Given the facts that:

- we want the ordering ftrace -> klp for load and klp -> ftrace for 
  unload

- we already do have separate klp_module_notify_coming() and 
  klp_module_notify_going(), and klp_module_notify() is just a stub that 
  decides which one to call

I'd suggest we keep just a single ftrace_module_notify() (with INT_MIN+1, 
priority), and then introduce separate notifiers

- klp_module_notify_going() with INT_MIN+2, that triggers action iff 
  action == MODULE_STATE_GOING

- klp_module_notify_going() with INT_MIN, that triggers action only iff 
  action == MODULE_STATE_COMING

Thanks,

-- 
Jiri Kosina
SUSE Labs

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [BUG] Incorrect ordering of klp and ftrace module notifier callbacks
  2016-01-28 20:59 ` Jiri Kosina
@ 2016-01-28 21:06   ` Jiri Kosina
  0 siblings, 0 replies; 3+ messages in thread
From: Jiri Kosina @ 2016-01-28 21:06 UTC (permalink / raw)
  To: Jessica Yu
  Cc: Josh Poimboeuf, Seth Jennings, Vojtech Pavlik, Miroslav Benes,
	Steven Rostedt, live-patching, linux-kernel

On Thu, 28 Jan 2016, Jiri Kosina wrote:

> - klp_module_notify_going() with INT_MIN+2, that triggers action iff 
>   action == MODULE_STATE_GOING
> 
> - klp_module_notify_going() with INT_MIN, that triggers action only iff 
>   action == MODULE_STATE_COMING

I *of course* created the mismatch between the notifier name and the 
action value above completely deliberately to excercise the attention of 
the readers :p

Sorry for the noise,

-- 
Jiri Kosina
SUSE Labs

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-01-28 21:06 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-28 20:40 [BUG] Incorrect ordering of klp and ftrace module notifier callbacks Jessica Yu
2016-01-28 20:59 ` Jiri Kosina
2016-01-28 21:06   ` Jiri Kosina

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®