Live-Patching Archive on lore.kernel.org
 help / color / Atom feed
* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
       [not found]                   ` <20191015135634.GK2328@hirez.programming.kicks-ass.net>
@ 2019-10-15 14:13                     ` Miroslav Benes
  2019-10-15 15:06                       ` Joe Lawrence
  0 siblings, 1 reply; 45+ messages in thread
From: Miroslav Benes @ 2019-10-15 14:13 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Jessica Yu, Steven Rostedt, x86, linux-kernel, mhiramat, bristot,
	jbaron, torvalds, tglx, mingo, namit, hpa, luto, ard.biesheuvel,
	jpoimboe, live-patching

[ live-patching ML CCed ]

On Tue, 15 Oct 2019, Peter Zijlstra wrote:

> On Tue, Oct 15, 2019 at 03:07:40PM +0200, Jessica Yu wrote:
> 
> > > The fact that it is executable; also the fact that you do it right after
> > > we mark it ro. Flipping the memory protections back and forth is just
> > > really poor everything.
> > > 
> > > Once this ftrace thing is sorted, we'll change x86 to _refuse_ to make
> > > executable (kernel) memory writable.
> > 
> > Not sure if relevant, but just thought I'd clarify: IIRC,
> > klp_module_coming() is not poking the coming module, but it calls
> > module_enable_ro() on itself (the livepatch module) so it can apply
> > relocations and such on the new code, which lives inside the livepatch
> > module, and it needs to possibly do this numerous times over the
> > lifetime of the patch module for any coming module it is responsible
> > for patching (i.e., call module_enable_ro() on the patch module, not
> > necessarily the coming module). So I am not be sure why
> > klp_module_coming() should be moved before complete_formation(). I
> > hope I'm remembering the details correctly, livepatch folks feel free
> > to chime in if I'm incorrect here.
> 
> You mean it does module_disable_ro() ? That would be broken and it needs
> to be fixed. Can some livepatch person explain what it does and why?

Yes, it does. klp_module_coming() calls module_disable_ro() on all 
patching modules which patch the coming module in order to call 
apply_relocate_add(). New (patching) code for a module can be relocated 
only when the relevant module is loaded.

Miroslav

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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2019-10-15 14:13                     ` [PATCH v3 5/6] x86/ftrace: Use text_poke() Miroslav Benes
@ 2019-10-15 15:06                       ` Joe Lawrence
  2019-10-15 15:31                         ` Jessica Yu
  2019-10-16  6:51                         ` Miroslav Benes
  0 siblings, 2 replies; 45+ messages in thread
From: Joe Lawrence @ 2019-10-15 15:06 UTC (permalink / raw)
  To: Miroslav Benes, Peter Zijlstra
  Cc: Jessica Yu, Steven Rostedt, x86, linux-kernel, mhiramat, bristot,
	jbaron, torvalds, tglx, mingo, namit, hpa, luto, ard.biesheuvel,
	jpoimboe, live-patching

On 10/15/19 10:13 AM, Miroslav Benes wrote:
> Yes, it does. klp_module_coming() calls module_disable_ro() on all
> patching modules which patch the coming module in order to call
> apply_relocate_add(). New (patching) code for a module can be relocated
> only when the relevant module is loaded.

FWIW, would the LPC blue-sky2 model (ie, Steve's suggestion @ plumber's 
where livepatches only patch a single object and updates are kept on 
disk to handle coming module updates as they are loaded) eliminate those 
outstanding relocations and the need to perform this late permission 
flipping?

-- Joe

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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2019-10-15 15:06                       ` Joe Lawrence
@ 2019-10-15 15:31                         ` Jessica Yu
  2019-10-15 22:17                           ` Joe Lawrence
  2019-10-16  6:51                         ` Miroslav Benes
  1 sibling, 1 reply; 45+ messages in thread
From: Jessica Yu @ 2019-10-15 15:31 UTC (permalink / raw)
  To: Joe Lawrence
  Cc: Miroslav Benes, Peter Zijlstra, Steven Rostedt, x86,
	linux-kernel, mhiramat, bristot, jbaron, torvalds, tglx, mingo,
	namit, hpa, luto, ard.biesheuvel, jpoimboe, live-patching

+++ Joe Lawrence [15/10/19 11:06 -0400]:
>On 10/15/19 10:13 AM, Miroslav Benes wrote:
>>Yes, it does. klp_module_coming() calls module_disable_ro() on all
>>patching modules which patch the coming module in order to call
>>apply_relocate_add(). New (patching) code for a module can be relocated
>>only when the relevant module is loaded.
>
>FWIW, would the LPC blue-sky2 model (ie, Steve's suggestion @ 
>plumber's where livepatches only patch a single object and updates are 
>kept on disk to handle coming module updates as they are loaded) 
>eliminate those outstanding relocations and the need to perform this 
>late permission flipping?

I wasn't at Plumbers sadly, was this idea documented/talked about in
detail somewhere? (recording, slides, etherpad, etc?). What do you
mean by updates are kept on disk? Maybe someone can explain it more
in detail? :)


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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2019-10-15 15:31                         ` Jessica Yu
@ 2019-10-15 22:17                           ` Joe Lawrence
  2019-10-15 22:27                             ` Steven Rostedt
  0 siblings, 1 reply; 45+ messages in thread
From: Joe Lawrence @ 2019-10-15 22:17 UTC (permalink / raw)
  To: Jessica Yu
  Cc: Miroslav Benes, Peter Zijlstra, Steven Rostedt, x86,
	linux-kernel, mhiramat, bristot, jbaron, torvalds, tglx, mingo,
	namit, hpa, luto, ard.biesheuvel, jpoimboe, live-patching

On 10/15/19 11:31 AM, Jessica Yu wrote:
> +++ Joe Lawrence [15/10/19 11:06 -0400]:
>> On 10/15/19 10:13 AM, Miroslav Benes wrote:
>>> Yes, it does. klp_module_coming() calls module_disable_ro() on all
>>> patching modules which patch the coming module in order to call
>>> apply_relocate_add(). New (patching) code for a module can be relocated
>>> only when the relevant module is loaded.
>>
>> FWIW, would the LPC blue-sky2 model (ie, Steve's suggestion @
>> plumber's where livepatches only patch a single object and updates are
>> kept on disk to handle coming module updates as they are loaded)
>> eliminate those outstanding relocations and the need to perform this
>> late permission flipping?
> 
> I wasn't at Plumbers sadly, was this idea documented/talked about in
> detail somewhere? (recording, slides, etherpad, etc?). What do you
> mean by updates are kept on disk? Maybe someone can explain it more
> in detail? :)
> 

Livepatching folks -- I don't have the LPC summary link (etherpad?) that 
Jiri put together.  Does someone have that handy for Jessica?

Thanks,

-- Joe

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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2019-10-15 22:17                           ` Joe Lawrence
@ 2019-10-15 22:27                             ` Steven Rostedt
  2019-10-16  7:42                               ` Peter Zijlstra
  2019-10-16  7:49                               ` Peter Zijlstra
  0 siblings, 2 replies; 45+ messages in thread
From: Steven Rostedt @ 2019-10-15 22:27 UTC (permalink / raw)
  To: Joe Lawrence
  Cc: Jessica Yu, Miroslav Benes, Peter Zijlstra, x86, linux-kernel,
	mhiramat, bristot, jbaron, torvalds, tglx, mingo, namit, hpa,
	luto, ard.biesheuvel, jpoimboe, live-patching

On Tue, 15 Oct 2019 18:17:43 -0400
Joe Lawrence <joe.lawrence@redhat.com> wrote:

> 
> Livepatching folks -- I don't have the LPC summary link (etherpad?) that 
> Jiri put together.  Does someone have that handy for Jessica?

Yes, and I'll be posting this on the LPC web site soon. But here's the
write up that Jiri sent me (with the etherpad link at the end):

Live patching miniconference covered 8 topics overall.

(1) First session, titled "What happened in kernel live patching over the last
year" was led by Miroslav Benes.  It was quite a natural followup to where we
ended at the LPC 2018 miniconf, summarizing which of the points that have been
agreed on back then have already been fully implemented, where obstacles have
been enounctered, etc.

The most prominent feature that has been merged during past year was "atomic
replace", which allows for easier stacking of patches. This is especially
useful for distros, as it naturally aligns with the way patches are being
distributed by them.
Another big step forward since LPC 2018 miniconf was addition of livepatching
selftests, which already tremendously helped in various cases, as it e.g.
helped to track down quite a few issues during development of reliable
stacktraces on s390. Proposal has been made that all major KLP features in the
future should be accompanied by accompanying selftest, which the audience
agreed on.
One of the last year's discussion topics / pain points were GCC optimizations
which are not compatible with livepatching. GCC upstream now has
-flive-patching option, which disables all those interfering optimizations.

(2) Second session, titled "Rethinking late module patching" was led by Miroslav
Benes again.
The problem statement is: in case when there is a patch loaded for module that
is yet to be loaded, it has to be patched before it starts executing. The
current solution relies on hooks in the module loader, and module is patched
when its being linked.  It gets a bit nasty with the arch-specifics of the
module loader handling all the relocations, patching of alternatives, etc. One
of the issues is that all the paravirt / jump label patching has to be done
after relocations are resolved, this is getting a bit fragile and not well
maintainable.
Miroslav sketched out the possible solutions:

	- livepatch would immediately load all the modules for which it has
	  patch via dependency; half-loading modules (not promoting to final
	  LIVE state)
	- splitting the currently one big monolithic livepatch to a per-object
	  structure; might cause issues with consistency model
	- "blue sky" idea from Joe Lawrence: livepatch loaded modules,
	  binary-patch .ko on disk, blacklist vulnerable version

Miroslav proposed to actually stick to the current solution, and improve
selftests coverage for all the considered-fragile arch-specific module linking
code hooks. The discussion then mostly focused, based on proposals from several
attendees (most prominently Steven Rostedt and Amit Shah), on expanding on the
"blue sky" idea.
The final proposal converged to having a separate .ko for livepatches that's
installed on the disk along with the module.  This addresses the module
signature issue (as signature does not actually change), as well as module
removal case (the case where a module was previously loaded while a livepatch
is applied, and then later unloaded and reloaded).  The slight downside is that
this will require changes to the module loader to also look for livepatches
when loading a module.  When unloading the module, the livepatch module will
also need to be unloaded.  Steven approved of this approach over his previous
suggestion.

(3) Third session, titled "Source-based livepatch creation tooling", was led by
Nicolai Stange.
The primary objective of the session was basing on the source-based creation
of livepatches, while avoiding the tedious (and error-prone task) of copying
a lot of kernel code around (from the source tree to the livepatch). Nicolai
spent par of last year writing a klp-ccp (KLP Copy and Paste) utility, which
automates a big chunk of the process.
Nicolai then presented the still open issues with the tool and with the process
around it, most promonent ones being:

	- obtaining original GCC commandline that was used to build the
	  original kernel
	- externalizability of static functions; we need to know whether GCC
	  emitted static function into the patched object

Miroslav proposed to extend existing IPA dumping capabiity of GCC to emit also
the information about dead code elimination; DWARF information is guaranteed
not to be reliable when it comes to IPA optimizations.

(4) Fourth session, titled "Objtool on power -- update", was led by Kamalesh
Babulal.
Kamalesh reported that as a followup to last year's miniconference, the objtool
support for powerpc actually came to life. It hasn't yet been posted upstream,
but is currently available on github [1].
Kamalesh further reported, that decoder has basic functionality (stack
operations + validation, branches, unreachable code, switch table (through gcc
plugin), conditional branches, prologue sequences). It turns out that stack
validation on powerpc is easier than on x86, as the ABI is much more strict
there; which leaves the validation phase to mostly focus on hand-written
assembly.
The next steps are basing on arm64 objtool code which already abstracted out
the arch-specific bits, and further optimizations can be stacked on top of that
(switch table detection, more testing, different gcc versions).

(5) Fifth session, titled "Do we need a Livepatch Developers Guide?", was led
by Joe Lawrence.
Joe postulated, that Current in-kernel documentation provides very good
documentation for individual features the infrastructure provides to the
livepatch author, but Joe further suggested to also include something along the
lines of what they currently have for kpatch, which takes a more general look
from the point of view of livepatch developer.

Proposals that have been brought up for discussion:
    - FAQ
    - collecting already existing CVE fixes and ammend them with a lot of
      commentary
    - creating a livepatch blog on people.kernel.org

Mark Brown asked for documenting what architectures need to implement in order
to support livepatching.
Amit Shah asked if the 'kpatch' and 'kpatch-build' script/program be renamed to
'livepatch'-friendly names so that kernel sources can also reference them for
the user docs part of it.
Both Mark's and Amit's remarks have been considered very valid and useful, and
agreement was reached that they will be taken care of.

(6) Sixth session, titled "API for state changes made by callbacks", was led
by Petr Mladek.

Petr described his proposal for API for changing, updating and disabling
patches (by callbacks). Example where this was needed: L1TF fix, which needed
to change PTE semantics (particular bits). This can't be done before all the
code understands this new PTE format/semantics. Therefore pre-patch and
post-patch callbacks had to do the actual modifications to all the existing
PTEs. What is also currently missing is tracking compatibilities / dependencies
between individual livepatches.
Petr's proposal (v2) is already on ML.
struct klp_state is being introduced which tracks the actual states of the
patch. klp_is_patch_compatible() checks the compatibility of the current states
to the states that the new livepatch is going to bring.
No principal issues / objections have been raised, and it's appreciated by the
patch author(s), so v3 will be submitted and pre-merge bikeshedding will start.

(7) Seventh session, titled "klp-convert and livepatch relocations", was led
by Joe Lawrence.

Joe started the session with problem statement: accessing non exported / static
symbols from inside the patch module. One possible workardound is manually via
kallsyms. Second workaround is klp-convert, which actually creates proper
relocations inside the livepatch module from the symbol database during the
final .ko link.
Currently module loader looks for special livepatch relocations and resolves
those during runtime; kernel support for these relocations have so far been
added for x86 only. Special livepatch relocations are supported and processed
also on other architectures. Special quirks/sections are not yet supported.
Plus klp-convert would still be needed even with late module patching update.
vmlinux or modules could have ambiguous static symbols.

It turns out that the features / bugs below have to be resolved before we
can claim the klp-convert support for relocation complete:
    - handle all the corner cases (jump labels, static keys, ...) properly and
      have a good regression tests in place
    - one day we might (or might not) add support for out-of-tree modules which
      need klp-convert
    - BFD bug 24456 (multiple relocations to the same .text section)

(8) Eight sesstion, titled "Making livepatching infrastructure better", was led
by Kamalesh Babulal.


The primary goal of the discussion as presented by Kamalesh was simple: how to
improve our testing coverage.  Currently we have sample modules + kselftests.
We seem to be currently missing specific unit cases and tests for corner cases.
What Kamalesh would also like to see would be more stress testing oriented
tests for the infrastructure. We should make sure that projects like kernelCI
are running with CONFIG_LIVEPATCH=y.
Another thing Kamalesh currently sees as missing are failure test cases too. 
It should be checked with sosreport and supportconfig guys whether those
diagnostic tools do provide necessary coverage of (at least) livepatching sysfs
state. This is especially a task for distro people to figure out.
Nicolai proposed as one of the testcases identity patching, as that should
reveal issues directly in the infrastructure.

(9) Last, ninth session, titled "Open sourcing live patching services", was led
by Alice Ferrazzi.
This session followed up on previous suggestion of having public repository for
livepatches against LTS kernel.
Alice reported on improviement of elivepatch since last year as having moved
everything to docker.
Alice proposed to more share livepatch sources; SUSE does publish those [2][3],
but it's important to mention that livepatches are very closely tied to
particular kernel version.

[1] https://github.com/kamalesh-babulal/linux/tree/objtool-v1
[2] On https://kernel.suse.com/
[3] Example source-based SUSE's livepatch is at https://kernel.suse.com/cgit/kernel-livepatch/tree/uname_patch/kgr_patch_uname.c?id=d4e00de0b0a3f858fec4e83640f12e1f17298667

Eherpad: https://etherpad.net/p/LPC2019_Live_Patching

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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2019-10-15 15:06                       ` Joe Lawrence
  2019-10-15 15:31                         ` Jessica Yu
@ 2019-10-16  6:51                         ` Miroslav Benes
  2019-10-16  9:23                           ` Peter Zijlstra
  2019-10-16 12:39                           ` Peter Zijlstra
  1 sibling, 2 replies; 45+ messages in thread
From: Miroslav Benes @ 2019-10-16  6:51 UTC (permalink / raw)
  To: Joe Lawrence
  Cc: Peter Zijlstra, Jessica Yu, Steven Rostedt, x86, linux-kernel,
	mhiramat, bristot, jbaron, torvalds, tglx, mingo, namit, hpa,
	luto, ard.biesheuvel, jpoimboe, live-patching

On Tue, 15 Oct 2019, Joe Lawrence wrote:

> On 10/15/19 10:13 AM, Miroslav Benes wrote:
> > Yes, it does. klp_module_coming() calls module_disable_ro() on all
> > patching modules which patch the coming module in order to call
> > apply_relocate_add(). New (patching) code for a module can be relocated
> > only when the relevant module is loaded.
> 
> FWIW, would the LPC blue-sky2 model (ie, Steve's suggestion @ plumber's where
> livepatches only patch a single object and updates are kept on disk to handle
> coming module updates as they are loaded) eliminate those outstanding
> relocations and the need to perform this late permission flipping?

Yes, it should, but we don't have to wait for it. PeterZ proposed a 
different solution to this specific issue in 
https://lore.kernel.org/lkml/20191015141111.GP2359@hirez.programming.kicks-ass.net/

It should not be a problem to create a live patch module like that and the 
code in kernel/livepatch/ is almost ready. Something like 
module_section_disable_ro(mod, section) (and similar for X protection) 
should be enough. Module reloads would still require juggling with the 
protections, but I think it is all feasible.

Miroslav

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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2019-10-15 22:27                             ` Steven Rostedt
@ 2019-10-16  7:42                               ` Peter Zijlstra
  2019-10-16 10:15                                 ` Miroslav Benes
  2019-10-21 15:05                                 ` Josh Poimboeuf
  2019-10-16  7:49                               ` Peter Zijlstra
  1 sibling, 2 replies; 45+ messages in thread
