[RFC 0/5] Introduce dynamic tracer using Livepatch

Ryo Takakura posted 5 patches 1 month ago
xen/arch/arm/Kconfig                  |  13 +
xen/arch/arm/arch.mk                  |   3 +
xen/arch/arm/arm64/Makefile           |   1 +
xen/arch/arm/arm64/insn.c             |   5 +
xen/arch/arm/arm64/livepatch-asm.S    |  68 +++
xen/arch/arm/arm64/livepatch.c        |  39 ++
xen/arch/arm/include/asm/arm64/insn.h |   1 +
xen/arch/arm/xen.lds.S                |   5 +
xen/arch/x86/Kconfig                  |  13 +
xen/arch/x86/Makefile                 |   1 +
xen/arch/x86/arch.mk                  |   3 +
xen/arch/x86/livepatch-asm.S          |  60 +++
xen/arch/x86/livepatch.c              |  33 ++
xen/arch/x86/xen.lds.S                |   5 +
xen/common/Kconfig                    |  12 +
xen/common/Makefile                   |   6 +
xen/common/function_trace.c           | 274 ++++++++++
xen/common/hypfs_trace.c              | 743 ++++++++++++++++++++++++++
xen/common/livepatch.c                | 137 ++++-
xen/include/xen/hypfs_trace.h         |  42 ++
xen/include/xen/livepatch.h           |  14 +
xen/include/xen/livepatch_payload.h   |   7 +-
22 files changed, 1478 insertions(+), 7 deletions(-)
create mode 100644 xen/arch/arm/arm64/livepatch-asm.S
create mode 100644 xen/arch/x86/livepatch-asm.S
create mode 100644 xen/common/function_trace.c
create mode 100644 xen/common/hypfs_trace.c
create mode 100644 xen/include/xen/hypfs_trace.h
[RFC 0/5] Introduce dynamic tracer using Livepatch
Posted by Ryo Takakura 1 month ago
Hello!

This RFC introduces a dynamic function tracing feature
which I proposed at Xen Summit in September:
https://xensummit2026.sched.com/event/2RDr8/improving-xen-traceability-more-than-just-livepatching

The goal is to improve debugging/tracing by making Xen possible to attach
callbacks to its functions. This series implements the infrastructure for
attaching callbacks using Livepatch on x86 and arm64, as well as
HypFS interface for controlling tracers. It is inspired by Linux ftrace:
https://docs.kernel.org/trace/ftrace.html

I appreciate any feedback on the implementation, as well as
your experience trying it out. Thanks!

# Implementation
The implementation follows Linux ftrace's approach of reserving
function entries that can be later patched for tracing at runtime.
Those entries get patched with a branch instruction to a trampoline,
which preserves registers before calling function callbacks.

Livepatch is used for patching those reserved function entries.
Since Livepatch operates on struct payload, it prepares a dedicated
payload which can be used by tracers for requesting livepatch of the
target function. Please see patch 2 and 3 for the detail.

New tracers can be added by defining a struct tracer and
registering it during Xen initialization. Each tracer can provide
a function callback for tracing and optional callbacks for managing
its state and output through the HypFS interface under /tracing.
Please see patch 4 for an example implementation.

# Configuration
In addition to Livepatch and HypFS:

CONFIG_LIVEPATCH=y
CONFIG_HYPFS=y

Enable the following configs:

CONFIG_ARM64_PATCHABLE_FUNCTION_ENTRY=y
or
CONFIG_X86_PATCHABLE_FUNCTION_ENTRY=y

CONFIG_LIVEPATCH_TRACER=y

# Usage
The interface is exposed HypFS /tracing directory.
It follows the naming and usage of Linux ftrace:
https://www.kernel.org/doc/html/latest/trace/ftrace.html#the-file-system

available_tracers     List registered tracers.
current_tracer        Read or select the active tracer.
available_functions   List functions available for tracing.
set_function_filter   Read, select, or clear the target function.
tracing_on            Enable or disable recording.
trace                 Read or clear recorded output.

## Example
// List the tracable functions.
xenhypfs cat /tracing/available_functions

