From nobody Mon Nov 23 22:39:58 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; arc=pass (i=1 dmarc=pass fromdomain=valinux.co.jp); dmarc=pass(p=none dis=none) header.from=valinux.co.jp ARC-Seal: i=2; a=rsa-sha256; t=1792731188; cv=pass; d=zohomail.com; s=zohoarc; b=k/ayWN7On1g3N0iHKoZDsdD247vut259pt3ePe3S/2lS+Qdu9blSp7yjOZNBYuqpzOLUVG7rYrslB5kjBeUfEp+p2S3bgG30VxoWPYpJ3RpcUWMnWnQ7dpNsdgU7mM+Wey5FIoaK+Pw3giZvcsbBnWqUpYej4WDB5CTIGkiG1nY= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792731188; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=B7ltYEKX+cKqsvP9h4OtOTgNQtNgp6o3XIpTxvBpIi8=; b=Pi4L+rtO512rI/LYw0QMj5LZxkEbzJWsCwZ6v+4WcjeM0pA5IIJpQ0xeeW7ECEAncOcgtUXMHaLJE0yn2fyxq9zzCogxRUmT/7HHx0IQYPl8KWwmYt/udD5bDF+sENkBvPc/8fb7/J7q7pLdQ/YK4Ua4g4McT3BLNy4x9bIHKeY= ARC-Authentication-Results: i=2; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; arc=pass (i=1 dmarc=pass fromdomain=valinux.co.jp); dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1792731188828378.75823487734783; Thu, 22 Oct 2026 21:53:08 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1458877.1670961 (Exim 4.92) (envelope-from ) id 1xK7Gm-0006z9-M4; Fri, 23 Oct 2026 04:52:44 +0000 Received: by outflank-mailman (output) from mailman id 1458877.1670961; Fri, 23 Oct 2026 04:52:44 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xK7Gm-0006yy-Id; Fri, 23 Oct 2026 04:52:44 +0000 Received: by outflank-mailman (input) for mailman id 1458877; Fri, 23 Oct 2026 04:52:43 +0000 Received: from mx.expurgate.net ([195.190.135.20]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xK7Gl-0006uW-JX for xen-devel@lists.xenproject.org; Fri, 23 Oct 2026 04:52:43 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1xK7Gl-00FSjJ-0I for xen-devel@lists.xenproject.org; Fri, 23 Oct 2026 06:52:43 +0200 Received: from [10.42.69.1] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6adae804-e002-0a2a0a5209dd-0a2a45018318-40 for ; Fri, 23 Oct 2026 06:52:42 +0200 Received: from [52.101.125.98] (helo=TYVP286CU001.outbound.protection.outlook.com) by tlsNG-d62444.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6adae817-5984-0a2a45010019-34657d6216d3-3 for ; Fri, 23 Oct 2026 06:52:41 +0200 Received: from TY5P286MB319683.JPNP286.PROD.OUTLOOK.COM (2603:1096:403:511::19) by TYRP286MB5946.JPNP286.PROD.OUTLOOK.COM (2603:1096:405:2f0::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.546.15; Fri, 23 Oct 2026 04:52:37 +0000 Received: from TY5P286MB319683.JPNP286.PROD.OUTLOOK.COM ([fe80::5f8d:a6bf:3b84:5a82]) by TY5P286MB319683.JPNP286.PROD.OUTLOOK.COM ([fe80::5f8d:a6bf:3b84:5a82%5]) with mapi id 15.21.0546.016; Fri, 23 Oct 2026 04:52:37 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=selector1 header.d=valinux.co.jp header.i="@valinux.co.jp" header.h="From:Date:Subject:Message-Id:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=hAxf8XsIn5+QdG2sQfi3C9FjG2nKn4SKJS4k2wUUeuUlZFIxltS4iCAQRcd8HnzbAblhZIXMnMT/rJ7DUbsT+QndkKndxLcEIVvbEmYi0wVletVJY+sz7sifzsqWyzlz6PMyg68jeAP+Yi7HpeCLwT0IQGEjUHj+zDRr08IP01LodbHfXQaoNmRui0lIXzQWlKN7R5Tu0PkH2EVecVwCk9JyhFJ3dLF1KILNTQTTlnl9KfwJLK0cMb8eNTbkaZc+FsztmQjHSmC9X9d6DYPzVDksnvVNEE52H0GWL2CO5jRozSB5lT9v+d4f8dONsHjeTdkZS80kl0ANeNf3prc/GQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=B7ltYEKX+cKqsvP9h4OtOTgNQtNgp6o3XIpTxvBpIi8=; b=HbzLjZ26tRAajMSU+y3GNU7j2cQS4dQGZGm2sr7DUVD3JwDo7UQ/i2NmIKiO9Yq9742YMVJZqyg86Dx797AV5YKdX5dDUrnQiSZ/KbBvVcKlY98IF8kQqFXoxDzM26pF/ADPj5Gfy5Y89CQIE5lCmNHq5ktuQacvD4QVsWwFnSaVxZoZ0OYgmA3lfw6cv2+n2tsmNfPDB9GkfwUWhhJmvnMCIWe6TwB1P6WuwvHJHxLVvQjrH8Td7xJWVFGVYaw+A8D0uNS1TQmVHlA+ZF8Wk+J6Hgpd4BPeJW3IsTmO6zphaSZH9Kytnzlv/4GN+8efnLkBhOgxnbruY/dIZslDzw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=valinux.co.jp; dmarc=pass action=none header.from=valinux.co.jp; dkim=pass header.d=valinux.co.jp; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=valinux.co.jp; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=B7ltYEKX+cKqsvP9h4OtOTgNQtNgp6o3XIpTxvBpIi8=; b=g0r0d7yGMnKbTDI7j6d9QdEW5/w+RrW8lWxo67jyDEBuAjA+ShgH9obADfY6lg8ZARNHWbj92FlqRowp1uTA80vtnYszCS3jjEvdhojcKnOY2cdJWXSeYC6bLUbh9XjBkShsKM127v3EOCY15ibDASU35AXougVneBFk+4MtoPc= Authentication-Results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=valinux.co.jp; From: Ryo Takakura To: xen-devel@lists.xenproject.org Cc: sstabellini@kernel.org, julien@xen.org, bertrand.marquis@arm.com, michal.orzel@amd.com, Volodymyr_Babchuk@epam.com, andrew.cooper3@citrix.com, anthony.perard@vates.tech, jbeulich@suse.com, roger@xenproject.org, ross.lagerwall@citrix.com, Ryo Takakura Subject: [RFC 1/5] xen/arm64: livepatch: enable attaching callbacks Date: Fri, 23 Oct 2026 13:52:22 +0900 Message-Id: <20261023045222.163817-1-takakura@valinux.co.jp> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261023044809.162759-1-takakura@valinux.co.jp> References: <20261023044809.162759-1-takakura@valinux.co.jp> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: TYCP301CA0025.JPNP301.PROD.OUTLOOK.COM (2603:1096:400:381::12) To TY5P286MB319683.JPNP286.PROD.OUTLOOK.COM (2603:1096:403:511::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: TY5P286MB319683:EE_|TYRP286MB5946:EE_ X-MS-Office365-Filtering-Correlation-Id: 5d1247ef-7435-44d3-8283-08df30c1759b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|376014|7416014|23010399003|1800799024|6133799003|10067099003|18002099003|261021123027099003|22082099003|261022105027099003|3023799007|261022075727099003|261021133027099003|261021130227099003|260925021311599003|260925021911599003|260925022911599003|261021135927099003|261021121427099003|261009223027099003|261022115727099003|261022113327099003|56012099006; X-Microsoft-Antispam-Message-Info: BIeYR45+uszKnZ2XZjVnfSfsVNvxU3SAZcsvIPDS4C9cKKL87xPOx3FNNJC9Ra7T1EGZgsVMhxcza5MLfgxrf4uP27hP10vWeaHvGDi7jpIa5vHesA+0gAc4jlWvQyiVtBxc4Vr1GBdUAjswwv3eN22cTdN9Sc36eY/AVVc5n4x/wdI3YoobbGQYyFvSdX8x/4hkPsvSD/EJC2EAIiYCPprOECuCss5w9keld38CC8oHiD9h9oZV7EDXDmuVc4pNmuwIgxPvnJ2sjNvgTG+ogny8tVF6HNuVCPclpoMYa0/lCzy1aH2PGV6QfARwuhrOgwPBnPIUHtBOiADZ07sUWXQEA6wTlR6goedEwK7AnLEOqz0xS+4dkALbFYAgVpT9O3nhqzG/SBVrPhkISwTkrB8DiifEjqQgFQe+wBTiaapbeO1aR1KYBRbJ9ezUo3qg3m9Safe3+FdnzVzjhNw0+xKSrR1ScqUV6XGCFt1KS8SQfRDd2zLUVEGouuZqZR5DT52naai5wXHtSUGnm+Yixw69FJjUtSGuFoIsHDCkWYNB98rvvDhm3kxy7w8d8witHQV9Ztd35uhjdEnAP55xj/AN9+3W9x1OMUIU45K6tpOBwiwj2nE1sdqMlouG6D+4vAUi12SgZuw6cVUfhYQn8LBsId8XRq8B/hQnTDQ8YVg= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:TY5P286MB319683.JPNP286.PROD.OUTLOOK.COM;PTR:;CAT:NONE;SFS:(13230040)(366016)(376014)(7416014)(23010399003)(1800799024)(6133799003)(10067099003)(18002099003)(261021123027099003)(22082099003)(261022105027099003)(3023799007)(261022075727099003)(261021133027099003)(261021130227099003)(260925021311599003)(260925021911599003)(260925022911599003)(261021135927099003)(261021121427099003)(261009223027099003)(261022115727099003)(261022113327099003)(56012099006);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?WZqNCmFqko6fYBtQwSszb07Xq/enqZq3AS/UEXJ7VbpV1xGuqyQmqMb23pKj?= =?us-ascii?Q?loH1IgZyNp8+7yfqAXAqhdouMuDXKMktRR8DgbnwVatP7yNuJ8Fx/rq6IhYY?= =?us-ascii?Q?teInlSxUIdIKYLF2EkT5U/UAL0qIFOItboUeTfHYPvAJ4pAED5NL1HiCh3Gj?= =?us-ascii?Q?BbzdfOOkpnVGJqaENOc4l2XY/BnXJCf8JyH4kA5IeizIyBKvZuEOvYJ9ELe1?= =?us-ascii?Q?VoSsHvyoeGJaeujBaTTc39RK5hNvhXcOchZJUXCkZGo0ScJDSEGdOfMpQdcO?= =?us-ascii?Q?KRY8WXNa9L0rsis0YcmqAlnOz9V63o0vR53pY3RB+KGi+vaNhOo9wl78w97T?= =?us-ascii?Q?cQo2LZX6bnOQyfbmLvqb2zAteTTogmpwKiFZMo2ziRosh/+dDojqtfB/QvxT?= =?us-ascii?Q?ECz0OiVNNSF6NzEERKaKjXeS03lcNjTA6SeYdxKwz100ep4IuJjgd00tl0Hx?= =?us-ascii?Q?biDgl8+w4NRH1XwrrBRWPPQ6hzpAnl3rfJD60zkqCghgFIKdQmlgPHZ6TDQY?= =?us-ascii?Q?+pwiqNXcG6hJYRyPRwE4Fb/mwrwa4xnbqA4Oz/cd3af/pwzaSuq3Oc8XkcCl?= =?us-ascii?Q?w89zEsdV8wgAxBtBlrOIy1x54hsYF3YFh1UWIH/pvt6PCxauZn9no6nmUNCi?= =?us-ascii?Q?6cInHKkIR/Fn7Z2//8y/KZ7PBztgRUmwddjIYQvkYzFrIqkrPvH5e1bI7hXB?= =?us-ascii?Q?NOla9/QMB6RsEottn2l0TfX0zYnzaur4d9D1B6Qs6GkGILZ1FP2YgAKbK4lR?= =?us-ascii?Q?g2LbXiO35JAz7w/Xm8QC6ZfbCG35018FicPXRCQ/TLohcnrusCSuTPhLgWpq?= =?us-ascii?Q?AE7/LT2or17KgPJFuNs+8pWfbVaarqorRD3xLFH/ArLmpREBnPqre9kTyG+b?= =?us-ascii?Q?3CkNFY0S/7koiKihYJ3JsG3EZOlGmQ0hCq7SzV9uvu4WIF9EVk3ZZjv5bqsb?= =?us-ascii?Q?fKmU6FJAASZluAWhoedx7KReAoqJJ9fy0GCmZUw3A31X4tRpeoP6MDb01kS8?= =?us-ascii?Q?JkWEr2Qr33ow1AN+2SY6M32BSrEvr3EHZjc1bcfGPWr76CIF7R9HakG3WIn1?= =?us-ascii?Q?R4Mz5/0/0FwgcTvDqQaiF1sSy44x35TRVRC5qAB7NdojO3ckfUwOqCbaRtEf?= =?us-ascii?Q?gZZp/OltV8yUyd73Yx8f0FhTiAO+EQ9/iuPut21ZHuac+tP0IphMVxZeLEKd?= =?us-ascii?Q?tqkxf4XzQF3iJn3UEhW9aA06nWAvfKikYXmYZLzTkB8XohngYCvQa09d3WrV?= =?us-ascii?Q?qRdo9keYx4AZOArPZ2tr0M5WmG7qYvIc+H+EMnxVmVdKu+4W7Fe0x40D/bcf?= =?us-ascii?Q?61k5Z18tZOhOfvNbL/XHi2b4dLxy28SW/lBulsMtHoF7EepxssBscpLe9UTY?= =?us-ascii?Q?yU/LFYp9klg3z4361kodJ0uEVNeDwcYkWWyTnYL3byOOA5O7J4gy1g+pJGQ5?= =?us-ascii?Q?/EnF5xGXeSNmdQ/rDQxkkjHuGeoC3soU3wSwRKOYWC8FnYo8iA0+sySsiXbV?= =?us-ascii?Q?YrtlCb1PTz3u9Iy1k2nSJLJLJiunMtaPHiTJDq3AaX5F/q0yLjGuqXAtS3F5?= =?us-ascii?Q?ob/vfYs0eWa6gyV6RwPbl/zGUXNZIfNt5BvZsLz18Gp+yhjNiKGrXs2Wif4c?= =?us-ascii?Q?ZW7QdZJwG3wVSKReQ0sKNVIkvhuxW8tdZuAavYiS3Nxv+PUyZ+iKYwnqif0f?= =?us-ascii?Q?iPerjlsRat3GGUMKrSsAoJtrpLJtVpIgMFitFO2NxOjteqPjzP5htmKlpqxa?= =?us-ascii?Q?pR7m6R02rw=3D=3D?= X-OriginatorOrg: valinux.co.jp X-MS-Exchange-CrossTenant-Network-Message-Id: 5d1247ef-7435-44d3-8283-08df30c1759b X-MS-Exchange-CrossTenant-AuthSource: TY5P286MB319683.JPNP286.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Oct 2026 04:52:37.0674 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 7a57bee8-f73d-4c5f-a4f7-d72c91c8c111 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 1hJDebCg+WYZRMeZ5nn1V3AWPX9tkniGm+8HMYql6P1pmWC4OwcYQujBbddB5cINzBtLLGl64A1qaW4XnePAdA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: TYRP286MB5946 X-purgate-ID: tlsNG-d62444/1792731162-1FC69757-54FF4759/0/0 X-purgate-type: clean X-purgate-size: 15814 X-ZohoMail-DKIM: pass (identity @valinux.co.jp) X-ZM-MESSAGEID: 1792731190238158500 Content-Type: text/plain; charset="utf-8" Linux ftrace allows registering callbacks which is useful for debugging and tracing events. On Linux, it is done by reserving NOPs at function entry at compile time which can later be patched with a branch instruction to a trampoline. This patch introduces similar callback feature adopting linux-like approach of reserving 2 NOPs at function entry. When user requests the feature, the NOPs will be livepatched with: mov x9, lr bl xen_livepatch_trace_caller where xen_livepatch_trace_caller serves as a trampoline for saving registers before calling callback functions. Callback functions receive two arguments: ip: Address of the traced function parent_ip: Address where the traced function was called One can request the feature by specifying the section for struct livepatch_func as .livepatch.traces. Signed-off-by: Ryo Takakura --- 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 | 34 ++++++++++++++ xen/arch/arm/include/asm/arm64/insn.h | 1 + xen/common/Kconfig | 3 ++ xen/common/livepatch.c | 68 +++++++++++++++++++++++++-- xen/include/xen/livepatch.h | 8 ++++ xen/include/xen/livepatch_payload.h | 7 +-- 11 files changed, 205 insertions(+), 6 deletions(-) create mode 100644 xen/arch/arm/arm64/livepatch-asm.S diff --git a/xen/arch/arm/Kconfig b/xen/arch/arm/Kconfig index b9c5c40678..37fdbcc416 100644 --- a/xen/arch/arm/Kconfig +++ b/xen/arch/arm/Kconfig @@ -286,6 +286,19 @@ config PCI_PASSTHROUGH help This option enables PCI device passthrough =20 +config ARM64_PATCHABLE_FUNCTION_ENTRY + bool "Reserve two NOPs at function entry for tracing" + depends on ARM_64 && LIVEPATCH && $(cc-option,-fpatchable-function-ent= ry=3D2) + select HAS_LIVEPATCH_TRACE + default n + help + This option compiles Xen with -fpatchable-function-entry=3D2, which + reserves two NOP instructions at the beginning of functions. The NOPs + will be livepatched with a branch instruction to callback functions + registered by user. + + Enable this to use livepatch tracing feature. + endmenu =20 menu "ARM errata workaround via the alternative framework" diff --git a/xen/arch/arm/arch.mk b/xen/arch/arm/arch.mk index dea8dbd18a..940f1217c4 100644 --- a/xen/arch/arm/arch.mk +++ b/xen/arch/arm/arch.mk @@ -19,6 +19,9 @@ endif CFLAGS-$(CONFIG_ARM_64) +=3D -mgeneral-regs-only # No fp registers etc $(call cc-option-add,CFLAGS-$(CONFIG_ARM_64),CC,-mno-outline-atomics) =20 +# Reserve entries for livepatch tracing +CFLAGS-$(CONFIG_ARM64_PATCHABLE_FUNCTION_ENTRY) +=3D -fpatchable-function-= entry=3D2 + ifneq ($(filter command line environment,$(origin CONFIG_EARLY_PRINTK)),) $(error You must use 'make menuconfig' to enable/disable early printk = now) endif diff --git a/xen/arch/arm/arm64/Makefile b/xen/arch/arm/arm64/Makefile index 6491c5350b..7f52b10e66 100644 --- a/xen/arch/arm/arm64/Makefile +++ b/xen/arch/arm/arm64/Makefile @@ -12,6 +12,7 @@ obj-y +=3D entry.o obj-y +=3D head.o obj-y +=3D insn.o obj-$(CONFIG_LIVEPATCH) +=3D livepatch.o +obj-$(CONFIG_ARM64_PATCHABLE_FUNCTION_ENTRY) +=3D livepatch-asm.o obj-y +=3D smc.o obj-y +=3D smpboot.o obj-$(CONFIG_ARM64_SVE) +=3D sve.o sve-asm.o diff --git a/xen/arch/arm/arm64/insn.c b/xen/arch/arm/arm64/insn.c index 6b97a84ba7..dc3ddc6351 100644 --- a/xen/arch/arm/arm64/insn.c +++ b/xen/arch/arm/arm64/insn.c @@ -218,6 +218,11 @@ u32 __kprobes aarch64_insn_gen_nop(void) return aarch64_insn_gen_hint(AARCH64_INSN_HINT_NOP); } =20 +u32 aarch64_insn_gen_move_reg(uint32_t rd, uint32_t rm) +{ + return 0xaa0003e0 | (rm << 16) | rd; +} + /* * Decode the imm field of a branch, and return the byte offset as a * signed value (so it can be used when computing a new branch diff --git a/xen/arch/arm/arm64/livepatch-asm.S b/xen/arch/arm/arm64/livepa= tch-asm.S new file mode 100644 index 0000000000..4f94e791a3 --- /dev/null +++ b/xen/arch/arm/arm64/livepatch-asm.S @@ -0,0 +1,68 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * xen/arch/arm/arm64/livepatch-asm.S + * + * Trampoline for livepatch tracer. + * + * If livepatch tracer is enabled, function entries are placed + * with two NOPs. arch_livepatch_apply_trace() will be livepatching + * those NOPs to: + * + * mov x9, x30 + * bl xen_livepatch_trace_caller + * + * x9 =3D traced function's original LR + * x30 =3D traced function + 8 + */ +FUNC(xen_livepatch_trace_caller) + /* + * Registers to save/restore: + * + * x0-x8 Function arguments + * x9 Original LR + * x29 Original FP + * x30 PC to return + */ + sub sp, sp, #96 + + /* Save function arguments */ + stp x0, x1, [sp, #0] + stp x2, x3, [sp, #16] + stp x4, x5, [sp, #32] + stp x6, x7, [sp, #48] + str x8, [sp, #64] + + /* Save LR, FP, PC */ + str x9, [sp, #72] + str x29, [sp, #80] + str x30, [sp, #88] + + /* + * xen_livepatch_trace_dispatcher(ip, parent_ip) + * + * ip: Address of the traced function. + * parent_ip: Address where the traced function was called. + */ + sub x0, x30, #8 + mov x1, x9 + bl xen_livepatch_trace_dispatcher + + /* Restore function arguments */ + ldp x0, x1, [sp, #0] + ldp x2, x3, [sp, #16] + ldp x4, x5, [sp, #32] + ldp x6, x7, [sp, #48] + ldr x8, [sp, #64] + + /* Restore FP, LR, PC */ + ldr x29, [sp, #80] + ldr x30, [sp, #72] + ldr x9, [sp, #88] + + /* Restore SP */ + add sp, sp, #96 + + /* ret x9 */ + .inst 0xd65f0120 + sb +END(xen_livepatch_trace_caller) diff --git a/xen/arch/arm/arm64/livepatch.c b/xen/arch/arm/arm64/livepatch.c index 39159ba8b5..17753f4463 100644 --- a/xen/arch/arm/arm64/livepatch.c +++ b/xen/arch/arm/arm64/livepatch.c @@ -14,6 +14,10 @@ #include #include =20 +#ifdef CONFIG_ARM64_PATCHABLE_FUNCTION_ENTRY +extern void xen_livepatch_trace_caller(void); +#endif + void arch_livepatch_apply(const struct livepatch_func *func, struct livepatch_fstate *state) { @@ -492,6 +496,36 @@ int arch_livepatch_perform_rela(struct livepatch_elf *= elf, return -EINVAL; } =20 +#ifdef CONFIG_ARM64_PATCHABLE_FUNCTION_ENTRY +void arch_livepatch_apply_trace(const struct livepatch_func *trace) +{ + uint32_t *new_ptr; + uint32_t insns[2]; + + new_ptr =3D trace->old_addr - (void *)_start + vmap_of_xen_text; + + insns[0] =3D aarch64_insn_gen_move_reg(9, 30); /* mov x9, lr */ + insns[1] =3D aarch64_insn_gen_branch_imm((unsigned long)trace->old_add= r + ARCH_PATCH_INSN_SIZE, + (unsigned long)xen_livepatch_tr= ace_caller, + AARCH64_INSN_BRANCH_LINK); + + memcpy(new_ptr, insns, sizeof(insns)); + clean_and_invalidate_dcache_va_range(new_ptr, sizeof(insns)); +} + +void arch_livepatch_revert_trace(const struct livepatch_func *trace) +{ + void *new_ptr =3D trace->old_addr - (void *)_start + vmap_of_xen_text; + uint32_t insns[2] =3D { + aarch64_insn_gen_nop(), + aarch64_insn_gen_nop(), + }; + + memcpy(new_ptr, insns, sizeof(insns)); + clean_and_invalidate_dcache_va_range(new_ptr, sizeof(insns)); +} +#endif /* CONFIG_ARM64_PATCHABLE_FUNCTION_ENTRY */ + /* * Local variables: * mode: C diff --git a/xen/arch/arm/include/asm/arm64/insn.h b/xen/arch/arm/include/a= sm/arm64/insn.h index ab290030ab..a83d8de0e4 100644 --- a/xen/arch/arm/include/asm/arm64/insn.h +++ b/xen/arch/arm/include/asm/arm64/insn.h @@ -82,6 +82,7 @@ u32 aarch64_insn_gen_branch_imm(unsigned long pc, unsigne= d long addr, enum aarch64_insn_branch_type type); u32 aarch64_insn_gen_hint(enum aarch64_insn_hint_op op); u32 aarch64_insn_gen_nop(void); +u32 aarch64_insn_gen_move_reg(uint32_t rd, uint32_t rm); =20 /* Wrapper for common code */ static inline bool insn_is_branch_imm(u32 insn) diff --git a/xen/common/Kconfig b/xen/common/Kconfig index 5b289e444f..7cf6937c2f 100644 --- a/xen/common/Kconfig +++ b/xen/common/Kconfig @@ -605,6 +605,9 @@ config TRACEBUFFER to be collected at run time for debugging or performance analysis. Memory and execution overhead when not active is minimal. =20 +config HAS_LIVEPATCH_TRACE + bool + config LLC_COLORING bool "Last Level Cache (LLC) coloring" if EXPERT depends on HAS_LLC_COLORING diff --git a/xen/common/livepatch.c b/xen/common/livepatch.c index 7515a040ad..70f601846f 100644 --- a/xen/common/livepatch.c +++ b/xen/common/livepatch.c @@ -198,6 +198,41 @@ static const char *cf_check livepatch_symbols_lookup( return n; } =20 +#ifdef CONFIG_HAS_LIVEPATCH_TRACE +void __attribute__((patchable_function_entry(0, 0))) +xen_livepatch_trace_dispatcher(unsigned long ip, unsigned long parent_ip) +{ + const struct payload *data; + unsigned int i; + const struct livepatch_func *f; + + rcu_read_lock(&rcu_payload_lock); + + list_for_each_entry_rcu ( data, &payload_list, list ) + { + if ( data->state !=3D LIVEPATCH_STATE_APPLIED || !data->is_trace ) + continue; + + for ( i =3D 0; i < data->nfuncs; ++i ) + { + f =3D &data->funcs[i]; + + if ( (unsigned long)f->old_addr !=3D ip ) + continue; + + ASSERT(f->new_addr); + ((livepatch_trace_func_t)f->new_addr)((unsigned long)f->old_ad= dr, parent_ip); + + goto out; + } + } + +out: + rcu_read_unlock(&rcu_payload_lock); +} + +#endif /* CONFIG_HAS_LIVEPATCH_TRACE */ + /* Lookup function's old address if not already resolved. */ static int resolve_old_address(struct livepatch_func *f, const struct livepatch_elf *elf) @@ -551,6 +586,9 @@ static int check_patching_sections(const struct livepat= ch_elf *elf) { unsigned int i; static const char *const names[] =3D { ELF_LIVEPATCH_FUNC, +#ifdef CONFIG_HAS_LIVEPATCH_TRACE + ELF_LIVEPATCH_TRACES, +#endif ELF_LIVEPATCH_LOAD_HOOKS, ELF_LIVEPATCH_UNLOAD_HOOKS, ELF_LIVEPATCH_PREAPPLY_HOOK, @@ -696,7 +734,14 @@ static int prepare_payload(struct payload *payload, struct virtual_region *region; int rc; =20 - sec =3D livepatch_elf_sec_by_name(elf, ELF_LIVEPATCH_FUNC); +#ifdef CONFIG_HAS_LIVEPATCH_TRACE + sec =3D livepatch_elf_sec_by_name(elf, ELF_LIVEPATCH_TRACES); + payload->is_trace =3D !!sec; + + if ( !sec ) +#endif + sec =3D livepatch_elf_sec_by_name(elf, ELF_LIVEPATCH_FUNC); + if ( sec ) { if ( !section_ok(elf, sec, sizeof(*payload->funcs)) ) @@ -721,6 +766,13 @@ static int prepare_payload(struct payload *payload, return -EOPNOTSUPP; } =20 + if ( payload->is_trace && !f->new_addr ) + { + printk(XENLOG_ERR LIVEPATCH "%s: Trace new_addr is NULL\n", + elf->name); + return -EINVAL; + } + /* 'old_addr', 'new_addr', 'new_size' can all be zero. */ if ( !f->old_size ) { @@ -1460,7 +1512,12 @@ static int apply_payload(struct payload *data) continue; } =20 - arch_livepatch_apply(func, state); +#ifdef CONFIG_HAS_LIVEPATCH_TRACE + if ( data->is_trace ) + arch_livepatch_apply_trace(func); + else +#endif + arch_livepatch_apply(func, state); state->applied =3D LIVEPATCH_FUNC_APPLIED; } =20 @@ -1509,7 +1566,12 @@ int revert_payload(struct payload *data) continue; } =20 - arch_livepatch_revert(func, state); +#ifdef CONFIG_HAS_LIVEPATCH_TRACE + if ( data->is_trace ) + arch_livepatch_revert_trace(func); + else +#endif + arch_livepatch_revert(func, state); state->applied =3D LIVEPATCH_FUNC_NOT_APPLIED; } =20 diff --git a/xen/include/xen/livepatch.h b/xen/include/xen/livepatch.h index 5dc8c61d37..ae34c938d6 100644 --- a/xen/include/xen/livepatch.h +++ b/xen/include/xen/livepatch.h @@ -26,6 +26,7 @@ struct xen_sysctl_livepatch_op; #define LIVEPATCH "livepatch: " /* ELF payload special section names. */ #define ELF_LIVEPATCH_FUNC ".livepatch.funcs" +#define ELF_LIVEPATCH_TRACES ".livepatch.traces" #define ELF_LIVEPATCH_DEPENDS ".livepatch.depends" #define ELF_LIVEPATCH_XEN_DEPENDS ".livepatch.xen_depends" #define ELF_BUILD_ID_NOTE ".note.gnu.build-id" @@ -86,6 +87,11 @@ void arch_livepatch_init(void); =20 int arch_livepatch_verify_func(const struct livepatch_func *func); =20 +typedef void (*livepatch_trace_func_t)(unsigned long ip, + unsigned long parent_ip); +void xen_livepatch_trace_dispatcher(unsigned long ip, + unsigned long parent_ip); + static inline unsigned int livepatch_insn_len(const struct livepatch_func *func, const struct livepatch_fstate *state) @@ -120,8 +126,10 @@ void arch_livepatch_revive(void); =20 void arch_livepatch_apply(const struct livepatch_func *func, struct livepatch_fstate *state); +void arch_livepatch_apply_trace(const struct livepatch_func *trace); void arch_livepatch_revert(const struct livepatch_func *func, struct livepatch_fstate *state); +void arch_livepatch_revert_trace(const struct livepatch_func *trace); void arch_livepatch_post_action(void); =20 void arch_livepatch_mask(void); diff --git a/xen/include/xen/livepatch_payload.h b/xen/include/xen/livepatc= h_payload.h index c6dc7cb5fa..3a81abe0de 100644 --- a/xen/include/xen/livepatch_payload.h +++ b/xen/include/xen/livepatch_payload.h @@ -52,9 +52,10 @@ struct payload { size_t ro_size; /* .. and its size (if any). */ unsigned int pages; /* Total pages for [text,rw,ro]_a= ddr */ struct list_head applied_list; /* Linked to 'applied_list'. */ - const struct livepatch_func *funcs; /* The array of functions to patc= h. */ - struct livepatch_fstate *fstate; /* State of patched functions. */ - unsigned int nfuncs; /* Nr of functions to patch. */ + const struct livepatch_func *funcs; /* The array of functions to patc= h or trace. */ + struct livepatch_fstate *fstate; /* State of patched/traced functi= ons. */ + unsigned int nfuncs; /* Nr of functions to patch or tr= ace. */ + bool is_trace; /* Is a trace payload. */ const struct livepatch_symbol *symtab; /* All symbols. */ const char *strtab; /* Pointer to .strtab. */ struct virtual_region region; /* symbol, bug.frame patching and --=20 2.34.1 From nobody Mon Nov 23 22:39:58 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; arc=pass (i=1 dmarc=pass fromdomain=valinux.co.jp); dmarc=pass(p=none dis=none) header.from=valinux.co.jp ARC-Seal: i=2; a=rsa-sha256; t=1792731263; cv=pass; d=zohomail.com; s=zohoarc; b=UU/tP2NWDh5j+9nwao9J5AuMuGiz1VvL3IUSxUjBOaZ3vgSaPHFxmaKX6901RKJwn4KxyjdOetRgHQMdBk3obG0EVR+nur5UwAlgfIsBcgE/fNjJAIOHw++c0E4QnktUfMoQ21o2bbUbAxp7oVsyZzQLIbLMFTO7loBhX4MK2UU= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792731263; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=xAdKilPb0/9LuUkjGbAwn7u5vPJDH2Al5yQD43o4oA0=; b=FLjVP38hBejEKVPsVSlgsDPR4iGWhQ1GhxTSktihpK8BHU7QY0Ij8rbZxmGTMKpmS7AVd1ocbX7vBWWL4YBFer0j87PU/R6Uh0/4cn/msaRwagZNC19eutjTBdw1DBWP9S360/3IaylZHlfkB2tku/Ij5g0UW0X/LyCgsU1I1Tg= ARC-Authentication-Results: i=2; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; arc=pass (i=1 dmarc=pass fromdomain=valinux.co.jp); dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 179273126326326.763947415342386; Thu, 22 Oct 2026 21:54:23 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1458891.1670970 (Exim 4.92) (envelope-from ) id 1xK7I9-00083o-3R; Fri, 23 Oct 2026 04:54:09 +0000 Received: by outflank-mailman (output) from mailman id 1458891.1670970; Fri, 23 Oct 2026 04:54:09 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xK7I9-00083h-03; Fri, 23 Oct 2026 04:54:09 +0000 Received: by outflank-mailman (input) for mailman id 1458891; Fri, 23 Oct 2026 04:54:07 +0000 Received: from mx.expurgate.net ([194.145.224.20]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xK7I6-00082p-IA for xen-devel@lists.xenproject.org; Fri, 23 Oct 2026 04:54:07 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1xK7I5-00CysQ-VH for xen-devel@lists.xenproject.org; Fri, 23 Oct 2026 06:54:05 +0200 Received: from [10.42.69.8] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6adae86c-e002-0a2a0a5209dd-0a2a4508bb80-4 for ; Fri, 23 Oct 2026 06:54:05 +0200 Received: from [52.101.125.122] (helo=TYVP286CU001.outbound.protection.outlook.com) by tlsNG-c1860d.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6adae86b-f659-0a2a45080019-34657d7a5bce-3 for ; Fri, 23 Oct 2026 06:54:05 +0200 Received: from TY5P286MB319683.JPNP286.PROD.OUTLOOK.COM (2603:1096:403:511::19) by TYYP286MB1715.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:fd::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.546.18; Fri, 23 Oct 2026 04:54:01 +0000 Received: from TY5P286MB319683.JPNP286.PROD.OUTLOOK.COM ([fe80::5f8d:a6bf:3b84:5a82]) by TY5P286MB319683.JPNP286.PROD.OUTLOOK.COM ([fe80::5f8d:a6bf:3b84:5a82%5]) with mapi id 15.21.0546.016; Fri, 23 Oct 2026 04:54:01 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=selector1 header.d=valinux.co.jp header.i="@valinux.co.jp" header.h="From:Date:Subject:Message-Id:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=hBiw4vR+DAgUd7jTENUklIfb8beVK0JJ9JwIY+mrYTBOl/oi2RgdsPvig8om9l+UMEPlT6P5b2Rb0piVrBxn4j7zZ/qYfzeqmP3GTiD9I0ZBDoMjX546IRLy0Nq5t0J/6x8kL74OYP1EFMcnaKmJ8WkQw6jmPMyslMk06S70sF26U3FjkN9M01/TYzHTTpDtONdkQmjlINey8w4RUSIUc6XW402UxDlmqSMgJ6Gr9pJOf7C6bcD2fYXIVXX9LvT0E200L+lUIm1yDCiV+SWMdzS03wQYoVONQzE8sGhzVvz6yBqjKyFAgoWMd193k0RZGV8NrgsqnJVtSBsvGNDAlg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=xAdKilPb0/9LuUkjGbAwn7u5vPJDH2Al5yQD43o4oA0=; b=h5uehEbZlMj0k/8afK7sLPPQxuAucddfpL8fbA8utBVj2nLtFPwImWCw3Q61RxLeJm9M3dKYXm1RGWg5JtVZ9lkW3BMdv4kqmkct9bE3gSrubY7FibfSMr4M9UPDQYV2PbDtqq3yqS8hPvmBm0Wjo8qNFr22tv5l6LyUSR1WAgwL1q9sg+XSMCLnfff/pkMgUO6OTekJWT0g1kRi3+cKFzrZ1r67oLacitM+3S1WSJCsJP4/GHf08EyVbcXxfDk+0ZlZr1FbLwg6Sk4g24wxCwacBgvbEU5R0CQW+bR5b4+LjdAMQ2L5bxPFIcQZiH8NV6uxOhOKz404SDgSUHkIkQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=valinux.co.jp; dmarc=pass action=none header.from=valinux.co.jp; dkim=pass header.d=valinux.co.jp; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=valinux.co.jp; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=xAdKilPb0/9LuUkjGbAwn7u5vPJDH2Al5yQD43o4oA0=; b=L8hXsUSNH3iC5wM/UiWVsiVYTNgHvn9odn8Psj1TQfJT08XHwU0vQlAflQ8axWVHol3gubSoVPfnwoQ7Qt+ECdH7vXEWTNMGCzfxyDAOsu5+caHb9Vs8iay2rGgnGmoeOugMjUU4bJIcjmombtF7kaH0QbUvLeFU5fwT21jcowc= Authentication-Results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=valinux.co.jp; From: Ryo Takakura To: xen-devel@lists.xenproject.org Cc: roger@xenproject.org, ross.lagerwall@citrix.com, Ryo Takakura Subject: [RFC 2/5] xen/livepatch: Allow tracers to schedule livepatch Date: Fri, 23 Oct 2026 13:53:46 +0900 Message-Id: <20261023045346.164171-1-takakura@valinux.co.jp> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261023044809.162759-1-takakura@valinux.co.jp> References: <20261023044809.162759-1-takakura@valinux.co.jp> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: TYWPR01CA0002.jpnprd01.prod.outlook.com (2603:1096:400:a9::7) To TY5P286MB319683.JPNP286.PROD.OUTLOOK.COM (2603:1096:403:511::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: TY5P286MB319683:EE_|TYYP286MB1715:EE_ X-MS-Office365-Filtering-Correlation-Id: adcc21f8-f078-41b9-b52d-08df30c1a7a5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|23010399003|1800799024|376014|18002099003|22082099003|261022105027099003|261022075727099003|261009223027099003|261021123027099003|261021121427099003|10067099003|56012099006; X-Microsoft-Antispam-Message-Info: OjlHTy4xndyGBcuGnZKPdVfKNYblO+WWm2wB1dC3nH8OQwnIBbYDtB439hoyrMxGemAyErn1DF5/iQ7CeUXca9CWESApPcZ4s+cicLFAJT7q0Tb9IptwExq3COqf/3Dfo0xT+841/8o+VC+/kSenT8WvooaYmjjSX6+fr23rMZhDPrBNxJCQUcTSxeA61o+nseDL6O2tbQTUNEdfEY49oGyTz7XwqU/0SPNY8T//Q8y26s12oObo45F0HWh0rnKvxlERnRwS/xPLwHAdmKoxGFXNPN1Z6AY7ohT0vjBsBTN5/CbCZZNsSVuoXXUqXybz5+uvyDoMFlQkUdSZoXEqrKWhpBYZHnP7Gh+rrwjspVLqXkLkPurDTLbYQUSS8d9Aq8S+m3KGtwBmsoZw9GxVmZmNAMOYPuHTu9NL0DCOAukhFrRpFfAr1MCZpAhF40VNOPMJ5wOZwdVidhzx+kKiu59p+LuX/i/LH1QXBesIKnAwgKTPSwfuQy6QYC6HllXYBoNTwPfp7FbvA5VB7ooCiE/6wyKecQWkf+mn9YLFPOPujiFNI/kWTEkTWKOzxllto1eIAiE7M756enn6ssTNmTN+Sqrskm7mg2MFa3QSQwTXkWLsnttHqtzWEL4RygkQlxI/M+vHZRDjr7m22cnMHQfaXCmQ5yYNcgFqnzDUg6w= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:TY5P286MB319683.JPNP286.PROD.OUTLOOK.COM;PTR:;CAT:NONE;SFS:(13230040)(366016)(23010399003)(1800799024)(376014)(18002099003)(22082099003)(261022105027099003)(261022075727099003)(261009223027099003)(261021123027099003)(261021121427099003)(10067099003)(56012099006);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?4KCOEWm6Gi0sMHUqN+14sRM+Ji6T4WypUqurLnsICu8fC64RWBCADnsMxIne?= =?us-ascii?Q?8xhkWIwQLSeLz72To8DZXsaNtE0RTNDN+spfanXtD7GZrhxD1MBvxzEgn9kf?= =?us-ascii?Q?cAOmeAy5hnQQr5JsED9F4mbqMsD3pdIuoFe3mnpQBEUBbIJeQSC5NP24wkyV?= =?us-ascii?Q?2noXPbY1YCzf4vYPB1cagnY6RhC/EQXbmjEY+wvuP5Dk2s0eAqN0cpPCJWnS?= =?us-ascii?Q?SNIdZwwnl91bMBaTsLVugp+4mzasiBk1H3EOe/5U8lKgAEmHa7lIatjMKgcu?= =?us-ascii?Q?bVknXE5wSGJwTJ0Mp+GGmdM1JnpWAU4Z+QiJqcyWv7zOKLiRHtcxazfe4rAg?= =?us-ascii?Q?EjkfnhwMs4modCvFR1vpdDFQ/fTm6g79xpmeUCcevx9s32SEueHgkLlEzW0l?= =?us-ascii?Q?IU5ZFW/+NitJfgGIHHJ7tMrXKvWg/LpzvDUSCXINTvR5mKvVSWyurMomaH5Y?= =?us-ascii?Q?y4sZtqjHXcnk92qIbRzGW3tTu1NpgFr+QoVeFW+mPe4GXV9BEyoM+ZXlIr7e?= =?us-ascii?Q?vEa4F7BcaaqoS/AvhJWN9a8VI2mfDl/wjJvUBZ/8j/ywyrSOrX+MfHBi9Ref?= =?us-ascii?Q?FFRtOEkvZ56PvkC/VjrQMEiDRkrXiE5hTVzZwu2sGqAiV93tZDq0aRmykk/P?= =?us-ascii?Q?5pGsSO0Y3pyziLPoJSE3QE1nHgz9Y8SNPIfzZSws2Y3BP2EyaXML9dZxt1nl?= =?us-ascii?Q?hi0U13BF3jhZNCteJm0mIvdbUHTmIZQ0uEjEpC+TJzBqEK1nTliAxCNYyw4f?= =?us-ascii?Q?RnvyHCG1zAIohhdupzupV4wULyvnN2MJYM8/2A+a7BN1qLbcKWWaqM9T603U?= =?us-ascii?Q?0L747kSIDf+Ra2xZxig6sG2RX2wYZu/fxfsDaw4dI/vuXSA1Nn9nAO7Byt+j?= =?us-ascii?Q?0+S+HKCpMEp+Oj629kYAs1QMKv9h7RaHJHKtO49MGh/TF2C/xH1GK4O1ildA?= =?us-ascii?Q?vk4kgSpojmAznfsS3rguAIZKUm9wXdOU56/j613an6FKvQMs1H5W0z/CdtP0?= =?us-ascii?Q?7eKZonuvNGG41ZjAb0zQhJS/UWvMmzXbaVszv0sOhdiSLgQQeIiSoFZ62xSD?= =?us-ascii?Q?vZFqKyFKyo5tACfL1M2operWOoqJsypWkURcpbxMOJQ5sNvoHKnebts8FISo?= =?us-ascii?Q?mlizuKWL+WAMpRt/fQXzN+CVgGs5CLQLOyaEtrPmu/ZE36bx2zMa4qP3GjM2?= =?us-ascii?Q?PonNGlOS8UGgwiFub29hoPMRHyCKAdw6lu/4ZdmG49OL/3216U4jt8x6dwHd?= =?us-ascii?Q?gyuk1IvjR+8+5Ec94tEporwzADDY31QAqkrZBpxJ6jryhnkx706pXA9XAXmZ?= =?us-ascii?Q?s60wAjP/6G7BS8OlvF8sRs9cuDtj6TTuXaoagY7B8yng9OdfON1a0o7dxoXy?= =?us-ascii?Q?6vOPrIY2QZpOFz2+baLLrRkPEYvWPitkcxAdQ1a7IM/dzyQkLgcyJvzgbetg?= =?us-ascii?Q?4EeT6iNhnOJee5NhLMHqYUVdURPFojGo/M8fRstKNLjBzi03+0pScstqehaX?= =?us-ascii?Q?ZuwhT5JyRwkoobJOf/Nuu0iohkrkSmjZBPHmgfvTqz5B8PSRA2kDcdAhx/xJ?= =?us-ascii?Q?yTn/mmy2PXfJUn25VtcewjE2H+PfnuERtK/5WOu2c9396RSlsi2IspsHrkTs?= =?us-ascii?Q?lKxKKa2McHjON5niGTOsXQq6kinFQ00IhzA5b5iVHwRNDGtsm6kUiWmQOhs0?= =?us-ascii?Q?1ofc/cdHQXWRbZf0DV3YiM36xexQXhwSK58X/qSuN3r2cVUEwtT2lvFlGUp3?= =?us-ascii?Q?tebzq4/Rzg=3D=3D?= X-OriginatorOrg: valinux.co.jp X-MS-Exchange-CrossTenant-Network-Message-Id: adcc21f8-f078-41b9-b52d-08df30c1a7a5 X-MS-Exchange-CrossTenant-AuthSource: TY5P286MB319683.JPNP286.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Oct 2026 04:54:01.0295 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 7a57bee8-f73d-4c5f-a4f7-d72c91c8c111 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: UhF16uKx639UFTaGoaVxagUEPIAxjccRLrmZ8Bhna5vV9+lT+sJGUMAn8wZL+V2LQMubbBa+/ae5ruanJrP/CQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: TYYP286MB1715 X-purgate-ID: tlsNG-c1860d/1792731245-D437587B-17EDFBCE/0/0 X-purgate-type: clean X-purgate-size: 3981 X-ZohoMail-DKIM: pass (identity @valinux.co.jp) X-ZM-MESSAGEID: 1792731265604158500 Content-Type: text/plain; charset="utf-8" Tracers need to patch function entries without uploading a fully filled payload. Add TRACE_ON and TRACE_OFF for scheduling partially filled payloads from tracers. It lets tracers reuse the existing livepatch logic and keeps the payload uploaded by tracers separate from the ones uploaded by users. Signed-off-by: Ryo Takakura --- xen/common/livepatch.c | 60 ++++++++++++++++++++++++++++++++++++- xen/include/xen/livepatch.h | 5 ++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/xen/common/livepatch.c b/xen/common/livepatch.c index 70f601846f..a76403af59 100644 --- a/xen/common/livepatch.c +++ b/xen/common/livepatch.c @@ -49,6 +49,9 @@ static LIST_HEAD(applied_list); static unsigned int payload_cnt; static unsigned int payload_version =3D 1; =20 +#define LIVEPATCH_TRACE_ON 100 +#define LIVEPATCH_TRACE_OFF 101 + /* Defines an outstanding patching action. */ struct livepatch_work { @@ -58,7 +61,7 @@ struct livepatch_work struct payload *data; /* The payload on which to act. */ volatile bool do_work; /* Signals work to do. */ volatile bool ready; /* Signals all CPUs synchronized. */ - unsigned int cmd; /* Action request: LIVEPATCH_ACTION_* */ + unsigned int cmd; /* Action request: LIVEPATCH_ACTION_* or = LIVEPATCH_TRACE_* */ }; =20 /* There can be only one outstanding patching action. */ @@ -1737,6 +1740,14 @@ static void livepatch_do_action(void) } break; =20 + case LIVEPATCH_TRACE_ON: + rc =3D apply_payload(data); + break; + + case LIVEPATCH_TRACE_OFF: + rc =3D revert_payload(data); + break; + default: rc =3D -EINVAL; /* Make GCC5 happy. */ ASSERT_UNREACHABLE(); @@ -1823,6 +1834,51 @@ static int schedule_work(struct payload *data, uint3= 2_t cmd, uint32_t timeout) return 0; } =20 +int livepatch_schedule_trace_on(struct payload *payload, struct livepatch_= func *func, unsigned long ip) +{ + void *old_addr; + int rc; + + spin_lock(&payload_lock); + if ( is_work_scheduled(payload) ) + { + rc =3D -EBUSY; + goto out; + } + + old_addr =3D func->old_addr; + func->old_addr =3D (void *)ip; + + payload->rc =3D -EAGAIN; + rc =3D schedule_work(payload, LIVEPATCH_TRACE_ON, 0); + if ( rc ) + func->old_addr =3D old_addr; + +out: + spin_unlock(&payload_lock); + + return rc; +} + +int livepatch_schedule_trace_off(struct payload *payload) +{ + int rc; + + spin_lock(&payload_lock); + if ( is_work_scheduled(payload) ) + { + spin_unlock(&payload_lock); + return -EBUSY; + } + + payload->rc =3D -EAGAIN; + rc =3D schedule_work(payload, LIVEPATCH_TRACE_OFF, 0); + + spin_unlock(&payload_lock); + + return rc; +} + static void cf_check tasklet_fn(void *unused) { this_cpu(work_to_do) =3D 1; @@ -1861,6 +1917,8 @@ static void noinline do_livepatch_work(void) ACTION(APPLY), ACTION(REVERT), ACTION(REPLACE), + [LIVEPATCH_TRACE_ON] =3D "TRACE_ON", + [LIVEPATCH_TRACE_OFF] =3D "TRACE_OFF", }; #undef ACTION unsigned int cpu =3D smp_processor_id(); diff --git a/xen/include/xen/livepatch.h b/xen/include/xen/livepatch.h index ae34c938d6..1b927af8ee 100644 --- a/xen/include/xen/livepatch.h +++ b/xen/include/xen/livepatch.h @@ -140,6 +140,11 @@ struct payload; int revert_payload(struct payload *data); void revert_payload_tail(struct payload *data); =20 +int livepatch_schedule_trace_on(struct payload *payload, + struct livepatch_func *func, + unsigned long ip); +int livepatch_schedule_trace_off(struct payload *payload); + #else /* !CONFIG_LIVEPATCH */ =20 static inline int livepatch_op(struct xen_sysctl_livepatch_op *op) --=20 2.34.1 From nobody Mon Nov 23 22:39:58 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; arc=pass (i=1 dmarc=pass fromdomain=valinux.co.jp); dmarc=pass(p=none dis=none) header.from=valinux.co.jp ARC-Seal: i=2; a=rsa-sha256; t=1792731379; cv=pass; d=zohomail.com; s=zohoarc; b=UEZ3E9KhPpCVAgDaqFqKjoZ4avxNUDp7ofqkiKZyiULUdoQ11a6DwFatt9EsdUbpTTKTZLsppDH9uwS9/MFjb6cEISmlXFk6R81WdRdwixB2i3xRdrSYqIn+iWqbunCVz6kbaamx6Q/35RR9QB29J/Y2kzbSZVvUyr8xsCQbSGM= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792731379; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=rMDLJE58fBspR1Ur0/K2trhZGV+jlKomdmhc4KMqC34=; b=bBuZYKdsGjkJh+fb8vCa35atEvmXYCkQnEFITf5N0HBmUO+QsNDsqT5th8HgpT3PgobmE3tlht8joXBpNieAQAjd/6BCtNkcFuGHf0AFJ5m3enqVLQWhpvk8gHVWS3lbKKDmHD6aaXTnuH0Asn93Da/eJH4dAPtU1jeReYoIgyM= ARC-Authentication-Results: i=2; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; arc=pass (i=1 dmarc=pass fromdomain=valinux.co.jp); dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1792731379979696.1446843125893; Thu, 22 Oct 2026 21:56:19 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1458909.1670980 (Exim 4.92) (envelope-from ) id 1xK7Jy-000096-G1; Fri, 23 Oct 2026 04:56:02 +0000 Received: by outflank-mailman (output) from mailman id 1458909.1670980; Fri, 23 Oct 2026 04:56:02 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xK7Jy-00008z-Ah; Fri, 23 Oct 2026 04:56:02 +0000 Received: by outflank-mailman (input) for mailman id 1458909; Fri, 23 Oct 2026 04:56:00 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xK7Jw-00008r-CK for xen-devel@lists.xenproject.org; Fri, 23 Oct 2026 04:56:00 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1xK7Jv-004l5c-PS for xen-devel@lists.xenproject.org; Fri, 23 Oct 2026 06:55:59 +0200 Received: from [10.42.69.6] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6adae8b6-2eae-0a2a0a5409dd-0a2a4506824e-26 for ; Fri, 23 Oct 2026 06:55:59 +0200 Received: from [40.107.74.78] (helo=OS0P286CU010.outbound.protection.outlook.com) by tlsNG-16d1c6.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6adae8dc-195a-0a2a45060019-286b4a4e9860-3 for ; Fri, 23 Oct 2026 06:55:58 +0200 Received: from TY5P286MB319683.JPNP286.PROD.OUTLOOK.COM (2603:1096:403:511::19) by TYYP286MB5665.JPNP286.PROD.OUTLOOK.COM (2603:1096:405:295::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.546.18; Fri, 23 Oct 2026 04:55:54 +0000 Received: from TY5P286MB319683.JPNP286.PROD.OUTLOOK.COM ([fe80::5f8d:a6bf:3b84:5a82]) by TY5P286MB319683.JPNP286.PROD.OUTLOOK.COM ([fe80::5f8d:a6bf:3b84:5a82%5]) with mapi id 15.21.0546.016; Fri, 23 Oct 2026 04:55:54 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=selector1 header.d=valinux.co.jp header.i="@valinux.co.jp" header.h="From:Date:Subject:Message-Id:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=nT5uKeO5lUvrTCOto4F3AVpMiwmGSZuwiBzuS0prSfhUsLFfGqmCWc+giqZjHp7UOaCqlJGijDoCUE0qxDBUKvTwZ6espd6Ue8oDisvQppNgQjAKIvfrwnBOG0X5uKhaYSQOrcc0ZYauqFwxjrtjXYJcuQ/AvLuGAppnoWyJzT+tgRKl+L+Ey8s/caHR+ry1Q+SSxaTMSYtNVysVgrsF80aVjB37Yf76x8XZ5xIwND3p5qYN1tAgBsTKSHynMoyRhnQRKM34xLwszlU4h/jSQMwz5ue8RaZXZ0h9+nvTDyyqv7qD6yQ+I8TLiwjF2iBBhI0WmoEJOnOHuGHWrvhfJg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=rMDLJE58fBspR1Ur0/K2trhZGV+jlKomdmhc4KMqC34=; b=Clof73CyGbwPxZnf/I8qlvfs+MVAzorDgy9sJ6x4kZTQU+hEO3OQ05aJbpwKv4wu1h7eO6h3XWMdvtVi6hVOQ+bEtc0/gxSimewynXS18h3IvF6qyNX5tOnlMDzaeldtNhWdPLINU06eu9CuAjP1j8leU7Z/OSnOzQmz6OurzTqA3vXjWoekab+rUefTbST8QUMInEu+UI5MKJ1oxdSZeDkiEtOUyMpvYePUqQL9k7Yvj7xDlP0KZODwipP0zhGO27XZ8mJQvP2Uv7JyVlj+DaDJSmFkPcK3rVoHkSRC76TvfqHo5j0xB7MZOfn67XQR2247txLYnD+lcIaSFO/UDg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=valinux.co.jp; dmarc=pass action=none header.from=valinux.co.jp; dkim=pass header.d=valinux.co.jp; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=valinux.co.jp; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=rMDLJE58fBspR1Ur0/K2trhZGV+jlKomdmhc4KMqC34=; b=SIJ1aRCCXSWmGUn/4drpHOiXGeAbfdiOHfTOaeIBu4e63YqJZw8cU4o68ozouQNRDhraR55mm+RN5NCXkh5atPueTrBZxJRtFjsUncw0PnzPjIDsv+jAJBSQ1e5HuA8I3jiWCGKj2dpStFuvzBS8TLwLrce07vIrxGEv/UGwKX8= Authentication-Results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=valinux.co.jp; From: Ryo Takakura To: xen-devel@lists.xenproject.org Cc: sstabellini@kernel.org, julien@xen.org, bertrand.marquis@arm.com, michal.orzel@amd.com, Volodymyr_Babchuk@epam.com, andrew.cooper3@citrix.com, anthony.perard@vates.tech, jbeulich@suse.com, roger@xenproject.org, ross.lagerwall@citrix.com, jgross@suse.com, Ryo Takakura Subject: [RFC 3/5] xen/tracing: prepare a livepatch payload and its controling interface Date: Fri, 23 Oct 2026 13:55:43 +0900 Message-Id: <20261023045543.164658-1-takakura@valinux.co.jp> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261023044809.162759-1-takakura@valinux.co.jp> References: <20261023044809.162759-1-takakura@valinux.co.jp> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: TY4P286CA0069.JPNP286.PROD.OUTLOOK.COM (2603:1096:405:371::11) To TY5P286MB319683.JPNP286.PROD.OUTLOOK.COM (2603:1096:403:511::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: TY5P286MB319683:EE_|TYYP286MB5665:EE_ X-MS-Office365-Filtering-Correlation-Id: 17593c5d-b6b8-493e-fed2-08df30c1eb0f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|1800799024|23010399003|366016|6133799003|18002099003|22082099003|3023799007|10067099003|5023799004|261009223027099003|261021123027099003|261022115727099003|261022113327099003|261021135927099003|261021121427099003|261022075727099003|261021133027099003|261021130227099003|261022105027099003|56012099006|260925022911599003|260925021311599003|260925021911599003; X-Microsoft-Antispam-Message-Info: nDO3I6MIMO6TOcf//tHaCQobfQtVJNh0FF0EZAUY4T+LE5WWpEBG+EkPpqSyqBysj3JYJU48E2XF6jDgjcqQhOD+pIcHLehFyntp9uOlpTo9hSyjBUAyacsF3K6NiFre4lLPPBip6ICUN//wX8oxgTYywjrLMByPQAxYhmBWxbn1d7Tc5dul+6rkCLLWU5eAfTyBLLTCFAQvXE70/V8eh7HFLu5h8Zp91LzfGk15u7ps3WXgZlnauG6oGdhQDU6+0WBhwtUn9TXtZi1In99kmgN0feWk1cfXBg/7Sdz776v8xSzi6g/t5Kno6ZEEut7FjL4YKUVmBdklsKHvPfXiXTXBCl31+UPxbvNPHuMWGtCkS4rq7M1saE36ZX5OlfL+uvbZbFkPqtdcOaHP8inWVUy7EjO314NHbdDvQzFnQDa5iLoZtixcgFBi+YnrUiw6IJfA5PrD9MN+k+5hH0k4LxeDqZlVO2JKmfWOQkMC1W7gaeHB/nJEuLYsRB0Tyni9k+QDZ5QoabT3SV36HHCB7fiBKpves4hshOuCe7Ger78Ac+XVYMj0HegEcHsqylGq9IYzLcS2GmIh5UXW0zsEzlhlCDyRd7b8xOPRaFkEt+gf1rAVlLZDn89yBo0lBeZnQLugvLlsmFmGiKg2AfPYqgXZhdDFssHlaBC2k8Khk4U= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:TY5P286MB319683.JPNP286.PROD.OUTLOOK.COM;PTR:;CAT:NONE;SFS:(13230040)(376014)(7416014)(1800799024)(23010399003)(366016)(6133799003)(18002099003)(22082099003)(3023799007)(10067099003)(5023799004)(261009223027099003)(261021123027099003)(261022115727099003)(261022113327099003)(261021135927099003)(261021121427099003)(261022075727099003)(261021133027099003)(261021130227099003)(261022105027099003)(56012099006)(260925022911599003)(260925021311599003)(260925021911599003);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?avrT0iujQZmbFJTYzITm9+H7RWtiMNDUXTbmaobkWNcUQaQ4Y44HL77Qo1lD?= =?us-ascii?Q?xLOh3zsws+nhUb1MbtzPSe6jLmCKsKmhhumG7Mv1D0zOz4w3qjWDmsvdx9Ky?= =?us-ascii?Q?6WM1KdPOpzhrxAK5k2/RmYHACo4aETNO2hKYI5a8XWTeBtpIKiw+tVIZ1nHk?= =?us-ascii?Q?E1GVO+V3VWq7rFFjvwxl8UGdWsfekydT8YUng522/LlUFbmRYEvfoE3C0HXo?= =?us-ascii?Q?hRnGUR4LxYrRMOcd+RI0V8aih0T970nOKfzPL1fCxWEKFr5+z9Beg45LTt16?= =?us-ascii?Q?5CkTF2IDJmpPtgJHw33IX+Qc0ATvWc95mZPerH/Libvhtn+acEjGGTsJRnd7?= =?us-ascii?Q?HLRivlVmQ/le8pgRK8nWI5ZmHH5C7MyrOHN10IcmH/KObX+BXUCXCmR3ocSs?= =?us-ascii?Q?l3FImukXVoHcw05FbWLTO5q8FY56L4BTsYM2Y4lZJ7ybPC3QJ6/bpTIGAPrr?= =?us-ascii?Q?o5kBW7yQiQNHns3KpeKn5mxIn6Hf668ZzdF5RugV5tstBCj3XpNb/gmlPjdn?= =?us-ascii?Q?8MulE2qQ9InjYuIVnY2daxQrE6M+tbWCwFYGycnHaTqWtErt7j6zZyuj8m7e?= =?us-ascii?Q?YPS0doOXlybBtx13SWmum1DPqROR1WeejypMIVTK45RtBfJ4Bo4Tb/Ggh71K?= =?us-ascii?Q?P2dwZRr5VDNNH9PxCq0VhB2+s8taKGBCckVrA3nMyFHJG40zKiCiV9wkhz/v?= =?us-ascii?Q?kaEZhehLwvwelXrQ5fOymOuSJTZ/K/S/3QU/ADeTNOUPM8L3V3kuVE9xmWIQ?= =?us-ascii?Q?9/8NThDpGRwHNsqJmGl6QhbZ/1ZJJMLf9nasbtOS2SPIx2cekLR5AA90j+6w?= =?us-ascii?Q?YplboZR91BvsRk4ewyiY6wCceA6+oNbw35PHN06YbstQIS9NncZqG+8g0ZgC?= =?us-ascii?Q?dG4P0unzFn6hRwMgBelugz46B0oRSA5Q19bf2MiWIEiGiGY1jILg2spVSQ6r?= =?us-ascii?Q?JuaiQyz5ppWdhzxpUolXqk7Ya6YfsVMkr/B9pUK2Iw3eTOr2PpoAmbCGzb3X?= =?us-ascii?Q?3YX/xO8iCexZckb/ZSRtR0hsY6OhkrJLQWoCH/Xljhw+grFp1c0YanB3XDYp?= =?us-ascii?Q?0Gd5VNNR7Ic6f4KKDwQoFDOxid9qShYSYnke4g+qeucniVxGUHlHvkZ0Jxpj?= =?us-ascii?Q?HdqTzfBvwkIqFzJAvCFin9MP2CvGVECISmUKbrVo+E0AifMkQRg2MJntBSKY?= =?us-ascii?Q?5FbNcaW4NONeFF6jHpZhX1J3XL32Lh5X9UNn8v0ZYYQyJdEmJsurcWeGOFgc?= =?us-ascii?Q?5z085ZdtYQ11kS2z7Av1CD4MEsg7s0i/Z+Og9U5nfVFzIQM7GS9h9Ks4xQP5?= =?us-ascii?Q?7aKRq2PNDVEQFgGOw/VDJ9KzFVymciScQdKfdqfS9pj7bRRDROzlzGfIWzRy?= =?us-ascii?Q?ve+jgEP4W3yW0hEaFqGQ3VTH2xI/8CI1uZTj4HCLxxMtIEcuzdpA3qq51xn/?= =?us-ascii?Q?/WkSoIe7wf3qoA/OKndjlpG3C1I3ciaRiS5sJohr+kORtvR5EHxaoTpID5iv?= =?us-ascii?Q?5E/hYORv59InrA8TJ881+xzLrV5NIYFAe6/SleG77Q6etXDrvzEzR8BwIvnj?= =?us-ascii?Q?0LWHjxh6JJn0kZA8l5TKIEQQR3cvbqHD6/oGthOXb/gL/fm1nbevBc3l5EVU?= =?us-ascii?Q?v29l6zyx7x/+B4YUTvVht6eimQWjSE0dRtlT3RZ69JJZX98A+gvRf3X+wSe2?= =?us-ascii?Q?axurg9bYpCCBajxRtP2FLqOwuomrpfE8IOkO/b/i+1jeEZLXcAyBWtSeYJQz?= =?us-ascii?Q?c60fK3TPpg=3D=3D?= X-OriginatorOrg: valinux.co.jp X-MS-Exchange-CrossTenant-Network-Message-Id: 17593c5d-b6b8-493e-fed2-08df30c1eb0f X-MS-Exchange-CrossTenant-AuthSource: TY5P286MB319683.JPNP286.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Oct 2026 04:55:54.1452 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 7a57bee8-f73d-4c5f-a4f7-d72c91c8c111 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: ltpb652elEbHlLMr3VM6f2TrRMIdHiZRzG0bWLfpsKtpsyPz6LC2LVgT2MA4C7YQdeg1dsuH1os5S1m4TuS1Hg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: TYYP286MB5665 X-purgate-ID: tlsNG-16d1c6/1792731359-1FACA77B-CC16EB0F/0/0 X-purgate-type: clean X-purgate-size: 26265 X-ZohoMail-DKIM: pass (identity @valinux.co.jp) X-ZM-MESSAGEID: 1792731381916158500 Content-Type: text/plain; charset="utf-8" Introduce a shared livepatch payload that tracers can use to patch their target functions. The payload will be scheduled to the TRACE_ON/OFF path which will let tracers to request livepatch without satisfying the requirements for regular livepatch payloads. Every tracers are represented as struct tracer with optional callbacks for managing its state and output. Expose these operations through HypFS files under /tracing, following Linux ftrace's naming and usage: 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: Read or change whether recording is enabled. trace: Read or clear output from tracers. Signed-off-by: Ryo Takakura --- xen/arch/arm/xen.lds.S | 5 + xen/common/Kconfig | 9 + xen/common/Makefile | 4 + xen/common/hypfs_trace.c | 741 ++++++++++++++++++++++++++++++++++ xen/common/livepatch.c | 9 + xen/include/xen/hypfs_trace.h | 42 ++ 6 files changed, 810 insertions(+) create mode 100644 xen/common/hypfs_trace.c create mode 100644 xen/include/xen/hypfs_trace.h diff --git a/xen/arch/arm/xen.lds.S b/xen/arch/arm/xen.lds.S index 613511390f..a85cab62af 100644 --- a/xen/arch/arm/xen.lds.S +++ b/xen/arch/arm/xen.lds.S @@ -61,6 +61,11 @@ SECTIONS *(.data.rel.ro) *(.data.rel.ro.*) =20 + . =3D ALIGN(8); + __start_patchable_function_entries =3D .; + KEEP(*(__patchable_function_entries)) + __stop_patchable_function_entries =3D .; + . =3D ALIGN(4); __proc_info_start =3D .; *(.proc.info) diff --git a/xen/common/Kconfig b/xen/common/Kconfig index 7cf6937c2f..ba118276f2 100644 --- a/xen/common/Kconfig +++ b/xen/common/Kconfig @@ -608,6 +608,15 @@ config TRACEBUFFER config HAS_LIVEPATCH_TRACE bool =20 +config LIVEPATCH_TRACER + bool "Livepatch Tracer" + depends on HYPFS && HAS_LIVEPATCH_TRACE + default n + help + Enable dynamic tracers. Tracers attach/deattach their callback functions + using livepatch infrastructure. User can select the tracer and target + function through the HypFS interface exposed as /tracing. + config LLC_COLORING bool "Last Level Cache (LLC) coloring" if EXPERT depends on HAS_LLC_COLORING diff --git a/xen/common/Makefile b/xen/common/Makefile index f69d47d189..303016f250 100644 --- a/xen/common/Makefile +++ b/xen/common/Makefile @@ -79,6 +79,10 @@ obj-$(CONFIG_UBSAN) +=3D ubsan/ obj-$(CONFIG_NEEDS_LIBELF) +=3D libelf/ obj-$(CONFIG_LIBFDT) +=3D libfdt/ =20 +obj-$(CONFIG_LIVEPATCH_TRACER) +=3D hypfs_trace.o +# Do not generate patchable entries for the tracer functions. +$(obj)/hypfs_trace.o: CFLAGS-y +=3D -fpatchable-function-entry=3D0,0 + CONF_FILE :=3D $(if $(patsubst /%,,$(KCONFIG_CONFIG)),$(objtree)/)$(KCONFI= G_CONFIG) $(obj)/config.gz: $(CONF_FILE) gzip -n -c $< >$@ diff --git a/xen/common/hypfs_trace.c b/xen/common/hypfs_trace.c new file mode 100644 index 0000000000..d9e06e092e --- /dev/null +++ b/xen/common/hypfs_trace.c @@ -0,0 +1,741 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAX_TRACER_SIZE 100 + +static LIST_HEAD(tracer_list); + +/* + * Only one tracer callback can be registered at a time, targeting a + * single function. Multiple callbacks and multiple target functions + * are not currently supported. + */ +static bool trace_callback_registered; +static DEFINE_SPINLOCK(trace_callback_lock); + +/* Table of patchable functions generated by -fpatchable-function-entry. */ +extern const unsigned long __start_patchable_function_entries[]; +extern const unsigned long __stop_patchable_function_entries[]; + +/* + * .old_addr holds the address of the target function. + * .new_addr holds the address of the selected tracer's callback. + */ +static struct livepatch_func global_trace_func; + +static struct livepatch_fstate global_trace_fstate =3D { + .applied =3D LIVEPATCH_FUNC_NOT_APPLIED, +}; + +/* Nop tracer is the default tracer */ +static unsigned int cf_check nop_get_trace_size(struct trace_context *tr) +{ + return sizeof("# tracer: nop\n"); +} + +static size_t cf_check nop_print_trace(struct trace_context *tr, char *buf= , size_t size) +{ + return scnprintf(buf, size, "# tracer: nop\n"); +} + +static struct tracer nop_tracer =3D { + .name =3D "nop", + .get_trace_size =3D nop_get_trace_size, + .print_trace =3D nop_print_trace, +}; + +/* + * Payload used by tracers for scheduling livepatch. + * + * It describes the target patch site through global_trace_func, + * with its patching state tracked by global_trace_fstate. + */ +static struct payload global_trace_payload =3D { + .name =3D "function-trace", + .funcs =3D &global_trace_func, + .fstate =3D &global_trace_fstate, + .nfuncs =3D 1, + .is_trace =3D true, +}; + +/* + * Context tracking the selected tracer, recording state, + * and references to the payload and target function used for patching. + * + * Initially, the nop tracer is selected as default. + */ +static struct trace_context global_trace =3D { + .current_trace =3D &nop_tracer, + .payload =3D &global_trace_payload, + .func =3D &global_trace_func, + .tracing_on =3D false, +}; + +/* Trim HypFS inputs */ +static char *strim(char *s) +{ + char *end; + + while ( isspace(*s) ) + s++; + + end =3D s + strlen(s); + while ( end > s && isspace(end[-1]) ) + end--; + + *end =3D '\0'; + return s; +} + +static bool tracing_is_on(void) +{ + return global_trace.tracing_on; +} + +static int tracing_set_on(bool on) +{ + const struct tracer *tracer =3D global_trace.current_trace; + int rc =3D 0; + + if ( global_trace.tracing_on =3D=3D on ) + return 0; + + if ( on ) + { + if ( tracer->start ) + rc =3D tracer->start(&global_trace); + } + else + { + if ( tracer->stop ) + rc =3D tracer->stop(&global_trace); + } + + if ( !rc ) + global_trace.tracing_on =3D on; + + return rc; +} + +void trace_dispatch_callbacks(unsigned long ip, unsigned long parent_ip) +{ + livepatch_trace_func_t callback; + + if ( !tracing_is_on() ) + return; + + if ( !trace_callback_registered ) + return; + + if ( !global_trace_func.old_addr ) + return; + + if ( (unsigned long)global_trace_func.old_addr !=3D ip ) + return; + + callback =3D global_trace_func.new_addr; + callback((unsigned long)global_trace_func.old_addr, parent_ip); +} + +int trace_register_callback(livepatch_trace_func_t callback) +{ + int rc =3D 0; + + spin_lock(&trace_callback_lock); + + if ( trace_callback_registered ) + { + rc =3D -EEXIST; + goto out_unlock; + } + + trace_callback_registered =3D true; + global_trace_func.new_addr =3D callback; + + out_unlock: + spin_unlock(&trace_callback_lock); + + return rc; +} + +int trace_unregister_callback(void) +{ + int rc =3D 0; + + spin_lock(&trace_callback_lock); + + if ( !trace_callback_registered ) + { + rc =3D -ENOENT; + goto out_unlock; + } + + trace_callback_registered =3D false; + global_trace_func.new_addr =3D NULL; + +out_unlock: + spin_unlock(&trace_callback_lock); + + return rc; +} + +static bool is_patchable_function_entry(unsigned long addr) +{ + const unsigned long *p; + + for ( p =3D __start_patchable_function_entries; + p < __stop_patchable_function_entries; p++ ) + { + if ( *p =3D=3D addr ) + return true; + } + + return false; +} + +static void get_patchable_function_entries(const unsigned long **start, + const unsigned long **stop) +{ + *start =3D __start_patchable_function_entries; + *stop =3D __stop_patchable_function_entries; +} + +static struct tracer *trace_find_tracer(const char *name) +{ + struct tracer *tracer; + + list_for_each_entry( tracer, &tracer_list, list ) + if ( !strcmp(tracer->name, name) ) + return tracer; + + return NULL; +} + +static int trace_set_tracer(const char *name) +{ + const struct tracer *prev =3D global_trace.current_trace; + const struct tracer *new; + int rc; + + new =3D trace_find_tracer(name); + if ( !new ) + return -EINVAL; + + if ( new =3D=3D prev ) + return 0; + + if ( prev->reset ) + { + rc =3D prev->reset(&global_trace); + if ( rc ) + return rc; + } + + global_trace.current_trace =3D &nop_tracer; + + if ( new->init ) + { + rc =3D new->init(&global_trace); + if ( rc ) + return rc; + } + + global_trace.current_trace =3D new; + + return 0; +} + +static int trace_set_filter(const char *name) +{ + struct payload *payload =3D global_trace.payload; + unsigned long ip =3D 0; + size_t len =3D strlen(name); + int rc; + + if ( len ) + { + ip =3D symbols_lookup_by_name(name); + if ( !ip || !is_patchable_function_entry(ip) ) + return -EINVAL; + } + + /* Schedule removal of the current patch. */ + if ( global_trace_fstate.applied =3D=3D LIVEPATCH_FUNC_APPLIED ) + { + /* The requested function is already patched. Nothing to change. = */ + if ( (unsigned long)global_trace_func.old_addr =3D=3D ip ) + return 0; + + rc =3D livepatch_schedule_trace_off(payload); + if ( rc ) + return rc; + + /* Removal has been scheduled. Clear the previous filter name. */ + global_trace.filter_name[0] =3D '\0'; + } + + /* Clear the filter if no target function was requested. */ + if ( !ip ) + { + /* + * Keep global_trace_func.old_addr since livepatching + * might be in flight. Only clear global_trace.filter_name + * which will effectively update /tracing/set_function_filter. + */ + global_trace.filter_name[0] =3D '\0'; + return 0; + } + + /* + * Schedule patching of the requested function. + * A previously scheduled removal may still be in flight, in which case + * livepatch_schedule_trace_on() returns -EBUSY and the user must retr= y. + */ + rc =3D livepatch_schedule_trace_on(payload, global_trace.func, ip); + if ( rc ) + return rc; + + strlcpy(global_trace.filter_name, name, sizeof(global_trace.filter_nam= e)); + + return 0; +} + +static HYPFS_DIR_INIT(tracing_dir, "tracing"); + +/* + * /tracing/available_tracers + * Lists the names of all registered tracers. + */ +static unsigned int available_tracers_size(void) +{ + const struct tracer *tracer; + unsigned int size =3D 0; + + list_for_each_entry( tracer, &tracer_list, list ) + size +=3D strlen(tracer->name) + 1; + + return size; +} + +static int cf_check available_tracers_read(const struct hypfs_entry *entry, + XEN_GUEST_HANDLE_PARAM(void) ua= ddr) +{ + const struct tracer *tracer; + unsigned int size =3D available_tracers_size(); + unsigned int pos =3D 0; + char *buf; + int rc =3D 0; + + buf =3D xmalloc_array(char, size); + if ( !buf ) + return -ENOMEM; + + list_for_each_entry( tracer, &tracer_list, list ) + { + unsigned int len =3D strlen(tracer->name); + + memcpy(buf + pos, tracer->name, len); + pos +=3D len; + buf[pos++] =3D ' '; + } + + buf[pos - 1] =3D '\0'; + + if ( copy_to_guest(uaddr, buf, size) ) + rc =3D -EFAULT; + + xfree(buf); + + return rc; +} + +static unsigned int cf_check available_tracers_getsize(const struct hypfs_= entry *entry) +{ + return available_tracers_size(); +} + +static const struct hypfs_funcs available_tracers_funcs =3D { + .enter =3D hypfs_node_enter, + .exit =3D hypfs_node_exit, + .read =3D available_tracers_read, + .write =3D hypfs_write_deny, + .getsize =3D available_tracers_getsize, + .findentry =3D hypfs_leaf_findentry, +}; + +static HYPFS_VARSIZE_INIT(available_tracers, XEN_HYPFS_TYPE_STRING, + "available_tracers", 0, &available_tracers_funcs= ); + +/* + * /tracing/current_tracer + * Print and select the active tracer. + */ +static int cf_check current_tracer_read(const struct hypfs_entry *entry, + XEN_GUEST_HANDLE_PARAM(void) uaddr) +{ + const char *name =3D global_trace.current_trace->name; + + return copy_to_guest(uaddr, name, strlen(name) + 1) ? -EFAULT : 0; +} + +static int cf_check current_tracer_write(struct hypfs_entry_leaf *leaf, + XEN_GUEST_HANDLE_PARAM(const_void= ) uaddr, + unsigned int ulen) +{ + char buf[MAX_TRACER_SIZE + 1]; + char *name; + + if ( ulen > MAX_TRACER_SIZE ) + return -EINVAL; + + if ( copy_from_guest(buf, uaddr, ulen) ) + return -EFAULT; + + buf[ulen] =3D '\0'; + name =3D strim(buf); + + if ( !*name ) + return -EINVAL; + + return trace_set_tracer(name); +} + +static unsigned int cf_check current_tracer_getsize(const struct hypfs_ent= ry *entry) +{ + return strlen(global_trace.current_trace->name) + 1; +} + +static const struct hypfs_funcs current_tracer_funcs =3D { + .enter =3D hypfs_node_enter, + .exit =3D hypfs_node_exit, + .read =3D current_tracer_read, + .write =3D current_tracer_write, + .getsize =3D current_tracer_getsize, + .findentry =3D hypfs_leaf_findentry, +}; + +static HYPFS_VARSIZE_INIT(current_tracer, XEN_HYPFS_TYPE_STRING, + "current_tracer", MAX_TRACER_SIZE + 2, ¤t_t= racer_funcs); + +/* + * /tracing/available_functions + * Lists the names of functions whose entries can be patched for tracing. + */ +static int cf_check available_functions_read(const struct hypfs_entry *ent= ry, + XEN_GUEST_HANDLE_PARAM(void) = uaddr) +{ + const unsigned long *start, *stop, *p; + char namebuf[KSYM_NAME_LEN + 1]; + const char *name; + unsigned long size, offset; + int ret =3D 0; + char *buf; + unsigned int buf_size =3D 0; + unsigned int buf_pos =3D 0; + + get_patchable_function_entries(&start, &stop); + + for ( p =3D start; p < stop; p++ ) + { + name =3D symbols_lookup(*p, &size, &offset, namebuf); + if ( name ) + buf_size +=3D strlen(name) + 1; + } + + if ( buf_size =3D=3D 0 ) + { + char empty =3D '\0'; + if ( copy_to_guest(uaddr, &empty, 1) ) + return -EFAULT; + return 0; + } + + buf =3D xmalloc_array(char, buf_size); + if ( !buf ) + return -ENOMEM; + + for ( p =3D start; p < stop; p++ ) + { + name =3D symbols_lookup(*p, &size, &offset, namebuf); + if ( name ) + { + unsigned int len =3D strlen(name); + memcpy(buf + buf_pos, name, len); + buf[buf_pos + len] =3D '\n'; + buf_pos +=3D len + 1; + } + } + buf[buf_pos - 1] =3D '\0'; + + if ( copy_to_guest(uaddr, buf, buf_size) ) + ret =3D -EFAULT; + + xfree(buf); + return ret; +} + +static unsigned int cf_check available_functions_getsize(const struct hypf= s_entry *entry) +{ + const unsigned long *start, *stop, *p; + char namebuf[KSYM_NAME_LEN + 1]; + const char *name; + unsigned long size, offset; + unsigned int buf_size =3D 0; + + get_patchable_function_entries(&start, &stop); + + for ( p =3D start; p < stop; p++ ) + { + name =3D symbols_lookup(*p, &size, &offset, namebuf); + if ( name ) + buf_size +=3D strlen(name) + 1; + } + + if ( buf_size =3D=3D 0 ) + return 1; + + return buf_size ? buf_size : 1; +} + +static const struct hypfs_funcs available_functions_funcs =3D { + .enter =3D hypfs_node_enter, + .exit =3D hypfs_node_exit, + .read =3D available_functions_read, + .write =3D hypfs_write_deny, + .getsize =3D available_functions_getsize, + .findentry =3D hypfs_leaf_findentry, +}; + +static HYPFS_VARSIZE_INIT(available_functions, XEN_HYPFS_TYPE_STRING, + "available_functions", 0, &available_functions_f= uncs); + +/* + * /tracing/set_function_filter + * Print and select the target function. + */ +static int cf_check set_function_filter_read(const struct hypfs_entry *ent= ry, + XEN_GUEST_HANDLE_PARAM(void) = uaddr) +{ + const char *name =3D global_trace.filter_name; + + return copy_to_guest(uaddr, name, strlen(name) + 1) ? -EFAULT : 0; +} + +static int cf_check set_function_filter_write(struct hypfs_entry_leaf *lea= f, + XEN_GUEST_HANDLE_PARAM(const= _void) uaddr, + unsigned int ulen) +{ + char buf[KSYM_NAME_LEN + 1]; + char *name; + + if ( ulen > KSYM_NAME_LEN ) + return -EINVAL; + + if ( copy_from_guest(buf, uaddr, ulen) ) + return -EFAULT; + + buf[ulen] =3D '\0'; + name =3D strim(buf); + + return trace_set_filter(name); +} + +static unsigned int cf_check set_function_filter_getsize(const struct hypf= s_entry *entry) +{ + return strlen(global_trace.filter_name) + 1; +} + +static const struct hypfs_funcs set_function_filter_funcs =3D { + .enter =3D hypfs_node_enter, + .exit =3D hypfs_node_exit, + .read =3D set_function_filter_read, + .write =3D set_function_filter_write, + .getsize =3D set_function_filter_getsize, + .findentry =3D hypfs_leaf_findentry, +}; + +static HYPFS_VARSIZE_INIT(set_function_filter, XEN_HYPFS_TYPE_STRING, + "set_function_filter", KSYM_NAME_LEN + 1, &set_f= unction_filter_funcs); + +/* + * /tracing/tracing_on + * Enable or disable tracing by writing 1 or 0. + */ +static int cf_check tracing_on_read(const struct hypfs_entry *entry, + XEN_GUEST_HANDLE_PARAM(void) uaddr) +{ + char buf[] =3D "0"; + + buf[0] =3D global_trace.tracing_on ? '1' : '0'; + + return copy_to_guest(uaddr, buf, sizeof(buf)) ? -EFAULT : 0; +} + +static int cf_check tracing_on_write(struct hypfs_entry_leaf *leaf, + XEN_GUEST_HANDLE_PARAM(const_void) ua= ddr, + unsigned int ulen) +{ + char buf[sizeof("0\n") + 1]; + char *enabled; + + if ( ulen > sizeof("0\n") ) + return -EINVAL; + + if ( copy_from_guest(buf, uaddr, ulen) ) + return -EFAULT; + + buf[ulen] =3D '\0'; + enabled =3D strim(buf); + + if ( strlen(enabled) !=3D 1 || (enabled[0] !=3D '0' && enabled[0] !=3D= '1') ) + return -EINVAL; + + return tracing_set_on(enabled[0] =3D=3D '1'); +} + +static unsigned int cf_check tracing_on_getsize(const struct hypfs_entry *= entry) +{ + return sizeof("0"); +} + +static const struct hypfs_funcs tracing_on_funcs =3D { + .enter =3D hypfs_node_enter, + .exit =3D hypfs_node_exit, + .read =3D tracing_on_read, + .write =3D tracing_on_write, + .getsize =3D tracing_on_getsize, + .findentry =3D hypfs_leaf_findentry, +}; + +static HYPFS_VARSIZE_INIT(tracing_on, XEN_HYPFS_TYPE_STRING, + "tracing_on", sizeof("0\n"), &tracing_on_funcs); + +/* + * /tracing/trace + * Print the output from the current tracer. + */ +static unsigned int cf_check trace_getsize(const struct hypfs_entry *entry) +{ + const struct tracer *tracer =3D global_trace.current_trace; + unsigned int size; + + if ( !tracer->get_trace_size || !tracer->print_trace ) + return sizeof(""); + + size =3D tracer->get_trace_size(&global_trace); + + return size ? size : sizeof(""); +} + +static int cf_check trace_read(const struct hypfs_entry *entry, + XEN_GUEST_HANDLE_PARAM(void) uaddr) +{ + const struct tracer *tracer =3D global_trace.current_trace; + unsigned int size; + char *buf; + int rc =3D 0; + + if ( tracing_is_on() ) + return -EBUSY; + + if ( !tracer->get_trace_size || !tracer->print_trace ) + return copy_to_guest(uaddr, "", sizeof("")) ? -EFAULT : 0; + + size =3D tracer->get_trace_size(&global_trace); + if ( !size ) + return copy_to_guest(uaddr, "", sizeof("")) ? -EFAULT : 0; + + buf =3D xzalloc_array(char, size); + if ( !buf ) + return -ENOMEM; + + tracer->print_trace(&global_trace, buf, size); + + if ( copy_to_guest(uaddr, buf, size) ) + rc =3D -EFAULT; + + xfree(buf); + + return rc; +} + +static int cf_check trace_write(struct hypfs_entry_leaf *leaf, + XEN_GUEST_HANDLE_PARAM(const_void) uaddr, + unsigned int ulen) +{ + const struct tracer *tracer =3D global_trace.current_trace; + + if ( tracing_is_on() ) + return -EBUSY; + + if ( !tracer->clear_trace ) + return 0; + + return tracer->clear_trace(&global_trace); +} + +static const struct hypfs_funcs trace_funcs =3D { + .enter =3D hypfs_node_enter, + .exit =3D hypfs_node_exit, + .read =3D trace_read, + .write =3D trace_write, + .getsize =3D trace_getsize, + .findentry =3D hypfs_leaf_findentry, +}; + +static HYPFS_VARSIZE_INIT(trace, XEN_HYPFS_TYPE_STRING, "trace", sizeof("0= \n"), &trace_funcs); + +int __init trace_register_tracer(struct tracer *tracer) +{ + if ( !tracer->name ) + return -EINVAL; + + if ( trace_find_tracer(tracer->name) ) + return -EEXIST; + + list_add_tail(&tracer->list, &tracer_list); + + return 0; +} + +static void __init initialize_hypfs_trace(void) +{ + int rc; + + rc =3D trace_register_tracer(&nop_tracer); + BUG_ON(rc); + + hypfs_string_set_reference(¤t_tracer, ""); + hypfs_string_set_reference(&available_tracers, ""); + hypfs_string_set_reference(&available_functions, ""); + hypfs_string_set_reference(&set_function_filter, ""); + hypfs_string_set_reference(&tracing_on, ""); + hypfs_string_set_reference(&trace, ""); + + hypfs_add_dir(&hypfs_root, &tracing_dir, true); + hypfs_add_leaf(&tracing_dir, &available_tracers, true); + hypfs_add_leaf(&tracing_dir, ¤t_tracer, true); + hypfs_add_leaf(&tracing_dir, &available_functions, true); + hypfs_add_leaf(&tracing_dir, &set_function_filter, true); + hypfs_add_leaf(&tracing_dir, &tracing_on, true); + hypfs_add_leaf(&tracing_dir, &trace, true); +} + +static int __init cf_check hypfs_trace_init(void) +{ + initialize_hypfs_trace(); + + return 0; +} +__initcall(hypfs_trace_init); diff --git a/xen/common/livepatch.c b/xen/common/livepatch.c index a76403af59..352a2df62c 100644 --- a/xen/common/livepatch.c +++ b/xen/common/livepatch.c @@ -26,6 +26,10 @@ #include #include =20 +#ifdef CONFIG_LIVEPATCH_TRACER +#include +#endif + #include #include =20 @@ -232,6 +236,11 @@ xen_livepatch_trace_dispatcher(unsigned long ip, unsig= ned long parent_ip) =20 out: rcu_read_unlock(&rcu_payload_lock); + +#ifdef CONFIG_LIVEPATCH_TRACER + /* Call tracer callbacks. */ + trace_dispatch_callbacks(ip, parent_ip); +#endif } =20 #endif /* CONFIG_HAS_LIVEPATCH_TRACE */ diff --git a/xen/include/xen/hypfs_trace.h b/xen/include/xen/hypfs_trace.h new file mode 100644 index 0000000000..11c4bba491 --- /dev/null +++ b/xen/include/xen/hypfs_trace.h @@ -0,0 +1,42 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __XEN_HYPFS_TRACE_H__ +#define __XEN_HYPFS_TRACE_H__ + +#include +#include +#include +#include +#include + +struct payload; +struct tracer; + +/* State tracking the selected tracer and target function */ +struct trace_context { + const struct tracer *current_trace; /* Currently selected tracer */ + struct payload *payload; /* Payload used for uploading tracers */ + struct livepatch_func *func; /* Target function and tracer's callback = address */ + bool tracing_on; /* Whether callbacks may record events */ + char filter_name[KSYM_NAME_LEN]; /* Name of the target function */ +}; + +/* Operations registered by each tracer */ +struct tracer { + const char *name; /* Name of the tracer */ + int (*init)(struct trace_context *tr); /* Called when this tracer is s= elected */ + int (*reset)(struct trace_context *tr); /* Called when this tracer is = being deselected */ + int (*start)(struct trace_context *tr); /* Called before recording get= s enabled */ + int (*stop)(struct trace_context *tr); /* Called before recording gets= disabled */ + int (*clear_trace)(struct trace_context *tr); /* Clear recorded events= at the user's request */ + unsigned int (*get_trace_size)(struct trace_context *tr); /* Required = output buffer size */ + size_t (*print_trace)(struct trace_context *tr, char *buf, size_t size= ); /* Format recorded output */ + struct list_head list; /* Linked to 'tracer_list' */ +}; + +int __init trace_register_tracer(struct tracer *tracer); + +int trace_register_callback(livepatch_trace_func_t callback); +int trace_unregister_callback(void); +void trace_dispatch_callbacks(unsigned long ip, unsigned long parent_ip); + +#endif /* __XEN_HYPFS_TRACE_H__ */ --=20 2.34.1 From nobody Mon Nov 23 22:39:58 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; arc=pass (i=1 dmarc=pass fromdomain=valinux.co.jp); dmarc=pass(p=none dis=none) header.from=valinux.co.jp ARC-Seal: i=2; a=rsa-sha256; t=1792731447; cv=pass; d=zohomail.com; s=zohoarc; b=L8apzI8l/YkynsTMXjHHnv+rtUtrzmzHpxJ25N21AHq4KhJwjA1HoK5388NS4clLcSYfviLPkfdkhXIi9v41REx+EZ0a0SOJ4eMt1wL+xN9zdj6wpt8x0M/CvGon9j73rC61OgUlHt8aoGfAcl5+LfJlXtw+9x6YqJt1qHzSXhM= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792731447; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=qnKv7oJzltLW6C26HaDuFk/xWeQEgKaGXCvC8U3iEFU=; b=loXb2Cnt/Jxagd2g6YjkL/+xir2JItpREqqWCgg2R4DyLhcC4gHPJp36NBraX8vg8BA10yAn14p3o790dT4KqZcdzeAABOLX3ekc5tv7pXCPGPgT/zIzQzffjCbflN7cv/GUm3g+dpwUOdkeYpOmsTz5626wXdPjWSTjYrxdJm0= ARC-Authentication-Results: i=2; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; arc=pass (i=1 dmarc=pass fromdomain=valinux.co.jp); dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1792731447740697.1233041242743; Thu, 22 Oct 2026 21:57:27 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1458919.1670988 (Exim 4.92) (envelope-from ) id 1xK7L9-0000gC-Ru; Fri, 23 Oct 2026 04:57:15 +0000 Received: by outflank-mailman (output) from mailman id 1458919.1670988; Fri, 23 Oct 2026 04:57:15 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xK7L9-0000g4-OM; Fri, 23 Oct 2026 04:57:15 +0000 Received: by outflank-mailman (input) for mailman id 1458919; Fri, 23 Oct 2026 04:57:14 +0000 Received: from mx.expurgate.net ([194.145.224.20]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xK7L7-0000fu-O8 for xen-devel@lists.xenproject.org; Fri, 23 Oct 2026 04:57:13 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1xK7L6-00CzBS-QV for xen-devel@lists.xenproject.org; Fri, 23 Oct 2026 06:57:12 +0200 Received: from [10.42.69.6] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6adae922-e002-0a2a0a5209dd-0a2a45069c7e-4 for ; Fri, 23 Oct 2026 06:57:12 +0200 Received: from [40.107.74.100] (helo=OS0P286CU010.outbound.protection.outlook.com) by tlsNG-16d1c6.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6adae925-195a-0a2a45060019-286b4a6428b0-3 for ; Fri, 23 Oct 2026 06:57:11 +0200 Received: from TY5P286MB319683.JPNP286.PROD.OUTLOOK.COM (2603:1096:403:511::19) by TYYP286MB5665.JPNP286.PROD.OUTLOOK.COM (2603:1096:405:295::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.546.18; Fri, 23 Oct 2026 04:57:08 +0000 Received: from TY5P286MB319683.JPNP286.PROD.OUTLOOK.COM ([fe80::5f8d:a6bf:3b84:5a82]) by TY5P286MB319683.JPNP286.PROD.OUTLOOK.COM ([fe80::5f8d:a6bf:3b84:5a82%5]) with mapi id 15.21.0546.016; Fri, 23 Oct 2026 04:57:08 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=selector1 header.d=valinux.co.jp header.i="@valinux.co.jp" header.h="From:Date:Subject:Message-Id:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=vs61KkxE3lmS+y4WraoQ/Gpi6IGQ1kCnO6vFt/AC9Om4uMVbgK2KyY310i6GiE1dtDbPTyyhNwY/ojj8jeWMQYcTXF38Jad+0+Qn6zRIIg7Kdud0crUyKJKLNWaF38fQOeZfWCggPHTC/NZVPoiz7EZkLGsk8LINo9qjxP9NITVjIbpn0BNBRjEGgNzhw57Oq0Y+MNZ9nU9dt73hq+pTgw5UeT7UfAQfgGoN7NmCECAaAV9n4mL6hPhy+tVTywSw4b7m2OF5MUvJW4f5WYX5DcQq6LwVKQzFogUc1SIEjMGwwOoyWEl9Rpht1G2Rt6Hp80Wj13EwFdjGqhh1JN4XBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=qnKv7oJzltLW6C26HaDuFk/xWeQEgKaGXCvC8U3iEFU=; b=AYUrWufR18zBiWjxonLtxk2jGCbrMmTQSQcODuK4JCSUg4XtDW+vyNyAXQi71GsdIy1eSLM1afbz9WfIvStd/OW27yfpmxuAt/BqKeT7XLnMojL10GklCcHohKnWp5UcyFxS4WN0XK0BIsIWK/4AWw6blGwx2xNe0RnJvUtIscJzHh69ShTjn3QuNGHRQVjHibf4T4F5a/9kV5Mns9K9oeZmVPy1IkYRZQLSANzj1aJCRuWXo0ufvxuuEl99zz8c44rmCdYnE5X890tNuG+oqdT7GSqciEqNHcVIi/OAw15ZXjEF7tnW0HftriinDxmpy+v1V0RwGpwkNqJYdapX4A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=valinux.co.jp; dmarc=pass action=none header.from=valinux.co.jp; dkim=pass header.d=valinux.co.jp; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=valinux.co.jp; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=qnKv7oJzltLW6C26HaDuFk/xWeQEgKaGXCvC8U3iEFU=; b=JFGR5ZN5Y6KswXAdU+Is9bs+dd4jEMvOThItGPevB+5/2cEcAP38+cuF/jU45ooDVrmnjpf0IeSqC3yM9vBGF7KUfR/O3hIDuPrCoAL3QCfV8kKRnKOaVOVysAdLkyJTsx3F07aILpR5Xr0TQlm5UBifH3+tBEOEU75043RETQY= Authentication-Results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=valinux.co.jp; From: Ryo Takakura To: xen-devel@lists.xenproject.org Cc: andrew.cooper3@citrix.com, anthony.perard@vates.tech, michal.orzel@amd.com, jbeulich@suse.com, julien@xen.org, roger@xenproject.org, sstabellini@kernel.org, Ryo Takakura Subject: [RFC 4/5] xen/tracing: Add a function tracer Date: Fri, 23 Oct 2026 13:56:55 +0900 Message-Id: <20261023045655.164966-1-takakura@valinux.co.jp> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261023044809.162759-1-takakura@valinux.co.jp> References: <20261023044809.162759-1-takakura@valinux.co.jp> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: TYCP286CA0033.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:29d::7) To TY5P286MB319683.JPNP286.PROD.OUTLOOK.COM (2603:1096:403:511::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: TY5P286MB319683:EE_|TYYP286MB5665:EE_ X-MS-Office365-Filtering-Correlation-Id: e5e9af30-b6ed-44d0-82d3-08df30c21750 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|1800799024|23010399003|366016|6133799003|18002099003|22082099003|10067099003|261009223027099003|261021121427099003|261022075727099003|261022105027099003|56012099006|260925022911599003|260925021311599003|260925021911599003; X-Microsoft-Antispam-Message-Info: /vybvP0qEK5lPKLIzNfDVoWN6dRufC4xq+hbBTUAlWoYtMK1AZwZKQvTb9X6xWAG3zRCftdUxcHxAuD5Ja9XWGECxka+YxJZLNsNBvjct1TV5mn7oPrctChlgq8wCrRCSx15KeFRIYZYdnq/mI6RJzYl35bTxui4IS1uoPfpWb72at59AmlsNR9wxj/lcWdbeZAOYxE1E3uwLLdoz6ZlX5KY/rFjstvdmATTMijF+SB2P8e1arQTD7TVuhAJN/2aRE9fY43+XR1gkkIjy0LcGr+oF/hJL996ShuJ6aVbEGA+E7GtWOtfJosY4hIFoS/Eebh1Y57A1VVesqKScAzImTtSFrg/CmS857GMC2HA8WK7PXXlAvB9rJgKCG58VZOCzHuq40ZWs2Carz8sTfpD/Ckd7LepC6RcyeYLOtClC1SKALflZ/SXpE2I5xvnwADVINpLlW7btgWu0I+bs4grJCcg5AZ3UrWcnMcKPSp89yaWVJCb3F73q3/tpXakG6XpxqbGD4+q/il5C0opHU5Hhc6WC1Wp8LtJ5apitAFXOJLLUCq6XxFcrcnIEsSf4JVgMNN+RQtA7D7EBhssLNoTUkDVrX41pK0OZcY/qOYzx9IMmwUlVhLTE0QfqGGCVFQh2nOyZm/siCGcsvsbqGmY4Xkw9HidT0C1LdZ39dJGlyA= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:TY5P286MB319683.JPNP286.PROD.OUTLOOK.COM;PTR:;CAT:NONE;SFS:(13230040)(376014)(1800799024)(23010399003)(366016)(6133799003)(18002099003)(22082099003)(10067099003)(261009223027099003)(261021121427099003)(261022075727099003)(261022105027099003)(56012099006)(260925022911599003)(260925021311599003)(260925021911599003);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?ShdadNF6fsCkp0IjVJSbTxujHTzqyS85iAoObGcB9eG9HvsdhMvUFx5wgjnh?= =?us-ascii?Q?GECWjbKK8K+ucHDgELWZbeKTOxPBfQMLJfZ2GgTfHDCE/KUXfETHGdCp9/NI?= =?us-ascii?Q?6WjIXgynpo59XxLTcAqSpqjyS57Bz2HC6GSYLU2qHHGWpBDevEZnWSqI5MW4?= =?us-ascii?Q?kT+js6dSWsO5iSZaZ5DiyMu3oXkTCs1aI1SOYpQLR7lcmqcex6gLJf12nKXl?= =?us-ascii?Q?nqtG8GyXH8N3SNiYMVcKYaHaUWPAvB3YDiKfBL6kUh83mKd0/JdxkngkJgSK?= =?us-ascii?Q?izdw84F1SV179FzWLwV1QD7xif/K2wzCCMUmHfMyFXCWNniqrdA9Css6prtg?= =?us-ascii?Q?Djm53buDemAEAsFP83jkoNNjrpue2CO6w8kz32FEjVQ/HFROpWZZo0NL7oce?= =?us-ascii?Q?S5M9Qw6iICEkNRhJrVJo9sGzNR+vL0HOxP2dtitS+OeUMGCmvzbAtYXWFIXD?= =?us-ascii?Q?66aPPzlL05/9wJgkpl6Pqcwsn6H/wwnNp6yrcyTjCBT3mEmYYavpZo7uioy5?= =?us-ascii?Q?nQyl0hVLU5/0FsmNOJpU1It45EZUCp/KaUWdnWldsHxcykw7rMYapwNCIWxu?= =?us-ascii?Q?jN8bvnt2zmg+jMWU7f0I5UEgGkZjaF1lXLh6TNV20LIprCv6NxZiKLk8fDhL?= =?us-ascii?Q?Nm8kErL9Xvy4HF1HkV8DTkgCEvYr7J4KnY6usBpvE7m4nXQClCjlT3BqmqMb?= =?us-ascii?Q?eJ+oVWjHigbTdZiv2gjXiUg6SpFXuMCAk4D8t6eX4DQVxUrzBNeAiL10iP/d?= =?us-ascii?Q?1CWkjNR8zWNYHbN6xQjsWMzdg7X9tdr1IYnfqsXoVoYD7lN87U57gb0jr8TL?= =?us-ascii?Q?4Kk2/zJL4UB57mjSmm67tzLitl7dgIZR3JIWPL4CEuU2J+EMmJLwSwsEXsDG?= =?us-ascii?Q?J5Ue5RPXbF4zIw9bsLLlEg1XbTjWeYVzDh0mcfNQvViolNIGzXKjilCGWVcJ?= =?us-ascii?Q?XwPltGBR+Z4uoV/73QWEif7IWXjiyyplrgawqP+lezaC1BLDe+gCVEV8bW8k?= =?us-ascii?Q?g2GaOoEcA/xHnDPLBem0atl8Q94Nz+ICw5xwLq6BIYFeUU4mUf0xjxYsn/XI?= =?us-ascii?Q?XIPhF6n8JmFiskDXEjp4oUANhosGfHmEfTjKkuyIAHNTF5mf9nbJpw3tQRWi?= =?us-ascii?Q?apzFUDK4OV/IiOFKrp4/eFjMU+6sFI5IJJkuvC4Mb1vN5gm27QAK/N2gHWEp?= =?us-ascii?Q?+/Op2azY8bukVe55rfBVfYU0t7NGjbsSbN/2JBk4aVub0SLv6ddXGP2TTwNJ?= =?us-ascii?Q?fAF/YBc9vAMLEyma6ZBZUuz4dhab7D61ChVZG/Dps8FqRV4aUUkd717Kigea?= =?us-ascii?Q?dMnkxoS+bwenOhQpLYw9TMpgRnQpwgp2XOZT94FZn3WMFcavSSkBMWD5Zb3N?= =?us-ascii?Q?fW+2rwJmCMziwe/XLJjg+j9a8d5+wykTTG4GhNBmfKZuhg1nXdMZMAc2oDsM?= =?us-ascii?Q?zQZoXgWzvvJTi9PSc7tRBp4UjxHyyO4ni0ShKyhgAxVQeGNuc1AaXRKsDsAr?= =?us-ascii?Q?dxmL7Z5MX3S60wR5vXJZvc1WaYZVZTXHN3lM/JvGIEBWyUtaxrupvM1hhtJ+?= =?us-ascii?Q?ojp2ZsYb6XR1JLSiWd3juvmcK/+v/uQ2qOktc2NdPxNEC98zTZ54ZY0VEuVZ?= =?us-ascii?Q?sEMp6YA2GMKfXXywRimi3ng/k0DQhbDnIyYE9AEMEPema1u0Pwvw1d3VzFRR?= =?us-ascii?Q?R5TOhD6918ziceu0ytS+OhZ6WXI540LJH/P04OQopN9+oUT5jO2j6u87gi10?= =?us-ascii?Q?iryYFW7t5w=3D=3D?= X-OriginatorOrg: valinux.co.jp X-MS-Exchange-CrossTenant-Network-Message-Id: e5e9af30-b6ed-44d0-82d3-08df30c21750 X-MS-Exchange-CrossTenant-AuthSource: TY5P286MB319683.JPNP286.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Oct 2026 04:57:08.3537 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 7a57bee8-f73d-4c5f-a4f7-d72c91c8c111 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: Z53L81driH+O7GunVu7RvDvsWzAIiV3G484T571yEe+r1FZqpk7GVzCBMbFFe2mMzFphHcUA6Ps/LCQNKop2Zw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: TYYP286MB5665 X-purgate-ID: tlsNG-16d1c6/1792731432-FD00977B-79E504D4/0/0 X-purgate-type: clean X-purgate-size: 11054 X-ZohoMail-DKIM: pass (identity @valinux.co.jp) X-ZM-MESSAGEID: 1792731449141158500 Content-Type: text/plain; charset="utf-8" Add a function tracer which records calls to the selected function. Each record includes the timestamp, CPU ID, and domain associated with the call. ~$ xenhypfs cat /tracing/trace # tracer: function # # entries-in-buffer/entries-written: 5/5 #P:1 # # _-----=3D> irqs-off # / _----=3D> need-resched # | / _---=3D> in-irq # || / _--=3D> preempt-depth # ||| / # DOMAIN/VCPU PCPU |||| TIMESTAMP FUNCTION # | | |||| | | d0/v0 [000] nnn 0 442.074071: xen_extra_version <-do_xe= n_version d0/v0 [000] nnn 0 443.160035: xen_extra_version <-do_xe= n_version d0/v0 [000] nnn 0 469.897195: xen_extra_version <-do_xe= n_version d0/v0 [000] nnn 0 471.295576: xen_extra_version <-do_xe= n_version d0/v0 [000] nnn 0 472.052187: xen_extra_version <-do_xe= n_version The tracer is inspired by Linux ftrace's function tracer. Signed-off-by: Ryo Takakura --- xen/common/Makefile | 2 + xen/common/function_trace.c | 274 ++++++++++++++++++++++++++++++++++++ 2 files changed, 276 insertions(+) create mode 100644 xen/common/function_trace.c diff --git a/xen/common/Makefile b/xen/common/Makefile index 303016f250..435423f694 100644 --- a/xen/common/Makefile +++ b/xen/common/Makefile @@ -80,8 +80,10 @@ obj-$(CONFIG_NEEDS_LIBELF) +=3D libelf/ obj-$(CONFIG_LIBFDT) +=3D libfdt/ =20 obj-$(CONFIG_LIVEPATCH_TRACER) +=3D hypfs_trace.o +obj-$(CONFIG_LIVEPATCH_TRACER) +=3D function_trace.o # Do not generate patchable entries for the tracer functions. $(obj)/hypfs_trace.o: CFLAGS-y +=3D -fpatchable-function-entry=3D0,0 +$(obj)/function_trace.o: CFLAGS-y +=3D -fpatchable-function-entry=3D0,0 =20 CONF_FILE :=3D $(if $(patsubst /%,,$(KCONFIG_CONFIG)),$(objtree)/)$(KCONFI= G_CONFIG) $(obj)/config.gz: $(CONF_FILE) diff --git a/xen/common/function_trace.c b/xen/common/function_trace.c new file mode 100644 index 0000000000..5dd4ccdec0 --- /dev/null +++ b/xen/common/function_trace.c @@ -0,0 +1,274 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#define FUNCTION_TRACE_CTX_NORMAL 0 +#define FUNCTION_TRACE_CTX_IRQ (1U << 0) +#define FUNCTION_TRACE_CTX_IDLE (1U << 1) +#define FUNCTION_TRACE_CTX_IRQS_OFF (1U << 2) +#define FUNCTION_TRACE_CTX_RESCHED (1U << 3) +#define FUNCTION_TRACE_RECORD_TEXT_SIZE (2 * KSYM_NAME_LEN + 128) +#define FUNCTION_TRACE_BUFSIZE 256 + +static const char function_trace_header[] =3D + "#\n" + "# _-----=3D> irqs-off\n" + "# / _----=3D> need-resched\n" + "# | / _---=3D> in-irq\n" + "# || / _--=3D> preempt-depth\n" + "# ||| /\n" + "# DOMAIN/VCPU PCPU |||| TIMESTAMP FUNCTION\n" + "# | | |||| | |\n"; + +struct function_trace_record { + uint64_t timestamp; /* Timestamp of when the event occurred */ + unsigned long ip; /* The address of the traced function */ + unsigned long parent_ip; /* The address where the traced function was = called */ + unsigned int pcpu; /* pCPU */ + domid_t domid; /* Domain ID */ + unsigned int vcpuid; /* vCPU */ + unsigned int context_flags; /* Execution context */ + unsigned int preempt_depth; /* Preempt count */ +}; + +struct function_trace_cpu_buffer { + struct function_trace_record records[FUNCTION_TRACE_BUFSIZE]; + unsigned int head; + uint64_t written; +}; + +struct buffer_iterator { + const struct function_trace_cpu_buffer *buffer; + unsigned int start; + unsigned int count; + unsigned int next; +}; + +static DEFINE_PER_CPU(struct function_trace_cpu_buffer, function_trace_buf= fer); +static DEFINE_PER_CPU(bool, function_trace_recursing); + +static void cf_check function_trace_callback(unsigned long ip, unsigned lo= ng parent_ip) +{ + struct function_trace_cpu_buffer *buf; + struct function_trace_record *rec; + unsigned int ctx =3D FUNCTION_TRACE_CTX_NORMAL; + unsigned long pending; + + if ( this_cpu(function_trace_recursing) ) + return; + + this_cpu(function_trace_recursing) =3D true; + + buf =3D &this_cpu(function_trace_buffer); + rec =3D &buf->records[buf->head]; + + rec->preempt_depth =3D preempt_count(); + + if ( !local_irq_is_enabled() ) + ctx |=3D FUNCTION_TRACE_CTX_IRQS_OFF; + + pending =3D softirq_pending(smp_processor_id()); + if ( pending & ((1UL << SCHEDULE_SOFTIRQ) | + (1UL << SCHED_SLAVE_SOFTIRQ)) ) + ctx |=3D FUNCTION_TRACE_CTX_RESCHED; + + rec->timestamp =3D NOW(); + rec->ip =3D ip; + rec->parent_ip =3D parent_ip; + rec->pcpu =3D smp_processor_id(); + + if ( in_irq() ) + ctx |=3D FUNCTION_TRACE_CTX_IRQ; + + if ( is_idle_vcpu(current) ) + ctx |=3D FUNCTION_TRACE_CTX_IDLE; + + rec->context_flags =3D ctx; + rec->domid =3D current->domain->domain_id; + rec->vcpuid =3D current->vcpu_id; + + buf->head =3D (buf->head + 1) % FUNCTION_TRACE_BUFSIZE; + buf->written++; + + this_cpu(function_trace_recursing) =3D false; +} + +static int cf_check function_tracer_clear(struct trace_context *tr) +{ + struct function_trace_cpu_buffer *buf; + unsigned int cpu; + + for_each_online_cpu ( cpu ) + { + buf =3D &per_cpu(function_trace_buffer, cpu); + buf->head =3D 0; + buf->written =3D 0; + } + + return 0; +} + +static int cf_check function_tracer_init(struct trace_context *tr) +{ + int rc; + + rc =3D function_tracer_clear(tr); + if ( rc ) + return rc; + + return trace_register_callback(function_trace_callback); +} + +static int cf_check function_tracer_reset(struct trace_context *tr) +{ + return trace_unregister_callback(); +} + +static void format_ip(char *buf, size_t size, unsigned long ip) +{ + const char *name; + unsigned long sym_size, offset; + char namebuf[KSYM_NAME_LEN + 1]; + + name =3D symbols_lookup(ip, &sym_size, &offset, namebuf); + if ( name ) + snprintf(buf, size, "%s", name); + else + snprintf(buf, size, "0x%lx", ip); +} + +static unsigned int cf_check function_tracer_get_trace_size(struct trace_c= ontext *tr) +{ + unsigned int size =3D sizeof(function_trace_header) + 128; + unsigned int cpu, count; + struct function_trace_cpu_buffer *buf; + + for_each_online_cpu ( cpu ) + { + buf =3D &per_cpu(function_trace_buffer, cpu); + count =3D min(buf->written, (uint64_t)FUNCTION_TRACE_BUFSIZE); + size +=3D count * FUNCTION_TRACE_RECORD_TEXT_SIZE; + } + + return size; +} + +static size_t cf_check function_tracer_print_trace(struct trace_context *t= r, char *buf, size_t size) +{ + struct buffer_iterator *iters; + unsigned int cpu, nr_iters =3D 0; + uint64_t entries =3D 0, written =3D 0; + size_t pos =3D 0; + + iters =3D xzalloc_array(struct buffer_iterator, nr_cpu_ids); + if ( !iters ) + return scnprintf(buf, size, "# unable to allocate trace iterators\= n"); + + pos +=3D scnprintf(buf + pos, size - pos, "# tracer: function\n#\n"); + + for_each_online_cpu ( cpu ) + { + const struct function_trace_cpu_buffer *cbuf =3D &per_cpu(function= _trace_buffer, cpu); + struct buffer_iterator *iter =3D &iters[nr_iters++]; + + iter->buffer =3D cbuf; + iter->count =3D min(cbuf->written, (uint64_t)FUNCTION_TRACE_BUFSIZ= E); + iter->start =3D cbuf->written >=3D FUNCTION_TRACE_BUFSIZE ? cbuf->= head : 0; + + entries +=3D iter->count; + written +=3D cbuf->written; + } + + pos +=3D scnprintf(buf + pos, size - pos, + "# entries-in-buffer/entries-written: " + "%"PRIu64"/%"PRIu64" #P:%u\n", + entries, written, num_online_cpus()); + pos +=3D scnprintf(buf + pos, size - pos, "%s", function_trace_header); + + while ( pos < size - 1 ) + { + const struct function_trace_record *rec =3D NULL; + unsigned int i, selected =3D 0; + char func_str[KSYM_NAME_LEN]; + char parent_str[KSYM_NAME_LEN]; + char vcpu_str[32]; + uint64_t seconds; + unsigned int useconds; + + for ( i =3D 0; i < nr_iters; i++ ) + { + const struct buffer_iterator *iter =3D &iters[i]; + const struct function_trace_record *next_rec; + unsigned int idx; + + if ( iter->next =3D=3D iter->count ) + continue; + + idx =3D (iter->start + iter->next) % FUNCTION_TRACE_BUFSIZE; + next_rec =3D &iter->buffer->records[idx]; + + if ( !rec || next_rec->timestamp < rec->timestamp ) + { + rec =3D next_rec; + selected =3D i; + } + } + + if ( !rec ) + break; + + iters[selected].next++; + + format_ip(func_str, sizeof(func_str), rec->ip); + format_ip(parent_str, sizeof(parent_str), rec->parent_ip); + + seconds =3D rec->timestamp / 1000000000ULL; + useconds =3D (rec->timestamp % 1000000000ULL) / 1000; + + if ( rec->context_flags & FUNCTION_TRACE_CTX_IDLE ) + snprintf(vcpu_str, sizeof(vcpu_str), "idle"); + else + snprintf(vcpu_str, sizeof(vcpu_str), "d%u/v%u", + rec->domid, rec->vcpuid); + + pos +=3D scnprintf(buf + pos, size - pos, + "%14s [%03u] %c%c%c%3u %13"PRIu64".%06u: %s <-%s\= n", + vcpu_str, rec->pcpu, + rec->context_flags & FUNCTION_TRACE_CTX_IRQS_OFF = ? 'y' : 'n', + rec->context_flags & FUNCTION_TRACE_CTX_RESCHED ?= 'y' : 'n', + rec->context_flags & FUNCTION_TRACE_CTX_IRQ ? 'y'= : 'n', + rec->preempt_depth, seconds, useconds, func_str, = parent_str); + } + + xfree(iters); + + return pos; +} + +static struct tracer function_tracer =3D { + .name =3D "function", + .init =3D function_tracer_init, + .reset =3D function_tracer_reset, + .clear_trace =3D function_tracer_clear, + .get_trace_size =3D function_tracer_get_trace_size, + .print_trace =3D function_tracer_print_trace, +}; + +static int __init cf_check function_trace_init(void) +{ + return trace_register_tracer(&function_tracer); +} +__initcall(function_trace_init); --=20 2.34.1 From nobody Mon Nov 23 22:39:58 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; arc=pass (i=1 dmarc=pass fromdomain=valinux.co.jp); dmarc=pass(p=none dis=none) header.from=valinux.co.jp ARC-Seal: i=2; a=rsa-sha256; t=1792731524; cv=pass; d=zohomail.com; s=zohoarc; b=Ny0vRgFtItZjKan6u3fCR+jJrkbPmC3kZ6ULv11VUQQA6EmxMdjG770z0quyu+t+FmbCGhxgT2d04mPERG86Ix300btfAPerQmSICUf9nCtbP4ouz0nSuH4C5kEEv//NynGo3H89hifjEE8GZBsYArnpTiJRUhldxV3PzzkZifQ= ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792731524; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=ZlKIeSunT1jOHyfhh+rhjOJdcYlWVJgHKK+gdl4jasA=; b=iIHR0DwQs2bBpVQqWgQ5Qk419jZdK+0BGNnHtC61Z3RFuj5BVcwCGPz4Vt3ARS3FLe1rjybiZRSZYRweH9ylP7VIY5uYUL/y8vgVralkquMBVW7PR2DfB4rxERktYK9zBsRNUd6+IwzV+tUBXQGvy4Q9nUGaCP54R/omMjgI8ug= ARC-Authentication-Results: i=2; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; arc=pass (i=1 dmarc=pass fromdomain=valinux.co.jp); dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1792731523707353.07481009544665; Thu, 22 Oct 2026 21:58:43 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1458923.1670997 (Exim 4.92) (envelope-from ) id 1xK7MN-0001XA-3y; Fri, 23 Oct 2026 04:58:31 +0000 Received: by outflank-mailman (output) from mailman id 1458923.1670997; Fri, 23 Oct 2026 04:58:31 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xK7MN-0001X3-19; Fri, 23 Oct 2026 04:58:31 +0000 Received: by outflank-mailman (input) for mailman id 1458923; Fri, 23 Oct 2026 04:58:29 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xK7ML-0001Vg-Ek for xen-devel@lists.xenproject.org; Fri, 23 Oct 2026 04:58:29 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1xK7MK-004lKN-Rm for xen-devel@lists.xenproject.org; Fri, 23 Oct 2026 06:58:28 +0200 Received: from [10.42.69.7] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6adae96c-8faa-0a2a0a5109dd-0a2a4507bf10-8 for ; Fri, 23 Oct 2026 06:58:28 +0200 Received: from [52.101.228.79] (helo=OS0P286CU011.outbound.protection.outlook.com) by tlsNG-ef75cf.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6adae972-b4ea-0a2a45070019-3465e44f08f5-3 for ; Fri, 23 Oct 2026 06:58:28 +0200 Received: from TY5P286MB319683.JPNP286.PROD.OUTLOOK.COM (2603:1096:403:511::19) by TYYP286MB5665.JPNP286.PROD.OUTLOOK.COM (2603:1096:405:295::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.546.18; Fri, 23 Oct 2026 04:58:22 +0000 Received: from TY5P286MB319683.JPNP286.PROD.OUTLOOK.COM ([fe80::5f8d:a6bf:3b84:5a82]) by TY5P286MB319683.JPNP286.PROD.OUTLOOK.COM ([fe80::5f8d:a6bf:3b84:5a82%5]) with mapi id 15.21.0546.016; Fri, 23 Oct 2026 04:58:21 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=selector1 header.d=valinux.co.jp header.i="@valinux.co.jp" header.h="From:Date:Subject:Message-Id:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=E+dj1esqJJFrUPP9yd6LpRA4OnidB04JDaYWQFpTTpLR4pxjxYKKZ6GmOo0TFYalLyfXdMbe8fjXDgwieA/v764r/26517ItxO7NobmlKgBNPjWG5ja7vpjSSaaBbLXougpV7cFmBb6qVwfXzRyHAdl1sAFrYkpOi/DlWX+Tde8AkxPd/7fGYoh7OfUQCPOfBsdUJgCeqwRA/mqsqjjC7SDjhiXYLlQFISzYaeH2SNAtivZROnPBmnenp1+m5d0c1Qk0bm15CjJk04GJfS1JtBsEFPRonZFrokikU7WXrDG6rh8IBlgfGoshZB9GOyXC9/l2Zfb2umTCLKdnfBlbRQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=ZlKIeSunT1jOHyfhh+rhjOJdcYlWVJgHKK+gdl4jasA=; b=cXXVvr4QzT4SqdvSuI7MxlMU9DLQP00OJW2OcRmkfleHL/BpJZmUsVoueFhz/SZ1zFsIkFq/OvL0Xi6lTgk4denuQDONU8e8FID7lNxg+p+l53byenjkJRDNkxrMAd99b8hhU6UKhfQS1kVo2FfqrdowzpzSLVFvyJfLu5DCpbyM7nugYZSAkgPXP+uFjVWQ2F/PqHkmoQekBYjK2oLqCHNUAuWdHqvGSSCSxAUND2duHQLar9t0t/TZLj2hBTwShW4wBl0iFg1hkwDS/MQRtWefTjw1kXKTlO77BDNH3crCo8HDTv5YQhKl8empmB/RC5jTz1WOmI51D7yDH/7VJA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=valinux.co.jp; dmarc=pass action=none header.from=valinux.co.jp; dkim=pass header.d=valinux.co.jp; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=valinux.co.jp; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ZlKIeSunT1jOHyfhh+rhjOJdcYlWVJgHKK+gdl4jasA=; b=jGQB/OGfk0Gvr5sdu1CrAh3VUBxoRFhtO/XBBSydO2rqHoUHwkZ+hw+CIZo3DnRdz9lY7r5F4niEdvSicu7nysMwgvKex3Ogg0jr5Bmx5QPDUWJ91mZSZCWCSuwI3pRnRJT9xkrkB9NZ/PWsIOJLIr02ADbtwpTYR8HZE0/MVVg= Authentication-Results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=valinux.co.jp; From: Ryo Takakura To: xen-devel@lists.xenproject.org Cc: roger@xenproject.org, ross.lagerwall@citrix.com, sstabellini@kernel.org, julien@xen.org, bertrand.marquis@arm.com, michal.orzel@amd.com, Volodymyr_Babchuk@epam.com, andrew.cooper3@citrix.com, anthony.perard@vates.tech, jbeulich@suse.com, teddy.astie@vates.tech, Ryo Takakura Subject: [RFC 5/5] xen/x86: livepatch: enable attaching callbacks Date: Fri, 23 Oct 2026 13:58:08 +0900 Message-Id: <20261023045808.165276-1-takakura@valinux.co.jp> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261023044809.162759-1-takakura@valinux.co.jp> References: <20261023044809.162759-1-takakura@valinux.co.jp> Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: TYCP286CA0169.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:3c6::12) To TY5P286MB319683.JPNP286.PROD.OUTLOOK.COM (2603:1096:403:511::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: TY5P286MB319683:EE_|TYYP286MB5665:EE_ X-MS-Office365-Filtering-Correlation-Id: 487dcbf5-bfa3-4399-7c81-08df30c242e5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|1800799024|23010399003|366016|6133799003|18002099003|22082099003|3023799007|10067099003|5023799004|261009223027099003|261021123027099003|261022115727099003|261022113327099003|261021135927099003|261021121427099003|261022075727099003|261021133027099003|261021130227099003|261022105027099003|56012099006|260925022911599003|260925021311599003|260925021911599003; X-Microsoft-Antispam-Message-Info: QH2In2uHRTEs9GYp84KyKDtTL3lGi9Vqjv4w1EFWZ+s3z+ea77b/y9Yo7qf0ItTwpUiuE7KbF48Ovxggk7C5dBOjvF3NXURDrWqFVT0ZrytO2X3SjtiAWX+LmsonJ7ePgloR5dJKJbE+IIaw+O51EKa29VVgD2vTlIIX4jcoor8L8uIWB19YtuIUQuYASZLncCfWUPCaGh9n8uFAmQSlRgWpdtQDmRkal3bjxq8AgCLoBJkFUiKJojteWelTZ6lffArqx1XpMpJzhGJAQBz6V0paUZ5HkuGwcy3jRXuwFrkv6dmv6u8fmAzzo0XzfZEDItafkQaif1cFKE84Dr3Xj8lYkV0aP2dA+VRBvJUSuuaYD7tEJbnvaDEEVck4/9FgQh9gFILX8f6cRkSbF7oVOuDtZvTUqMq1HYtPHDLla2QdQ7envFu3/FmYh32srl6tFMdLUzJa/tvPxEbqf1KJmLAQRV600n6W/CSCGpZEZptokt2EIvUdM/ZNtY8FVCdAQ6OsdoSvMTdI6BqXe2Xjeim+/tNliOm82NEOEkwZyEf/lIkUzc/zG3H9k/q4J8jpPE9++5KlUm4hWnvND1V58MS7+dKTHRYmPaTZ3Mp7Frc4TpXG0S7MFZ5kSS7Lg2qdsJ2vGncKkpYaWebiRqFUpJOVN4sZUh1Z4dgUdPR5mAs= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:TY5P286MB319683.JPNP286.PROD.OUTLOOK.COM;PTR:;CAT:NONE;SFS:(13230040)(376014)(7416014)(1800799024)(23010399003)(366016)(6133799003)(18002099003)(22082099003)(3023799007)(10067099003)(5023799004)(261009223027099003)(261021123027099003)(261022115727099003)(261022113327099003)(261021135927099003)(261021121427099003)(261022075727099003)(261021133027099003)(261021130227099003)(261022105027099003)(56012099006)(260925022911599003)(260925021311599003)(260925021911599003);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?jOKD2MKU1bu7vHg76AmZDNBghNg9I9AZT1XKOmlNieYuPFEaj50+Uy6LTTL5?= =?us-ascii?Q?ZyuafUqjbDfK4vk617FYCGlruRZhBPxcUP1GyInlYS+fAZkLd68ZGKnm2nEB?= =?us-ascii?Q?ZTVaiy5Sifj3yCfG2DIKiXtXGjJoF4lxVHv0ylcDzsLbaaNjqdyQ1bejg0oe?= =?us-ascii?Q?mTualmYczU2N1mxoAi4UDexAbPZDtmgntHKQD8JBFUMNGYDhTIQTFJJtGzOk?= =?us-ascii?Q?uPOLccz63jWEUks10023dq5+/x2A0NU2yUULtJUEw0VJ7ml8JPAEUeJ0E4Lx?= =?us-ascii?Q?Qf8dbfpBwhQA56AfrpJiR5YWk2YxxLvG6lTXr2VEboXSoX6PEShsibuLFokC?= =?us-ascii?Q?AVeczsNFDDs0Ok7wAg+h6K7mduJkoUuhQz1SN1RpUo1cEXxVgd66NHl5uBtN?= =?us-ascii?Q?m/l4C2EZWzwAVBqYsftevXTgyixIQRabTsQQwCettXCS/Qr7shNxZQaBMlua?= =?us-ascii?Q?Xo4/0iGlWzWBUnPTXHr5d+6l3GWrOH/TRu7TifQiOLYnt6igsxhnDNptggro?= =?us-ascii?Q?CJtRie2JLO1z7tmKS3cpck7X28G4NmBJaahSj31LCzTe0T+9HTLDWGItawR8?= =?us-ascii?Q?JPsEveSWcwUTEqeKUsx0MFWK2RuVpPeKaavT/V1/dg0bF2ZF0UqIoyXrSr/0?= =?us-ascii?Q?I+D91Mw7xQUUZ3tMp5ucx3Rj3QuHB9L8UqyR8/ynjZcm4iHOVZlgBy1hqeUT?= =?us-ascii?Q?xXhuJq4Kwmu9/64TKHyjzT69E0MYuv4qTLVpNd+w4TGe83fl2wPEkZy7takI?= =?us-ascii?Q?PPHw3g+G0X07exZIwtvV+qO+jSdVw5cPOBWHepO+fHShVb1Yyq51djaW1Mpe?= =?us-ascii?Q?AUsU5SP2PdehVAGQaP+FcQvLTx4u2lA022c6pJm/GGGS+DooG50Um6/eKxT/?= =?us-ascii?Q?3QAK7Y6ldcZN7VJqOCTCmsMwfjtkutNHD8I93NgZ+oq2a5Z00BjUZ7VzRl5p?= =?us-ascii?Q?YZCC0N6XJnqcP7RmVfkLpD9BztTxiUcPM0x2UzKanExv3ollUkN5kbWtu4pq?= =?us-ascii?Q?oIGqYJOCcqSSi0Vr6L7fCE4tEJL1SWNh18X2yHDSYqQcfj27VTkBqbgY45dP?= =?us-ascii?Q?5ffIwW0n5BuFFRIEfSpJYS3x7uJDz0mHjrv2BCLO83EKYbd5q0PjjYj6Htr9?= =?us-ascii?Q?ebd/bD4eHxQIKrUTr1caOok9ZjbLOYOZrd2c6yJoFXuzhhMPzTpuatfw4pcv?= =?us-ascii?Q?+1jtBSeqrhIN9lN8JLSJNylzobG+GxZ3v4WQY1C/gLmRWPX5tGORVCu5+H9M?= =?us-ascii?Q?6HK9OMTmHfq95mtoKkP2QjNjeClareGJdKZR54w7tJFoQPAUOVIwF4C3olfq?= =?us-ascii?Q?66O21MiHI2M62WWcOn7Q2qAkn+Z3Me68mikFfeJvLKtrwYpKemFmjrW0T2//?= =?us-ascii?Q?PQ5i+37q5iBGzxzvUjO+3CZ59xyWZgktpC8qDGIHPkCh1nU5/gkrUJd7gKEU?= =?us-ascii?Q?ZhwTdWrUdWgnBuu7oqH6PCYApAaQpd58USkJp3/v8/NyDr6wXe3wVCZA69h9?= =?us-ascii?Q?qQA2A4xjSwmMCJBj6lzF4PdKzj1g63GfDz4DojNDNAduLTmjhD0e2ZpatGYl?= =?us-ascii?Q?KQO4Td4Mg8J4nkbnrwhqZIxMLn13xnC4Pv52pzOc+MS9XmOURmm3QPLxqk8A?= =?us-ascii?Q?zoBmFu0+ffs1B5AjsQoaunuMJw21oXvThsh/innS4JMBCAjLNtLfH21z9ZJt?= =?us-ascii?Q?U03zG3f4kNaFRBIT1I0hmCJ4Q+CRYf4OJ8EGVBv+iHYBVEFTtk1QOZod3FUL?= =?us-ascii?Q?OIxXhVxpCA=3D=3D?= X-OriginatorOrg: valinux.co.jp X-MS-Exchange-CrossTenant-Network-Message-Id: 487dcbf5-bfa3-4399-7c81-08df30c242e5 X-MS-Exchange-CrossTenant-AuthSource: TY5P286MB319683.JPNP286.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Oct 2026 04:58:21.4674 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 7a57bee8-f73d-4c5f-a4f7-d72c91c8c111 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: k21OgQlIRV8XNNNgPZOfEjxDKF1ZUUXke2HPyAFav/RX7WPy730Qb4+HhMAQL1DuvPb1i2noey+jKbwgn9Eejg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: TYYP286MB5665 X-purgate-ID: tlsNG-ef75cf/1792731508-358C1AE4-F122782A/0/0 X-purgate-type: clean X-purgate-size: 9681 X-ZohoMail-DKIM: pass (identity @valinux.co.jp) X-ZM-MESSAGEID: 1792731526635158500 Content-Type: text/plain; charset="utf-8" Add x86 support for attaching callbacks following the ARM64 implementation. It reserves function entry(5 NOPs) and replaces them with: call xen_livepatch_trace_caller xen_livepatch_trace_caller serves as a trampoline for saving registers before calling xen_livepatch_trace_dispatcher where callbacks are invoked. Callbacks receive: ip: Address of the traced function parent_ip: Address where the traced function was called One can request the feature by specifying the section for struct livepatch_func as .livepatch.traces. This patch will also let x86 use the livepatch tracer. Signed-off-by: Ryo Takakura --- xen/arch/arm/arm64/livepatch.c | 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/hypfs_trace.c | 4 ++- xen/common/livepatch.c | 2 +- xen/include/xen/livepatch.h | 1 + 10 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 xen/arch/x86/livepatch-asm.S diff --git a/xen/arch/arm/arm64/livepatch.c b/xen/arch/arm/arm64/livepatch.c index 17753f4463..b4ae381ef5 100644 --- a/xen/arch/arm/arm64/livepatch.c +++ b/xen/arch/arm/arm64/livepatch.c @@ -497,6 +497,11 @@ int arch_livepatch_perform_rela(struct livepatch_elf *= elf, } =20 #ifdef CONFIG_ARM64_PATCHABLE_FUNCTION_ENTRY +unsigned long arch_livepatch_trace_entry(unsigned long addr) +{ + return addr; +} + void arch_livepatch_apply_trace(const struct livepatch_func *trace) { uint32_t *new_ptr; diff --git a/xen/arch/x86/Kconfig b/xen/arch/x86/Kconfig index e5535ac484..bfb6a4d78e 100644 --- a/xen/arch/x86/Kconfig +++ b/xen/arch/x86/Kconfig @@ -355,6 +355,19 @@ config MICROCODE_SCAN_DEFAULT Enable if you have a Linux-based dom0 with microcode attached to the initramfs. =20 +config X86_PATCHABLE_FUNCTION_ENTRY + bool "Reserve five NOPs at function entry for tracing" + depends on LIVEPATCH && $(cc-option,-fpatchable-function-entry=3D5) + select HAS_LIVEPATCH_TRACE + default n + help + This option compiles Xen with -fpatchable-function-entry=3D5, which + reserves five NOP instructions at the beginning of functions. The NOPs + will be livepatched with a branch instruction to callback functions + registered by user. + + Enable this to use livepatch tracing feature. + endmenu =20 source "common/Kconfig" diff --git a/xen/arch/x86/Makefile b/xen/arch/x86/Makefile index 2be90ea098..06b39b7530 100644 --- a/xen/arch/x86/Makefile +++ b/xen/arch/x86/Makefile @@ -46,6 +46,7 @@ obj-$(CONFIG_PV) +=3D ioport_emulate.o obj-y +=3D io_apic.o obj-y +=3D irq.o obj-$(CONFIG_LIVEPATCH) +=3D livepatch.o +obj-$(CONFIG_X86_PATCHABLE_FUNCTION_ENTRY) +=3D livepatch-asm.o obj-$(CONFIG_KEXEC) +=3D machine_kexec.o obj-y +=3D mm.o x86_64/mm.o obj-$(CONFIG_VM_EVENT) +=3D monitor.o diff --git a/xen/arch/x86/arch.mk b/xen/arch/x86/arch.mk index cd0602a79a..31af1a15c4 100644 --- a/xen/arch/x86/arch.mk +++ b/xen/arch/x86/arch.mk @@ -129,6 +129,9 @@ endif # Set up the assembler include path properly for older toolchains. CFLAGS +=3D -Wa,-I$(objtree)/include -Wa,-I$(srctree)/include =20 +# Reserve entries for livepatch tracing +CFLAGS-$(CONFIG_X86_PATCHABLE_FUNCTION_ENTRY) +=3D -fpatchable-function-en= try=3D5 + # The GNU assembler will interpret '/' as a comment start marker instead o= f a # divide for some ELF targets. Pass --divide when when available to signa= l '/' # is always used as an operator in assembly. diff --git a/xen/arch/x86/livepatch-asm.S b/xen/arch/x86/livepatch-asm.S new file mode 100644 index 0000000000..005765b476 --- /dev/null +++ b/xen/arch/x86/livepatch-asm.S @@ -0,0 +1,60 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include + + .text + +/* + * xen/arch/x86/livepatch-asm.S + * + * Trampoline for livepatch tracer. + * + * If livepatch tracer is enabled, function entries are placed + * with five NOPs. arch_livepatch_apply_trace() will be livepatching + * those NOPs to: + * + * CALL xen_livepatch_trace_caller + * + * 0(%rsp): traced function + 5 + * 8(%rsp): return address to the traced function's caller + */ +FUNC(xen_livepatch_trace_caller) + pushq %rbp + movq %rsp, %rbp + pushfq + + /* Preserve general-purpose registers. */ + pushq %rax + pushq %rcx + pushq %rdx + pushq %rsi + pushq %rdi + pushq %r8 + pushq %r9 + pushq %r10 + pushq %r11 + + /* + * xen_livepatch_trace_dispatcher(ip, parent_ip) + * + * ip: Address of the traced function. + * parent_ip: Address where the traced function was called. + */ + movq 8(%rbp), %rdi + subq $5, %rdi + movq 16(%rbp), %rsi + call xen_livepatch_trace_dispatcher + + /* Restore the saved state. */ + popq %r11 + popq %r10 + popq %r9 + popq %r8 + popq %rdi + popq %rsi + popq %rdx + popq %rcx + popq %rax + popfq + popq %rbp + ret +END(xen_livepatch_trace_caller) diff --git a/xen/arch/x86/livepatch.c b/xen/arch/x86/livepatch.c index 5158e91f7e..1dfcc3e750 100644 --- a/xen/arch/x86/livepatch.c +++ b/xen/arch/x86/livepatch.c @@ -19,6 +19,10 @@ #include #include =20 +#ifdef CONFIG_X86_PATCHABLE_FUNCTION_ENTRY +extern void xen_livepatch_trace_caller(void); +#endif + static bool has_active_waitqueue(const struct vm_event_domain *ved) { /* ved may be xzalloc()'d without INIT_LIST_HEAD() yet. */ @@ -365,6 +369,35 @@ int arch_livepatch_secure(const void *va, unsigned int= pages, enum va_type type) return modify_xen_mappings(start, start + pages * PAGE_SIZE, flag); } =20 +#ifdef CONFIG_X86_PATCHABLE_FUNCTION_ENTRY +unsigned long arch_livepatch_trace_entry(unsigned long addr) +{ + if ( (is_endbr64((const void *)addr) || is_endbr64_poison((const void = *)addr)) ) + addr +=3D ENDBR64_LEN; + + return addr; +} + +void noinline arch_livepatch_apply_trace(const struct livepatch_func *trac= e) +{ + void *site =3D (void *)arch_livepatch_trace_entry((unsigned long)trace= ->old_addr); + const void *new_addr =3D xen_livepatch_trace_caller; + uint8_t insn[ARCH_PATCH_INSN_SIZE]; + int32_t val =3D new_addr - (site + ARCH_PATCH_INSN_SIZE); + + insn[0] =3D 0xe8; /* CALL rel32. */ + memcpy(&insn[1], &val, sizeof(val)); + memcpy(site, insn, sizeof(insn)); +} + +void noinline arch_livepatch_revert_trace(const struct livepatch_func *tra= ce) +{ + void *site =3D (void *)arch_livepatch_trace_entry((unsigned long)trace= ->old_addr); + + memset(site, 0x90, ARCH_PATCH_INSN_SIZE); +} +#endif + void __init arch_livepatch_init(void) { void *start, *end; diff --git a/xen/arch/x86/xen.lds.S b/xen/arch/x86/xen.lds.S index 8f943e11ea..1f7d052c30 100644 --- a/xen/arch/x86/xen.lds.S +++ b/xen/arch/x86/xen.lds.S @@ -126,6 +126,11 @@ SECTIONS *(.data.rel.ro) *(.data.rel.ro.*) =20 + . =3D ALIGN(8); + __start_patchable_function_entries =3D .; + KEEP(*(__patchable_function_entries)) + __stop_patchable_function_entries =3D .; + #if defined(BUILD_ID) && defined(EFI) && !defined(BUILD_ID_EFI) /* * No mechanism to put an PT_NOTE in the EFI file - so put diff --git a/xen/common/hypfs_trace.c b/xen/common/hypfs_trace.c index d9e06e092e..314cce09bc 100644 --- a/xen/common/hypfs_trace.c +++ b/xen/common/hypfs_trace.c @@ -141,7 +141,7 @@ void trace_dispatch_callbacks(unsigned long ip, unsigne= d long parent_ip) if ( !global_trace_func.old_addr ) return; =20 - if ( (unsigned long)global_trace_func.old_addr !=3D ip ) + if ( arch_livepatch_trace_entry((unsigned long)global_trace_func.old_a= ddr) !=3D ip ) return; =20 callback =3D global_trace_func.new_addr; @@ -194,6 +194,8 @@ static bool is_patchable_function_entry(unsigned long a= ddr) { const unsigned long *p; =20 + addr =3D arch_livepatch_trace_entry(addr); + for ( p =3D __start_patchable_function_entries; p < __stop_patchable_function_entries; p++ ) { diff --git a/xen/common/livepatch.c b/xen/common/livepatch.c index 352a2df62c..295a3ebd57 100644 --- a/xen/common/livepatch.c +++ b/xen/common/livepatch.c @@ -224,7 +224,7 @@ xen_livepatch_trace_dispatcher(unsigned long ip, unsign= ed long parent_ip) { f =3D &data->funcs[i]; =20 - if ( (unsigned long)f->old_addr !=3D ip ) + if ( arch_livepatch_trace_entry((unsigned long)f->old_addr) != =3D ip ) continue; =20 ASSERT(f->new_addr); diff --git a/xen/include/xen/livepatch.h b/xen/include/xen/livepatch.h index 1b927af8ee..0e5af5fdc5 100644 --- a/xen/include/xen/livepatch.h +++ b/xen/include/xen/livepatch.h @@ -130,6 +130,7 @@ void arch_livepatch_apply_trace(const struct livepatch_= func *trace); void arch_livepatch_revert(const struct livepatch_func *func, struct livepatch_fstate *state); void arch_livepatch_revert_trace(const struct livepatch_func *trace); +unsigned long arch_livepatch_trace_entry(unsigned long addr); void arch_livepatch_post_action(void); =20 void arch_livepatch_mask(void); --=20 2.34.1