From: Peter Zijlstra @ 2019-10-16  7:42 UTC (permalink / raw)
  To: Steven Rostedt
  Cc: Joe Lawrence, Jessica Yu, Miroslav Benes, x86, linux-kernel,
	mhiramat, bristot, jbaron, torvalds, tglx, mingo, namit, hpa,
	luto, ard.biesheuvel, jpoimboe, live-patching

> which are not compatible with livepatching. GCC upstream now has
> -flive-patching option, which disables all those interfering optimizations.

Which, IIRC, has a significant performance impact and should thus really
not be used...

If distros ship that crap, I'm going to laugh at them the next time they
want a single digit performance improvement because *important*.

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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2019-10-15 22:27                             ` Steven Rostedt
  2019-10-16  7:42                               ` Peter Zijlstra
@ 2019-10-16  7:49                               ` Peter Zijlstra
  2019-10-16 10:20                                 ` Miroslav Benes
  1 sibling, 1 reply; 45+ messages in thread
From: Peter Zijlstra @ 2019-10-16  7:49 UTC (permalink / raw)
  To: Steven Rostedt
  Cc: Joe Lawrence, Jessica Yu, Miroslav Benes, x86, linux-kernel,
	mhiramat, bristot, jbaron, torvalds, tglx, mingo, namit, hpa,
	luto, ard.biesheuvel, jpoimboe, live-patching

On Tue, Oct 15, 2019 at 06:27:05PM -0400, Steven Rostedt wrote:

> (7) Seventh session, titled "klp-convert and livepatch relocations", was led
> by Joe Lawrence.
> 
> Joe started the session with problem statement: accessing non exported / static
> symbols from inside the patch module. One possible workardound is manually via
> kallsyms. Second workaround is klp-convert, which actually creates proper
> relocations inside the livepatch module from the symbol database during the
> final .ko link.
> Currently module loader looks for special livepatch relocations and resolves
> those during runtime; kernel support for these relocations have so far been
> added for x86 only. Special livepatch relocations are supported and processed
> also on other architectures. Special quirks/sections are not yet supported.
> Plus klp-convert would still be needed even with late module patching update.
> vmlinux or modules could have ambiguous static symbols.
> 
> It turns out that the features / bugs below have to be resolved before we
> can claim the klp-convert support for relocation complete:
>     - handle all the corner cases (jump labels, static keys, ...) properly and
>       have a good regression tests in place

I suppose all the patches in this series-of-series here will make life
harder for KLP, static_call() and 2 byte jumps etc..

>     - one day we might (or might not) add support for out-of-tree modules which
>       need klp-convert
>     - BFD bug 24456 (multiple relocations to the same .text section)



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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2019-10-16  6:51                         ` Miroslav Benes
@ 2019-10-16  9:23                           ` Peter Zijlstra
  2019-10-16  9:36                             ` Jessica Yu
  2019-10-16 12:39                           ` Peter Zijlstra
  1 sibling, 1 reply; 45+ messages in thread
From: Peter Zijlstra @ 2019-10-16  9:23 UTC (permalink / raw)
  To: Miroslav Benes
  Cc: Joe Lawrence, Jessica Yu, Steven Rostedt, x86, linux-kernel,
	mhiramat, bristot, jbaron, torvalds, tglx, mingo, namit, hpa,
	luto, ard.biesheuvel, jpoimboe, live-patching

On Wed, Oct 16, 2019 at 08:51:27AM +0200, Miroslav Benes wrote:
> On Tue, 15 Oct 2019, Joe Lawrence wrote:
> 
> > On 10/15/19 10:13 AM, Miroslav Benes wrote:
> > > Yes, it does. klp_module_coming() calls module_disable_ro() on all
> > > patching modules which patch the coming module in order to call
> > > apply_relocate_add(). New (patching) code for a module can be relocated
> > > only when the relevant module is loaded.
> > 
> > FWIW, would the LPC blue-sky2 model (ie, Steve's suggestion @ plumber's where
> > livepatches only patch a single object and updates are kept on disk to handle
> > coming module updates as they are loaded) eliminate those outstanding
> > relocations and the need to perform this late permission flipping?
> 
> Yes, it should, but we don't have to wait for it. PeterZ proposed a 
> different solution to this specific issue in 
> https://lore.kernel.org/lkml/20191015141111.GP2359@hirez.programming.kicks-ass.net/
> 
> It should not be a problem to create a live patch module like that and the 
> code in kernel/livepatch/ is almost ready. Something like 
> module_section_disable_ro(mod, section) (and similar for X protection) 
> should be enough. Module reloads would still require juggling with the 
> protections, but I think it is all feasible.

Just had a browse around the module code, and while the section info is
in load_info, it doesn't get retained for active modules.

So I suppose I'll go add that for KLP enabled things.

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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2019-10-16  9:23                           ` Peter Zijlstra
@ 2019-10-16  9:36                             ` Jessica Yu
  2019-10-16  9:51                               ` Peter Zijlstra
  0 siblings, 1 reply; 45+ messages in thread
From: Jessica Yu @ 2019-10-16  9:36 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Miroslav Benes, Joe Lawrence, Steven Rostedt, x86, linux-kernel,
	mhiramat, bristot, jbaron, torvalds, tglx, mingo, namit, hpa,
	luto, ard.biesheuvel, jpoimboe, live-patching

+++ Peter Zijlstra [16/10/19 11:23 +0200]:
>On Wed, Oct 16, 2019 at 08:51:27AM +0200, Miroslav Benes wrote:
>> On Tue, 15 Oct 2019, Joe Lawrence wrote:
>>
>> > On 10/15/19 10:13 AM, Miroslav Benes wrote:
>> > > Yes, it does. klp_module_coming() calls module_disable_ro() on all
>> > > patching modules which patch the coming module in order to call
>> > > apply_relocate_add(). New (patching) code for a module can be relocated
>> > > only when the relevant module is loaded.
>> >
>> > FWIW, would the LPC blue-sky2 model (ie, Steve's suggestion @ plumber's where
>> > livepatches only patch a single object and updates are kept on disk to handle
>> > coming module updates as they are loaded) eliminate those outstanding
>> > relocations and the need to perform this late permission flipping?
>>
>> Yes, it should, but we don't have to wait for it. PeterZ proposed a
>> different solution to this specific issue in
>> https://lore.kernel.org/lkml/20191015141111.GP2359@hirez.programming.kicks-ass.net/
>>
>> It should not be a problem to create a live patch module like that and the
>> code in kernel/livepatch/ is almost ready. Something like
>> module_section_disable_ro(mod, section) (and similar for X protection)
>> should be enough. Module reloads would still require juggling with the
>> protections, but I think it is all feasible.
>
>Just had a browse around the module code, and while the section info is
>in load_info, it doesn't get retained for active modules.
>
>So I suppose I'll go add that for KLP enabled things.

Elf section info does get saved for livepatch modules though, see
mod->klp_info. And wouldn't this only be needed for livepatch modules?

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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2019-10-16  9:36                             ` Jessica Yu
@ 2019-10-16  9:51                               ` Peter Zijlstra
  0 siblings, 0 replies; 45+ messages in thread
From: Peter Zijlstra @ 2019-10-16  9:51 UTC (permalink / raw)
  To: Jessica Yu
  Cc: Miroslav Benes, Joe Lawrence, Steven Rostedt, x86, linux-kernel,
	mhiramat, bristot, jbaron, torvalds, tglx, mingo, namit, hpa,
	luto, ard.biesheuvel, jpoimboe, live-patching

On Wed, Oct 16, 2019 at 11:36:10AM +0200, Jessica Yu wrote:
> +++ Peter Zijlstra [16/10/19 11:23 +0200]:
> > On Wed, Oct 16, 2019 at 08:51:27AM +0200, Miroslav Benes wrote:
> > > On Tue, 15 Oct 2019, Joe Lawrence wrote:
> > > 
> > > > On 10/15/19 10:13 AM, Miroslav Benes wrote:
> > > > > Yes, it does. klp_module_coming() calls module_disable_ro() on all
> > > > > patching modules which patch the coming module in order to call
> > > > > apply_relocate_add(). New (patching) code for a module can be relocated
> > > > > only when the relevant module is loaded.
> > > >
> > > > FWIW, would the LPC blue-sky2 model (ie, Steve's suggestion @ plumber's where
> > > > livepatches only patch a single object and updates are kept on disk to handle
> > > > coming module updates as they are loaded) eliminate those outstanding
> > > > relocations and the need to perform this late permission flipping?
> > > 
> > > Yes, it should, but we don't have to wait for it. PeterZ proposed a
> > > different solution to this specific issue in
> > > https://lore.kernel.org/lkml/20191015141111.GP2359@hirez.programming.kicks-ass.net/
> > > 
> > > It should not be a problem to create a live patch module like that and the
> > > code in kernel/livepatch/ is almost ready. Something like
> > > module_section_disable_ro(mod, section) (and similar for X protection)
> > > should be enough. Module reloads would still require juggling with the
> > > protections, but I think it is all feasible.
> > 
> > Just had a browse around the module code, and while the section info is
> > in load_info, it doesn't get retained for active modules.
> > 
> > So I suppose I'll go add that for KLP enabled things.
> 
> Elf section info does get saved for livepatch modules though, see
> mod->klp_info. And wouldn't this only be needed for livepatch modules?

Right, I just found that, but it is x86 only for some mysterious reason.
And yes, it's KLP only.

I was thikning of adding a KLP only list of {name,addr,size} sections
that start with ".klp" in layout_sections(). Would that not work across
architectures?

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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2019-10-16  7:42                               ` Peter Zijlstra
@ 2019-10-16 10:15                                 ` Miroslav Benes
  2019-10-21 15:05                                 ` Josh Poimboeuf
  1 sibling, 0 replies; 45+ messages in thread
From: Miroslav Benes @ 2019-10-16 10:15 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Steven Rostedt, Joe Lawrence, Jessica Yu, x86, linux-kernel,
	mhiramat, bristot, jbaron, torvalds, tglx, mingo, namit, hpa,
	luto, ard.biesheuvel, jpoimboe, live-patching

On Wed, 16 Oct 2019, Peter Zijlstra wrote:

> > which are not compatible with livepatching. GCC upstream now has
> > -flive-patching option, which disables all those interfering optimizations.
> 
> Which, IIRC, has a significant performance impact and should thus really
> not be used...

Fortunately, the impact is negligible.

Miroslav

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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2019-10-16  7:49                               ` Peter Zijlstra
@ 2019-10-16 10:20                                 ` Miroslav Benes
  2019-10-16 13:29                                   ` Miroslav Benes
  0 siblings, 1 reply; 45+ messages in thread
From: Miroslav Benes @ 2019-10-16 10:20 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Steven Rostedt, Joe Lawrence, Jessica Yu, x86, linux-kernel,
	mhiramat, bristot, jbaron, torvalds, tglx, mingo, namit, hpa,
	luto, ard.biesheuvel, jpoimboe, live-patching, pmladek

On Wed, 16 Oct 2019, Peter Zijlstra wrote:

> On Tue, Oct 15, 2019 at 06:27:05PM -0400, Steven Rostedt wrote:
> 
> > (7) Seventh session, titled "klp-convert and livepatch relocations", was led
> > by Joe Lawrence.
> > 
> > Joe started the session with problem statement: accessing non exported / static
> > symbols from inside the patch module. One possible workardound is manually via
> > kallsyms. Second workaround is klp-convert, which actually creates proper
> > relocations inside the livepatch module from the symbol database during the
> > final .ko link.
> > Currently module loader looks for special livepatch relocations and resolves
> > those during runtime; kernel support for these relocations have so far been
> > added for x86 only. Special livepatch relocations are supported and processed
> > also on other architectures. Special quirks/sections are not yet supported.
> > Plus klp-convert would still be needed even with late module patching update.
> > vmlinux or modules could have ambiguous static symbols.
> > 
> > It turns out that the features / bugs below have to be resolved before we
> > can claim the klp-convert support for relocation complete:
> >     - handle all the corner cases (jump labels, static keys, ...) properly and
> >       have a good regression tests in place
> 
> I suppose all the patches in this series-of-series here will make life
> harder for KLP, static_call() and 2 byte jumps etc..

Yes, I think so. We'll have to deal with that once it lands. That is why 
we want to get rid of all this arch-specific code in livepatch and 
reinvent the late module patching. So it is perhaps better to start 
working on it sooner than later. Adding Petr, who hesitantly signed up for 
the task...

Miroslav

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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2019-10-16  6:51                         ` Miroslav Benes
  2019-10-16  9:23                           ` Peter Zijlstra
@ 2019-10-16 12:39                           ` Peter Zijlstra
  2019-10-22  8:45                             ` Miroslav Benes
  1 sibling, 1 reply; 45+ messages in thread
From: Peter Zijlstra @ 2019-10-16 12:39 UTC (permalink / raw)
  To: Miroslav Benes
  Cc: Joe Lawrence, Jessica Yu, Steven Rostedt, x86, linux-kernel,
	mhiramat, bristot, jbaron, torvalds, tglx, mingo, namit, hpa,
	luto, ard.biesheuvel, jpoimboe, live-patching

On Wed, Oct 16, 2019 at 08:51:27AM +0200, Miroslav Benes wrote:
> On Tue, 15 Oct 2019, Joe Lawrence wrote:
> 
> > On 10/15/19 10:13 AM, Miroslav Benes wrote:
> > > Yes, it does. klp_module_coming() calls module_disable_ro() on all
> > > patching modules which patch the coming module in order to call
> > > apply_relocate_add(). New (patching) code for a module can be relocated
> > > only when the relevant module is loaded.
> > 
> > FWIW, would the LPC blue-sky2 model (ie, Steve's suggestion @ plumber's where
> > livepatches only patch a single object and updates are kept on disk to handle
> > coming module updates as they are loaded) eliminate those outstanding
> > relocations and the need to perform this late permission flipping?
> 
> Yes, it should, but we don't have to wait for it. PeterZ proposed a 
> different solution to this specific issue in 
> https://lore.kernel.org/lkml/20191015141111.GP2359@hirez.programming.kicks-ass.net/
> 
> It should not be a problem to create a live patch module like that and the 
> code in kernel/livepatch/ is almost ready. Something like 
> module_section_disable_ro(mod, section) (and similar for X protection) 
> should be enough. Module reloads would still require juggling with the 
> protections, but I think it is all feasible.

Something a little like so.. completely fresh of the keyboard.

---
--- a/include/linux/module.h
+++ b/include/linux/module.h
@@ -853,6 +853,18 @@ static inline void module_enable_ro(cons
 static inline void module_disable_ro(const struct module *mod) { }
 #endif
 
+#if defined(CONFIG_STRICT_MODULE_RWX) && defined(CONFIG_LIVEPATCH)
+extern void module_section_disable_ro(struct module *mod, const char *sec);
+extern void module_section_enable_ro(struct module *mod, const char *sec);
+extern void module_section_disable_x(struct module *mod, const char *sec);
+extern void module_section_enable_x(struct module *mod, const char *sec);
+#else
+static inline void module_section_disable_ro(struct module *mod, const char *sec) { }
+static inline void module_section_enable_ro(struct module *mod, const char *sec) { }
+static inline void module_section_disable_x(struct module *mod, const char *sec) { }
+static inline void module_section_enable_x(struct module *mod, const char *sec) { }
+#endif
+
 #ifdef CONFIG_GENERIC_BUG
 void module_bug_finalize(const Elf_Ehdr *, const Elf_Shdr *,
 			 struct module *);
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -2107,6 +2107,54 @@ static void free_module_elf(struct modul
 	kfree(mod->klp_info->secstrings);
 	kfree(mod->klp_info);
 }
+
+#ifdef CONFIG_STRICT_MODULE_RWX
+
+static void __frob_section(struct Elf_Shdr *sec, int (*set_memory)(unsigned long start, int num_pages))
+{
+	BUG_ON((unsigned long)sec->sh_addr & (PAGE_SIZE-1));
+	BUG_ON((unsigned long)sec->sh_size & (PAGE_SIZE-1));
+	set_memory((unsigned long)sec->sh_addr, sec->sh_size >> PAGE_SHIFT);
+}
+
+static void frob_section(struct module *mod, const char *section,
+			 int (*set_memory)(unsigned long start, int num_pages))
+{
+	struct klp_modinfo *info = mod->klp_info;
+	const char *secname;
+	Elf_Shdr *s;
+
+	for (s = info->sechdrs; s < info->sechdrs + info->hdr.e_shnum; s++) {
+		secname = mod->klp_info->secstrings + s->sh_name;
+		if (strcmp(secname, section))
+			continue;
+
+		__frob_section(s, set_memory);
+	}
+}
+
+void module_section_disable_ro(struct module *mod, const char *section)
+{
+	frob_section(mod, section, set_memory_rw);
+}
+
+void module_section_enable_ro(struct module *mod, const char *section)
+{
+	frob_section(mod, section, set_memory_ro);
+}
+
+void module_section_disable_x(struct module *mod, const char *section)
+{
+	frob_section(mod, section, set_memory_nx);
+}
+
+void module_section_enable_x(struct module *mod, const char *section)
+{
+	frob_section(mod, section, set_memory_x);
+}
+
+#endif /* ONFIG_STRICT_MODULE_RWX */
+
 #else /* !CONFIG_LIVEPATCH */
 static int copy_module_elf(struct module *mod, struct load_info *info)
 {

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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2019-10-16 10:20                                 ` Miroslav Benes
@ 2019-10-16 13:29                                   ` Miroslav Benes
  2019-10-18 13:03                                     ` Jessica Yu
  0 siblings, 1 reply; 45+ messages in thread