// List the available tracers. Currently "nop" and "function" tracers are available.
xenhypfs cat /tracing/available_tracers

// Select the function to trace.
xenhypfs write /tracing/set_function_filter xen_extra_version

// Select the tracer.
xenhypfs write /tracing/current_tracer function

// Record calls to the selected function.
xenhypfs write /tracing/tracing_on 1

// Call xen_extra_version().
cat /sys/hypervisor/version/extra

// Stop recording and read the output.
xenhypfs write /tracing/tracing_on 0
xenhypfs cat /tracing/trace

// Reset.
xenhypfs write /tracing/set_function_filter ""
xenhypfs write /tracing/current_tracer nop
xenhypfs write /tracing/trace ""

Sincerely,
Ryo Takakura

Ryo Takakura (5):
  xen/arm64: livepatch: enable attaching callbacks
  xen/livepatch: Allow tracers to schedule livepatch
  xen/tracing: prepare a livepatch payload and its controling interface
  xen/tracing: Add a function tracer
  xen/x86: livepatch: enable attaching callbacks

 xen/arch/arm/Kconfig                  |  13 +
 xen/arch/arm/arch.mk                  |   3 +
 xen/arch/arm/arm64/Makefile           |   1 +
 xen/arch/arm/arm64/insn.c             |   5 +
 xen/arch/arm/arm64/livepatch-asm.S    |  68 +++
 xen/arch/arm/arm64/livepatch.c        |  39 ++
 xen/arch/arm/include/asm/arm64/insn.h |   1 +
 xen/arch/arm/xen.lds.S                |   5 +
 xen/arch/x86/Kconfig                  |  13 +
 xen/arch/x86/Makefile                 |   1 +
 xen/arch/x86/arch.mk                  |   3 +
 xen/arch/x86/livepatch-asm.S          |  60 +++
 xen/arch/x86/livepatch.c              |  33 ++
 xen/arch/x86/xen.lds.S                |   5 +
 xen/common/Kconfig                    |  12 +
 xen/common/Makefile                   |   6 +
 xen/common/function_trace.c           | 274 ++++++++++
 xen/common/hypfs_trace.c              | 743 ++++++++++++++++++++++++++
 xen/common/livepatch.c                | 137 ++++-
 xen/include/xen/hypfs_trace.h         |  42 ++
 xen/include/xen/livepatch.h           |  14 +
 xen/include/xen/livepatch_payload.h   |   7 +-
 22 files changed, 1478 insertions(+), 7 deletions(-)
 create mode 100644 xen/arch/arm/arm64/livepatch-asm.S
 create mode 100644 xen/arch/x86/livepatch-asm.S
 create mode 100644 xen/common/function_trace.c
 create mode 100644 xen/common/hypfs_trace.c
 create mode 100644 xen/include/xen/hypfs_trace.h

-- 
2.34.1
Re: [RFC 0/5] Introduce dynamic tracer using Livepatch
Posted by Andrew Cooper 4 weeks ago
On 23/10/2026 5:48 am, Ryo Takakura wrote:
> # Configuration
> In addition to Livepatch and HypFS:
>
> CONFIG_LIVEPATCH=y
> CONFIG_HYPFS=y
>
> Enable the following configs:
>
> CONFIG_ARM64_PATCHABLE_FUNCTION_ENTRY=y
> or
> CONFIG_X86_PATCHABLE_FUNCTION_ENTRY=y
>
> CONFIG_LIVEPATCH_TRACER=y

This ends up having two almost-identical symbols, depending on
architecture, where really this is a common piece of functionality.  It
also conflates several different tasks.

I think what you want is something like this:

In xen/Kconfig, create CC_HAS_PATCHABLE_FUNCTION_ENTRY which performs
the $(cc-option,-fpatchable-function-entry=0,0) check.

You are also going to need a GCC_PATCHABLE_FUNCTION_ENTRY_BROKEN symbol,
because of https://godbolt.org/z/sde4faxaz.  See the ASM_GOTO_OUTPUT for
another example of this pattern.  Just use a plain version check rather
than tying it to XEN_IBT; this is a new feature and people wanting to
use it can use a new enough compiler, and it avoids variability
depending on other options.