From: Miroslav Benes @ 2019-10-16 13:29 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Steven Rostedt, Joe Lawrence, Jessica Yu, x86, linux-kernel,
	mhiramat, bristot, jbaron, torvalds, tglx, mingo, namit, hpa,
	luto, ard.biesheuvel, jpoimboe, live-patching, pmladek

On Wed, 16 Oct 2019, Miroslav Benes wrote:

> On Wed, 16 Oct 2019, Peter Zijlstra wrote:
> 
> > On Tue, Oct 15, 2019 at 06:27:05PM -0400, Steven Rostedt wrote:
> > 
> > > (7) Seventh session, titled "klp-convert and livepatch relocations", was led
> > > by Joe Lawrence.
> > > 
> > > Joe started the session with problem statement: accessing non exported / static
> > > symbols from inside the patch module. One possible workardound is manually via
> > > kallsyms. Second workaround is klp-convert, which actually creates proper
> > > relocations inside the livepatch module from the symbol database during the
> > > final .ko link.
> > > Currently module loader looks for special livepatch relocations and resolves
> > > those during runtime; kernel support for these relocations have so far been
> > > added for x86 only. Special livepatch relocations are supported and processed
> > > also on other architectures. Special quirks/sections are not yet supported.
> > > Plus klp-convert would still be needed even with late module patching update.
> > > vmlinux or modules could have ambiguous static symbols.
> > > 
> > > It turns out that the features / bugs below have to be resolved before we
> > > can claim the klp-convert support for relocation complete:
> > >     - handle all the corner cases (jump labels, static keys, ...) properly and
> > >       have a good regression tests in place
> > 
> > I suppose all the patches in this series-of-series here will make life
> > harder for KLP, static_call() and 2 byte jumps etc..
> 
> Yes, I think so. We'll have to deal with that once it lands. That is why 
> we want to get rid of all this arch-specific code in livepatch and 
> reinvent the late module patching. So it is perhaps better to start 
> working on it sooner than later. Adding Petr, who hesitantly signed up for 
> the task...

Thinking about it more... crazy idea. I think we could leverage these new 
ELF .text per vmlinux/module sections for the reinvention I was talking 
about. If we teach module loader to relocate (and apply alternatives and 
so on, everything in arch-specific module_finalize()) not the whole module 
in case of live patch modules, but separate ELF .text sections, it could 
solve the issue with late module patching we have. It is a variation on 
Steven's idea. When live patch module is loaded, only its section for 
present modules would be processed. Then whenever a to-be-patched module 
is loaded, its .text section in all present patch module would be 
processed.

The upside is that almost no work would be required on patch modules 
creation side. The downside is that klp_modinfo must stay. Module loader 
needs to be hacked a lot in both cases. So it remains to be seen which 
idea is easier to implement.

Jessica, do you think it would be feasible?

Petr, Joe, Josh, am I missing something or would it work?

Miroslav

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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2019-10-16 13:29                                   ` Miroslav Benes
@ 2019-10-18 13:03                                     ` Jessica Yu
  2019-10-18 13:40                                       ` Petr Mladek
  2019-10-22  8:27                                       ` Miroslav Benes
  0 siblings, 2 replies; 45+ messages in thread
From: Jessica Yu @ 2019-10-18 13:03 UTC (permalink / raw)
  To: Miroslav Benes
  Cc: Peter Zijlstra, Steven Rostedt, Joe Lawrence, x86, linux-kernel,
	mhiramat, bristot, jbaron, torvalds, tglx, mingo, namit, hpa,
	luto, ard.biesheuvel, jpoimboe, live-patching, pmladek

+++ Miroslav Benes [16/10/19 15:29 +0200]:
>On Wed, 16 Oct 2019, Miroslav Benes wrote:
>
>> On Wed, 16 Oct 2019, Peter Zijlstra wrote:
>>
>> > On Tue, Oct 15, 2019 at 06:27:05PM -0400, Steven Rostedt wrote:
>> >
>> > > (7) Seventh session, titled "klp-convert and livepatch relocations", was led
>> > > by Joe Lawrence.
>> > >
>> > > Joe started the session with problem statement: accessing non exported / static
>> > > symbols from inside the patch module. One possible workardound is manually via
>> > > kallsyms. Second workaround is klp-convert, which actually creates proper
>> > > relocations inside the livepatch module from the symbol database during the
>> > > final .ko link.
>> > > Currently module loader looks for special livepatch relocations and resolves
>> > > those during runtime; kernel support for these relocations have so far been
>> > > added for x86 only. Special livepatch relocations are supported and processed
>> > > also on other architectures. Special quirks/sections are not yet supported.
>> > > Plus klp-convert would still be needed even with late module patching update.
>> > > vmlinux or modules could have ambiguous static symbols.
>> > >
>> > > It turns out that the features / bugs below have to be resolved before we
>> > > can claim the klp-convert support for relocation complete:
>> > >     - handle all the corner cases (jump labels, static keys, ...) properly and
>> > >       have a good regression tests in place
>> >
>> > I suppose all the patches in this series-of-series here will make life
>> > harder for KLP, static_call() and 2 byte jumps etc..
>>
>> Yes, I think so. We'll have to deal with that once it lands. That is why
>> we want to get rid of all this arch-specific code in livepatch and
>> reinvent the late module patching. So it is perhaps better to start
>> working on it sooner than later. Adding Petr, who hesitantly signed up for
>> the task...
>
>Thinking about it more... crazy idea. I think we could leverage these new
>ELF .text per vmlinux/module sections for the reinvention I was talking
>about. If we teach module loader to relocate (and apply alternatives and
>so on, everything in arch-specific module_finalize()) not the whole module
>in case of live patch modules, but separate ELF .text sections, it could
>solve the issue with late module patching we have. It is a variation on
>Steven's idea. When live patch module is loaded, only its section for
>present modules would be processed. Then whenever a to-be-patched module
>is loaded, its .text section in all present patch module would be
>processed.
>
>The upside is that almost no work would be required on patch modules
>creation side. The downside is that klp_modinfo must stay. Module loader
>needs to be hacked a lot in both cases. So it remains to be seen which
>idea is easier to implement.
>
>Jessica, do you think it would be feasible?

I think that does sound feasible. I'm trying to visualize how that
would look. I guess there would need to be various livepatching hooks
called during the different stages (apply_relocate_add(),
module_finalize(), module_enable_ro/x()).

So maybe something like the following?

When a livepatch module loads:
    apply_relocate_add()
        klp hook: apply .klp.rela.$objname relocations *only* for
        already loaded modules
    module_finalize()
        klp hook: apply .klp.arch.$objname changes for already loaded modules
    module_enable_ro()
        klp hook: only enable ro/x for .klp.text.$objname for already
        loaded modules

When a to-be-patched module loads:
    apply_relocate_add()
        klp hook: for each patch module that patches the coming
        module, apply .klp.rela.$objname relocations for this object
    module_finalize()
        klp hook: for each patch module that patches the coming
        module, apply .klp.arch.$objname changes for this object
    module_enable_ro()
        klp hook: for each patch module, apply ro/x permissions for
        .klp.text.$objname for this object

Then, in klp_module_coming, we only need to do the callbacks and
enable the patch, and get rid of the module_disable_ro->apply
relocs->module_enable_ro block.

Does that sound like what you had in mind or am I totally off?

Thanks!

Jessica


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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2019-10-18 13:03                                     ` Jessica Yu
@ 2019-10-18 13:40                                       ` Petr Mladek
  2019-10-21 14:14                                         ` Jessica Yu
  2019-10-21 15:31                                         ` Josh Poimboeuf
  2019-10-22  8:27                                       ` Miroslav Benes
  1 sibling, 2 replies; 45+ messages in thread
From: Petr Mladek @ 2019-10-18 13:40 UTC (permalink / raw)
  To: Jessica Yu
  Cc: Miroslav Benes, Peter Zijlstra, Steven Rostedt, Joe Lawrence,
	x86, linux-kernel, mhiramat, bristot, jbaron, torvalds, tglx,
	mingo, namit, hpa, luto, ard.biesheuvel, jpoimboe, live-patching

On Fri 2019-10-18 15:03:42, Jessica Yu wrote:
> +++ Miroslav Benes [16/10/19 15:29 +0200]:
> > On Wed, 16 Oct 2019, Miroslav Benes wrote:
> > Thinking about it more... crazy idea. I think we could leverage these new
> > ELF .text per vmlinux/module sections for the reinvention I was talking
> > about. If we teach module loader to relocate (and apply alternatives and
> > so on, everything in arch-specific module_finalize()) not the whole module
> > in case of live patch modules, but separate ELF .text sections, it could
> > solve the issue with late module patching we have. It is a variation on
> > Steven's idea. When live patch module is loaded, only its section for
> > present modules would be processed. Then whenever a to-be-patched module
> > is loaded, its .text section in all present patch module would be
> > processed.
> > 
> > The upside is that almost no work would be required on patch modules
> > creation side. The downside is that klp_modinfo must stay. Module loader
> > needs to be hacked a lot in both cases. So it remains to be seen which
> > idea is easier to implement.
> > 
> > Jessica, do you think it would be feasible?
> 
> I think that does sound feasible. I'm trying to visualize how that
> would look. I guess there would need to be various livepatching hooks
> called during the different stages (apply_relocate_add(),
> module_finalize(), module_enable_ro/x()).
> 
> So maybe something like the following?
> 
> When a livepatch module loads:
>    apply_relocate_add()
>        klp hook: apply .klp.rela.$objname relocations *only* for
>        already loaded modules
>    module_finalize()
>        klp hook: apply .klp.arch.$objname changes for already loaded modules
>    module_enable_ro()
>        klp hook: only enable ro/x for .klp.text.$objname for already
>        loaded modules

Just for record. We should also set ro for the not-yet used
.klp.text.$objname at this stage so that it can't be modified
easily "by accident".


> When a to-be-patched module loads:
>    apply_relocate_add()
>        klp hook: for each patch module that patches the coming
>        module, apply .klp.rela.$objname relocations for this object
>    module_finalize()
>        klp hook: for each patch module that patches the coming
>        module, apply .klp.arch.$objname changes for this object
>    module_enable_ro()
>        klp hook: for each patch module, apply ro/x permissions for
>        .klp.text.$objname for this object
> 
> Then, in klp_module_coming, we only need to do the callbacks and
> enable the patch, and get rid of the module_disable_ro->apply
> relocs->module_enable_ro block.
> 
> Does that sound like what you had in mind or am I totally off?

Makes sense to me.

Well, I wonder if it is really any better from what we have now.
We would still need special delayed handling for the module-specific
elf sections. Also we still would not need to clear the modifications
in these sections when the livepatched object gets unloaded.

I am afraid that the real difference might come when we split
the livepatch into per-livepatched object modules. This would
move the complexity to another parts of the code ;-) I am
unable to say what approach is easier and more safe to maintain
at the moment.

Best Regards,
Petr

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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2019-10-18 13:40                                       ` Petr Mladek
@ 2019-10-21 14:14                                         ` Jessica Yu
  2019-10-21 15:31                                         ` Josh Poimboeuf
  1 sibling, 0 replies; 45+ messages in thread
From: Jessica Yu @ 2019-10-21 14:14 UTC (permalink / raw)
  To: Petr Mladek
  Cc: Miroslav Benes, Peter Zijlstra, Steven Rostedt, Joe Lawrence,
	x86, linux-kernel, mhiramat, bristot, jbaron, torvalds, tglx,
	mingo, namit, hpa, luto, ard.biesheuvel, jpoimboe, live-patching

+++ Petr Mladek [18/10/19 15:40 +0200]:
>On Fri 2019-10-18 15:03:42, Jessica Yu wrote:
>> +++ Miroslav Benes [16/10/19 15:29 +0200]:
>> > On Wed, 16 Oct 2019, Miroslav Benes wrote:
>> > Thinking about it more... crazy idea. I think we could leverage these new
>> > ELF .text per vmlinux/module sections for the reinvention I was talking
>> > about. If we teach module loader to relocate (and apply alternatives and
>> > so on, everything in arch-specific module_finalize()) not the whole module
>> > in case of live patch modules, but separate ELF .text sections, it could
>> > solve the issue with late module patching we have. It is a variation on
>> > Steven's idea. When live patch module is loaded, only its section for
>> > present modules would be processed. Then whenever a to-be-patched module
>> > is loaded, its .text section in all present patch module would be
>> > processed.
>> >
>> > The upside is that almost no work would be required on patch modules
>> > creation side. The downside is that klp_modinfo must stay. Module loader
>> > needs to be hacked a lot in both cases. So it remains to be seen which
>> > idea is easier to implement.
>> >
>> > Jessica, do you think it would be feasible?
>>
>> I think that does sound feasible. I'm trying to visualize how that
>> would look. I guess there would need to be various livepatching hooks
>> called during the different stages (apply_relocate_add(),
>> module_finalize(), module_enable_ro/x()).
>>
>> So maybe something like the following?
>>
>> When a livepatch module loads:
>>    apply_relocate_add()
>>        klp hook: apply .klp.rela.$objname relocations *only* for
>>        already loaded modules
>>    module_finalize()
>>        klp hook: apply .klp.arch.$objname changes for already loaded modules
>>    module_enable_ro()
>>        klp hook: only enable ro/x for .klp.text.$objname for already
>>        loaded modules
>
>Just for record. We should also set ro for the not-yet used
>.klp.text.$objname at this stage so that it can't be modified
>easily "by accident".

If we also set ro protection already for .klp.text.$objname for
not-yet loaded modules, I think this would unfortunately mean we would
still have to do the protection flipping for late module patching that
Peter was trying to avoid, right?

That is, we *still* end up having to do the whole module_disable_ro()
-> apply_relocate_add() -> module_finalize() -> module_enable_ro()
thing for late module patching, except now we've moved that work to
the module loader instead of in klp_module_coming.. It sounds just as
complicated as the current way :/

However, I think this complaint would not apply if livepatch switches
to the one patch module per module model..


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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2019-10-16  7:42                               ` Peter Zijlstra
  2019-10-16 10:15                                 ` Miroslav Benes
@ 2019-10-21 15:05                                 ` Josh Poimboeuf
  2020-01-20 16:50                                   ` Josh Poimboeuf
  1 sibling, 1 reply; 45+ messages in thread
From: Josh Poimboeuf @ 2019-10-21 15:05 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Steven Rostedt, Joe Lawrence, Jessica Yu, Miroslav Benes, x86,
	linux-kernel, mhiramat, bristot, jbaron, torvalds, tglx, mingo,
	namit, hpa, luto, ard.biesheuvel, live-patching

On Wed, Oct 16, 2019 at 09:42:17AM +0200, Peter Zijlstra wrote:
> > which are not compatible with livepatching. GCC upstream now has
> > -flive-patching option, which disables all those interfering optimizations.
> 
> Which, IIRC, has a significant performance impact and should thus really
> not be used...
> 
> If distros ship that crap, I'm going to laugh at them the next time they
> want a single digit performance improvement because *important*.

I have a crazy plan to try to use objtool to detect function changes at
a binary level, which would hopefully allow us to drop this flag.

But regardless, I wonder if we enabled this flag prematurely.  We still
don't have a reasonable way to use it for creating source-based live
patches upstream, and it should really be optional for CONFIG_LIVEPATCH,
since kpatch-build doesn't need it.

-- 
Josh


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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2019-10-18 13:40                                       ` Petr Mladek
  2019-10-21 14:14                                         ` Jessica Yu
@ 2019-10-21 15:31                                         ` Josh Poimboeuf
  1 sibling, 0 replies; 45+ messages in thread
From: Josh Poimboeuf @ 2019-10-21 15:31 UTC (permalink / raw)
  To: Petr Mladek
  Cc: Jessica Yu, Miroslav Benes, Peter Zijlstra, Steven Rostedt,
	Joe Lawrence, x86, linux-kernel, mhiramat, bristot, jbaron,
	torvalds, tglx, mingo, namit, hpa, luto, ard.biesheuvel,
	live-patching

On Fri, Oct 18, 2019 at 03:40:58PM +0200, Petr Mladek wrote:
> On Fri 2019-10-18 15:03:42, Jessica Yu wrote:
> > +++ Miroslav Benes [16/10/19 15:29 +0200]:
> > > On Wed, 16 Oct 2019, Miroslav Benes wrote:
> > > Thinking about it more... crazy idea. I think we could leverage these new
> > > ELF .text per vmlinux/module sections for the reinvention I was talking
> > > about. If we teach module loader to relocate (and apply alternatives and
> > > so on, everything in arch-specific module_finalize()) not the whole module
> > > in case of live patch modules, but separate ELF .text sections, it could
> > > solve the issue with late module patching we have. It is a variation on
> > > Steven's idea. When live patch module is loaded, only its section for
> > > present modules would be processed. Then whenever a to-be-patched module
> > > is loaded, its .text section in all present patch module would be
> > > processed.
> > > 
> > > The upside is that almost no work would be required on patch modules
> > > creation side. The downside is that klp_modinfo must stay. Module loader
> > > needs to be hacked a lot in both cases. So it remains to be seen which
> > > idea is easier to implement.
> > > 
> > > Jessica, do you think it would be feasible?
> > 
> > I think that does sound feasible. I'm trying to visualize how that
> > would look. I guess there would need to be various livepatching hooks
> > called during the different stages (apply_relocate_add(),
> > module_finalize(), module_enable_ro/x()).
> > 
> > So maybe something like the following?
> > 
> > When a livepatch module loads:
> >    apply_relocate_add()
> >        klp hook: apply .klp.rela.$objname relocations *only* for
> >        already loaded modules
> >    module_finalize()
> >        klp hook: apply .klp.arch.$objname changes for already loaded modules
> >    module_enable_ro()
> >        klp hook: only enable ro/x for .klp.text.$objname for already
> >        loaded modules
> 
> Just for record. We should also set ro for the not-yet used
> .klp.text.$objname at this stage so that it can't be modified
> easily "by accident".
> 
> 
> > When a to-be-patched module loads:
> >    apply_relocate_add()
> >        klp hook: for each patch module that patches the coming
> >        module, apply .klp.rela.$objname relocations for this object
> >    module_finalize()
> >        klp hook: for each patch module that patches the coming
> >        module, apply .klp.arch.$objname changes for this object
> >    module_enable_ro()
> >        klp hook: for each patch module, apply ro/x permissions for
> >        .klp.text.$objname for this object
> > 
> > Then, in klp_module_coming, we only need to do the callbacks and
> > enable the patch, and get rid of the module_disable_ro->apply
> > relocs->module_enable_ro block.
> > 
> > Does that sound like what you had in mind or am I totally off?
> 
> Makes sense to me.
> 
> Well, I wonder if it is really any better from what we have now.

AFAICT, this would still have a lot of the same problems we have today.
It has a lot of complexity.  It needs arch-specific livepatch code and
sections, and introduces special cases in the module code.

I'd much prefer the proposal from LPC to have per-module live patches.
It's simpler and has less things that can go wrong IMO.

-- 
Josh


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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2019-10-18 13:03                                     ` Jessica Yu
  2019-10-18 13:40                                       ` Petr Mladek
@ 2019-10-22  8:27                                       ` Miroslav Benes
  2019-10-22 14:31                                         ` Josh Poimboeuf
  1 sibling, 1 reply; 45+ messages in thread
From: Miroslav Benes @ 2019-10-22  8:27 UTC (permalink / raw)
  To: Jessica Yu
  Cc: Peter Zijlstra, Steven Rostedt, Joe Lawrence, x86, linux-kernel,
	mhiramat, bristot, jbaron, torvalds, tglx, mingo, namit, hpa,
	luto, ard.biesheuvel, jpoimboe, live-patching, pmladek

On Fri, 18 Oct 2019, Jessica Yu wrote:

> +++ Miroslav Benes [16/10/19 15:29 +0200]:
> >On Wed, 16 Oct 2019, Miroslav Benes wrote:
> >
> >> On Wed, 16 Oct 2019, Peter Zijlstra wrote:
> >>
> >> > On Tue, Oct 15, 2019 at 06:27:05PM -0400, Steven Rostedt wrote:
> >> >
> >> > > (7) Seventh session, titled "klp-convert and livepatch relocations",
> >> > > was led
> >> > > by Joe Lawrence.
> >> > >
> >> > > Joe started the session with problem statement: accessing non exported
> >> > > / static
> >> > > symbols from inside the patch module. One possible workardound is
> >> > > manually via
> >> > > kallsyms. Second workaround is klp-convert, which actually creates
> >> > > proper
> >> > > relocations inside the livepatch module from the symbol database during
> >> > > the
> >> > > final .ko link.
> >> > > Currently module loader looks for special livepatch relocations and
> >> > > resolves
> >> > > those during runtime; kernel support for these relocations have so far
> >> > > been
> >> > > added for x86 only. Special livepatch relocations are supported and
> >> > > processed
> >> > > also on other architectures. Special quirks/sections are not yet
> >> > > supported.
> >> > > Plus klp-convert would still be needed even with late module patching
> >> > > update.
> >> > > vmlinux or modules could have ambiguous static symbols.
> >> > >
> >> > > It turns out that the features / bugs below have to be resolved before
> >> > > we
> >> > > can claim the klp-convert support for relocation complete:
> >> > >     - handle all the corner cases (jump labels, static keys, ...)
> >> > >     properly and
> >> > >       have a good regression tests in place
> >> >
> >> > I suppose all the patches in this series-of-series here will make life
> >> > harder for KLP, static_call() and 2 byte jumps etc..
> >>
> >> Yes, I think so. We'll have to deal with that once it lands. That is why
> >> we want to get rid of all this arch-specific code in livepatch and
> >> reinvent the late module patching. So it is perhaps better to start
> >> working on it sooner than later. Adding Petr, who hesitantly signed up for
> >> the task...
> >
> >Thinking about it more... crazy idea. I think we could leverage these new
> >ELF .text per vmlinux/module sections for the reinvention I was talking
> >about. If we teach module loader to relocate (and apply alternatives and
> >so on, everything in arch-specific module_finalize()) not the whole module
> >in case of live patch modules, but separate ELF .text sections, it could
> >solve the issue with late module patching we have. It is a variation on
> >Steven's idea. When live patch module is loaded, only its section for
> >present modules would be processed. Then whenever a to-be-patched module
> >is loaded, its .text section in all present patch module would be
> >processed.
> >
> >The upside is that almost no work would be required on patch modules
> >creation side. The downside is that klp_modinfo must stay. Module loader
> >needs to be hacked a lot in both cases. So it remains to be seen which
> >idea is easier to implement.
> >
> >Jessica, do you think it would be feasible?
> 
> I think that does sound feasible. I'm trying to visualize how that
> would look. I guess there would need to be various livepatching hooks
> called during the different stages (apply_relocate_add(),
> module_finalize(), module_enable_ro/x()).
> 
> So maybe something like the following?
> 
> When a livepatch module loads:
>    apply_relocate_add()
>        klp hook: apply .klp.rela.$objname relocations *only* for
>        already loaded modules
>    module_finalize()
>        klp hook: apply .klp.arch.$objname changes for already loaded modules
>    module_enable_ro()
>        klp hook: only enable ro/x for .klp.text.$objname for already
>        loaded modules
> 
> When a to-be-patched module loads:
>    apply_relocate_add()
>        klp hook: for each patch module that patches the coming
>        module, apply .klp.rela.$objname relocations for this object
>    module_finalize()
>        klp hook: for each patch module that patches the coming
>        module, apply .klp.arch.$objname changes for this object
>    module_enable_ro()
>        klp hook: for each patch module, apply ro/x permissions for
>        .klp.text.$objname for this object
> 
> Then, in klp_module_coming, we only need to do the callbacks and
> enable the patch, and get rid of the module_disable_ro->apply
> relocs->module_enable_ro block.
> 
> Does that sound like what you had in mind or am I totally off?

Sort of. What I had in mind was that we could get rid of all special .klp 
ELF section if module loader guarantees that only sections for loaded 
modules are processed. Then .klp.rela.$objname is not needed and proper 
.rela.text.$objname (or whatever its text section is named) should be 
sufficient. The same for the rest (.klp.arch).

Only then it would be useful.

Miroslav

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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2019-10-16 12:39                           ` Peter Zijlstra
@ 2019-10-22  8:45                             ` Miroslav Benes
  0 siblings, 0 replies; 45+ messages in thread
From: Miroslav Benes @ 2019-10-22  8:45 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Joe Lawrence, Jessica Yu, Steven Rostedt, x86, linux-kernel,
	mhiramat, bristot, jbaron, torvalds, tglx, mingo, namit, hpa,
	luto, ard.biesheuvel, jpoimboe, live-patching

On Wed, 16 Oct 2019, Peter Zijlstra wrote:

> On Wed, Oct 16, 2019 at 08:51:27AM +0200, Miroslav Benes wrote:
> > On Tue, 15 Oct 2019, Joe Lawrence wrote:
> > 
> > > On 10/15/19 10:13 AM, Miroslav Benes wrote:
> > > > Yes, it does. klp_module_coming() calls module_disable_ro() on all
> > > > patching modules which patch the coming module in order to call
> > > > apply_relocate_add(). New (patching) code for a module can be relocated
> > > > only when the relevant module is loaded.
> > > 
> > > FWIW, would the LPC blue-sky2 model (ie, Steve's suggestion @ plumber's where
> > > livepatches only patch a single object and updates are kept on disk to handle
> > > coming module updates as they are loaded) eliminate those outstanding
> > > relocations and the need to perform this late permission flipping?
> > 
> > Yes, it should, but we don't have to wait for it. PeterZ proposed a 
> > different solution to this specific issue in 
> > https://lore.kernel.org/lkml/20191015141111.GP2359@hirez.programming.kicks-ass.net/
> > 
> > It should not be a problem to create a live patch module like that and the 
> > code in kernel/livepatch/ is almost ready. Something like 
> > module_section_disable_ro(mod, section) (and similar for X protection) 
> > should be enough. Module reloads would still require juggling with the 
> > protections, but I think it is all feasible.
> 
> Something a little like so.. completely fresh of the keyboard.

Yes, but I noticed you found different and better way through text_poke() 
(I was not aware that text_poke() works around the protections).

Miroslav
 
> ---
> --- a/include/linux/module.h
> +++ b/include/linux/module.h
> @@ -853,6 +853,18 @@ static inline void module_enable_ro(cons
>  static inline void module_disable_ro(const struct module *mod) { }
>  #endif
>  
> +#if defined(CONFIG_STRICT_MODULE_RWX) && defined(CONFIG_LIVEPATCH)
> +extern void module_section_disable_ro(struct module *mod, const char *sec);
> +extern void module_section_enable_ro(struct module *mod, const char *sec);
> +extern void module_section_disable_x(struct module *mod, const char *sec);
> +extern void module_section_enable_x(struct module *mod, const char *sec);
> +#else
> +static inline void module_section_disable_ro(struct module *mod, const char *sec) { }
> +static inline void module_section_enable_ro(struct module *mod, const char *sec) { }
> +static inline void module_section_disable_x(struct module *mod, const char *sec) { }
> +static inline void module_section_enable_x(struct module *mod, const char *sec) { }
> +#endif
> +
>  #ifdef CONFIG_GENERIC_BUG
>  void module_bug_finalize(const Elf_Ehdr *, const Elf_Shdr *,
>  			 struct module *);
> --- a/kernel/module.c
> +++ b/kernel/module.c
> @@ -2107,6 +2107,54 @@ static void free_module_elf(struct modul
>  	kfree(mod->klp_info->secstrings);
>  	kfree(mod->klp_info);
>  }
> +
> +#ifdef CONFIG_STRICT_MODULE_RWX
> +
> +static void __frob_section(struct Elf_Shdr *sec, int (*set_memory)(unsigned long start, int num_pages))
> +{
> +	BUG_ON((unsigned long)sec->sh_addr & (PAGE_SIZE-1));
> +	BUG_ON((unsigned long)sec->sh_size & (PAGE_SIZE-1));
> +	set_memory((unsigned long)sec->sh_addr, sec->sh_size >> PAGE_SHIFT);
> +}
> +
> +static void frob_section(struct module *mod, const char *section,
> +			 int (*set_memory)(unsigned long start, int num_pages))
> +{
> +	struct klp_modinfo *info = mod->klp_info;
> +	const char *secname;
> +	Elf_Shdr *s;
> +
> +	for (s = info->sechdrs; s < info->sechdrs + info->hdr.e_shnum; s++) {
> +		secname = mod->klp_info->secstrings + s->sh_name;
> +		if (strcmp(secname, section))
> +			continue;
> +
> +		__frob_section(s, set_memory);
> +	}
> +}
> +
> +void module_section_disable_ro(struct module *mod, const char *section)
> +{
> +	frob_section(mod, section, set_memory_rw);
> +}
> +
> +void module_section_enable_ro(struct module *mod, const char *section)
> +{
> +	frob_section(mod, section, set_memory_ro);
> +}
> +
> +void module_section_disable_x(struct module *mod, const char *section)
> +{
> +	frob_section(mod, section, set_memory_nx);
> +}
> +
> +void module_section_enable_x(struct module *mod, const char *section)
> +{
> +	frob_section(mod, section, set_memory_x);
> +}
> +
> +#endif /* ONFIG_STRICT_MODULE_RWX */
> +
>  #else /* !CONFIG_LIVEPATCH */
>  static int copy_module_elf(struct module *mod, struct load_info *info)
>  {
> 


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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2019-10-22  8:27                                       ` Miroslav Benes
@ 2019-10-22 14:31                                         ` Josh Poimboeuf
  2019-10-23  9:04                                           ` Miroslav Benes
  0 siblings, 1 reply; 45+ messages in thread
From: Josh Poimboeuf @ 2019-10-22 14:31 UTC (permalink / raw)
  To: Miroslav Benes
  Cc: Jessica Yu, Peter Zijlstra, Steven Rostedt, Joe Lawrence, x86,
	linux-kernel, mhiramat, bristot, jbaron, torvalds, tglx, mingo,
	namit, hpa, luto, ard.biesheuvel, live-patching, pmladek

On Tue, Oct 22, 2019 at 10:27:49AM +0200, Miroslav Benes wrote:
> > Does that sound like what you had in mind or am I totally off?
> 
> Sort of. What I had in mind was that we could get rid of all special .klp 
> ELF section if module loader guarantees that only sections for loaded 
> modules are processed. Then .klp.rela.$objname is not needed and proper 
> .rela.text.$objname (or whatever its text section is named) should be 
> sufficient. The same for the rest (.klp.arch).

If I understand correctly, using kvm as an example to-be-patched module,
we'd have:

  .text.kvm
  .rela.text.kvm
  .altinstructions.kvm
  .rela.altinstructions.kvm
  __jump_table.kvm
  .rela__jump_table.kvm

etc.  i.e. any "special" sections would need to be renamed.

Is that right?

But also I think *any* sections which need relocations would need to be
renamed, for example:

  .rodata.kvm
  .rela.rodata.kvm
  .orc_unwind_ip.kvm
  .rela.orc_unwind_ip.kvm


It's an interesting idea.

We'd have to be careful about ordering issues.  For example, there are
module-specific jump labels stored in mod->jump_entries.  Right now
that's just a pointer to the module's __jump_table section.  With late
module patching, when kvm is loaded we'd have to insert the klp module's
__jump_table.kvm entries into kvm's mod->jump_entries list somehow.

Presumably we'd also have that issue for other sections.  Handling that
_might_ be as simple as just hacking up find_module_sections() to
re-allocate sections and append "patched sections" to them.

But then you still have to worry about when to apply the relocations.
If you apply them before patching the sections, then relative
relocations would have the wrong values.  If you apply them after, then
you have to figure out where the appended relocations are.

And if we allow unpatching then we'd presumably have to be able to
remove entries from the module specific section lists.

So I get the feeling a lot of complexity would creep in.  Even just
thinking about it requires more mental gymnastics than the
one-patch-per-module idea, so I view that as a bad sign.

-- 
Josh


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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2019-10-22 14:31                                         ` Josh Poimboeuf
@ 2019-10-23  9:04                                           ` Miroslav Benes
  0 siblings, 0 replies; 45+ messages in thread
From: Miroslav Benes @ 2019-10-23  9:04 UTC (permalink / raw)
  To: Josh Poimboeuf
  Cc: Jessica Yu, Peter Zijlstra, Steven Rostedt, Joe Lawrence, x86,
	linux-kernel, mhiramat, bristot, jbaron, torvalds, tglx, mingo,
	namit, hpa, luto, ard.biesheuvel, live-patching, pmladek

On Tue, 22 Oct 2019, Josh Poimboeuf wrote:

> On Tue, Oct 22, 2019 at 10:27:49AM +0200, Miroslav Benes wrote:
> > > Does that sound like what you had in mind or am I totally off?
> > 
> > Sort of. What I had in mind was that we could get rid of all special .klp 
> > ELF section if module loader guarantees that only sections for loaded 
> > modules are processed. Then .klp.rela.$objname is not needed and proper 
> > .rela.text.$objname (or whatever its text section is named) should be 
> > sufficient. The same for the rest (.klp.arch).
> 
> If I understand correctly, using kvm as an example to-be-patched module,
> we'd have:
> 
>   .text.kvm
>   .rela.text.kvm
>   .altinstructions.kvm
>   .rela.altinstructions.kvm
>   __jump_table.kvm
>   .rela__jump_table.kvm
> 
> etc.  i.e. any "special" sections would need to be renamed.
> 
> Is that right?

Yes.
 
> But also I think *any* sections which need relocations would need to be
> renamed, for example:
> 
>   .rodata.kvm
>   .rela.rodata.kvm
>   .orc_unwind_ip.kvm
>   .rela.orc_unwind_ip.kvm

Correct.
 
> It's an interesting idea.
> 
> We'd have to be careful about ordering issues.  For example, there are
> module-specific jump labels stored in mod->jump_entries.  Right now
> that's just a pointer to the module's __jump_table section.  With late
> module patching, when kvm is loaded we'd have to insert the klp module's
> __jump_table.kvm entries into kvm's mod->jump_entries list somehow.

Yes.
 