You also want a HAS_PATCHABLE_FUNCTION_ENTRY in xen/common/Kconfig,
which you select from the main config x86/ARM/etc in each patch where
you add the architecture support.

You want the single visible entry LIVEPATCH_TRACER which depends on
HAS_PATCHABLE_FUNCTION_ENTRY and CC_HAS_PATCHABLE_FUNCTION_ENTRY.  You
want to make the text architecture-neutral.  Instead of the exact number
of NOPs, just say "reserves space" which will be entirely fine; the
target audience for Kconfig text is sysadmins/distro integrators, not
developers.

Finally, you want PATCHABLE_FUNCTION_ENTRY which is selected by
LIVEPATCH_TRACER, which triggers the Makefile changes.

As a general note, you don't need "default n" lines in Kconfig.  That is
the default.

All together, this separates "can the compiler do it?" from "does the
user want it?" from "what to do when the user wants it".


Separately, you have a number of MISRA violations which you're
introducing in your series.  I see you've got cf_check, which suggests
you have built in GitlabCI.  Have you also run Eclair?

~Andrew

Re: [RFC 0/5] Introduce dynamic tracer using Livepatch
Posted by Ryo Takakura 3 weeks, 6 days ago
Hi Andrew!

On Mon, 26 Oct 2026 09:29:04 +0000, Andrew Cooper wrote:
>On 23/10/2026 5:48 am, Ryo Takakura wrote:
>> # Configuration
>> In addition to Livepatch and HypFS:
>>
>> CONFIG_LIVEPATCH=y
>> CONFIG_HYPFS=y
>>
>> Enable the following configs:
>>
>> CONFIG_ARM64_PATCHABLE_FUNCTION_ENTRY=y
>> or
>> CONFIG_X86_PATCHABLE_FUNCTION_ENTRY=y
>>
>> CONFIG_LIVEPATCH_TRACER=y
>
>
>This ends up having two almost-identical symbols, depending on
>architecture, where really this is a common piece of functionality.  It
>also conflates several different tasks.

Thank you for the feedback in detail. It was very helpful.
The suggested config layout makes much more sense.

>I think what you want is something like this:
>
>In xen/Kconfig, create CC_HAS_PATCHABLE_FUNCTION_ENTRY which performs
>the $(cc-option,-fpatchable-function-entry=0,0) check.

Understood.

>You are also going to need a GCC_PATCHABLE_FUNCTION_ENTRY_BROKEN symbol,
>because of https://godbolt.org/z/sde4faxaz.  See the ASM_GOTO_OUTPUT for
>another example of this pattern.  Just use a plain version check rather
>than tying it to XEN_IBT; this is a new feature and people wanting to
>use it can use a new enough compiler, and it avoids variability
>depending on other options.

I didn't know about the compiler behaviour. Thank you for the detail.
I'll make sure to check the acceptable compiler version and add a version
check as you suggest.

>You also want a HAS_PATCHABLE_FUNCTION_ENTRY in xen/common/Kconfig,
>which you select from the main config x86/ARM/etc in each patch where
>you add the architecture support.

Understood.

>You want the single visible entry LIVEPATCH_TRACER which depends on
>HAS_PATCHABLE_FUNCTION_ENTRY and CC_HAS_PATCHABLE_FUNCTION_ENTRY.  You
>want to make the text architecture-neutral.  Instead of the exact number
>of NOPs, just say "reserves space" which will be entirely fine; the
>target audience for Kconfig text is sysadmins/distro integrators, not
>developers.

Understood.
And I'll revise the feature description as well.

>Finally, you want PATCHABLE_FUNCTION_ENTRY which is selected by
>LIVEPATCH_TRACER, which triggers the Makefile changes.

Understood.

>As a general note, you don't need "default n" lines in Kconfig.  That is
>the default.

I see. I'll make sure to remove them.

>All together, this separates "can the compiler do it?" from "does the
>user want it?" from "what to do when the user wants it".

Understood!
I'll make those changes in the next version.

>Separately, you have a number of MISRA violations which you're
>introducing in your series.  I see you've got cf_check, which suggests
>you have built in GitlabCI.  Have you also run Eclair?

Thank you for pointing this out.
My apologies for the violations. I wasn't aware of the tests
and haven't run either of them. I added the cf_check following
the existing code that I referred to when preparing this series.
I'll make sure to run those tests before sending next version.

Sincerely,
Ryo Takakura
Re: [RFC 0/5] Introduce dynamic tracer using Livepatch
Posted by Andrew Cooper 3 weeks, 6 days ago
On 27/10/2026 4:37 am, Ryo Takakura wrote:
>> You are also going to need a GCC_PATCHABLE_FUNCTION_ENTRY_BROKEN symbol,
>> because of https://godbolt.org/z/sde4faxaz.  See the ASM_GOTO_OUTPUT for
>> another example of this pattern.  Just use a plain version check rather
>> than tying it to XEN_IBT; this is a new feature and people wanting to
>> use it can use a new enough compiler, and it avoids variability
>> depending on other options.
> I didn't know about the compiler behaviour. Thank you for the detail.
> I'll make sure to check the acceptable compiler version and add a version
> check as you suggest.

https://maskray.me/blog/fpatchable-function-entry seems to have a
blow-by-blow of the bugs.

The ordering with respect to ENDBR instructions is the only issue which
will affect Xen, I think.  We don't use COMDAT, or BTI on ARM yet.

Clang support seems to have been mostly correct at the point of being
added, which is rather more than can be said for the GCC side.
>> Separately, you have a number of MISRA violations which you're
>> introducing in your series.  I see you've got cf_check, which suggests
>> you have built in GitlabCI.  Have you also run Eclair?
> Thank you for pointing this out.
> My apologies for the violations. I wasn't aware of the tests
> and haven't run either of them. I added the cf_check following
> the existing code that I referred to when preparing this series.
> I'll make sure to run those tests before sending next version.

Do you have an account on Gitlab to run the CI testing?  If not, reach
out on Matrix.

~Andrew

Re: [RFC 0/5] Introduce dynamic tracer using Livepatch
Posted by Ryo Takakura 3 weeks, 5 days ago
On Tue, 27 Oct 2026 10:18:44 +0000, Andrew Cooper wrote:
>On 27/10/2026 4:37 am, Ryo Takakura wrote:
>>> You are also going to need a GCC_PATCHABLE_FUNCTION_ENTRY_BROKEN symbol,
>>> because of https://godbolt.org/z/sde4faxaz.  See the ASM_GOTO_OUTPUT for
>>> another example of this pattern.  Just use a plain version check rather
>>> than tying it to XEN_IBT; this is a new feature and people wanting to
>>> use it can use a new enough compiler, and it avoids variability
>>> depending on other options.
>> I didn't know about the compiler behaviour. Thank you for the detail.
>> I'll make sure to check the acceptable compiler version and add a version
>> check as you suggest.
>
>https://maskray.me/blog/fpatchable-function-entry seems to have a
>blow-by-blow of the bugs.

Interesting!

>The ordering with respect to ENDBR instructions is the only issue which
>will affect Xen, I think.  We don't use COMDAT, or BTI on ARM yet.
>
>Clang support seems to have been mostly correct at the point of being
>added, which is rather more than can be said for the GCC side.

I see. Thank you for the advise.
I follow them when preparing the check.

>>> Separately, you have a number of MISRA violations which you're
>>> introducing in your series.  I see you've got cf_check, which suggests
>>> you have built in GitlabCI.  Have you also run Eclair?
>> Thank you for pointing this out.
>> My apologies for the violations. I wasn't aware of the tests
>> and haven't run either of them. I added the cf_check following
>> the existing code that I referred to when preparing this series.
>> I'll make sure to run those tests before sending next version.
>
>Do you have an account on Gitlab to run the CI testing?  If not, reach
>out on Matrix.

I don't have an account yet.
Ok, I'll ask on Matrix.