> Presumably we'd also have that issue for other sections.  Handling that
> _might_ be as simple as just hacking up find_module_sections() to
> re-allocate sections and append "patched sections" to them.
>
> But then you still have to worry about when to apply the relocations.
> If you apply them before patching the sections, then relative
> relocations would have the wrong values.  If you apply them after, then
> you have to figure out where the appended relocations are.

Ah, right. That is a valid remark.
 
> And if we allow unpatching then we'd presumably have to be able to
> remove entries from the module specific section lists.

Correct.

> So I get the feeling a lot of complexity would creep in.  Even just
> thinking about it requires more mental gymnastics than the
> one-patch-per-module idea, so I view that as a bad sign.

Yes, the devil is in the details. It would be better if the approach 
helped even someone/something else in the kernel. Without it, it is 
probably better to stick to Steven's proposal and handle the complexity 
elsewhere.

Thanks
Miroslav

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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2019-10-21 15:05                                 ` Josh Poimboeuf
@ 2020-01-20 16:50                                   ` Josh Poimboeuf
  2020-01-21  8:35                                     ` Miroslav Benes
  0 siblings, 1 reply; 45+ messages in thread
From: Josh Poimboeuf @ 2020-01-20 16:50 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Steven Rostedt, Joe Lawrence, Jessica Yu, Miroslav Benes, x86,
	linux-kernel, mhiramat, bristot, jbaron, torvalds, tglx, mingo,
	namit, hpa, luto, ard.biesheuvel, live-patching, Randy Dunlap

On Mon, Oct 21, 2019 at 10:05:49AM -0500, Josh Poimboeuf wrote:
> On Wed, Oct 16, 2019 at 09:42:17AM +0200, Peter Zijlstra wrote:
> > > which are not compatible with livepatching. GCC upstream now has
> > > -flive-patching option, which disables all those interfering optimizations.
> > 
> > Which, IIRC, has a significant performance impact and should thus really
> > not be used...
> > 
> > If distros ship that crap, I'm going to laugh at them the next time they
> > want a single digit performance improvement because *important*.
> 
> I have a crazy plan to try to use objtool to detect function changes at
> a binary level, which would hopefully allow us to drop this flag.
> 
> But regardless, I wonder if we enabled this flag prematurely.  We still
> don't have a reasonable way to use it for creating source-based live
> patches upstream, and it should really be optional for CONFIG_LIVEPATCH,
> since kpatch-build doesn't need it.

I also just discovered that -flive-patching is responsible for all those
"unreachable instruction" objtool warnings which Randy has been
dutifully bugging me about over the last several months.  For some
reason it subtly breaks GCC implicit noreturn detection for local
functions.

At this point, I only see downsides of -flive-patching, at least until
we actually have real upstream code which needs it.

If there aren't any objections I'll be posting a patch soon to revert.

-- 
Josh


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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2020-01-20 16:50                                   ` Josh Poimboeuf
@ 2020-01-21  8:35                                     ` Miroslav Benes
  2020-01-21 16:10                                       ` Josh Poimboeuf
  0 siblings, 1 reply; 45+ messages in thread
From: Miroslav Benes @ 2020-01-21  8:35 UTC (permalink / raw)
  To: Josh Poimboeuf
  Cc: Peter Zijlstra, Steven Rostedt, Joe Lawrence, Jessica Yu, x86,
	linux-kernel, mhiramat, bristot, jbaron, torvalds, tglx, mingo,
	namit, hpa, luto, ard.biesheuvel, live-patching, Randy Dunlap

On Mon, 20 Jan 2020, Josh Poimboeuf wrote:

> On Mon, Oct 21, 2019 at 10:05:49AM -0500, Josh Poimboeuf wrote:
> > On Wed, Oct 16, 2019 at 09:42:17AM +0200, Peter Zijlstra wrote:
> > > > which are not compatible with livepatching. GCC upstream now has
> > > > -flive-patching option, which disables all those interfering optimizations.
> > > 
> > > Which, IIRC, has a significant performance impact and should thus really
> > > not be used...
> > > 
> > > If distros ship that crap, I'm going to laugh at them the next time they
> > > want a single digit performance improvement because *important*.
> > 
> > I have a crazy plan to try to use objtool to detect function changes at
> > a binary level, which would hopefully allow us to drop this flag.
> > 
> > But regardless, I wonder if we enabled this flag prematurely.  We still
> > don't have a reasonable way to use it for creating source-based live
> > patches upstream, and it should really be optional for CONFIG_LIVEPATCH,
> > since kpatch-build doesn't need it.
> 
> I also just discovered that -flive-patching is responsible for all those
> "unreachable instruction" objtool warnings which Randy has been
> dutifully bugging me about over the last several months.  For some
> reason it subtly breaks GCC implicit noreturn detection for local
> functions.

Ugh, that is unfortunate. Have you reported it?
 
> At this point, I only see downsides of -flive-patching, at least until
> we actually have real upstream code which needs it.

Can you explain this? The option makes GCC to avoid optimizations which 
are difficult to detect and would make live patching unsafe. I consider it 
useful as it is, so if you shared the other downsides and what you meant 
by real upstream code, we could discuss it.

> If there aren't any objections I'll be posting a patch soon to revert.

I think it would be a setback.

Regards
Miroslav

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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2020-01-21  8:35                                     ` Miroslav Benes
@ 2020-01-21 16:10                                       ` Josh Poimboeuf
  2020-01-22 10:09                                         ` Miroslav Benes
  2020-01-22 12:15                                         ` Miroslav Benes
  0 siblings, 2 replies; 45+ messages in thread
From: Josh Poimboeuf @ 2020-01-21 16:10 UTC (permalink / raw)
  To: Miroslav Benes
  Cc: Peter Zijlstra, Steven Rostedt, Joe Lawrence, Jessica Yu, x86,
	linux-kernel, mhiramat, bristot, jbaron, torvalds, tglx, mingo,
	namit, hpa, luto, ard.biesheuvel, live-patching, Randy Dunlap

On Tue, Jan 21, 2020 at 09:35:28AM +0100, Miroslav Benes wrote:
> On Mon, 20 Jan 2020, Josh Poimboeuf wrote:
> 
> > On Mon, Oct 21, 2019 at 10:05:49AM -0500, Josh Poimboeuf wrote:
> > > On Wed, Oct 16, 2019 at 09:42:17AM +0200, Peter Zijlstra wrote:
> > > > > which are not compatible with livepatching. GCC upstream now has
> > > > > -flive-patching option, which disables all those interfering optimizations.
> > > > 
> > > > Which, IIRC, has a significant performance impact and should thus really
> > > > not be used...
> > > > 
> > > > If distros ship that crap, I'm going to laugh at them the next time they
> > > > want a single digit performance improvement because *important*.
> > > 
> > > I have a crazy plan to try to use objtool to detect function changes at
> > > a binary level, which would hopefully allow us to drop this flag.
> > > 
> > > But regardless, I wonder if we enabled this flag prematurely.  We still
> > > don't have a reasonable way to use it for creating source-based live
> > > patches upstream, and it should really be optional for CONFIG_LIVEPATCH,
> > > since kpatch-build doesn't need it.
> > 
> > I also just discovered that -flive-patching is responsible for all those
> > "unreachable instruction" objtool warnings which Randy has been
> > dutifully bugging me about over the last several months.  For some
> > reason it subtly breaks GCC implicit noreturn detection for local
> > functions.
> 
> Ugh, that is unfortunate. Have you reported it?

Not yet (but I plan to).

> > At this point, I only see downsides of -flive-patching, at least until
> > we actually have real upstream code which needs it.
> 
> Can you explain this? The option makes GCC to avoid optimizations which 
> are difficult to detect and would make live patching unsafe. I consider it 
> useful as it is, so if you shared the other downsides and what you meant 
> by real upstream code, we could discuss it.

Only SLES needs it right?  Why inflict it on other livepatch users?  By
"real upstream code" I mean there's no (documented) way to create live
patches using the method which relies on this flag.  So I don't see any
upstream benefits for having it enabled.

-- 
Josh


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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2020-01-21 16:10                                       ` Josh Poimboeuf
@ 2020-01-22 10:09                                         ` Miroslav Benes
  2020-01-22 21:42                                           ` Josh Poimboeuf
  2020-01-22 12:15                                         ` Miroslav Benes
  1 sibling, 1 reply; 45+ messages in thread
From: Miroslav Benes @ 2020-01-22 10:09 UTC (permalink / raw)
  To: Josh Poimboeuf
  Cc: Peter Zijlstra, Steven Rostedt, Joe Lawrence, Jessica Yu, x86,
	linux-kernel, mhiramat, bristot, jbaron, torvalds, tglx, mingo,
	namit, hpa, luto, ard.biesheuvel, live-patching, Randy Dunlap


> > > At this point, I only see downsides of -flive-patching, at least until
> > > we actually have real upstream code which needs it.
> > 
> > Can you explain this? The option makes GCC to avoid optimizations which 
> > are difficult to detect and would make live patching unsafe. I consider it 
> > useful as it is, so if you shared the other downsides and what you meant 
> > by real upstream code, we could discuss it.
> 
> Only SLES needs it right?  Why inflict it on other livepatch users?  By
> "real upstream code" I mean there's no (documented) way to create live
> patches using the method which relies on this flag.  So I don't see any
> upstream benefits for having it enabled.

I'd put it differently. SLES and upstream need it, RHEL does not need it. 
Or anyone using kpatch-build. It is perfectly fine to prepare live patches 
just from the source code using upstream live patching infrastructure. 
After all, SLES is nothing else than upstream here. We were creating live 
patches manually for quite a long time and only recently we have been 
using Nicolai's klp-ccp automation (https://github.com/SUSE/klp-ccp).

So, everyone using upstream directly relies on the flag, which seems to be 
a clear benefit to me. Reverting the patch would be a step back.

Also I think we're moving in the right direction to make the life of 
upstream user easier with a proposal of klp-ccp and Petr's patch set to 
split live patch modules. It is a path from inconvenient to comfortable 
and not from impossible to possible.

Miroslav

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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2020-01-21 16:10                                       ` Josh Poimboeuf
  2020-01-22 10:09                                         ` Miroslav Benes
@ 2020-01-22 12:15                                         ` Miroslav Benes
  2020-01-22 15:05                                           ` Miroslav Benes
  1 sibling, 1 reply; 45+ messages in thread
From: Miroslav Benes @ 2020-01-22 12:15 UTC (permalink / raw)
  To: Josh Poimboeuf
  Cc: Peter Zijlstra, Steven Rostedt, Joe Lawrence, Jessica Yu, x86,
	linux-kernel, mhiramat, bristot, jbaron, torvalds, tglx, mingo,
	namit, hpa, luto, ard.biesheuvel, live-patching, Randy Dunlap

On Tue, 21 Jan 2020, Josh Poimboeuf wrote:

> On Tue, Jan 21, 2020 at 09:35:28AM +0100, Miroslav Benes wrote:
> > On Mon, 20 Jan 2020, Josh Poimboeuf wrote:
> > 
> > > On Mon, Oct 21, 2019 at 10:05:49AM -0500, Josh Poimboeuf wrote:
> > > > On Wed, Oct 16, 2019 at 09:42:17AM +0200, Peter Zijlstra wrote:
> > > > > > which are not compatible with livepatching. GCC upstream now has
> > > > > > -flive-patching option, which disables all those interfering optimizations.
> > > > > 
> > > > > Which, IIRC, has a significant performance impact and should thus really
> > > > > not be used...
> > > > > 
> > > > > If distros ship that crap, I'm going to laugh at them the next time they
> > > > > want a single digit performance improvement because *important*.
> > > > 
> > > > I have a crazy plan to try to use objtool to detect function changes at
> > > > a binary level, which would hopefully allow us to drop this flag.
> > > > 
> > > > But regardless, I wonder if we enabled this flag prematurely.  We still
> > > > don't have a reasonable way to use it for creating source-based live
> > > > patches upstream, and it should really be optional for CONFIG_LIVEPATCH,
> > > > since kpatch-build doesn't need it.
> > > 
> > > I also just discovered that -flive-patching is responsible for all those
> > > "unreachable instruction" objtool warnings which Randy has been
> > > dutifully bugging me about over the last several months.  For some
> > > reason it subtly breaks GCC implicit noreturn detection for local
> > > functions.
> > 
> > Ugh, that is unfortunate. Have you reported it?
> 
> Not yet (but I plan to).

My findings so far...

I bisected through GCC options which -flive-patching disables and 
-fno-ipa-pure-const is the culprit. I got no warnings without the option 
with my config.

Then I found out allmodconfig was ok even with -flive-patching. 
CONFIG_GCOV is the difference. CONFIG_GCOV=y seems to make the warnings go 
away here.

/me goes staring

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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2020-01-22 12:15                                         ` Miroslav Benes
@ 2020-01-22 15:05                                           ` Miroslav Benes
  2020-01-22 22:03                                             ` Josh Poimboeuf
  0 siblings, 1 reply; 45+ messages in thread
From: Miroslav Benes @ 2020-01-22 15:05 UTC (permalink / raw)
  To: Josh Poimboeuf
  Cc: Peter Zijlstra, Steven Rostedt, Joe Lawrence, Jessica Yu, x86,
	linux-kernel, mhiramat, bristot, jbaron, torvalds, tglx, mingo,
	namit, hpa, luto, ard.biesheuvel, live-patching, Randy Dunlap,
	mjambor

On Wed, 22 Jan 2020, Miroslav Benes wrote:

> On Tue, 21 Jan 2020, Josh Poimboeuf wrote:
> 
> > On Tue, Jan 21, 2020 at 09:35:28AM +0100, Miroslav Benes wrote:
> > > On Mon, 20 Jan 2020, Josh Poimboeuf wrote:
> > > 
> > > > On Mon, Oct 21, 2019 at 10:05:49AM -0500, Josh Poimboeuf wrote:
> > > > > On Wed, Oct 16, 2019 at 09:42:17AM +0200, Peter Zijlstra wrote:
> > > > > > > which are not compatible with livepatching. GCC upstream now has
> > > > > > > -flive-patching option, which disables all those interfering optimizations.
> > > > > > 
> > > > > > Which, IIRC, has a significant performance impact and should thus really
> > > > > > not be used...
> > > > > > 
> > > > > > If distros ship that crap, I'm going to laugh at them the next time they
> > > > > > want a single digit performance improvement because *important*.
> > > > > 
> > > > > I have a crazy plan to try to use objtool to detect function changes at
> > > > > a binary level, which would hopefully allow us to drop this flag.
> > > > > 
> > > > > But regardless, I wonder if we enabled this flag prematurely.  We still
> > > > > don't have a reasonable way to use it for creating source-based live
> > > > > patches upstream, and it should really be optional for CONFIG_LIVEPATCH,
> > > > > since kpatch-build doesn't need it.
> > > > 
> > > > I also just discovered that -flive-patching is responsible for all those
> > > > "unreachable instruction" objtool warnings which Randy has been
> > > > dutifully bugging me about over the last several months.  For some
> > > > reason it subtly breaks GCC implicit noreturn detection for local
> > > > functions.
> > > 
> > > Ugh, that is unfortunate. Have you reported it?
> > 
> > Not yet (but I plan to).
> 
> My findings so far...
> 
> I bisected through GCC options which -flive-patching disables and 
> -fno-ipa-pure-const is the culprit. I got no warnings without the option 
> with my config.
> 
> Then I found out allmodconfig was ok even with -flive-patching. 
> CONFIG_GCOV is the difference. CONFIG_GCOV=y seems to make the warnings go 
> away here.

Sorry, that was a red herring. See 867ac9d73709 ("objtool: Fix gcov check 
for older versions of GCC").

I started looking at some btrfs reports and then found out those were 
already fixed. 
https://lore.kernel.org/linux-btrfs/cd4091e4-1c04-a880-f239-00bc053f46a2@infradead.org/

arch/x86/kernel/cpu/mce/core.o: warning: objtool: mce_panic()+0x11b: unreachable instruction
was next...

Broken code (-fno-ipa-pure-const):
...
    1186:       e8 a5 fe ff ff          callq  1030 <wait_for_panic>
    118b:       e9 23 ff ff ff          jmpq   10b3 <mce_panic+0x43>
</end of function>

Working code (-fipa-pure-const):
     753:       e8 88 fe ff ff          callq  5e0 <wait_for_panic>
     758:       0f 1f 84 00 00 00 00    nopl   0x0(%rax,%rax,1)
     75f:       00 

mce_panic() has:
                if (atomic_inc_return(&mce_panicked) > 1)                                                              
                        wait_for_panic();
                barrier();
                
                bust_spinlocks(1);                                                                                     

jmpq in the broken code goes to bust_spinlocks(1), because GCC does not 
know that wait_for_panic() is noreturn... because it is not. 
wait_for_panic() calls panic() unconditionally in the end, which is 
noreturn.

So the question is why ipa-pure-const optimization knows about panic()'s 
noreturn. The answer is that it is right one of the things the 
optimization does. It propagates inner noreturns to its callers. (Martin 
Jambor CCed).

Marking wait_for_panic() as noreturn (__noreturn), of course, fixes it 
then. Now I don't know what the right fix should be. Should we mark all 
these sites as noreturn, or is it ok for the kernel to rely on GCC 
behaviour in this case? Could we teach objtool to recognize this?

Miroslav

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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2020-01-22 10:09                                         ` Miroslav Benes
@ 2020-01-22 21:42                                           ` Josh Poimboeuf
  2020-01-28  9:28                                             ` Miroslav Benes
  0 siblings, 1 reply; 45+ messages in thread
From: Josh Poimboeuf @ 2020-01-22 21:42 UTC (permalink / raw)
  To: Miroslav Benes
  Cc: Peter Zijlstra, Steven Rostedt, Joe Lawrence, Jessica Yu, x86,
	linux-kernel, mhiramat, bristot, jbaron, torvalds, tglx, mingo,
	namit, hpa, luto, ard.biesheuvel, live-patching, Randy Dunlap

On Wed, Jan 22, 2020 at 11:09:56AM +0100, Miroslav Benes wrote:
> 
> > > > At this point, I only see downsides of -flive-patching, at least until
> > > > we actually have real upstream code which needs it.
> > > 
> > > Can you explain this? The option makes GCC to avoid optimizations which 
> > > are difficult to detect and would make live patching unsafe. I consider it 
> > > useful as it is, so if you shared the other downsides and what you meant 
> > > by real upstream code, we could discuss it.
> > 
> > Only SLES needs it right?  Why inflict it on other livepatch users?  By
> > "real upstream code" I mean there's no (documented) way to create live
> > patches using the method which relies on this flag.  So I don't see any
> > upstream benefits for having it enabled.
> 
> I'd put it differently. SLES and upstream need it, RHEL does not need it. 
> Or anyone using kpatch-build.

I'm confused about why you think upstream needs it.

Is all the tooling available somewhere?  Is there documentation
available which describes how to build patches using that method from
start to finish?  Are there actual users other than SUSE?

BTW, kpatch-build has a *lot* of users other than RHEL.  All its tooling
and documentation are available on Github.

> It is perfectly fine to prepare live patches just from the source code
> using upstream live patching infrastructure. 

Do you mean the dangerous method used by the livepatch sample code which
completely ignores interprocedural optimizations?  I wouldn't call that
perfectly fine.

> After all, SLES is nothing else than upstream here. We were creating live 
> patches manually for quite a long time and only recently we have been 
> using Nicolai's klp-ccp automation (https://github.com/SUSE/klp-ccp).
> 
> So, everyone using upstream directly relies on the flag, which seems to be 
> a clear benefit to me. Reverting the patch would be a step back.

Who exactly is "everyone using upstream"?

From what I can tell, kpatch-build is the only known way (to those
outside of SUSE) to make safe patches for an upstream kernel.  And it
doesn't need this flag and the problems associated with it: performance,
LTO incompatibility, clang incompatibility (I think?), the GCC dead code
issue.

-- 
Josh


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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2020-01-22 15:05                                           ` Miroslav Benes
@ 2020-01-22 22:03                                             ` Josh Poimboeuf
  2020-01-23 10:19                                               ` Martin Jambor
  0 siblings, 1 reply; 45+ messages in thread
From: Josh Poimboeuf @ 2020-01-22 22:03 UTC (permalink / raw)
  To: Miroslav Benes
  Cc: Peter Zijlstra, Steven Rostedt, Joe Lawrence, Jessica Yu, x86,
	linux-kernel, mhiramat, bristot, jbaron, torvalds, tglx, mingo,
	namit, hpa, luto, ard.biesheuvel, live-patching, Randy Dunlap,
	mjambor

On Wed, Jan 22, 2020 at 04:05:27PM +0100, Miroslav Benes wrote:
> I started looking at some btrfs reports and then found out those were 
> already fixed. 
> https://lore.kernel.org/linux-btrfs/cd4091e4-1c04-a880-f239-00bc053f46a2@infradead.org/
> 
> arch/x86/kernel/cpu/mce/core.o: warning: objtool: mce_panic()+0x11b: unreachable instruction
> was next...
> 
> Broken code (-fno-ipa-pure-const):
> ...
>     1186:       e8 a5 fe ff ff          callq  1030 <wait_for_panic>
>     118b:       e9 23 ff ff ff          jmpq   10b3 <mce_panic+0x43>
> </end of function>
> 
> Working code (-fipa-pure-const):
>      753:       e8 88 fe ff ff          callq  5e0 <wait_for_panic>
>      758:       0f 1f 84 00 00 00 00    nopl   0x0(%rax,%rax,1)
>      75f:       00 
> 
> mce_panic() has:
>                 if (atomic_inc_return(&mce_panicked) > 1)                                                              
>                         wait_for_panic();
>                 barrier();
>                 
>                 bust_spinlocks(1);                                                                                     
> 
> jmpq in the broken code goes to bust_spinlocks(1), because GCC does not 
> know that wait_for_panic() is noreturn... because it is not. 
> wait_for_panic() calls panic() unconditionally in the end, which is 
> noreturn.
> 
> So the question is why ipa-pure-const optimization knows about panic()'s 
> noreturn. The answer is that it is right one of the things the 
> optimization does. It propagates inner noreturns to its callers. (Martin 
> Jambor CCed).
> 
> Marking wait_for_panic() as noreturn (__noreturn), of course, fixes it 
> then. Now I don't know what the right fix should be. Should we mark all 
> these sites as noreturn, or is it ok for the kernel to rely on GCC 
> behaviour in this case? Could we teach objtool to recognize this?

Thanks for looking at it.  I cam to a similar conclusion and I already
had the manual noreturns added (see patch below) before I realized that
-flive-patching was the culprit.

The patch works, but the problem is that more warnings will pop up in
the future and it'll be my job to fix them...

Global noreturns are already a pain today.  There's no way for objtool
to know whether GCC considered a function to be noreturn, we have
already have to keep a hard-coded list of global noreturns in objtool.
It's been a constant source of annoyance and this will add to that.


diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c
index 8d91b0428af1..8a8696b32120 100644
--- a/drivers/gpu/drm/ttm/ttm_bo.c
+++ b/drivers/gpu/drm/ttm/ttm_bo.c
@@ -192,7 +192,7 @@ static void ttm_bo_add_mem_to_lru(struct ttm_buffer_object *bo,
 	}
 }
 
-static void ttm_bo_ref_bug(struct kref *list_kref)
+static void __noreturn ttm_bo_ref_bug(struct kref *list_kref)
 {
 	BUG();
 }
diff --git a/drivers/message/fusion/mptbase.h b/drivers/message/fusion/mptbase.h
index 813d46311f6a..2932ecef4dcf 100644
--- a/drivers/message/fusion/mptbase.h
+++ b/drivers/message/fusion/mptbase.h
@@ -945,7 +945,7 @@ extern int	mpt_raid_phys_disk_get_num_paths(MPT_ADAPTER *ioc,
 		u8 phys_disk_num);
 extern int	 mpt_set_taskmgmt_in_progress_flag(MPT_ADAPTER *ioc);
 extern void	 mpt_clear_taskmgmt_in_progress_flag(MPT_ADAPTER *ioc);
-extern void     mpt_halt_firmware(MPT_ADAPTER *ioc);
+extern void     mpt_halt_firmware(MPT_ADAPTER *ioc) __noreturn;
 
 
 /*
diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index eb8bd0258360..4db39fef3b56 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -655,7 +655,7 @@ alloc_extent_state_atomic(struct extent_state *prealloc)
 	return prealloc;
 }
 
-static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
+static void __noreturn extent_io_tree_panic(struct extent_io_tree *tree, int err)
 {
 	struct inode *inode = tree->private_data;
 
diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
index d897a8e5e430..b7a94b1739ae 100644
--- a/fs/btrfs/relocation.c
+++ b/fs/btrfs/relocation.c
@@ -321,7 +321,7 @@ static struct rb_node *tree_search(struct rb_root *root, u64 bytenr)
 	return NULL;
 }
 
-static void backref_tree_panic(struct rb_node *rb_node, int errno, u64 bytenr)
+static void __noreturn backref_tree_panic(struct rb_node *rb_node, int errno, u64 bytenr)
 {
 
 	struct btrfs_fs_info *fs_info = NULL;
diff --git a/include/linux/cred.h b/include/linux/cred.h
index 18639c069263..3ee230a3dee2 100644
--- a/include/linux/cred.h
+++ b/include/linux/cred.h
@@ -175,7 +175,7 @@ extern void __init cred_init(void);
  * check for validity of credentials
  */
 #ifdef CONFIG_DEBUG_CREDENTIALS
-extern void __invalid_creds(const struct cred *, const char *, unsigned);
+extern void __noreturn __invalid_creds(const struct cred *, const char *, unsigned);
 extern void __validate_process_creds(struct task_struct *,
 				     const char *, unsigned);
 
diff --git a/include/linux/sched/task.h b/include/linux/sched/task.h
index f1879884238e..44ca6000b5f1 100644
--- a/include/linux/sched/task.h
+++ b/include/linux/sched/task.h
@@ -86,7 +86,7 @@ static inline void exit_thread(struct task_struct *tsk)
 {
 }
 #endif
-extern void do_group_exit(int);
+extern void __noreturn do_group_exit(int);
 
 extern void exit_files(struct task_struct *);
 extern void exit_itimers(struct signal_struct *);
diff --git a/net/core/skbuff.c b/net/core/skbuff.c
index 973a71f4bc89..29024c578997 100644
--- a/net/core/skbuff.c
+++ b/net/core/skbuff.c
@@ -98,8 +98,8 @@ EXPORT_SYMBOL(sysctl_max_skb_frags);
  *	Keep out of line to prevent kernel bloat.
  *	__builtin_return_address is not used because it is not always reliable.
  */
-static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
-		      const char msg[])
+static void __noreturn
+skb_panic(struct sk_buff *skb, unsigned int sz, void *addr, const char msg[])
 {
 	pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
 		 msg, addr, skb->len, sz, skb->head, skb->data,
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index b6da413bcbd6..ac8807732b10 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -145,6 +145,9 @@ static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
 		"machine_real_restart",
 		"rewind_stack_do_exit",
 		"kunit_try_catch_throw",
+		"__invalid_creds",
+		"do_group_exit",
+		"mpt_halt_firmware",
 	};
 
 	if (!func)


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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2020-01-22 22:03                                             ` Josh Poimboeuf
@ 2020-01-23 10:19                                               ` Martin Jambor
  0 siblings, 0 replies; 45+ messages in thread
From: Martin Jambor @ 2020-01-23 10:19 UTC (permalink / raw)
  To: Josh Poimboeuf, Miroslav Benes
  Cc: Peter Zijlstra, Steven Rostedt, Joe Lawrence, Jessica Yu, x86,
	linux-kernel, mhiramat, bristot, jbaron, torvalds, tglx, mingo,
	namit, hpa, luto, ard.biesheuvel, live-patching, Randy Dunlap

Hello,

On Wed, Jan 22 2020, Josh Poimboeuf wrote:
> Global noreturns are already a pain today.  There's no way for objtool
> to know whether GCC considered a function to be noreturn,

You should be able to get a good idea with -Wsuggest-attribute=noreturn:

$ cat a.c
int __attribute__((noreturn)) my_abort (void)
{
  __builtin_abort ();
}

int foo (void)
{
  return my_abort ();
}

int bar (int flag)
{
  if (flag)
    foo ();
  return 4;
}

$ gcc -S -O2 -Wsuggest-attribute=noreturn a.c 
a.c: In function ‘foo’:
a.c:6:5: warning: function might be candidate for attribute ‘noreturn’ [-Wsuggest-attribute=noreturn]
    6 | int foo (void)
      |     ^~~

GCC 9 and newer even have -fdiagnostics-format=json if you are into that
kind of thing.

Hope this helps a little,

Martin


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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2020-01-22 21:42                                           ` Josh Poimboeuf
@ 2020-01-28  9:28                                             ` Miroslav Benes
  2020-01-28 15:00                                               ` Josh Poimboeuf
  0 siblings, 1 reply; 45+ messages in thread
From: Miroslav Benes @ 2020-01-28  9:28 UTC (permalink / raw)
  To: Josh Poimboeuf
  Cc: Peter Zijlstra, Steven Rostedt, Joe Lawrence, Jessica Yu, x86,
	linux-kernel, mhiramat, bristot, jbaron, torvalds, tglx, mingo,
	namit, hpa, luto, ard.biesheuvel, live-patching, Randy Dunlap

On Wed, 22 Jan 2020, Josh Poimboeuf wrote:

> On Wed, Jan 22, 2020 at 11:09:56AM +0100, Miroslav Benes wrote:
> > 
> > > > > At this point, I only see downsides of -flive-patching, at least until
> > > > > we actually have real upstream code which needs it.
> > > > 
> > > > Can you explain this? The option makes GCC to avoid optimizations which 
> > > > are difficult to detect and would make live patching unsafe. I consider it 
> > > > useful as it is, so if you shared the other downsides and what you meant 
> > > > by real upstream code, we could discuss it.
> > > 
> > > Only SLES needs it right?  Why inflict it on other livepatch users?  By
> > > "real upstream code" I mean there's no (documented) way to create live
> > > patches using the method which relies on this flag.  So I don't see any
> > > upstream benefits for having it enabled.
> > 
> > I'd put it differently. SLES and upstream need it, RHEL does not need it. 
> > Or anyone using kpatch-build.
> 
> I'm confused about why you think upstream needs it.
> 
> Is all the tooling available somewhere?  Is there documentation
> available which describes how to build patches using that method from
> start to finish?  Are there actual users other than SUSE?
> 
> BTW, kpatch-build has a *lot* of users other than RHEL.  All its tooling
> and documentation are available on Github.
> 
> > It is perfectly fine to prepare live patches just from the source code
> > using upstream live patching infrastructure. 
> 
> Do you mean the dangerous method used by the livepatch sample code which
> completely ignores interprocedural optimizations?  I wouldn't call that
> perfectly fine.
> 
> > After all, SLES is nothing else than upstream here. We were creating live 
> > patches manually for quite a long time and only recently we have been 
> > using Nicolai's klp-ccp automation (https://github.com/SUSE/klp-ccp).
> > 
> > So, everyone using upstream directly relies on the flag, which seems to be 
> > a clear benefit to me. Reverting the patch would be a step back.
> 
> Who exactly is "everyone using upstream"?
> 
> >From what I can tell, kpatch-build is the only known way (to those
> outside of SUSE) to make safe patches for an upstream kernel.  And it
> doesn't need this flag and the problems associated with it: performance,
> LTO incompatibility, clang incompatibility (I think?), the GCC dead code
> issue.

I don't think we have something special at SUSE not generally available...

...and I don't think it is really important to discuss that and replying 
to the above, because there is a legitimate use case which relies on the 
flag. We decided to support different use cases right at the beginning.

I understand it currently complicates things for objtool, but objtool is 
sensitive to GCC code generation by definition. "Issues" appear with every 
new GCC version. I see no difference here and luckily it is not so 
difficult to fix it.

I am happy to help with acting on those objtool warning reports you 
mentioned in the other email. Just Cc me where appropriate. We will take a 
look.

Regards
Miroslav

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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2020-01-28  9:28                                             ` Miroslav Benes
@ 2020-01-28 15:00                                               ` Josh Poimboeuf
  2020-01-28 15:40                                                 ` Petr Mladek
  0 siblings, 1 reply; 45+ messages in thread
From: Josh Poimboeuf @ 2020-01-28 15:00 UTC (permalink / raw)
  To: Miroslav Benes
  Cc: Peter Zijlstra, Steven Rostedt, Joe Lawrence, Jessica Yu, x86,
	linux-kernel, mhiramat, bristot, jbaron, torvalds, tglx, mingo,
	namit, hpa, luto, ard.biesheuvel, live-patching, Randy Dunlap

On Tue, Jan 28, 2020 at 10:28:07AM +0100, Miroslav Benes wrote:
> I don't think we have something special at SUSE not generally available...
> 
> ...and I don't think it is really important to discuss that and replying 
> to the above, because there is a legitimate use case which relies on the 
> flag. We decided to support different use cases right at the beginning.
> 
> I understand it currently complicates things for objtool, but objtool is 
> sensitive to GCC code generation by definition. "Issues" appear with every 
> new GCC version. I see no difference here and luckily it is not so 
> difficult to fix it.
> 
> I am happy to help with acting on those objtool warning reports you 
> mentioned in the other email. Just Cc me where appropriate. We will take a 
> look.

As I said, the objtool warnings aren't even the main issue.

There are N users[*] of CONFIG_LIVEPATCH, where N is perhaps dozens.
For N-1 users, they have to suffer ALL the drawbacks, with NONE of the
benefits.

And, even if they wanted those benefits, they have no idea how to get
them because the patch creation process isn't documented.

And, there's no direct upstream usage of the flag, i.e. the only user
does so in a distro which can easily modify KCFLAGS in the spec file.

As best as I can tell, these are facts, which you seem to keep glossing
over.  Did I get any of the facts wrong?


[*] The term 'user' describes the creator/distributor of the
    live patches.

-- 
Josh


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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2020-01-28 15:00                                               ` Josh Poimboeuf
@ 2020-01-28 15:40                                                 ` Petr Mladek
  2020-01-28 17:02                                                   ` Josh Poimboeuf
  0 siblings, 1 reply; 45+ messages in thread
From: Petr Mladek @ 2020-01-28 15:40 UTC (permalink / raw)
  To: Josh Poimboeuf
  Cc: Miroslav Benes, Peter Zijlstra, Steven Rostedt, Joe Lawrence,
	Jessica Yu, x86, linux-kernel, mhiramat, bristot, jbaron,
	torvalds, tglx, mingo, namit, hpa, luto, ard.biesheuvel,
	live-patching, Randy Dunlap