Sincerely,
Ryo Takakura
Re: [RFC 0/5] Introduce dynamic tracer using Livepatch
Posted by Andrew Cooper 3 weeks, 5 days ago
On 28/10/2026 8:08 am, Ryo Takakura wrote:
> On Tue, 27 Oct 2026 10:18:44 +0000, Andrew Cooper wrote:
>>
>>>> Separately, you have a number of MISRA violations which you're
>>>> introducing in your series.  I see you've got cf_check, which suggests
>>>> you have built in GitlabCI.  Have you also run Eclair?
>>> Thank you for pointing this out.
>>> My apologies for the violations. I wasn't aware of the tests
>>> and haven't run either of them. I added the cf_check following
>>> the existing code that I referred to when preparing this series.
>>> I'll make sure to run those tests before sending next version.
>> Do you have an account on Gitlab to run the CI testing?  If not, reach
>> out on Matrix.
> I don't have an account yet.
> Ok, I'll ask on Matrix.

To get you started, I put this series through:

  https://gitlab.com/xen-project/hardware/xen-staging/-/pipelines/3000384733

There's one Eclair failure on ARM.

Interestingly, it's not the failure I was expecting to see.  I was
expecting it to complain about

+    uint32_t insns[2] = {
+        aarch64_insn_gen_nop(),
+        aarch64_insn_gen_nop(),
+    };

because of the order those function calls being undefined.

I was also expecting to see some failures due to arithmetic on pointers
of different types, but maybe we've not made those rules enforcing yet.

~Andrew

Re: [RFC 0/5] Introduce dynamic tracer using Livepatch
Posted by Ryo Takakura 3 weeks, 4 days ago
On Wed, 28 Oct 2026 18:22:21 +0000, Andrew Cooper wrote:
>On 28/10/2026 8:08 am, Ryo Takakura wrote:
>> On Tue, 27 Oct 2026 10:18:44 +0000, Andrew Cooper wrote:
>>>
>>>>> Separately, you have a number of MISRA violations which you're
>>>>> introducing in your series.  I see you've got cf_check, which suggests
>>>>> you have built in GitlabCI.  Have you also run Eclair?
>>>> Thank you for pointing this out.
>>>> My apologies for the violations. I wasn't aware of the tests
>>>> and haven't run either of them. I added the cf_check following
>>>> the existing code that I referred to when preparing this series.
>>>> I'll make sure to run those tests before sending next version.
>>> Do you have an account on Gitlab to run the CI testing?  If not, reach
>>> out on Matrix.
>> I don't have an account yet.
>> Ok, I'll ask on Matrix.
>
>To get you started, I put this series through:
>
>  https://gitlab.com/xen-project/hardware/xen-staging/-/pipelines/3000384733
>
>There's one Eclair failure on ARM.

Thank you for running the test.
I see the report saying its violating MC3A2.R1.1.
I'll fix the issue and make sure it passes the test.

>Interestingly, it's not the failure I was expecting to see.  I was
>expecting it to complain about
>
>+    uint32_t insns[2] = {
>+        aarch64_insn_gen_nop(),
>+        aarch64_insn_gen_nop(),
>+    };
>
>because of the order those function calls being undefined.
>
>I was also expecting to see some failures due to arithmetic on pointers
>of different types, but maybe we've not made those rules enforcing yet.

I see.
I'll make sure to address them as well in the next version.