On Tue 2020-01-28 09:00:14, Josh Poimboeuf wrote:
> On Tue, Jan 28, 2020 at 10:28:07AM +0100, Miroslav Benes wrote:
> > I don't think we have something special at SUSE not generally available...
> > 
> > ...and I don't think it is really important to discuss that and replying 
> > to the above, because there is a legitimate use case which relies on the 
> > flag. We decided to support different use cases right at the beginning.
> > 
> > I understand it currently complicates things for objtool, but objtool is 
> > sensitive to GCC code generation by definition. "Issues" appear with every 
> > new GCC version. I see no difference here and luckily it is not so 
> > difficult to fix it.
> > 
> > I am happy to help with acting on those objtool warning reports you 
> > mentioned in the other email. Just Cc me where appropriate. We will take a 
> > look.
> 
> As I said, the objtool warnings aren't even the main issue.

Great.

Anyway, I think that we might make your life easier with using
the proposed -Wsuggest-attribute=noreturn.

Also it might be possible to create the list of global
noreturn functions using some gcc tool. Similar way that we get
the list of functions that need to be livepatched explicitly
because of the problematic optimizations.

It sounds like a win-win approach.


> There are N users[*] of CONFIG_LIVEPATCH, where N is perhaps dozens.
> For N-1 users, they have to suffer ALL the drawbacks, with NONE of the
> benefits.

You wrote in the other mail:

  > The problems associated with it: performance, LTO incompatibility,
  > clang incompatibility (I think?), the GCC dead code issue.

SUSE performance team did extensive testing and did not found
any real performance issues. It was discussed when the option
was enabled upstream.

Are the other problems affecting real life usage, please?
Could you be more specific about them, please?


> And, even if they wanted those benefits, they have no idea how to get
> them because the patch creation process isn't documented.

I do not understand this. All the sample modules and selftests are
using source based livepatches. It is actually the only somehow
documented way. Sure, the documentation might get improved.
Patches are welcome.

The option is not currently needed by the selftests only because
there is no selftest for this type of problems. But the problems
are real. They would actually deserve selftests. Again, patches
are welcome.

My understanding is that the source based livepatches is the future.
N-1 users are just waiting until the 1 user develops more helper
tools for this. I would really like to hear about some serious problems
before we do this step back in upstream.

Best Regards,
Petr

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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2020-01-28 15:40                                                 ` Petr Mladek
@ 2020-01-28 17:02                                                   ` Josh Poimboeuf
  2020-01-29  0:46                                                     ` Jiri Kosina
  2020-01-29 12:28                                                     ` Miroslav Benes
  0 siblings, 2 replies; 45+ messages in thread
From: Josh Poimboeuf @ 2020-01-28 17:02 UTC (permalink / raw)
  To: Petr Mladek
  Cc: Miroslav Benes, Peter Zijlstra, Steven Rostedt, Joe Lawrence,
	Jessica Yu, x86, linux-kernel, mhiramat, bristot, jbaron,
	torvalds, tglx, mingo, namit, hpa, luto, ard.biesheuvel,
	live-patching, Randy Dunlap

On Tue, Jan 28, 2020 at 04:40:46PM +0100, Petr Mladek wrote:
> On Tue 2020-01-28 09:00:14, Josh Poimboeuf wrote:
> > On Tue, Jan 28, 2020 at 10:28:07AM +0100, Miroslav Benes wrote:
> > > I don't think we have something special at SUSE not generally available...
> > > 
> > > ...and I don't think it is really important to discuss that and replying 
> > > to the above, because there is a legitimate use case which relies on the 
> > > flag. We decided to support different use cases right at the beginning.
> > > 
> > > I understand it currently complicates things for objtool, but objtool is 
> > > sensitive to GCC code generation by definition. "Issues" appear with every 
> > > new GCC version. I see no difference here and luckily it is not so 
> > > difficult to fix it.
> > > 
> > > I am happy to help with acting on those objtool warning reports you 
> > > mentioned in the other email. Just Cc me where appropriate. We will take a 
> > > look.
> > 
> > As I said, the objtool warnings aren't even the main issue.
> 
> Great.
> 
> Anyway, I think that we might make your life easier with using
> the proposed -Wsuggest-attribute=noreturn.

Maybe.  Though if I understand correctly, this doesn't help for any of
the new warnings because they're for static functions, and this only
warns about global functions.

> Also it might be possible to create the list of global
> noreturn functions using some gcc tool. Similar way that we get
> the list of functions that need to be livepatched explicitly
> because of the problematic optimizations.
> 
> It sounds like a win-win approach.

I don't quite get how that could be done in an automated way, but ideas
about how to implement it would certainly be welcome.

> > There are N users[*] of CONFIG_LIVEPATCH, where N is perhaps dozens.
> > For N-1 users, they have to suffer ALL the drawbacks, with NONE of the
> > benefits.
> 
> You wrote in the other mail:
> 
>   > The problems associated with it: performance, LTO incompatibility,
>   > clang incompatibility (I think?), the GCC dead code issue.
> 
> SUSE performance team did extensive testing and did not found
> any real performance issues. It was discussed when the option
> was enabled upstream.
> 
> Are the other problems affecting real life usage, please?
> Could you be more specific about them, please?

The original commit mentioned 1-3% scheduler degradation.  And I'd
expect things to worsen over time as interprocedural optimizations
improve.

Also, LTO is coming whether we like it or not.  As is Clang.  Those are
real-world things which will need to work with livepatching sooner or
later.

> > And, even if they wanted those benefits, they have no idea how to get
> > them because the patch creation process isn't documented.
> 
> I do not understand this. All the sample modules and selftests are
> using source based livepatches.

We're talking in circles.  Have you read the thread?

The samples are a (dangerous) joke.  With or without -flive-patching.

> It is actually the only somehow documented way. Sure, the
> documentation might get improved.  Patches are welcome.

Are you suggesting for *me* to send documentation for how *you* build
patches?

> The option is not currently needed by the selftests only because there
> is no selftest for this type of problems. But the problems are real.
> They would actually deserve selftests. Again, patches are welcome.
> 
> My understanding is that the source based livepatches is the future.

I think that still remains to be seen.

> N-1 users are just waiting until the 1 user develops more helper tools
> for this.

No.  N-1 users have no idea how to make (safe) source-based patches in
the first place.  And if *you* don't need the tools, why would anyone
else?  Why not document the process and encourage the existence of other
users so they can get involved and help with the tooling?

> I would really like to hear about some serious problems
> before we do this step back in upstream.

Sometimes you need to take 1 step back before you can take 2 steps
forward.  I regret ACKing the original patch.  It was too early.

-- 
Josh


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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2020-01-28 17:02                                                   ` Josh Poimboeuf
@ 2020-01-29  0:46                                                     ` Jiri Kosina
  2020-01-29  2:17                                                       ` Josh Poimboeuf
  2020-01-29 12:28                                                     ` Miroslav Benes
  1 sibling, 1 reply; 45+ messages in thread
From: Jiri Kosina @ 2020-01-29  0:46 UTC (permalink / raw)
  To: Josh Poimboeuf
  Cc: Petr Mladek, Miroslav Benes, Peter Zijlstra, Steven Rostedt,
	Joe Lawrence, Jessica Yu, x86, linux-kernel, mhiramat, bristot,
	jbaron, torvalds, tglx, mingo, namit, hpa, luto, ard.biesheuvel,
	live-patching, Randy Dunlap

On Tue, 28 Jan 2020, Josh Poimboeuf wrote:

> > Anyway, I think that we might make your life easier with using the 
> > proposed -Wsuggest-attribute=noreturn.
> 
> Maybe.  Though if I understand correctly, this doesn't help for any of 
> the new warnings because they're for static functions, and this only 
> warns about global functions.

Could you please provide a pointer where those have been 
reported/analyzed?

For the cases I've seen so far, it has always been gcc deciding under 
certain circumstances not to propagate __attribute__((__noreturn__)) from 
callee to caller even in the cases when caller unconditionally called 
callee.

AFAIU, the behavior is (and always will) be dependent on the state of gcc 
optimizations, and therefore I don't see any other way than adding 
__noreturn anotation transitively everywhere in order to silence objtool.

So those cases have to be fixed anyway.

What are the other cases please? Either I have completely missed those, or 
they haven't been mentioned in this thread.

Thanks,

-- 
Jiri Kosina
SUSE Labs


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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2020-01-29  0:46                                                     ` Jiri Kosina
@ 2020-01-29  2:17                                                       ` Josh Poimboeuf
  2020-01-29  3:14                                                         ` Jiri Kosina
  0 siblings, 1 reply; 45+ messages in thread
From: Josh Poimboeuf @ 2020-01-29  2:17 UTC (permalink / raw)
  To: Jiri Kosina
  Cc: Petr Mladek, Miroslav Benes, Peter Zijlstra, Steven Rostedt,
	Joe Lawrence, Jessica Yu, x86, linux-kernel, mhiramat, bristot,
	jbaron, torvalds, tglx, mingo, namit, hpa, luto, ard.biesheuvel,
	live-patching, Randy Dunlap

On Wed, Jan 29, 2020 at 01:46:55AM +0100, Jiri Kosina wrote:
> On Tue, 28 Jan 2020, Josh Poimboeuf wrote:
> 
> > > Anyway, I think that we might make your life easier with using the 
> > > proposed -Wsuggest-attribute=noreturn.
> > 
> > Maybe.  Though if I understand correctly, this doesn't help for any of 
> > the new warnings because they're for static functions, and this only 
> > warns about global functions.
> 
> Could you please provide a pointer where those have been 
> reported/analyzed?
> 
> For the cases I've seen so far, it has always been gcc deciding under 
> certain circumstances not to propagate __attribute__((__noreturn__)) from 
> callee to caller even in the cases when caller unconditionally called 
> callee.
> 
> AFAIU, the behavior is (and always will) be dependent on the state of gcc 
> optimizations, and therefore I don't see any other way than adding 
> __noreturn anotation transitively everywhere in order to silence objtool.
> 
> So those cases have to be fixed anyway.
> 
> What are the other cases please? Either I have completely missed those, or 
> they haven't been mentioned in this thread.

For example, see:

  https://git.kernel.org/pub/scm/linux/kernel/git/jpoimboe/linux.git/commit/?h=objtool-fixes&id=6265238af90b395a1e5e5032a41f012a552d8a9e

Many of those callees are static noreturns, for which we've *never*
needed annotations.  Disabling -fipa-pure-const has apparently changed
that.

-Wsuggest-attribute=noreturn doesn't seem to suggest annotations for
static functions, probably because most reasonable setups use -O2 which
allows GCC to detect them.

-- 
Josh


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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2020-01-29  2:17                                                       ` Josh Poimboeuf
@ 2020-01-29  3:14                                                         ` Jiri Kosina
  0 siblings, 0 replies; 45+ messages in thread
From: Jiri Kosina @ 2020-01-29  3:14 UTC (permalink / raw)
  To: Josh Poimboeuf
  Cc: Petr Mladek, Miroslav Benes, Peter Zijlstra, Steven Rostedt,
	Joe Lawrence, Jessica Yu, x86, linux-kernel, mhiramat, bristot,
	jbaron, torvalds, tglx, mingo, namit, hpa, luto, ard.biesheuvel,
	live-patching, Randy Dunlap

On Tue, 28 Jan 2020, Josh Poimboeuf wrote:

> > For the cases I've seen so far, it has always been gcc deciding under 
> > certain circumstances not to propagate __attribute__((__noreturn__)) from 
> > callee to caller even in the cases when caller unconditionally called 
> > callee.
> > 
> > AFAIU, the behavior is (and always will) be dependent on the state of gcc 
> > optimizations, and therefore I don't see any other way than adding 
> > __noreturn anotation transitively everywhere in order to silence objtool.
> > 
> > So those cases have to be fixed anyway.
> > 
> > What are the other cases please? Either I have completely missed those, or 
> > they haven't been mentioned in this thread.
> 
> For example, see:
> 
>   https://git.kernel.org/pub/scm/linux/kernel/git/jpoimboe/linux.git/commit/?h=objtool-fixes&id=6265238af90b395a1e5e5032a41f012a552d8a9e
> 
> Many of those callees are static noreturns, for which we've *never*
> needed annotations.  Disabling -fipa-pure-const has apparently changed
> that.

For some reason I thought you were talking about static inlines, sorry for 
the noise.

Yeah, so I agree with you -- whether we need those anotations depends on 
compiler implementation of optimizations, and most importantly on (the 
current state of) internal implementation of specific optimizations in 
gcc.

Leaving live patching completely aside for the sake of this discussion for 
now -- I believe we either fully rely on gcc to propagate the 'noreturn' 
propery throughout the callstack upward, or we don't.

If we don't, then we do need the anotations (both the global and static 
ones), and problem solved.

If we do, well, where is the 'this is *the* behavior of any current/future 
clang^Wcompiler' invariant guarantee?

Thanks,

-- 
Jiri Kosina
SUSE Labs


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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2020-01-28 17:02                                                   ` Josh Poimboeuf
  2020-01-29  0:46                                                     ` Jiri Kosina
@ 2020-01-29 12:28                                                     ` Miroslav Benes
  2020-01-29 15:59                                                       ` Josh Poimboeuf
  1 sibling, 1 reply; 45+ messages in thread
From: Miroslav Benes @ 2020-01-29 12:28 UTC (permalink / raw)
  To: Josh Poimboeuf
  Cc: Petr Mladek, Peter Zijlstra, Steven Rostedt, Joe Lawrence,
	Jessica Yu, x86, linux-kernel, mhiramat, bristot, jbaron,
	torvalds, tglx, mingo, namit, hpa, luto, ard.biesheuvel,
	live-patching, Randy Dunlap, nstange

> > > There are N users[*] of CONFIG_LIVEPATCH, where N is perhaps dozens.
> > > For N-1 users, they have to suffer ALL the drawbacks, with NONE of the
> > > benefits.
> > 
> > You wrote in the other mail:
> > 
> >   > The problems associated with it: performance, LTO incompatibility,
> >   > clang incompatibility (I think?), the GCC dead code issue.
> > 
> > SUSE performance team did extensive testing and did not found
> > any real performance issues. It was discussed when the option
> > was enabled upstream.
> > 
> > Are the other problems affecting real life usage, please?
> > Could you be more specific about them, please?
> 
> The original commit mentioned 1-3% scheduler degradation.  And I'd
> expect things to worsen over time as interprocedural optimizations
> improve.

Or maybe not.

Anyway, -flive-patching does not disable all interprocedural 
optimizations. By far. Only a subset of optimizations whose usage on the 
linux kernel is reportedly even not that prominent (compared to heavily 
C++ template based source codes). Reportedly, because we did some tests 
but nothing exhaustive. So I'd leave any expectations aside now.

The fact is that -fno-ipa-pure-const caused the objtool issue. One could 
argue that it should be fixed anyway, because it relies on GCC internal 
implementation which could easily change, and we luckily found it out 
thanks to -flive-patching. But you pointed out that was not even the main 
problem here, so I'd leave it for the separate subthread which Jiri 
started. 

Regarding the scheduler degradation. hackbench performance degradation to 
make it clear. It might be interesting to find out what really changed 
there. Which disabled optimization caused it and how. Maybe it could be 
gained back if proven again (because it may have changed, right?).

It all sound artificial to me though. I am not saying the degradation is 
not there, but many people also lived with frame pointers enabled for 
quite a long time and no one seemed to be bothered. And that was even more 
serious because the decline was bigger and it was measurable in many 
workflows. Not just a schedule microbenchmark. That is why Petr asked 
about real life reports, I guess.
 
> Also, LTO is coming whether we like it or not.  As is Clang.  Those are
> real-world things which will need to work with livepatching sooner or
> later.

Yes, but we are not there yet. Once a user has problem with that, we will 
try to solve it.

LTO might not be a big problem. The number of ipa clones would probably 
grow, but that is not directly dangerous. It remains to be seen.

I don't know much about Clang.

> > > And, even if they wanted those benefits, they have no idea how to get
> > > them because the patch creation process isn't documented.
> > 
> > I do not understand this. All the sample modules and selftests are
> > using source based livepatches.
> 
> We're talking in circles.  Have you read the thread?
>
> The samples are a (dangerous) joke.  With or without -flive-patching.

How come?

In my opinion, the samples and selftests try to show the way to prepare a 
(simple, yes) live patch. We try to ensure it always works (selftests 
should).

After all, there is not much more we do at SUSE to prepare a live patch.

1. take a patch and put all touched functions in a live patch
2. if the functions cannot be patched, patch their callers
3. do the function closure and/or add references (relocations or 
   kallsyms trick) so it can all be compiled.
4. done

See? Samples and selftests are not different. Our live patches are not 
different (https://kernel.suse.com/cgit/kernel-livepatch/). Can we 
implement the samples and selftests without -flive-patching? No, not 
really. Or we could, but no guarantees they would work.

For 2., we use -fdump-ipa-clones and Martin Liska's tool 
(https://github.com/marxin/kgraft-analysis-tool) to parse the output.

Yes, sometimes it is more complicated. Source based approach allows us to 
cope with that quite well. But that is case by case and cannot be easily 
documented.

Do we lack the documentation of our approach? Definitely. We are moving to 
klp-ccp automation now (https://github.com/SUSE/klp-ccp) and once done 
completely, we will hopefully have some documention. CCing Nicolai if he 
wants to add something.

Should it be upstream? I don't know. I don't think so. For the same reason 
kpatch-build documentation is not upstream either. Use cases of the 
infrastructure differ. Maybe there are users who use it in a completely 
different way. I don't know. In fact, it does not matter to me. I think we 
should support it all if they make sense.

And that is my message which (in my opinion) makes more sense. Definitely 
more sense than your "kpatch-build is the only safe way to prepare a live 
patch" mantra you are trying to sell here for whatever reason. I don't 
agree with it.

> > It is actually the only somehow documented way. Sure, the
> > documentation might get improved.  Patches are welcome.
> 
> Are you suggesting for *me* to send documentation for how *you* build
> patches?

I don't think that is what Petr meant (he will definitely correct me). If 
you think there is a space for improvement in our upstream documentation 
of the infrastructure, you are welcome to send patches. The space is 
definitely there.

> > The option is not currently needed by the selftests only because there
> > is no selftest for this type of problems. But the problems are real.
> > They would actually deserve selftests. Again, patches are welcome.
> > 
> > My understanding is that the source based livepatches is the future.
> 
> I think that still remains to be seen.
> 
> > N-1 users are just waiting until the 1 user develops more helper tools
> > for this.
> 
> No.  N-1 users have no idea how to make (safe) source-based patches in
> the first place.  And if *you* don't need the tools, why would anyone
> else?  Why not document the process and encourage the existence of other
> users so they can get involved and help with the tooling?

I replied to this one above. You are right we should document our approach 
better. I think it is off topic of the thread and problem here.

Regards
Miroslav

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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2020-01-29 12:28                                                     ` Miroslav Benes
@ 2020-01-29 15:59                                                       ` Josh Poimboeuf
  2020-01-30  9:53                                                         ` Petr Mladek
  0 siblings, 1 reply; 45+ messages in thread
From: Josh Poimboeuf @ 2020-01-29 15:59 UTC (permalink / raw)
  To: Miroslav Benes
  Cc: Petr Mladek, Peter Zijlstra, Steven Rostedt, Joe Lawrence,
	Jessica Yu, x86, linux-kernel, mhiramat, bristot, jbaron,
	torvalds, tglx, mingo, namit, hpa, luto, ard.biesheuvel,
	live-patching, Randy Dunlap, nstange

On Wed, Jan 29, 2020 at 01:28:30PM +0100, Miroslav Benes wrote:
> The fact is that -fno-ipa-pure-const caused the objtool issue. One could 
> argue that it should be fixed anyway, because it relies on GCC internal 
> implementation which could easily change, and we luckily found it out 
> thanks to -flive-patching. But you pointed out that was not even the main 
> problem here, so I'd leave it for the separate subthread which Jiri 
> started. 

It's not an objtool "issue".  The warnings were correct.  And objtool
*has* to rely on GCC internals.

And why would this particular internal implementation ever change
(detecting static noreturns)?  I don't see why optimizing the call
interface to a pure/const static function would break GCC's implicit
noreturn detection anyway.  It smells like a GCC bug.

> Regarding the scheduler degradation. hackbench performance degradation to 
> make it clear. It might be interesting to find out what really changed 
> there. Which disabled optimization caused it and how. Maybe it could be 
> gained back if proven again (because it may have changed, right?).

Fixing the scheduler performance regression would be a good thing to
have done *before* merging the patch.

> It all sound artificial to me though. I am not saying the degradation is 
> not there, but many people also lived with frame pointers enabled for 
> quite a long time and no one seemed to be bothered. And that was even more 
> serious because the decline was bigger and it was measurable in many 
> workflows. Not just a schedule microbenchmark. That is why Petr asked 
> about real life reports, I guess.

Many people were happy to get rid of frame pointers.

> > The samples are a (dangerous) joke.  With or without -flive-patching.
> 
> How come?
> 
> In my opinion, the samples and selftests try to show the way to prepare a 
> (simple, yes) live patch. We try to ensure it always works (selftests 
> should).
> 
> After all, there is not much more we do at SUSE to prepare a live patch.
> 
> 1. take a patch and put all touched functions in a live patch
> 2. if the functions cannot be patched, patch their callers
> 3. do the function closure and/or add references (relocations or 
>    kallsyms trick) so it can all be compiled.
> 4. done
> 
> See? Samples and selftests are not different.

How much ABI optimization analysis was done before creating the samples?
(hint: none)

And how would somebody using the samples as a guide know to do all that?

> Do we lack the documentation of our approach? Definitely. We are moving to 
> klp-ccp automation now (https://github.com/SUSE/klp-ccp) and once done 
> completely, we will hopefully have some documention. CCing Nicolai if he 
> wants to add something.
> 
> Should it be upstream? I don't know. I don't think so. For the same reason 
> kpatch-build documentation is not upstream either. Use cases of the 
> infrastructure differ. Maybe there are users who use it in a completely 
> different way. I don't know. In fact, it does not matter to me. I think we 
> should support it all if they make sense.

Of course the documentation should be in-tree.  Otherwise the samples
are *very* misleading.

> And that is my message which (in my opinion) makes more sense. Definitely 
> more sense than your "kpatch-build is the only safe way to prepare a live 
> patch" mantra you are trying to sell here for whatever reason. I don't 
> agree with it.

Of course I didn't say that.

The only thing I'm trying to "sell" is that this flag has several
drawbacks and no benefits for the upstream community.  Why do you keep
dancing around that unavoidable fact?

> > > It is actually the only somehow documented way. Sure, the
> > > documentation might get improved.  Patches are welcome.
> > 
> > Are you suggesting for *me* to send documentation for how *you* build
> > patches?
> 
> I don't think that is what Petr meant (he will definitely correct me). If 
> you think there is a space for improvement in our upstream documentation 
> of the infrastructure, you are welcome to send patches. The space is 
> definitely there.

If you want to use the -flive-patching flag for CONFIG_LIVEPATCH, then
yes, there's a huge gap in the documentation.  I don't understand why
you seem to be suggesting that I'm the one whose qualified to write that
documentation.

> > > N-1 users are just waiting until the 1 user develops more helper tools
> > > for this.
> > 
> > No.  N-1 users have no idea how to make (safe) source-based patches in
> > the first place.  And if *you* don't need the tools, why would anyone
> > else?  Why not document the process and encourage the existence of other
> > users so they can get involved and help with the tooling?
> 
> I replied to this one above. You are right we should document our approach 
> better. I think it is off topic of the thread and problem here.

It's actually very much on-topic.  It's one of the main reasons why I
wanted to revert the patch.  Surely that's clear by now?

In retrospect, the prerequisites for merging it should have been:


1) Document how source-based patches can be safely generated;

2) Fix the scheduler performance regression;

3) Figure out if there are any other regressions by detecting which
   function interfaces are affected by the flag and seeing if they're
   hot path;

4) Provide a way for the N-1 users to opt-out

5) Fix the objtool warnings (or is it a GCC bug)

6) Make -flive-patching compatible with LTO (or at least acknowledge
   that it should and will be done soon)

7) At least make it build- or runtime-incompatible with Clang-built
   kernels to prevent people from assuming it's safe.


If you don't want to revert the patch, then address my concerns instead
of minimizing and deflecting at every opportunity.

-- 
Josh


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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2020-01-29 15:59                                                       ` Josh Poimboeuf
@ 2020-01-30  9:53                                                         ` Petr Mladek
  2020-01-30 14:17                                                           ` Josh Poimboeuf
  0 siblings, 1 reply; 45+ messages in thread
From: Petr Mladek @ 2020-01-30  9:53 UTC (permalink / raw)
  To: Josh Poimboeuf
  Cc: Miroslav Benes, Peter Zijlstra, Steven Rostedt, Joe Lawrence,
	Jessica Yu, x86, linux-kernel, mhiramat, bristot, jbaron,
	torvalds, tglx, mingo, namit, hpa, luto, ard.biesheuvel,
	live-patching, Randy Dunlap, nstange

On Wed 2020-01-29 09:59:51, Josh Poimboeuf wrote:
> In retrospect, the prerequisites for merging it should have been:

OK, let me do one more move in this game.


> 1) Document how source-based patches can be safely generated;

I agree that the information are really scattered over many files
in Documentation/livepatch/. Anyway, there is a lot of useful
hints:

   + structure and behavior of the livepatch module, link
     to a sample, limitations, are described in livepatch.rst

   + many other catches are described in the other files:
     callbacks, module-elf-fomat, cumulative-patches,
     system-state.

Yes, it would be great to have a better structure, more information.
But do not get me wrong. Anyone, Joe definitely, is able to create
livepatch from sources by this information.

Anyone could play with it, ask questions, and improve the
documentation. Better documentation would help but it is
not a blocker, definitely.


> 2) Fix the scheduler performance regression;

The optimizations are disabled only when livepatching is enabled.
I would consider this as a prize for the feature. There are
many things like this.

As it was said. It was 1-3 percent in scheduler microbenchmark.
It would make sense to fix it only when it causes such a regression
in real workloads. Do you have any?


> 3) Figure out if there are any other regressions by detecting which
>    function interfaces are affected by the flag and seeing if they're
>    hot path;

IMHO, benchmarks are much more effective and we spent non-trivial
resources when running them.


> 4) Provide a way for the N-1 users to opt-out

AFAIK, the only prize is the 1-3 percent scheduler performance degradation.
If you really do not want to pay this prize, let's make it configurable.

But the option is definitely needed when source livepatches are used.
There is no other reasonable way to detect and workaround these
problems. For this, it has to be in upstream kernel. It is in line
with the effort to make livepatching less and less error prone.

And please, let's stop playing this multi-user games. There is at least
one known user of source based livepatches. By coincidence, it is also
a big contributor to this subsystem. Adding an extra option into
CFLAGS is quite error prone. You can imagine how complicated is
a kernel rpm spec file for more kernel flavors. The only safe way
is to have the optimization tight with the CONFIG option in
kernel sources.


> 5) Fix the objtool warnings (or is it a GCC bug)

Nobody was aware of them. I wonder if they even existed at that time.
We have a simple fix now. Let's continue in the thread started by
Jikos if we could get a better solution.


> 6) Make -flive-patching compatible with LTO (or at least acknowledge
>    that it should and will be done soon)

Is LTO officially supported upstream?
Are all patches, features tested for LTO compactibility?
Is there any simple way to build and run LTO kernel?


> 7) At least make it build- or runtime-incompatible with Clang-built
>    kernels to prevent people from assuming it's safe.

Same questions as for LTO.


> If you don't want to revert the patch, then address my concerns instead
> of minimizing and deflecting at every opportunity.

I would really like to keep focusing on realistic problems and
realistic solutions:

   + make the optimization configurable if you resist on it
   + fix the objtool warnings

Anything else is out of scope of this thread from my POV.

Best Regards,
Petr

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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2020-01-30  9:53                                                         ` Petr Mladek
@ 2020-01-30 14:17                                                           ` Josh Poimboeuf
  2020-01-31  7:17                                                             ` Petr Mladek
  0 siblings, 1 reply; 45+ messages in thread
From: Josh Poimboeuf @ 2020-01-30 14:17 UTC (permalink / raw)
  To: Petr Mladek
  Cc: Miroslav Benes, Peter Zijlstra, Steven Rostedt, Joe Lawrence,
	Jessica Yu, x86, linux-kernel, mhiramat, bristot, jbaron,
	torvalds, tglx, mingo, namit, hpa, luto, ard.biesheuvel,
	live-patching, Randy Dunlap, nstange

On Thu, Jan 30, 2020 at 10:53:46AM +0100, Petr Mladek wrote:
> On Wed 2020-01-29 09:59:51, Josh Poimboeuf wrote:
> > In retrospect, the prerequisites for merging it should have been:
> 
> OK, let me do one more move in this game.
> 
> 
> > 1) Document how source-based patches can be safely generated;
> 
> I agree that the information are really scattered over many files
> in Documentation/livepatch/.

Once again you're blithely ignoring my point and pretending I'm saying
something else.  And you did that again further down in the email, but
what's the point of arguing if you're not going to listen.

This has nothing to do with the organization of the existing
documentation.  When did I say that?

Adding the -flive-patching flag doesn't remove *all*
function-ABI-breaking optimizations.  It's only a partial solution.  The
rest of the solution involves tooling and processes which need to be
documented.  But you already know that.

If we weren't co-maintainers I would have reverted the patch days ago.
I've tried to give you all the benefit of the doubt.  But you seem to be
playing company politics.

I would ask that you please put on your upstream hats and stop playing
politics.  If the patch creation process is a secret, then by all means,
keep it secret.  But then keep your GCC flag to yourself.

-- 
Josh


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

* Re: [PATCH v3 5/6] x86/ftrace: Use text_poke()
  2020-01-30 14:17                                                           ` Josh Poimboeuf
@ 2020-01-31  7:17                                                             ` Petr Mladek
  0 siblings, 0 replies; 45+ messages in thread
From: Petr Mladek @ 2020-01-31  7:17 UTC (permalink / raw)
  To: Josh Poimboeuf
  Cc: Miroslav Benes, Peter Zijlstra, Steven Rostedt, Joe Lawrence,
	Jessica Yu, x86, linux-kernel, mhiramat, bristot, jbaron,
	torvalds, tglx, mingo, namit, hpa, luto, ard.biesheuvel,
	live-patching, Randy Dunlap, nstange

On Thu 2020-01-30 08:17:33, Josh Poimboeuf wrote:
> On Thu, Jan 30, 2020 at 10:53:46AM +0100, Petr Mladek wrote:
> > On Wed 2020-01-29 09:59:51, Josh Poimboeuf wrote:
> > > In retrospect, the prerequisites for merging it should have been:
> > 
> > OK, let me do one more move in this game.
> > 
> > 
> > > 1) Document how source-based patches can be safely generated;
> > 
> > I agree that the information are really scattered over many files
> > in Documentation/livepatch/.
> 
> Once again you're blithely ignoring my point and pretending I'm saying
> something else.  And you did that again further down in the email, but
> what's the point of arguing if you're not going to listen.

I have exactly the same feeling but the opposite way.

> I would ask that you please put on your upstream hats and stop playing
> politics.  If the patch creation process is a secret, then by all means,
> keep it secret.  But then keep your GCC flag to yourself.

The thing is that we do not have any magic secret.

Best Regards,
Petr

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

end of thread, back to index

Thread overview: 45+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20191007081945.10951536.8@infradead.org>
     [not found] ` <20191008104335.6fcd78c9@gandalf.local.home>
     [not found]   ` <20191009224135.2dcf7767@oasis.local.home>
     [not found]     ` <20191010092054.GR2311@hirez.programming.kicks-ass.net>
     [not found]       ` <20191010091956.48fbcf42@gandalf.local.home>
     [not found]         ` <20191010140513.GT2311@hirez.programming.kicks-ass.net>
     [not found]           ` <20191010115449.22044b53@gandalf.local.home>
     [not found]             ` <20191010172819.GS2328@hirez.programming.kicks-ass.net>
     [not found]               ` <20191011125903.GN2359@hirez.programming.kicks-ass.net>
     [not found]                 ` <20191015130739.GA23565@linux-8ccs>
     [not found]                   ` <20191015135634.GK2328@hirez.programming.kicks-ass.net>
2019-10-15 14:13                     ` [PATCH v3 5/6] x86/ftrace: Use text_poke() Miroslav Benes
2019-10-15 15:06                       ` Joe Lawrence
2019-10-15 15:31                         ` Jessica Yu
2019-10-15 22:17                           ` Joe Lawrence
2019-10-15 22:27                             ` Steven Rostedt
2019-10-16  7:42                               ` Peter Zijlstra
2019-10-16 10:15                                 ` Miroslav Benes
2019-10-21 15:05                                 ` Josh Poimboeuf
2020-01-20 16:50                                   ` Josh Poimboeuf
2020-01-21  8:35                                     ` Miroslav Benes
2020-01-21 16:10                                       ` Josh Poimboeuf
2020-01-22 10:09                                         ` Miroslav Benes
2020-01-22 21:42                                           ` Josh Poimboeuf
2020-01-28  9:28                                             ` Miroslav Benes
2020-01-28 15:00                                               ` Josh Poimboeuf
2020-01-28 15:40                                                 ` Petr Mladek
2020-01-28 17:02                                                   ` Josh Poimboeuf
2020-01-29  0:46                                                     ` Jiri Kosina
2020-01-29  2:17                                                       ` Josh Poimboeuf
2020-01-29  3:14                                                         ` Jiri Kosina
2020-01-29 12:28                                                     ` Miroslav Benes
2020-01-29 15:59                                                       ` Josh Poimboeuf
2020-01-30  9:53                                                         ` Petr Mladek
2020-01-30 14:17                                                           ` Josh Poimboeuf
2020-01-31  7:17                                                             ` Petr Mladek
2020-01-22 12:15                                         ` Miroslav Benes
2020-01-22 15:05                                           ` Miroslav Benes
2020-01-22 22:03                                             ` Josh Poimboeuf
2020-01-23 10:19                                               ` Martin Jambor
2019-10-16  7:49                               ` Peter Zijlstra
2019-10-16 10:20                                 ` Miroslav Benes
2019-10-16 13:29                                   ` Miroslav Benes
2019-10-18 13:03                                     ` Jessica Yu
2019-10-18 13:40                                       ` Petr Mladek
2019-10-21 14:14                                         ` Jessica Yu
2019-10-21 15:31                                         ` Josh Poimboeuf
2019-10-22  8:27                                       ` Miroslav Benes
2019-10-22 14:31                                         ` Josh Poimboeuf
2019-10-23  9:04                                           ` Miroslav Benes
2019-10-16  6:51                         ` Miroslav Benes
2019-10-16  9:23                           ` Peter Zijlstra
2019-10-16  9:36                             ` Jessica Yu
2019-10-16  9:51                               ` Peter Zijlstra
2019-10-16 12:39                           ` Peter Zijlstra
2019-10-22  8:45                             ` Miroslav Benes

Live-Patching Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/live-patching/0 live-patching/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 live-patching live-patching/ https://lore.kernel.org/live-patching \
		live-patching@vger.kernel.org
	public-inbox-index live-patching

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.live-patching


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git