Sincerely,
Ryo Takakura
Re: [RFC 0/5] Introduce dynamic tracer using Livepatch
Posted by Jan Beulich 3 weeks, 3 days ago
On 23.10.2026 06:48, Ryo Takakura wrote:
> Ryo Takakura (5):
>   xen/arm64: livepatch: enable attaching callbacks
>   xen/livepatch: Allow tracers to schedule livepatch
>   xen/tracing: prepare a livepatch payload and its controling interface
>   xen/tracing: Add a function tracer
>   xen/x86: livepatch: enable attaching callbacks
> 
>  xen/arch/arm/Kconfig                  |  13 +
>  xen/arch/arm/arch.mk                  |   3 +
>  xen/arch/arm/arm64/Makefile           |   1 +
>  xen/arch/arm/arm64/insn.c             |   5 +
>  xen/arch/arm/arm64/livepatch-asm.S    |  68 +++
>  xen/arch/arm/arm64/livepatch.c        |  39 ++
>  xen/arch/arm/include/asm/arm64/insn.h |   1 +
>  xen/arch/arm/xen.lds.S                |   5 +
>  xen/arch/x86/Kconfig                  |  13 +
>  xen/arch/x86/Makefile                 |   1 +
>  xen/arch/x86/arch.mk                  |   3 +
>  xen/arch/x86/livepatch-asm.S          |  60 +++
>  xen/arch/x86/livepatch.c              |  33 ++
>  xen/arch/x86/xen.lds.S                |   5 +
>  xen/common/Kconfig                    |  12 +
>  xen/common/Makefile                   |   6 +
>  xen/common/function_trace.c           | 274 ++++++++++
>  xen/common/hypfs_trace.c              | 743 ++++++++++++++++++++++++++
>  xen/common/livepatch.c                | 137 ++++-
>  xen/include/xen/hypfs_trace.h         |  42 ++
>  xen/include/xen/livepatch.h           |  14 +
>  xen/include/xen/livepatch_payload.h   |   7 +-
>  22 files changed, 1478 insertions(+), 7 deletions(-)
>  create mode 100644 xen/arch/arm/arm64/livepatch-asm.S
>  create mode 100644 xen/arch/x86/livepatch-asm.S
>  create mode 100644 xen/common/function_trace.c
>  create mode 100644 xen/common/hypfs_trace.c
>  create mode 100644 xen/include/xen/hypfs_trace.h

Nit: Dashes in favor of underscores please in new files' names.

Jan
Re: [RFC 0/5] Introduce dynamic tracer using Livepatch
Posted by Ryo Takakura 3 weeks, 1 day ago
Hi, Jan!

Thank you for taking a look at the series.

On, Fri, 30 Oct 2026 11:31:23 +0100, Jan Beulich wrote:
>On 23.10.2026 06:48, Ryo Takakura wrote:
>> Ryo Takakura (5):
>>   xen/arm64: livepatch: enable attaching callbacks
>>   xen/livepatch: Allow tracers to schedule livepatch
>>   xen/tracing: prepare a livepatch payload and its controling interface
>>   xen/tracing: Add a function tracer
>>   xen/x86: livepatch: enable attaching callbacks
>> 
>>  xen/arch/arm/Kconfig                  |  13 +
>>  xen/arch/arm/arch.mk                  |   3 +
>>  xen/arch/arm/arm64/Makefile           |   1 +
>>  xen/arch/arm/arm64/insn.c             |   5 +
>>  xen/arch/arm/arm64/livepatch-asm.S    |  68 +++
>>  xen/arch/arm/arm64/livepatch.c        |  39 ++
>>  xen/arch/arm/include/asm/arm64/insn.h |   1 +
>>  xen/arch/arm/xen.lds.S                |   5 +
>>  xen/arch/x86/Kconfig                  |  13 +
>>  xen/arch/x86/Makefile                 |   1 +
>>  xen/arch/x86/arch.mk                  |   3 +
>>  xen/arch/x86/livepatch-asm.S          |  60 +++
>>  xen/arch/x86/livepatch.c              |  33 ++
>>  xen/arch/x86/xen.lds.S                |   5 +
>>  xen/common/Kconfig                    |  12 +
>>  xen/common/Makefile                   |   6 +
>>  xen/common/function_trace.c           | 274 ++++++++++
>>  xen/common/hypfs_trace.c              | 743 ++++++++++++++++++++++++++
>>  xen/common/livepatch.c                | 137 ++++-
>>  xen/include/xen/hypfs_trace.h         |  42 ++
>>  xen/include/xen/livepatch.h           |  14 +
>>  xen/include/xen/livepatch_payload.h   |   7 +-
>>  22 files changed, 1478 insertions(+), 7 deletions(-)
>>  create mode 100644 xen/arch/arm/arm64/livepatch-asm.S
>>  create mode 100644 xen/arch/x86/livepatch-asm.S
>>  create mode 100644 xen/common/function_trace.c
>>  create mode 100644 xen/common/hypfs_trace.c
>>  create mode 100644 xen/include/xen/hypfs_trace.h
>
>Nit: Dashes in favor of underscores please in new files' names.

Understood. I'll rename them in the next version.

Sincerely,
Ryo Takakura