* Re: [PATCH 3/3] tools/accounting: simplify 32-bit time_t overflow check in format_timespec()
From: xu.xin16 @ 2026-07-18 13:28 UTC (permalink / raw)
To: wang.yaxin
Cc: wang.yaxin, akpm, fan.yu9, yang.yang29, corbet, linux-kernel,
linux-doc
In-Reply-To: <20260718133822444iLo8o-zXcS91NMNEg17Gv@zte.com.cn>
> From: Wang Yaxin <wang.yaxin@zte.com.cn>
>
> Replace the Y2038 overflow guard in format_timespec() with a direct
> narrowing truncation check ((long long)time_sec != ts->tv_sec), which
> is both simpler and more robust across platforms. Also move the
> time_sec assignment earlier to avoid duplication.
>
> While at it, fix a minor alignment issue in delaytop.c by adding a
> leading space to the output format string.
>
> Signed-off-by: Wang Yaxin <wang.yaxin@zte.com.cn>
Acked-by: Xu Xin <xu.xin16@zte.com.cn>
^ permalink raw reply
* Re: [PATCH 2/3] tools/accounting: factor out shared format_timespec() implementation
From: xu.xin16 @ 2026-07-18 13:27 UTC (permalink / raw)
To: wang.yaxin
Cc: wang.yaxin, akpm, fan.yu9, yang.yang29, corbet, linux-kernel,
linux-doc
In-Reply-To: <20260718133702354_hJQf_RdG3c52A42Onwa_@zte.com.cn>
> From: Wang Yaxin <wang.yaxin@zte.com.cn>
>
> The same __kernel_timespec formatting logic existed independently in
> both getdelays.c and delaytop.c with minor differences (strftime vs
> snprintf, __kernel_time64_t vs time_t).
>
> Create a shared format_timespec.c/h with a canonical implementation
> (strftime + time_t), remove the static copies from both files, and
> link both programs against the common object.
>
> Also simplify the Makefile with a pattern rule for %.o and a static
> pattern rule for the two programs that need format_timespec.o.
>
> Signed-off-by: Wang Yaxin <wang.yaxin@zte.com.cn>
Acked-by: Xu Xin <xu.xin16@zte.com.cn>
^ permalink raw reply
* Re: [PATCH 1/3] delaytop: refactor repetitive delay fields into array with enum
From: xu.xin16 @ 2026-07-18 13:25 UTC (permalink / raw)
To: wang.yaxin
Cc: wang.yaxin, akpm, fan.yu9, yang.yang29, corbet, linux-kernel,
linux-doc
In-Reply-To: <202607181335459184foygLX7rXegGUPftV9-3@zte.com.cn>
> Replace the 9 groups of (count, delay_total, delay_max, delay_max_ts)
> named fields in struct task_info with a struct delay_metrics array
> indexed by enum delay_type. This eliminates all unsafe pointer
> arithmetic via offsetof() from compare_tasks(),
> field_delay_max_and_ts(), and get_field_delay_values().
>
> The struct field_desc now stores an enum delay_type index instead of
> four separate unsigned long offset values.
>
> Signed-off-by: Wang Yaxin <wang.yaxin@zte.com.cn>
You missed the subject prefix 'v3'.
Acked-by: Xu Xin <xu.xin16@zte.com.cn>
^ permalink raw reply
* [PATCH] cgroup/cpu: document cpu.stat nice_usec and core_sched.force_idle_usec
From: Tao Cui @ 2026-07-18 8:01 UTC (permalink / raw)
To: cgroups, Tejun Heo
Cc: Johannes Weiner, Michal Koutný, Jonathan Corbet, Shuah Khan,
Joshua Hahn, linux-doc, linux-kernel, cuitao, cui.tao
From: Tao Cui <cuitao@kylinos.cn>
cgroup_base_stat_cputime_show() unconditionally prints a fourth base
stat, nice_usec, in addition to usage_usec/user_usec/system_usec, and
also prints core_sched.force_idle_usec when CONFIG_SCHED_CORE is enabled.
Neither field is currently described in the cpu.stat section of
cgroup-v2.rst, which still states it "always reports the following three
stats".
nice_usec was added by commit aefa398d93d5 ("cgroup/rstat: Tracking
cgroup-level niced CPU time") and core_sched.force_idle_usec by commit
1fcf54deb767 ("sched/core: add forced idle accounting for cgroups"); the
documentation was not updated in either case.
Update the cpu.stat description to list both fields and fix the count.
Signed-off-by: Tao Cui <cuitao@kylinos.cn>
---
Documentation/admin-guide/cgroup-v2.rst | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst
index 0df15a672cf3..778b759dd892 100644
--- a/Documentation/admin-guide/cgroup-v2.rst
+++ b/Documentation/admin-guide/cgroup-v2.rst
@@ -1144,12 +1144,18 @@ will be referred to. All time durations are in microseconds.
A read-only flat-keyed file.
This file exists whether the controller is enabled or not.
- It always reports the following three stats, which account for all the
+ It always reports the following four stats, which account for all the
processes in the cgroup (including those in descendant cgroups):
- usage_usec
- user_usec
- system_usec
+ - nice_usec
+
+ When core scheduling (CONFIG_SCHED_CORE) is enabled, an additional
+ stat is reported:
+
+ - core_sched.force_idle_usec
and the following five when the controller is enabled, which account for
only the processes under the fair-class scheduler:
--
2.43.0
^ permalink raw reply related
* Re: [PATCH] f2fs: introduce gcless mount option to avoid foreground GC
From: kernel test robot @ 2026-07-18 7:51 UTC (permalink / raw)
To: Yonggil Song, jaegeuk@kernel.org, chao@kernel.org, corbet@lwn.net
Cc: oe-kbuild-all, Yonggil Song,
linux-f2fs-devel@lists.sourceforge.net, linux-doc@vger.kernel.org,
linux-kernel@vger.kernel.org, Dongjin Kim, Seokhwan Kim,
Daejun Park
In-Reply-To: <20260706062043epcms2p16928216befdca1242bc2d3cd7cb34afe@epcms2p1>
Hi Yonggil,
kernel test robot noticed the following build warnings:
[auto build test WARNING on cb8ff3ead9a3fc43727980be58c7099506f65261]
url: https://github.com/intel-lab-lkp/linux/commits/Yonggil-Song/f2fs-introduce-gcless-mount-option-to-avoid-foreground-GC/20260706-154229
base: cb8ff3ead9a3fc43727980be58c7099506f65261
patch link: https://lore.kernel.org/r/20260706062043epcms2p16928216befdca1242bc2d3cd7cb34afe%40epcms2p1
patch subject: [PATCH] f2fs: introduce gcless mount option to avoid foreground GC
config: i386-randconfig-2006-20250825 (https://download.01.org/0day-ci/archive/20260718/202607180917.doNNtTXN-lkp@intel.com/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260718/202607180917.doNNtTXN-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202607180917.doNNtTXN-lkp@intel.com/
All warnings (new ones prefixed by >>):
In file included from include/linux/bits.h:5,
from include/linux/bitops.h:6,
from include/linux/log2.h:12,
from arch/x86/include/asm/div64.h:8,
from include/linux/math.h:6,
from include/linux/math64.h:6,
from include/linux/time.h:6,
from include/linux/stat.h:19,
from include/linux/fs_dirent.h:5,
from include/linux/fs/super_types.h:5,
from include/linux/fs/super.h:5,
from include/linux/fs.h:5,
from fs/f2fs/dir.c:9:
fs/f2fs/f2fs.h: In function 'inc_valid_node_count':
>> include/vdso/bits.h:7:40: warning: left shift count >= width of type [-Wshift-count-overflow]
7 | #define BIT(nr) (UL(1) << (nr))
| ^~
fs/f2fs/f2fs.h:150:33: note: in expansion of macro 'BIT'
150 | (F2FS_OPTION(sbi).opt & BIT(F2FS_MOUNT_##option))
| ^~~
fs/f2fs/f2fs.h:2945:25: note: in expansion of macro 'test_opt'
2945 | test_opt(sbi, RESERVE_NODE));
| ^~~~~~~~
>> include/vdso/bits.h:7:40: warning: left shift count >= width of type [-Wshift-count-overflow]
7 | #define BIT(nr) (UL(1) << (nr))
| ^~
fs/f2fs/f2fs.h:150:33: note: in expansion of macro 'BIT'
150 | (F2FS_OPTION(sbi).opt & BIT(F2FS_MOUNT_##option))
| ^~~
fs/f2fs/f2fs.h:2953:13: note: in expansion of macro 'test_opt'
2953 | if (test_opt(sbi, RESERVE_NODE) &&
| ^~~~~~~~
--
In file included from include/linux/bits.h:5,
from include/linux/ratelimit_types.h:5,
from include/linux/printk.h:9,
from include/asm-generic/bug.h:31,
from arch/x86/include/asm/bug.h:193,
from arch/x86/include/asm/alternative.h:9,
from arch/x86/include/asm/barrier.h:5,
from include/linux/list.h:11,
from include/linux/module.h:12,
from fs/f2fs/super.c:8:
fs/f2fs/f2fs.h: In function 'inc_valid_node_count':
>> include/vdso/bits.h:7:40: warning: left shift count >= width of type [-Wshift-count-overflow]
7 | #define BIT(nr) (UL(1) << (nr))
| ^~
fs/f2fs/f2fs.h:150:33: note: in expansion of macro 'BIT'
150 | (F2FS_OPTION(sbi).opt & BIT(F2FS_MOUNT_##option))
| ^~~
fs/f2fs/f2fs.h:2945:25: note: in expansion of macro 'test_opt'
2945 | test_opt(sbi, RESERVE_NODE));
| ^~~~~~~~
>> include/vdso/bits.h:7:40: warning: left shift count >= width of type [-Wshift-count-overflow]
7 | #define BIT(nr) (UL(1) << (nr))
| ^~
fs/f2fs/f2fs.h:150:33: note: in expansion of macro 'BIT'
150 | (F2FS_OPTION(sbi).opt & BIT(F2FS_MOUNT_##option))
| ^~~
fs/f2fs/f2fs.h:2953:13: note: in expansion of macro 'test_opt'
2953 | if (test_opt(sbi, RESERVE_NODE) &&
| ^~~~~~~~
fs/f2fs/super.c: In function 'limit_reserve_root':
>> include/vdso/bits.h:7:40: warning: left shift count >= width of type [-Wshift-count-overflow]
7 | #define BIT(nr) (UL(1) << (nr))
| ^~
fs/f2fs/f2fs.h:150:33: note: in expansion of macro 'BIT'
150 | (F2FS_OPTION(sbi).opt & BIT(F2FS_MOUNT_##option))
| ^~~
fs/f2fs/super.c:520:13: note: in expansion of macro 'test_opt'
520 | if (test_opt(sbi, RESERVE_NODE) &&
| ^~~~~~~~
>> include/vdso/bits.h:7:40: warning: left shift count >= width of type [-Wshift-count-overflow]
7 | #define BIT(nr) (UL(1) << (nr))
| ^~
fs/f2fs/f2fs.h:150:33: note: in expansion of macro 'BIT'
150 | (F2FS_OPTION(sbi).opt & BIT(F2FS_MOUNT_##option))
| ^~~
fs/f2fs/super.c:526:46: note: in expansion of macro 'test_opt'
526 | if (!test_opt(sbi, RESERVE_ROOT) && !test_opt(sbi, RESERVE_NODE) &&
| ^~~~~~~~
fs/f2fs/super.c: In function 'f2fs_check_opt_consistency':
>> include/vdso/bits.h:7:40: warning: left shift count >= width of type [-Wshift-count-overflow]
7 | #define BIT(nr) (UL(1) << (nr))
| ^~
fs/f2fs/f2fs.h:150:33: note: in expansion of macro 'BIT'
150 | (F2FS_OPTION(sbi).opt & BIT(F2FS_MOUNT_##option))
| ^~~
fs/f2fs/super.c:1528:13: note: in expansion of macro 'test_opt'
1528 | if (test_opt(sbi, RESERVE_NODE) &&
| ^~~~~~~~
>> include/vdso/bits.h:7:40: warning: left shift count >= width of type [-Wshift-count-overflow]
7 | #define BIT(nr) (UL(1) << (nr))
| ^~
fs/f2fs/super.c:1529:42: note: in expansion of macro 'BIT'
1529 | (ctx->opt_mask & BIT(F2FS_MOUNT_RESERVE_NODE)) &&
| ^~~
>> include/vdso/bits.h:7:40: warning: left shift count >= width of type [-Wshift-count-overflow]
7 | #define BIT(nr) (UL(1) << (nr))
| ^~
fs/f2fs/super.c:1534:35: note: in expansion of macro 'BIT'
1534 | ctx->opt_mask &= ~BIT(F2FS_MOUNT_RESERVE_NODE);
| ^~~
fs/f2fs/super.c: In function 'f2fs_show_options':
>> include/vdso/bits.h:7:40: warning: left shift count >= width of type [-Wshift-count-overflow]
7 | #define BIT(nr) (UL(1) << (nr))
| ^~
fs/f2fs/f2fs.h:150:33: note: in expansion of macro 'BIT'
150 | (F2FS_OPTION(sbi).opt & BIT(F2FS_MOUNT_##option))
| ^~~
fs/f2fs/super.c:2475:44: note: in expansion of macro 'test_opt'
2475 | if (test_opt(sbi, RESERVE_ROOT) || test_opt(sbi, RESERVE_NODE))
| ^~~~~~~~
vim +7 include/vdso/bits.h
3945ff37d2f48d Vincenzo Frascino 2020-03-20 6
3945ff37d2f48d Vincenzo Frascino 2020-03-20 @7 #define BIT(nr) (UL(1) << (nr))
cbdb1f163af2bb Andy Shevchenko 2022-11-28 8 #define BIT_ULL(nr) (ULL(1) << (nr))
3945ff37d2f48d Vincenzo Frascino 2020-03-20 9
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply
* Re: [PATCH v18 11/13] PCI: Cache PCI DSN into pci_dev->dsn during probe
From: Lukas Wunner @ 2026-07-18 7:02 UTC (permalink / raw)
To: Terry Bowman
Cc: Bjorn Helgaas, Dan Williams, Dave Jiang, Ira Weiny,
Jonathan Cameron, Len Brown, Rafael J . Wysocki, Robert Richter,
linux-acpi, linux-cxl, linux-doc, linux-kernel, linux-pci,
linuxppc-dev, Alejandro Lucero, Alison Schofield, Ankit Agrawal,
Ard Biesheuvel, Ben Cheatham, Borislav Petkov, Breno Leitao,
Davidlohr Bueso, Fabio M . De Francesco, Gregory Price,
Hanjun Guo, Jonathan Corbet, Kees Cook,
Kuppuswamy Sathyanarayanan, Li Ming, Mahesh J Salgaonkar,
Mauro Carvalho Chehab, Oliver O'Halloran, Shiju Jose,
Shuah Khan, Shuai Xue, Smita Koralahalli, Tony Luck, Vishal Verma
In-Reply-To: <20260717222706.3540281-12-terry.bowman@amd.com>
On Fri, Jul 17, 2026 at 05:27:04PM -0500, Terry Bowman wrote:
> Add a u64 dsn field to struct pci_dev and populate it from pci_get_dsn()
> during pci_init_capabilities() at probe time via pci_dsn_init(). Only
> write dev->dsn when the read succeeds. The zero initial value from
> pci_dev allocation already represents 'no DSN available.'
The DSN is already cached on (natively handled) PCIe hotplug ports
to detect device replacement during system sleep, see struct controller
in drivers/pci/hotplug/pciehp.h.
Please remove that member from struct controller, remove the two
assignments to the member in pciehp_configure_device() and pcie_init()
and change the comparison in pciehp_device_replaced() to use the new
member in struct pci_dev. You can do this either as part of this patch
or in a separate patch.
Thanks,
Lukas
^ permalink raw reply
* [PATCH 3/3] tools/accounting: simplify 32-bit time_t overflow check in format_timespec()
From: wang.yaxin @ 2026-07-18 5:38 UTC (permalink / raw)
To: wang.yaxin
Cc: akpm, fan.yu9, yang.yang29, corbet, linux-kernel, linux-doc,
xu.xin16
In-Reply-To: <20260718133420538UvsJFurowueTqxAaZ1cRP@zte.com.cn>
From: Wang Yaxin <wang.yaxin@zte.com.cn>
Replace the Y2038 overflow guard in format_timespec() with a direct
narrowing truncation check ((long long)time_sec != ts->tv_sec), which
is both simpler and more robust across platforms. Also move the
time_sec assignment earlier to avoid duplication.
While at it, fix a minor alignment issue in delaytop.c by adding a
leading space to the output format string.
Signed-off-by: Wang Yaxin <wang.yaxin@zte.com.cn>
---
tools/accounting/delaytop.c | 6 +++++-
tools/accounting/format_timespec.c | 12 +++++-------
tools/accounting/getdelays.c | 4 ++++
3 files changed, 14 insertions(+), 8 deletions(-)
diff --git a/tools/accounting/delaytop.c b/tools/accounting/delaytop.c
index 1144ca325447..d32aedbfa07d 100644
--- a/tools/accounting/delaytop.c
+++ b/tools/accounting/delaytop.c
@@ -728,10 +728,14 @@ static void fetch_and_fill_task_info(int pid, const char *comm)
nl_len = GENLMSG_PAYLOAD(&resp.n);
na = (struct nlattr *) GENLMSG_DATA(&resp);
while (nl_len > 0) {
+ if (na->nla_len < NLA_HDRLEN)
+ break;
if (na->nla_type == TASKSTATS_TYPE_AGGR_PID) {
nested = (struct nlattr *) NLA_DATA(na);
nested_len = NLA_PAYLOAD(na->nla_len);
while (nested_len > 0) {
+ if (nested->nla_len < NLA_HDRLEN)
+ break;
if (nested->nla_type == TASKSTATS_TYPE_STATS) {
size_t payload_len = NLA_PAYLOAD(nested->nla_len);
@@ -1099,7 +1103,7 @@ static void display_results(int psi_ret)
get_field_delay_values(&tasks[i], cfg.type_field, &avg_ms,
&max_ms, &max_ts);
- suc &= BOOL_FPRINT(out, "%12.2f %12.2f %20s\n",
+ suc &= BOOL_FPRINT(out, " %12.2f %12.2f %20s\n",
avg_ms, max_ms, format_timespec(&max_ts));
} else if (cfg.display_mode == MODE_MEMVERBOSE) {
suc &= BOOL_FPRINT(out, DELAY_FMT_MEMVERBOSE,
diff --git a/tools/accounting/format_timespec.c b/tools/accounting/format_timespec.c
index 1dba50cac895..d7bfd307c00b 100644
--- a/tools/accounting/format_timespec.c
+++ b/tools/accounting/format_timespec.c
@@ -14,22 +14,20 @@ const char *format_timespec(const struct __kernel_timespec *ts)
{
static char buffer[32];
struct tm tm_info;
- time_t time_sec;
+ time_t time_sec = ts->tv_sec;
if (ts->tv_sec == 0 && ts->tv_nsec == 0)
return "N/A";
/*
* On 32-bit platforms time_t is 32-bit and cannot represent
- * dates beyond Y2038. The kernel timestamp is always 64-bit,
- * so reject values that would overflow.
+ * timestamps outside [INT32_MIN, INT32_MAX]. A 64-bit kernel
+ * timestamp that does not survive the narrowing truncation is
+ * rejected to avoid silent data corruption.
*/
- if (sizeof(time_t) < sizeof(ts->tv_sec) &&
- ts->tv_sec > (__u64)((1ULL << (sizeof(time_t) * 8 - 1)) - 1))
+ if ((long long)time_sec != ts->tv_sec)
return "N/A";
- time_sec = ts->tv_sec;
-
if (!localtime_r(&time_sec, &tm_info))
return "N/A";
diff --git a/tools/accounting/getdelays.c b/tools/accounting/getdelays.c
index d3193f670d89..f47b9d008352 100644
--- a/tools/accounting/getdelays.c
+++ b/tools/accounting/getdelays.c
@@ -659,6 +659,8 @@ int main(int argc, char *argv[])
na = (struct nlattr *) GENLMSG_DATA(&msg);
len = 0;
while (len < rep_len) {
+ if (na->nla_len < NLA_HDRLEN)
+ break;
len += NLA_ALIGN(na->nla_len);
switch (na->nla_type) {
case TASKSTATS_TYPE_AGGR_TGID:
@@ -669,6 +671,8 @@ int main(int argc, char *argv[])
/* For nested attributes, na follows */
na = (struct nlattr *) NLA_DATA(na);
while (len2 < aggr_len) {
+ if (na->nla_len < NLA_HDRLEN)
+ break;
switch (na->nla_type) {
case TASKSTATS_TYPE_PID:
rtid = *(int *) NLA_DATA(na);
--
2.27.0
^ permalink raw reply related
* [PATCH 2/3] tools/accounting: factor out shared format_timespec() implementation
From: wang.yaxin @ 2026-07-18 5:37 UTC (permalink / raw)
To: wang.yaxin
Cc: akpm, fan.yu9, yang.yang29, corbet, linux-kernel, linux-doc,
xu.xin16
In-Reply-To: <20260718133420538UvsJFurowueTqxAaZ1cRP@zte.com.cn>
From: Wang Yaxin <wang.yaxin@zte.com.cn>
The same __kernel_timespec formatting logic existed independently in
both getdelays.c and delaytop.c with minor differences (strftime vs
snprintf, __kernel_time64_t vs time_t).
Create a shared format_timespec.c/h with a canonical implementation
(strftime + time_t), remove the static copies from both files, and
link both programs against the common object.
Also simplify the Makefile with a pattern rule for %.o and a static
pattern rule for the two programs that need format_timespec.o.
Signed-off-by: Wang Yaxin <wang.yaxin@zte.com.cn>
---
tools/accounting/Makefile | 12 ++++++-
tools/accounting/delaytop.c | 39 ++-------------------
tools/accounting/format_timespec.c | 39 +++++++++++++++++++++
tools/accounting/format_timespec.h | 9 +++++
tools/accounting/getdelays.c | 32 ++---------------
tools/include/uapi/linux/time_types.h | 49 +++++++++++++++++++++++++++
6 files changed, 113 insertions(+), 67 deletions(-)
create mode 100644 tools/accounting/format_timespec.c
create mode 100644 tools/accounting/format_timespec.h
create mode 100644 tools/include/uapi/linux/time_types.h
diff --git a/tools/accounting/Makefile b/tools/accounting/Makefile
index 007c0bb8cbbb..5224e03f5e94 100644
--- a/tools/accounting/Makefile
+++ b/tools/accounting/Makefile
@@ -3,8 +3,18 @@ CC := $(CROSS_COMPILE)gcc
CFLAGS := -I../include/uapi/
PROGS := getdelays procacct delaytop
+OBJS := format_timespec.o
all: $(PROGS)
+getdelays delaytop: %: %.o $(OBJS)
+ $(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
+
+procacct: procacct.o
+ $(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
+
+%.o: %.c
+ $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<
+
clean:
- rm -fr $(PROGS)
+ rm -fr $(PROGS) *.o
diff --git a/tools/accounting/delaytop.c b/tools/accounting/delaytop.c
index f1d26ff98792..1144ca325447 100644
--- a/tools/accounting/delaytop.c
+++ b/tools/accounting/delaytop.c
@@ -44,6 +44,8 @@
#include <linux/cgroupstats.h>
#include <stddef.h>
+#include "format_timespec.h"
+
#define PSI_PATH "/proc/pressure"
#define PSI_CPU_PATH "/proc/pressure/cpu"
#define PSI_MEMORY_PATH "/proc/pressure/memory"
@@ -817,41 +819,6 @@ static double average_ms(unsigned long long total, unsigned long long count)
return (double)total / 1000000.0 / count;
}
-/*
- * Format __kernel_timespec to human readable string (YYYY-MM-DDTHH:MM:SS)
- * Returns formatted string or "N/A" if timestamp is zero
- */
-static const char *format_kernel_timespec(struct __kernel_timespec *ts)
-{
- static char buffer[32];
- time_t time_sec;
- struct tm tm_info;
-
- /* Check if timestamp is zero (not set) */
- if (ts->tv_sec == 0 && ts->tv_nsec == 0)
- return "N/A";
-
- /* Avoid Y2038 truncation: check if timestamp fits in time_t on 32-bit platforms */
- if (sizeof(time_t) < sizeof(ts->tv_sec) &&
- ts->tv_sec > (__u64)((1ULL << (sizeof(time_t) * 8 - 1)) - 1))
- return "N/A";
-
- time_sec = (time_t)ts->tv_sec;
-
- if (localtime_r(&time_sec, &tm_info) == NULL)
- return "N/A";
-
- snprintf(buffer, sizeof(buffer), "%04d-%02d-%02dT%02d:%02d:%02d",
- tm_info.tm_year + 1900,
- tm_info.tm_mon + 1,
- tm_info.tm_mday,
- tm_info.tm_hour,
- tm_info.tm_min,
- tm_info.tm_sec);
-
- return buffer;
-}
-
/* Comparison function for sorting tasks */
static int compare_tasks(const void *a, const void *b)
{
@@ -1133,7 +1100,7 @@ static void display_results(int psi_ret)
&max_ms, &max_ts);
suc &= BOOL_FPRINT(out, "%12.2f %12.2f %20s\n",
- avg_ms, max_ms, format_kernel_timespec(&max_ts));
+ avg_ms, max_ms, format_timespec(&max_ts));
} else if (cfg.display_mode == MODE_MEMVERBOSE) {
suc &= BOOL_FPRINT(out, DELAY_FMT_MEMVERBOSE,
TASK_AVG(tasks[i], MEM),
diff --git a/tools/accounting/format_timespec.c b/tools/accounting/format_timespec.c
new file mode 100644
index 000000000000..1dba50cac895
--- /dev/null
+++ b/tools/accounting/format_timespec.c
@@ -0,0 +1,39 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Shared __kernel_timespec formatting for tools/accounting/
+ *
+ * Formats a __kernel_timespec to an ISO 8601 timestamp string
+ * (YYYY-MM-DDTHH:MM:SS). Returns "N/A" if the timestamp is zero
+ * or does not fit in time_t.
+ */
+#include <time.h>
+#include <linux/time_types.h>
+#include "format_timespec.h"
+
+const char *format_timespec(const struct __kernel_timespec *ts)
+{
+ static char buffer[32];
+ struct tm tm_info;
+ time_t time_sec;
+
+ if (ts->tv_sec == 0 && ts->tv_nsec == 0)
+ return "N/A";
+
+ /*
+ * On 32-bit platforms time_t is 32-bit and cannot represent
+ * dates beyond Y2038. The kernel timestamp is always 64-bit,
+ * so reject values that would overflow.
+ */
+ if (sizeof(time_t) < sizeof(ts->tv_sec) &&
+ ts->tv_sec > (__u64)((1ULL << (sizeof(time_t) * 8 - 1)) - 1))
+ return "N/A";
+
+ time_sec = ts->tv_sec;
+
+ if (!localtime_r(&time_sec, &tm_info))
+ return "N/A";
+
+ strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S", &tm_info);
+
+ return buffer;
+}
diff --git a/tools/accounting/format_timespec.h b/tools/accounting/format_timespec.h
new file mode 100644
index 000000000000..960496ebf5c2
--- /dev/null
+++ b/tools/accounting/format_timespec.h
@@ -0,0 +1,9 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef FORMAT_TIMESPEC_H
+#define FORMAT_TIMESPEC_H
+
+#include <linux/time_types.h>
+
+const char *format_timespec(const struct __kernel_timespec *ts);
+
+#endif
diff --git a/tools/accounting/getdelays.c b/tools/accounting/getdelays.c
index 6ac30d4f96f7..d3193f670d89 100644
--- a/tools/accounting/getdelays.c
+++ b/tools/accounting/getdelays.c
@@ -30,6 +30,8 @@
#include <linux/taskstats.h>
#include <linux/cgroupstats.h>
+#include "format_timespec.h"
+
/*
* Generic macros for dealing with netlink sockets. Might be duplicated
* elsewhere. It is recommended that commercial grade applications use
@@ -221,36 +223,6 @@ static int get_family_id(int sd)
#define average_ms(t, c) (t / 1000000ULL / (c ? c : 1))
#define delay_ms(t) (t / 1000000ULL)
-/*
- * Format __kernel_timespec to human readable string (YYYY-MM-DD HH:MM:SS)
- * Returns formatted string or "N/A" if timestamp is zero
- */
-static const char *format_timespec(struct __kernel_timespec *ts)
-{
- static char buffer[32];
- struct tm tm_info;
- __kernel_time_t time_sec;
-
- /* Check if timestamp is zero (not set) */
- if (ts->tv_sec == 0 && ts->tv_nsec == 0)
- return "N/A";
-
- /* Avoid Y2038 truncation on 32-bit platforms */
- if (sizeof(time_sec) < sizeof(ts->tv_sec) &&
- ts->tv_sec > (__u64)((1ULL << (sizeof(time_sec) * 8 - 1)) - 1))
- return "N/A";
-
- time_sec = ts->tv_sec;
-
- /* Use thread-safe localtime_r */
- if (localtime_r(&time_sec, &tm_info) == NULL)
- return "N/A";
-
- strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S", &tm_info);
-
- return buffer;
-}
-
/*
* Version compatibility note:
* Field availability depends on taskstats version (t->version),
diff --git a/tools/include/uapi/linux/time_types.h b/tools/include/uapi/linux/time_types.h
new file mode 100644
index 000000000000..1b9d576d7cc5
--- /dev/null
+++ b/tools/include/uapi/linux/time_types.h
@@ -0,0 +1,49 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+#ifndef _TOOLS_UAPI_LINUX_TIME_TYPES_H
+#define _TOOLS_UAPI_LINUX_TIME_TYPES_H
+
+#include <linux/types.h>
+#include <asm/posix_types.h>
+
+/*
+ * Copied from include/uapi/linux/time_types.h
+ *
+ * Uses the arch-dependent __kernel_long_t rather than a hardcoded
+ * 'long' so that x32 (ILP32 on x86_64), where the kernel defines
+ * __kernel_long_t as 'long long', gets the correct struct layout.
+ *
+ * __kernel_long_t is provided by <asm/posix_types.h> - included
+ * below via <linux/types.h> on most systems, or directly here.
+ */
+
+struct __kernel_timespec {
+ long long tv_sec;
+ long long tv_nsec;
+};
+
+struct __kernel_itimerspec {
+ struct __kernel_timespec it_interval;
+ struct __kernel_timespec it_value;
+};
+
+struct __kernel_old_timeval {
+ __kernel_long_t tv_sec;
+ __kernel_long_t tv_usec;
+};
+
+struct __kernel_old_timespec {
+ __kernel_long_t tv_sec;
+ long tv_nsec;
+};
+
+struct __kernel_old_itimerval {
+ struct __kernel_old_timeval it_interval;
+ struct __kernel_old_timeval it_value;
+};
+
+struct __kernel_sock_timeval {
+ __s64 tv_sec;
+ __s64 tv_usec;
+};
+
+#endif /* _TOOLS_UAPI_LINUX_TIME_TYPES_H */
--
2.27.0
^ permalink raw reply related
* [PATCH 1/3] delaytop: refactor repetitive delay fields into array with enum
From: wang.yaxin @ 2026-07-18 5:35 UTC (permalink / raw)
To: wang.yaxin
Cc: akpm, fan.yu9, yang.yang29, corbet, linux-kernel, linux-doc,
xu.xin16
In-Reply-To: <20260718133420538UvsJFurowueTqxAaZ1cRP@zte.com.cn>
From: Wang Yaxin <wang.yaxin@zte.com.cn>
Replace the 9 groups of (count, delay_total, delay_max, delay_max_ts)
named fields in struct task_info with a struct delay_metrics array
indexed by enum delay_type. This eliminates all unsafe pointer
arithmetic via offsetof() from compare_tasks(),
field_delay_max_and_ts(), and get_field_delay_values().
The struct field_desc now stores an enum delay_type index instead of
four separate unsigned long offset values.
Signed-off-by: Wang Yaxin <wang.yaxin@zte.com.cn>
---
tools/accounting/delaytop.c | 220 +++++++++++++++---------------------
1 file changed, 88 insertions(+), 132 deletions(-)
diff --git a/tools/accounting/delaytop.c b/tools/accounting/delaytop.c
index 1c40bb477320..f1d26ff98792 100644
--- a/tools/accounting/delaytop.c
+++ b/tools/accounting/delaytop.c
@@ -61,31 +61,51 @@
#define MAX_MSG_SIZE 1024
#define MAX_TASKS 1000
#define MAX_BUF_LEN 256
-#define SET_TASK_STAT(task_count, field) tasks[task_count].field = stats.field
#define BOOL_FPRINT(stream, fmt, ...) \
({ \
int ret = fprintf(stream, fmt, ##__VA_ARGS__); \
ret >= 0; \
})
-#define TASK_AVG(task, field) average_ms((task).field##_delay_total, (task).field##_count)
+#define TASK_AVG(task, type) \
+ average_ms((task).delays[DELAY_##type].delay_total, \
+ (task).delays[DELAY_##type].count)
#define PSI_LINE_FORMAT "%-12s %6.1f%%/%6.1f%%/%6.1f%%/%8llu(ms)\n"
#define DELAY_FMT_DEFAULT "%8.2f %8.2f %8.2f %8.2f\n"
#define DELAY_FMT_MEMVERBOSE "%8.2f %8.2f %8.2f %8.2f %8.2f %8.2f\n"
-#define SORT_FIELD(name, cmd, modes) \
- {#name, #cmd, \
- offsetof(struct task_info, name##_delay_total), \
- offsetof(struct task_info, name##_count), \
- offsetof(struct task_info, name##_delay_max), \
- offsetof(struct task_info, name##_delay_max_ts), \
- modes}
-#define SORT_FIELD_NO_MAX(name, cmd, modes) \
- {#name, #cmd, \
- offsetof(struct task_info, name##_delay_total), \
- offsetof(struct task_info, name##_count), \
- 0, \
- 0, \
- modes}
-#define END_FIELD {NULL, 0, 0, 0, 0, 0, 0}
+#define COPY_DELAY(task_idx, type, stats_prefix) \
+ do { \
+ tasks[task_idx].delays[DELAY_##type].count = \
+ stats.stats_prefix##_count; \
+ tasks[task_idx].delays[DELAY_##type].delay_total = \
+ stats.stats_prefix##_delay_total; \
+ tasks[task_idx].delays[DELAY_##type].delay_max = \
+ stats.stats_prefix##_delay_max; \
+ tasks[task_idx].delays[DELAY_##type].delay_max_ts = \
+ stats.stats_prefix##_delay_max_ts; \
+ } while (0)
+#define SORT_FIELD(name, cmd, type, modes, has_max) \
+ {#name, #cmd, type, modes, has_max}
+#define END_FIELD {NULL, 0, 0, 0, false}
+
+enum delay_type {
+ DELAY_CPU,
+ DELAY_BLKIO,
+ DELAY_SWAPIN,
+ DELAY_FREEPAGES,
+ DELAY_THRASHING,
+ DELAY_COMPACT,
+ DELAY_WPCOPY,
+ DELAY_IRQ,
+ DELAY_MEM,
+ NUM_DELAY_TYPES
+};
+
+struct delay_metrics {
+ unsigned long long count;
+ unsigned long long delay_total;
+ unsigned long long delay_max;
+ struct __kernel_timespec delay_max_ts;
+};
/* Display mode types */
#define MODE_TYPE_ALL (0xFFFFFFFF)
@@ -116,40 +136,7 @@ struct task_info {
int pid;
int tgid;
char command[TASK_COMM_LEN];
- unsigned long long cpu_count;
- unsigned long long cpu_delay_total;
- unsigned long long cpu_delay_max;
- struct __kernel_timespec cpu_delay_max_ts;
- unsigned long long blkio_count;
- unsigned long long blkio_delay_total;
- unsigned long long blkio_delay_max;
- struct __kernel_timespec blkio_delay_max_ts;
- unsigned long long swapin_count;
- unsigned long long swapin_delay_total;
- unsigned long long swapin_delay_max;
- struct __kernel_timespec swapin_delay_max_ts;
- unsigned long long freepages_count;
- unsigned long long freepages_delay_total;
- unsigned long long freepages_delay_max;
- struct __kernel_timespec freepages_delay_max_ts;
- unsigned long long thrashing_count;
- unsigned long long thrashing_delay_total;
- unsigned long long thrashing_delay_max;
- struct __kernel_timespec thrashing_delay_max_ts;
- unsigned long long compact_count;
- unsigned long long compact_delay_total;
- unsigned long long compact_delay_max;
- struct __kernel_timespec compact_delay_max_ts;
- unsigned long long wpcopy_count;
- unsigned long long wpcopy_delay_total;
- unsigned long long wpcopy_delay_max;
- struct __kernel_timespec wpcopy_delay_max_ts;
- unsigned long long irq_count;
- unsigned long long irq_delay_total;
- unsigned long long irq_delay_max;
- struct __kernel_timespec irq_delay_max_ts;
- unsigned long long mem_count;
- unsigned long long mem_delay_total;
+ struct delay_metrics delays[NUM_DELAY_TYPES];
};
/* Container statistics structure */
@@ -165,11 +152,9 @@ struct container_stats {
struct field_desc {
const char *name; /* Field name for cmdline argument */
const char *cmd_char; /* Interactive command */
- unsigned long total_offset; /* Offset of total delay in task_info */
- unsigned long count_offset; /* Offset of count in task_info */
- unsigned long max_offset; /* Offset of max delay in task_info */
- unsigned long max_ts_offset; /* Offset of max delay timestamp in task_info */
+ enum delay_type type; /* Index into task_info.delays[] */
size_t supported_modes; /* Supported display modes */
+ bool has_max; /* Whether this field has max/ts */
};
/* Program settings structure */
@@ -193,15 +178,15 @@ static int task_count;
static int running = 1;
static struct container_stats container_stats;
static const struct field_desc sort_fields[] = {
- SORT_FIELD(cpu, c, MODE_DEFAULT | MODE_TYPE),
- SORT_FIELD(blkio, i, MODE_DEFAULT | MODE_TYPE),
- SORT_FIELD(irq, q, MODE_DEFAULT | MODE_TYPE),
- SORT_FIELD_NO_MAX(mem, m, MODE_DEFAULT | MODE_MEMVERBOSE),
- SORT_FIELD(swapin, s, MODE_MEMVERBOSE | MODE_TYPE),
- SORT_FIELD(freepages, r, MODE_MEMVERBOSE | MODE_TYPE),
- SORT_FIELD(thrashing, t, MODE_MEMVERBOSE | MODE_TYPE),
- SORT_FIELD(compact, p, MODE_MEMVERBOSE | MODE_TYPE),
- SORT_FIELD(wpcopy, w, MODE_MEMVERBOSE | MODE_TYPE),
+ SORT_FIELD(cpu, c, DELAY_CPU, MODE_DEFAULT | MODE_TYPE, true),
+ SORT_FIELD(blkio, i, DELAY_BLKIO, MODE_DEFAULT | MODE_TYPE, true),
+ SORT_FIELD(irq, q, DELAY_IRQ, MODE_DEFAULT | MODE_TYPE, true),
+ SORT_FIELD(mem, m, DELAY_MEM, MODE_DEFAULT | MODE_MEMVERBOSE, false),
+ SORT_FIELD(swapin, s, DELAY_SWAPIN, MODE_MEMVERBOSE | MODE_TYPE, true),
+ SORT_FIELD(freepages, r, DELAY_FREEPAGES, MODE_MEMVERBOSE | MODE_TYPE, true),
+ SORT_FIELD(thrashing, t, DELAY_THRASHING, MODE_MEMVERBOSE | MODE_TYPE, true),
+ SORT_FIELD(compact, p, DELAY_COMPACT, MODE_MEMVERBOSE | MODE_TYPE, true),
+ SORT_FIELD(wpcopy, w, DELAY_WPCOPY, MODE_MEMVERBOSE | MODE_TYPE, true),
END_FIELD
};
static int sort_selected;
@@ -433,20 +418,20 @@ static void parse_args(int argc, char **argv)
/* Calculate average delay in milliseconds for overall memory */
static void set_mem_delay_total(struct task_info *t)
{
- t->mem_delay_total = t->swapin_delay_total +
- t->freepages_delay_total +
- t->thrashing_delay_total +
- t->compact_delay_total +
- t->wpcopy_delay_total;
+ t->delays[DELAY_MEM].delay_total = t->delays[DELAY_SWAPIN].delay_total +
+ t->delays[DELAY_FREEPAGES].delay_total +
+ t->delays[DELAY_THRASHING].delay_total +
+ t->delays[DELAY_COMPACT].delay_total +
+ t->delays[DELAY_WPCOPY].delay_total;
}
static void set_mem_count(struct task_info *t)
{
- t->mem_count = t->swapin_count +
- t->freepages_count +
- t->thrashing_count +
- t->compact_count +
- t->wpcopy_count;
+ t->delays[DELAY_MEM].count = t->delays[DELAY_SWAPIN].count +
+ t->delays[DELAY_FREEPAGES].count +
+ t->delays[DELAY_THRASHING].count +
+ t->delays[DELAY_COMPACT].count +
+ t->delays[DELAY_WPCOPY].count;
}
/* Create a raw netlink socket and bind */
@@ -758,38 +743,14 @@ static void fetch_and_fill_task_info(int pid, const char *comm)
strncpy(tasks[task_count].command, comm,
TASK_COMM_LEN - 1);
tasks[task_count].command[TASK_COMM_LEN - 1] = '\0';
- SET_TASK_STAT(task_count, cpu_count);
- SET_TASK_STAT(task_count, cpu_delay_total);
- SET_TASK_STAT(task_count, cpu_delay_max);
- SET_TASK_STAT(task_count, cpu_delay_max_ts);
- SET_TASK_STAT(task_count, blkio_count);
- SET_TASK_STAT(task_count, blkio_delay_total);
- SET_TASK_STAT(task_count, blkio_delay_max);
- SET_TASK_STAT(task_count, blkio_delay_max_ts);
- SET_TASK_STAT(task_count, swapin_count);
- SET_TASK_STAT(task_count, swapin_delay_total);
- SET_TASK_STAT(task_count, swapin_delay_max);
- SET_TASK_STAT(task_count, swapin_delay_max_ts);
- SET_TASK_STAT(task_count, freepages_count);
- SET_TASK_STAT(task_count, freepages_delay_total);
- SET_TASK_STAT(task_count, freepages_delay_max);
- SET_TASK_STAT(task_count, freepages_delay_max_ts);
- SET_TASK_STAT(task_count, thrashing_count);
- SET_TASK_STAT(task_count, thrashing_delay_total);
- SET_TASK_STAT(task_count, thrashing_delay_max);
- SET_TASK_STAT(task_count, thrashing_delay_max_ts);
- SET_TASK_STAT(task_count, compact_count);
- SET_TASK_STAT(task_count, compact_delay_total);
- SET_TASK_STAT(task_count, compact_delay_max);
- SET_TASK_STAT(task_count, compact_delay_max_ts);
- SET_TASK_STAT(task_count, wpcopy_count);
- SET_TASK_STAT(task_count, wpcopy_delay_total);
- SET_TASK_STAT(task_count, wpcopy_delay_max);
- SET_TASK_STAT(task_count, wpcopy_delay_max_ts);
- SET_TASK_STAT(task_count, irq_count);
- SET_TASK_STAT(task_count, irq_delay_total);
- SET_TASK_STAT(task_count, irq_delay_max);
- SET_TASK_STAT(task_count, irq_delay_max_ts);
+ COPY_DELAY(task_count, CPU, cpu);
+ COPY_DELAY(task_count, BLKIO, blkio);
+ COPY_DELAY(task_count, SWAPIN, swapin);
+ COPY_DELAY(task_count, FREEPAGES, freepages);
+ COPY_DELAY(task_count, THRASHING, thrashing);
+ COPY_DELAY(task_count, COMPACT, compact);
+ COPY_DELAY(task_count, WPCOPY, wpcopy);
+ COPY_DELAY(task_count, IRQ, irq);
set_mem_count(&tasks[task_count]);
set_mem_delay_total(&tasks[task_count]);
task_count++;
@@ -912,10 +873,10 @@ static int compare_tasks(const void *a, const void *b)
return 0;
}
- total1 = *(unsigned long long *)((char *)t1 + cfg.sort_field->total_offset);
- total2 = *(unsigned long long *)((char *)t2 + cfg.sort_field->total_offset);
- count1 = *(unsigned long long *)((char *)t1 + cfg.sort_field->count_offset);
- count2 = *(unsigned long long *)((char *)t2 + cfg.sort_field->count_offset);
+ total1 = t1->delays[cfg.sort_field->type].delay_total;
+ total2 = t2->delays[cfg.sort_field->type].delay_total;
+ count1 = t1->delays[cfg.sort_field->type].count;
+ count2 = t2->delays[cfg.sort_field->type].count;
avg1 = average_ms(total1, count1);
avg2 = average_ms(total2, count2);
@@ -929,22 +890,17 @@ static int compare_tasks(const void *a, const void *b)
static void field_delay_max_and_ts(const struct task_info *task, const struct field_desc *field,
unsigned long long *max_ns, struct __kernel_timespec *max_ts)
{
- if (!field || !field->max_offset) {
+ if (!field || !field->has_max) {
*max_ns = 0;
if (max_ts)
memset(max_ts, 0, sizeof(*max_ts));
return;
}
- *max_ns = *(unsigned long long *)((char *)task + field->max_offset);
+ *max_ns = task->delays[field->type].delay_max;
- if (max_ts) {
- if (field->max_ts_offset)
- *max_ts = *(struct __kernel_timespec *)((char *)task +
- field->max_ts_offset);
- else
- memset(max_ts, 0, sizeof(*max_ts));
- }
+ if (max_ts)
+ *max_ts = task->delays[field->type].delay_max_ts;
}
/* Get delay values for a specific field */
@@ -954,15 +910,15 @@ static void get_field_delay_values(const struct task_info *task, const struct fi
{
unsigned long long total, count, max;
- if (!field || !field->max_offset) {
+ if (!field) {
*avg_ms = 0;
*max_ms = 0;
memset(max_ts, 0, sizeof(*max_ts));
return;
}
- total = *(unsigned long long *)((char *)task + field->total_offset);
- count = *(unsigned long long *)((char *)task + field->count_offset);
+ total = task->delays[field->type].delay_total;
+ count = task->delays[field->type].count;
*avg_ms = average_ms(total, count);
field_delay_max_and_ts(task, field, &max, max_ts);
@@ -1180,18 +1136,18 @@ static void display_results(int psi_ret)
avg_ms, max_ms, format_kernel_timespec(&max_ts));
} else if (cfg.display_mode == MODE_MEMVERBOSE) {
suc &= BOOL_FPRINT(out, DELAY_FMT_MEMVERBOSE,
- TASK_AVG(tasks[i], mem),
- TASK_AVG(tasks[i], swapin),
- TASK_AVG(tasks[i], freepages),
- TASK_AVG(tasks[i], thrashing),
- TASK_AVG(tasks[i], compact),
- TASK_AVG(tasks[i], wpcopy));
+ TASK_AVG(tasks[i], MEM),
+ TASK_AVG(tasks[i], SWAPIN),
+ TASK_AVG(tasks[i], FREEPAGES),
+ TASK_AVG(tasks[i], THRASHING),
+ TASK_AVG(tasks[i], COMPACT),
+ TASK_AVG(tasks[i], WPCOPY));
} else {
suc &= BOOL_FPRINT(out, DELAY_FMT_DEFAULT,
- TASK_AVG(tasks[i], cpu),
- TASK_AVG(tasks[i], blkio),
- TASK_AVG(tasks[i], irq),
- TASK_AVG(tasks[i], mem));
+ TASK_AVG(tasks[i], CPU),
+ TASK_AVG(tasks[i], BLKIO),
+ TASK_AVG(tasks[i], IRQ),
+ TASK_AVG(tasks[i], MEM));
}
}
--
2.27.0
^ permalink raw reply related
* [PATCH v3 0/3] tools/accounting: refactor delay fields and share format_timespec()
From: wang.yaxin @ 2026-07-18 5:34 UTC (permalink / raw)
To: akpm, fan.yu9, yang.yang29
Cc: corbet, linux-kernel, linux-doc, xu.xin16, wang.yaxin
From: Wang Yaxin <wang.yaxin@zte.com.cn>
- Convert per-field delay members in struct task_info to an array indexed
by enum delay_type, eliminating offsetof() pointer arithmetic.
- Factor out a common format_timespec() implementation shared by getdelays
and delaytop, using strftime for cleaner timestamp formatting.
- Replace the complex sizeof/ULL/shift Y2038 guard with a direct narrowing
truncation check ((long long)time_sec != ts->tv_sec).
Change Log
==========
v2->v3:
update patch 2/3 and 3/3 according to the suggestion:
https://sashiko.dev/#/patchset/20260715124740929HC7tDDb2SK3kRxbuPruHd@zte.com.cn
1. tools/include/uapi/linux/time_types.h: use __kernel_long_t instead of
long to fix ABI mismatch on x32
2. tools/accounting: add nla_len < NLA_HDRLEN checks in netlink attribute
parsing loops to prevent infinite loop on malformed messages
v1->v2:
Only update patch 2/3 according to the suggestion:
https://sashiko.dev/#/patchset/20260711173112482SCQEM08VED2PT1pxUYOXk@zte.com.cn
1. tools/include/uapi/linux/time_types.h: expand stub to full set of
6 structs from kernel UAPI to avoid shadowing system header
2. tools/accounting/Makefile: add $(LDFLAGS), $(LDLIBS), $(CPPFLAGS)
to restore compatibility with standard build variables
Wang Yaxin (3):
delaytop: refactor repetitive delay fields into array with enum
tools/accounting: factor out shared format_timespec() implementation
tools/accounting: simplify 32-bit time_t overflow check in
format_timespec()
tools/accounting/Makefile | 12 +-
tools/accounting/delaytop.c | 265 ++++++++++----------------
tools/accounting/format_timespec.c | 37 ++++
tools/accounting/format_timespec.h | 9 +
tools/accounting/getdelays.c | 36 +---
tools/include/uapi/linux/time_types.h | 49 +++++
6 files changed, 208 insertions(+), 200 deletions(-)
create mode 100644 tools/accounting/format_timespec.c
create mode 100644 tools/accounting/format_timespec.h
create mode 100644 tools/include/uapi/linux/time_types.h
--
2.27.0
^ permalink raw reply
* Re: [PATCH 0/2] usbcore: Add quirk for 255-byte initial config read
From: Nikhil Solanke @ 2026-07-18 5:30 UTC (permalink / raw)
To: linux-usb
Cc: gregkh, linux-kernel, stern, michal.pecio, corbet, skhan,
linux-doc, stable
In-Reply-To: <20260717195336.98500-1-nikhilsolanke5@gmail.com>
Sorry, forgot to mark this as v3. This should be [PATCH v3 0/2].
Changes from v2:
- put strings in a single line
- copy bytes from desc to bigbuffer instead of pointer aliasing.
- change tabs to spaces in documentation
- reworded some comments
- drop USB_CONFIG_WINDOWS_REQ_SIZE macro
- revert USB_DELAY_INIT to original behavior. no delay before 1st request.
- split announce_device() so we can report device ids early before
vendor strings
Thanks,
Nikhil Solanke
^ permalink raw reply
* Re: [PATCH v2 0/2] mm/zswap: Fixes and improves the zswap global shrinker
From: Andrew Morton @ 2026-07-18 4:40 UTC (permalink / raw)
To: Yosry Ahmed
Cc: Hao Jia, tj, hannes, shakeel.butt, mhocko, mkoutny, nphamcs,
chengming.zhou, muchun.song, roman.gushchin, linux-mm,
linux-kernel, linux-doc, Hao Jia
In-Reply-To: <CAO9r8zOrXqsEaCEJUUAxbAhhN_OQb8ejogeUa6k8hX_JV8hxxw@mail.gmail.com>
On Fri, 17 Jul 2026 18:28:04 -0700 Yosry Ahmed <yosry@kernel.org> wrote:
> >
> > The [1/2] changelog lacks a description of how the flaw impacts users.
> > Please describe this fully and maintain that info within the
> > changelogging. This info helps -stable maintainers and others
> > understand why we're proposing a backport and helps myself and others
> > with timing decisions.
>
> The first line in the changelog should be sufficient imo: "Zswap
> writeback on hitting the pool limit is broken when memory cgroup is
> disabled"
"broken"? Perhaps this means "fails to occur".
But what is the userspace-visible impact? IOW, why are we proposing a
backport?
^ permalink raw reply
* Re: [PATCH v9 00/10] tracing: wprobe: x86: Add wprobe for watchpoint
From: Borislav Petkov @ 2026-07-18 2:51 UTC (permalink / raw)
To: Masami Hiramatsu
Cc: Steven Rostedt, Peter Zijlstra, Ingo Molnar, x86, Jinchao Wang,
Mathieu Desnoyers, Thomas Gleixner, Dave Hansen, H . Peter Anvin,
Alexander Shishkin, Ian Rogers, linux-kernel, linux-trace-kernel,
linux-doc, linux-perf-users
In-Reply-To: <20260718084741.c2ae6e41f2a1510ffa06d12e@kernel.org>
On July 17, 2026 11:47:41 PM UTC, Masami Hiramatsu <mhiramat@kernel.org> wrote:
>On Fri, 17 Jul 2026 09:28:57 -0700
>Borislav Petkov <bp@alien8.de> wrote:
>
>> Hi,
>>
>> On Fri, Jul 17, 2026 at 11:19:30PM +0900, Masami Hiramatsu (Google) wrote:
>> > Here is the 9th version of the series for adding new wprobe (watch probe)
>>
>> any chance you won't blast out your patchset every day?
>
>Ah, sorry about that. I'll rate limit myself this weekend on this series.
Once a week is perfectly fine.
--
Small device. Typos and formatting crap
^ permalink raw reply
* [PATCH v3] Docs/admin-guide/cgroup-v2: document io.latency rotational vs non-rotational behavior
From: Tao Cui @ 2026-07-18 2:07 UTC (permalink / raw)
To: Tejun Heo
Cc: Michal Koutný, Josef Bacik, Jens Axboe, cgroups, linux-doc,
linux-kernel, cui.tao, cuitao
From: Tao Cui <cuitao@kylinos.cn>
io.latency is documented only in terms of average latency and the avg_lat
stat, which matches rotational devices. On non-rotational devices a group
misses its target when 10% or more of the IOs in the window individually
exceed it, and io.stat reports missed/total rather than avg_lat/win.
Describe both cases: how a miss is detected, note that the avg_lat tuning
guidance is rotational-only, and update the io.stat field list (mark
avg_lat/win as rotational-only, document missed/total).
Acked-by: Michal Koutný <mkoutny@suse.com>
Signed-off-by: Tao Cui <cuitao@kylinos.cn>
---
Changes in v3:
- Move the missing-target note to the missed field and avoid naming the
exact threshold there (Michal Koutný).
- Add Acked-by from Michal Koutný.
---
Documentation/admin-guide/cgroup-v2.rst | 50 ++++++++++++++++++-------
1 file changed, 36 insertions(+), 14 deletions(-)
diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst
index 0df15a672cf3..d60c2361feb5 100644
--- a/Documentation/admin-guide/cgroup-v2.rst
+++ b/Documentation/admin-guide/cgroup-v2.rst
@@ -2260,9 +2260,12 @@ IO Latency
~~~~~~~~~~
This is a cgroup v2 controller for IO workload protection. You provide a group
-with a latency target, and if the average latency exceeds that target the
-controller will throttle any peers that have a lower latency target than the
-protected workload.
+with a latency target, and if the group misses its target the controller will
+throttle any peers that have a lower latency target than the protected
+workload. How a miss is detected depends on the device: on rotational devices
+the average latency over the window must exceed the target, while on
+non-rotational devices a miss is counted when 10% or more of the IOs in the
+window individually exceed the target.
The limits are only applied at the peer level in the hierarchy. This means that
in the diagram below, only groups A, B, and C will influence each other, and
@@ -2279,10 +2282,12 @@ So the ideal way to configure this is to set io.latency in groups A, B, and C.
Generally you do not want to set a value lower than the latency your device
supports. Experiment to find the value that works best for your workload.
Start at higher than the expected latency for your device and, with
-blkcg_debug_stats enabled, watch the avg_lat value in io.stat for your
-workload group to get an idea of the latency you see during normal operation.
-Use the avg_lat value as a basis for your real setting, setting at 10-15%
-higher than the value in io.stat.
+blkcg_debug_stats enabled, observe io.stat for your workload group to get an
+idea of the latency you see during normal operation. On rotational devices,
+use the avg_lat value as a basis for your real setting, setting it 10-15%
+higher. On non-rotational devices io.stat reports no average latency; set
+the target based on your device and use the missed/total fields to verify it
+is being met.
How IO Latency Throttling Works
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -2324,19 +2329,36 @@ IO Latency Interface Files
the blkcg_debug_stats module parameter is enabled (it is disabled by
default).
+ The reported latency fields depend on the device. Rotational devices
+ report avg_lat and win; non-rotational devices report missed and total
+ instead. missed and total are live counters for the current window and
+ may change between reads.
+
depth
This is the current queue depth for the group.
avg_lat
- This is an exponential moving average with a decay rate of 1/exp
- bound by the sampling interval. The decay rate interval can be
- calculated by multiplying the win value in io.stat by the
- corresponding number of samples based on the win value.
+ (Rotational devices only.) This is an exponential moving
+ average with a decay rate of 1/exp bound by the sampling
+ interval. The decay rate interval can be calculated by
+ multiplying the win value in io.stat by the corresponding number
+ of samples based on the win value.
win
- The sampling window size in milliseconds. This is the minimum
- duration of time between evaluation events. Windows only elapse
- with IO activity. Idle periods extend the most recent window.
+ (Rotational devices only.) The sampling window size in
+ milliseconds. This is the minimum duration of time between
+ evaluation events. Windows only elapse with IO activity. Idle
+ periods extend the most recent window.
+
+ missed
+ (Non-rotational devices only.) The number of IOs in the
+ current window whose latency exceeded the target. A group is
+ considered to be missing its target once missed reaches a
+ certain ratio of total.
+
+ total
+ (Non-rotational devices only.) The total number of IOs
+ accounted in the current window.
IO Priority
~~~~~~~~~~~
--
2.43.0
^ permalink raw reply related
* [PATCH v7 11/11] x86/virt/tdx: Optimize tdx_pamt_get/put()
From: Rick Edgecombe @ 2026-07-18 1:45 UTC (permalink / raw)
To: bp, dave.hansen, hpa, kas, kvm, linux-coco, linux-doc,
linux-kernel, mingo, nik.borisov, pbonzini, seanjc, tglx,
vannapurve, x86, chao.gao, yan.y.zhao, kai.huang, tony.lindgren,
binbin.wu
Cc: rick.p.edgecombe
In-Reply-To: <20260718014500.2231262-1-rick.p.edgecombe@intel.com>
From: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
The Dynamic PAMT get/put helpers use a global spinlock to serialize all
refcount updates and SEAMCALL invocations. This gives correct behavior for
concurrent callers, but leads to contention. It is especially bad from the
KVM side, which is designed to allow faulting in EPT under a shared lock.
With the global spinlock, not only is the lock an exclusive one, but it is
for all TDs instead of just a single one.
But taking the global lock each time is actually unnecessary. Only the 0->1
and 1->0 refcount transitions actually need the lock (to pair with
SEAMCALLs that actually add and remove with the Dynamic PAMT pages). The
common case of incrementing or decrementing a non-zero refcount can be
done locklessly.
So create a fast and slow path. Check the refcount outside the lock and
only take it for the slow path (0->1 and 1->0 transitions).
On the put side make the refcount adjustment and lock taking atomic so if
a 'get' happens between them, it doesn't cause the Dynamic PAMT to be
freed incorrectly. On the get side there is no technique for doing the
refcount adjustment and lock atomically, so check the refcount again
inside the lock.
AI was used under supervision to collect/apply feedback, review code and
workshop logs. It assisted in identifying/evaluating the stale
conditionals for the races resolved from the atomic_dec_and_lock() change.
Separate from atomic_dec_and_lock() fallout, it suggested to change
atomic_inc() to atomic_set(pamt_refcount, 1) in the put error path for the
sake of being more precise, which Kiryl had also suggested in the past.
The model also suggested updated comments following the
atomic_dec_and_lock() change based on some directed prompting. The
comments were subsequently edited or further prompted for fine tuning.
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Co-developed-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Reviewed-by: Chao Gao <chao.gao@intel.com>
Reviewed-by: Tony Lindgren <tony.lindgren@linux.intel.com>
---
v7:
- Drop Assisted-by tag and cover AI use in log (Dave)
- Move to end of the series
- Use atomic_inc_not_zero() in this patch inside the spin_lock(), as
suggested on non-optimized patch by. (Dave)
v6:
- Fix "tdx_pamt_add()" typo to "tdx_pamt_get()" in lost-race comment
- Fix error path bug: set ret = -EIO and use WARN_ON_ONCE() instead of
pr_err() for unexpected PAMT.ADD failures (Sean)
- Use "set the refcount 0->1" wording to match atomic_set() usage
- Wrap comments to 80 columns
- Switch to atomic_dec_and_lock() and remove handling of races that are
no longer needed as a result. Adjust comments as appropriate. (Dave)
- Adjustments from dropping error helper patches
---
arch/x86/virt/vmx/tdx/tdx.c | 44 ++++++++++++++++++++++++-------------
1 file changed, 29 insertions(+), 15 deletions(-)
diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c
index f397ea17248e3..cb0f0ef0dd5f1 100644
--- a/arch/x86/virt/vmx/tdx/tdx.c
+++ b/arch/x86/virt/vmx/tdx/tdx.c
@@ -2166,28 +2166,41 @@ int tdx_pamt_get(kvm_pfn_t pfn, struct tdx_pamt_cache *cache)
if (!tdx_supports_dynamic_pamt(&tdx_sysinfo))
return 0;
- ret = alloc_pamt_array(pamt_pages, cache);
- if (ret)
- return ret;
-
pamt_refcount = tdx_find_pamt_refcount(pfn);
- spin_lock(&pamt_lock);
-
/*
* If the pamt page is already added (i.e. refcount >= 1),
* then just increment the refcount.
*/
+ if (atomic_inc_not_zero(pamt_refcount))
+ return 0;
+
+ ret = alloc_pamt_array(pamt_pages, cache);
+ if (ret)
+ return ret;
+
+ spin_lock(&pamt_lock);
+
+ /*
+ * Unlike tdx_pamt_put() which uses atomic_dec_and_lock() to
+ * atomically handle the 1->0 transition, the get side has no
+ * equivalent combined primitive for 0->1. Recheck under the
+ * lock since another get may have already done the 0->1
+ * transition after both saw atomic_inc_not_zero() fail.
+ */
if (atomic_inc_not_zero(pamt_refcount))
goto out_free;
- /* Try to add the pamt page and take the refcount 0->1. */
tdx_status = tdh_phymem_pamt_add(pfn, pamt_pages);
if (WARN_ON_ONCE(tdx_status != TDX_SUCCESS)) {
ret = -EIO;
goto out_free;
}
+ /*
+ * The refcount is zero, and this locked path is the
+ * only way to increase it from 0->1.
+ */
atomic_set(pamt_refcount, 1);
spin_unlock(&pamt_lock);
return 0;
@@ -2212,17 +2225,13 @@ void tdx_pamt_put(kvm_pfn_t pfn)
pamt_refcount = tdx_find_pamt_refcount(pfn);
- spin_lock(&pamt_lock);
/*
* If there is more than 1 reference on the pamt page, don't
* remove it yet. Just decrement the refcount.
*/
- if (atomic_read(pamt_refcount) > 1) {
- atomic_dec(pamt_refcount);
- goto out_unlock;
- }
+ if (!atomic_dec_and_lock(pamt_refcount, &pamt_lock))
+ return;
- /* Try to remove the pamt page and take the refcount 1->0. */
tdx_status = tdh_phymem_pamt_remove(pfn, pamt_pages);
/*
@@ -2232,10 +2241,15 @@ void tdx_pamt_put(kvm_pfn_t pfn)
* failure indicates a kernel bug, memory is being leaked, and
* the dangling PAMT entry may cause future operations to fail.
*/
- if (WARN_ON_ONCE(tdx_status != TDX_SUCCESS))
+ if (WARN_ON_ONCE(tdx_status != TDX_SUCCESS)) {
+ /*
+ * atomic_dec_and_lock() already decremented it to 0,
+ * but the PAMT entry still exists since REMOVE failed.
+ */
+ atomic_set(pamt_refcount, 1);
goto out_unlock;
+ }
- atomic_set(pamt_refcount, 0);
spin_unlock(&pamt_lock);
free_pamt_array(pamt_pages);
return;
--
2.54.0
^ permalink raw reply related
* [PATCH v7 10/11] Documentation/x86: Add documentation for TDX's Dynamic PAMT
From: Rick Edgecombe @ 2026-07-18 1:44 UTC (permalink / raw)
To: bp, dave.hansen, hpa, kas, kvm, linux-coco, linux-doc,
linux-kernel, mingo, nik.borisov, pbonzini, seanjc, tglx,
vannapurve, x86, chao.gao, yan.y.zhao, kai.huang, tony.lindgren,
binbin.wu
Cc: rick.p.edgecombe, Binbin Wu
In-Reply-To: <20260718014500.2231262-1-rick.p.edgecombe@intel.com>
From: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
Expand TDX documentation to include information on the Dynamic PAMT
feature.
The new section explains PAMT support in the TDX module and how Dynamic
PAMT affects the kernel memory use.
AI was used under supervision to review the docs.
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Co-developed-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Reviewed-by: Binbin Wu <binbin.wu@linux.intel.com>
Reviewed-by: Tony Lindgren <tony.lindgren@linux.intel.com>
---
v7:
- Spell out PAMT acronym (Binbin)
- Drop Assisted-by tag and cover AI use in log (Dave)
- Add info about kernel parameter
v6:
- Add missing word (Binbin)
- Use "::" instead of ":"
- Make format of dmesg example accurate
---
.../admin-guide/kernel-parameters.txt | 3 ++
Documentation/arch/x86/tdx.rst | 28 +++++++++++++++++++
2 files changed, 31 insertions(+)
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index be4928489b02f..7c8627ce07eb4 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -1328,6 +1328,9 @@ Kernel parameters
Valid parameters: "on", "off"
Default: "off"
+ For details see:
+ Documentation/arch/x86/tdx.rst
+
reg_file_data_sampling=
[X86] Controls mitigation for Register File Data
Sampling (RFDS) vulnerability. RFDS is a CPU
diff --git a/Documentation/arch/x86/tdx.rst b/Documentation/arch/x86/tdx.rst
index 3303499ad4c6f..a1c2309230580 100644
--- a/Documentation/arch/x86/tdx.rst
+++ b/Documentation/arch/x86/tdx.rst
@@ -200,6 +200,34 @@ reflects the TCB of the currently running TDX module and therefore
changes after an update. By contrast, TEE_TCB_SVN reflects the TCB at TD
launch time and is not affected.
+Dynamic PAMT
+------------
+
+Physical Address Metadata Table (PAMT) is memory that the TDX module needs
+to keep data about each page (think like struct page). It needs to be handed
+to the TDX module for its exclusive use. For normal PAMT, this is installed
+when the TDX module is first loaded and comes to about 0.4% of system memory.
+
+Dynamic PAMT is a TDX module feature that allows VMM to allocate part of the
+PAMT as needed (the parts for tracking 4KB size pages). The other page sizes
+(1GB and 2MB) are still allocated statically at the time of TDX module
+initialization. This reduces the amount of memory that TDX uses while TDs are
+not in use.
+
+When Dynamic PAMT is in use, dmesg shows it like::
+
+ [..] virt/tdx: Enable Dynamic PAMT
+ [..] virt/tdx: 10092 KB allocated for PAMT
+ [..] virt/tdx: TDX-Module initialized
+
+Dynamic PAMT is only enabled when supported and the ``tdx_dpamt=`` kernel
+parameter is set to "on". The feature is off by default because TDX module
+internal details prevent Dynamic PAMT from working on all keyid partitioning
+configurations. When the TDX module is fixed to include these constraints in
+its enumeration of Dynamic PAMT support, kernel support can be changed to
+default on. For more information, consult the Intel TDX documentation about
+Dynamic PAMT.
+
TDX Interaction to Other Kernel Components
------------------------------------------
--
2.54.0
^ permalink raw reply related
* [PATCH v7 08/11] KVM: TDX: Get/put PAMT pages when (un)mapping private memory
From: Rick Edgecombe @ 2026-07-18 1:44 UTC (permalink / raw)
To: bp, dave.hansen, hpa, kas, kvm, linux-coco, linux-doc,
linux-kernel, mingo, nik.borisov, pbonzini, seanjc, tglx,
vannapurve, x86, chao.gao, yan.y.zhao, kai.huang, tony.lindgren,
binbin.wu
Cc: rick.p.edgecombe, Binbin Wu
In-Reply-To: <20260718014500.2231262-1-rick.p.edgecombe@intel.com>
From: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
Add Dynamic PAMT support to KVM's S-EPT MMU by "getting" a PAMT page when
adding guest memory (PAGE.ADD or PAGE.AUG), and "putting" the page when
removing guest memory (PAGE.REMOVE).
To access the per-vCPU PAMT caches without plumbing @vcpu throughout the
TDP MMU, begrudgingly use kvm_get_running_vcpu() to get the vCPU, and bug
the VM if KVM attempts to set an S-EPT leaf without an active vCPU. KVM
only supports creating _new_ mappings in page (pre)fault paths, all of
which require an active vCPU.
The PAMT memory holds metadata for TDX protected memory. With Dynamic
PAMT, PAMT_4K is allocated on demand. The kernel supplies the TDX module
with a few pages that cover 2MB of host physical memory.
Releases are balanced via tdx_pamt_put(): every control-page free goes
through tdx_free_control_page(), and guest data pages are put directly on
the successful tdh_mem_page_remove() path and in the
tdx_mem_page_add/aug() error path.
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Co-developed-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
[rick: enhance log, reviewing, rebase, with help from AI tooling]
Co-developed-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Reviewed-by: Binbin Wu <binbin.wu@linux.intel.com>
Reviewed-by: Tony Lindgren <tony.lindgren@linux.intel.com>
---
v7:
- Don't do to_tdx() before NULL check for readability (Binbin, Sean)
- Fixup tags (Sean)
- Export tdx_supports_dynamic_pamt() since it's used in KVM here and no
longer an static inline.
v6:
- Don't have topup op take a min param (Yan, Sean)
- Make log match style of the rest of the series
- Adjustments from dropping error helper patches
---
arch/x86/include/asm/kvm-x86-ops.h | 1 +
arch/x86/include/asm/kvm_host.h | 2 +
arch/x86/kvm/mmu/mmu.c | 4 ++
arch/x86/kvm/vmx/tdx.c | 63 ++++++++++++++++++++++++++----
arch/x86/kvm/vmx/tdx.h | 2 +
arch/x86/virt/vmx/tdx/tdx.c | 1 +
6 files changed, 65 insertions(+), 8 deletions(-)
diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h
index 83dc5086138b3..588563dfe88d5 100644
--- a/arch/x86/include/asm/kvm-x86-ops.h
+++ b/arch/x86/include/asm/kvm-x86-ops.h
@@ -98,6 +98,7 @@ KVM_X86_OP_OPTIONAL_RET0(tdp_has_smep)
KVM_X86_OP(load_mmu_pgd)
KVM_X86_OP_OPTIONAL_RET0(set_external_spte)
KVM_X86_OP_OPTIONAL(free_external_spt)
+KVM_X86_OP_OPTIONAL_RET0(topup_external_cache)
KVM_X86_OP(has_wbinvd_exit)
KVM_X86_OP(get_l2_tsc_offset)
KVM_X86_OP(get_l2_tsc_multiplier)
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 5f6c1ce9673b7..1c706e2d773b0 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -1922,6 +1922,8 @@ struct kvm_x86_ops {
/* Update external page tables for page table about to be freed. */
void (*free_external_spt)(struct kvm *kvm, struct kvm_mmu_page *sp);
+ int (*topup_external_cache)(struct kvm_vcpu *vcpu, int min_nr_spts);
+
bool (*has_wbinvd_exit)(void);
diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index 234d0a95abf53..6dab99654f170 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -614,6 +614,10 @@ static int mmu_topup_memory_caches(struct kvm_vcpu *vcpu, bool maybe_indirect)
PT64_ROOT_MAX_LEVEL);
if (r)
return r;
+
+ r = kvm_x86_call(topup_external_cache)(vcpu, PT64_ROOT_MAX_LEVEL);
+ if (r)
+ return r;
}
r = kvm_mmu_topup_memory_cache(&vcpu->arch.mmu_shadow_page_cache,
PT64_ROOT_MAX_LEVEL);
diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
index f6c01eab8113b..c3b1d1f056cea 100644
--- a/arch/x86/kvm/vmx/tdx.c
+++ b/arch/x86/kvm/vmx/tdx.c
@@ -681,6 +681,8 @@ int tdx_vcpu_create(struct kvm_vcpu *vcpu)
if (!irqchip_split(vcpu->kvm))
return -EINVAL;
+ tdx_init_pamt_cache(&tdx->pamt_cache);
+
fpstate_set_confidential(&vcpu->arch.guest_fpu);
vcpu->arch.apic->guest_apic_protected = true;
INIT_LIST_HEAD(&tdx->vt.pi_wakeup_list);
@@ -866,6 +868,8 @@ void tdx_vcpu_free(struct kvm_vcpu *vcpu)
struct vcpu_tdx *tdx = to_tdx(vcpu);
int i;
+ tdx_free_pamt_cache(&tdx->pamt_cache);
+
if (vcpu->cpu != -1) {
KVM_BUG_ON(tdx->state == VCPU_TD_STATE_INITIALIZED, vcpu->kvm);
tdx_flush_vp_on_cpu(vcpu);
@@ -1621,6 +1625,16 @@ void tdx_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa, int pgd_level)
td_vmcs_write64(to_tdx(vcpu), SHARED_EPT_POINTER, root_hpa);
}
+static int tdx_topup_external_pamt_cache(struct kvm_vcpu *vcpu, int min_nr_spts)
+{
+ /*
+ * Don't cover the root SPT, but cover a possible 4KB private
+ * page in addition to the SPTs. So -1 to exclude the root
+ * SPT, and +1 for the guest page cancel out.
+ */
+ return tdx_topup_pamt_cache(&to_tdx(vcpu)->pamt_cache, min_nr_spts);
+}
+
static int tdx_mem_page_add(struct kvm *kvm, gfn_t gfn, enum pg_level level,
kvm_pfn_t pfn)
{
@@ -1679,16 +1693,28 @@ static struct page *tdx_spte_to_sept_pt(struct kvm *kvm, gfn_t gfn,
static int tdx_sept_map_nonleaf_spte(struct kvm *kvm, gfn_t gfn,
enum pg_level level, u64 new_spte)
{
+ struct kvm_vcpu *vcpu = kvm_get_running_vcpu();
gpa_t gpa = gfn_to_gpa(gfn);
u64 err, entry, level_state;
struct page *sept_pt;
+ int ret;
+
+ if (KVM_BUG_ON(!vcpu, kvm))
+ return -EIO;
sept_pt = tdx_spte_to_sept_pt(kvm, gfn, new_spte, level);
if (!sept_pt)
return -EIO;
+ ret = tdx_pamt_get(page_to_pfn(sept_pt), &to_tdx(vcpu)->pamt_cache);
+ if (ret)
+ return ret;
+
err = tdh_mem_sept_add(&to_kvm_tdx(kvm)->td, gpa, level, sept_pt,
&entry, &level_state);
+ if (err)
+ tdx_pamt_put(page_to_pfn(sept_pt));
+
if (unlikely(tdx_operand_busy(err)))
return -EBUSY;
@@ -1701,8 +1727,13 @@ static int tdx_sept_map_nonleaf_spte(struct kvm *kvm, gfn_t gfn,
static int tdx_sept_map_leaf_spte(struct kvm *kvm, gfn_t gfn, enum pg_level level,
u64 new_spte)
{
+ struct kvm_vcpu *vcpu = kvm_get_running_vcpu();
struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm);
kvm_pfn_t pfn = spte_to_pfn(new_spte);
+ int ret;
+
+ if (KVM_BUG_ON(!vcpu, kvm))
+ return -EIO;
/* TODO: handle large pages. */
if (KVM_BUG_ON(level != PG_LEVEL_4K, kvm))
@@ -1710,6 +1741,10 @@ static int tdx_sept_map_leaf_spte(struct kvm *kvm, gfn_t gfn, enum pg_level leve
WARN_ON_ONCE((new_spte & VMX_EPT_RWX_MASK) != VMX_EPT_RWX_MASK);
+ ret = tdx_pamt_get(pfn, &to_tdx(vcpu)->pamt_cache);
+ if (ret)
+ return ret;
+
/*
* Ensure pre_fault_allowed is read by kvm_arch_vcpu_pre_fault_memory()
* before kvm_tdx->state. Userspace must not be allowed to pre-fault
@@ -1722,10 +1757,15 @@ static int tdx_sept_map_leaf_spte(struct kvm *kvm, gfn_t gfn, enum pg_level leve
* If the TD isn't finalized/runnable, then userspace is initializing
* the VM image via KVM_TDX_INIT_MEM_REGION; ADD the page to the TD.
*/
- if (unlikely(kvm_tdx->state != TD_STATE_RUNNABLE))
- return tdx_mem_page_add(kvm, gfn, level, pfn);
+ if (likely(kvm_tdx->state == TD_STATE_RUNNABLE))
+ ret = tdx_mem_page_aug(kvm, gfn, level, pfn);
+ else
+ ret = tdx_mem_page_add(kvm, gfn, level, pfn);
- return tdx_mem_page_aug(kvm, gfn, level, pfn);
+ if (ret)
+ tdx_pamt_put(pfn);
+
+ return ret;
}
/*
@@ -1822,6 +1862,7 @@ static int tdx_sept_remove_leaf_spte(struct kvm *kvm, gfn_t gfn,
return -EIO;
tdx_quirk_reset_paddr(PFN_PHYS(pfn), PAGE_SIZE);
+ tdx_pamt_put(pfn);
return 0;
}
@@ -1865,6 +1906,8 @@ static int tdx_sept_set_private_spte(struct kvm *kvm, gfn_t gfn, u64 old_spte,
*/
static void tdx_sept_free_private_spt(struct kvm *kvm, struct kvm_mmu_page *sp)
{
+ struct page *sept_pt = virt_to_page(sp->external_spt);
+
/*
* KVM doesn't (yet) zap page table pages in mirror page table while
* TD is active, though guest pages mapped in mirror page table could be
@@ -1878,15 +1921,15 @@ static void tdx_sept_free_private_spt(struct kvm *kvm, struct kvm_mmu_page *sp)
* the page to prevent the kernel from accessing the encrypted page.
*/
if (KVM_BUG_ON(is_hkid_assigned(to_kvm_tdx(kvm)), kvm) ||
- tdx_reclaim_page(virt_to_page(sp->external_spt)))
+ tdx_reclaim_page(sept_pt))
goto out;
/*
- * Immediately free the S-EPT page because RCU-time free is unnecessary
- * after TDH.PHYMEM.PAGE.RECLAIM ensures there are no outstanding
- * readers.
+ * Immediately free the S-EPT page as the TDX subsystem doesn't support
+ * freeing pages from RCU callbacks, and more importantly because
+ * TDH.PHYMEM.PAGE.RECLAIM ensures there are no outstanding readers.
*/
- free_page((unsigned long)sp->external_spt);
+ tdx_free_control_page(sept_pt);
out:
sp->external_spt = NULL;
}
@@ -3482,6 +3525,10 @@ int __init tdx_hardware_setup(void)
vt_x86_ops.set_external_spte = tdx_sept_set_private_spte;
vt_x86_ops.free_external_spt = tdx_sept_free_private_spt;
+
+ if (tdx_supports_dynamic_pamt(tdx_sysinfo))
+ vt_x86_ops.topup_external_cache = tdx_topup_external_pamt_cache;
+
vt_x86_ops.protected_apic_has_interrupt = tdx_protected_apic_has_interrupt;
return 0;
diff --git a/arch/x86/kvm/vmx/tdx.h b/arch/x86/kvm/vmx/tdx.h
index ac8323a68b163..fd368e3ee060b 100644
--- a/arch/x86/kvm/vmx/tdx.h
+++ b/arch/x86/kvm/vmx/tdx.h
@@ -72,6 +72,8 @@ struct vcpu_tdx {
u64 map_gpa_next;
u64 map_gpa_end;
+
+ struct tdx_pamt_cache pamt_cache;
};
void tdh_vp_rd_failed(struct vcpu_tdx *tdx, char *uclass, u32 field, u64 err);
diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c
index e2f11b0ba46ce..61f48fff69df6 100644
--- a/arch/x86/virt/vmx/tdx/tdx.c
+++ b/arch/x86/virt/vmx/tdx/tdx.c
@@ -2044,6 +2044,7 @@ bool tdx_supports_dynamic_pamt(const struct tdx_sys_info *sysinfo)
/* To be enabled when kernel is ready. */
return false;
}
+EXPORT_SYMBOL_FOR_KVM(tdx_supports_dynamic_pamt);
static struct page *tdx_alloc_page_pamt_cache(struct tdx_pamt_cache *cache)
{
--
2.54.0
^ permalink raw reply related
* [PATCH v7 09/11] x86/virt/tdx: Enable Dynamic PAMT
From: Rick Edgecombe @ 2026-07-18 1:44 UTC (permalink / raw)
To: bp, dave.hansen, hpa, kas, kvm, linux-coco, linux-doc,
linux-kernel, mingo, nik.borisov, pbonzini, seanjc, tglx,
vannapurve, x86, chao.gao, yan.y.zhao, kai.huang, tony.lindgren,
binbin.wu
Cc: rick.p.edgecombe
In-Reply-To: <20260718014500.2231262-1-rick.p.edgecombe@intel.com>
From: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
The Physical Address Metadata Table (PAMT) holds TDX metadata for
physical memory and must be allocated by the kernel during TDX module
initialization. Dynamic PAMT is a TDX module feature that can reduce this
memory use by allocating part of the PAMT dynamically.
The TDX module exposes whether Dynamic PAMT is supported via a bit in the
'features0' metadata. Unfortunately, the TDX module exposes the feature as
supported even when it does not support using it with the number of keyids
currently configured in the BIOS. Since no TDX modules exist today with
that issue fixed, make the feature default off to prevent users from
upgrading their kernel and encountering TDX erroring out when trying to
enable Dynamic PAMT.
For the decision of whether to make it a boot time option and/or compile
time option, consider that Dynamic PAMT's memory savings are significant
enough to make it a good default configuration. That is most TDX users
should want it unless they have strange keyid configurations.
The feature increases the kernel size by 2KB (when TDX is configured in the
build).
All pieces are in place to enable Dynamic PAMT if it is supported and the
user passes a kernel parameter.
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Co-developed-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Reviewed-by: Tony Lindgren <tony.lindgren@linux.intel.com>
---
v7:
- Add kernel parameter following some twists and turns, derriving
originally from a comment by (Chao)
v6:
- After Nikolai pointed out that the TDX docs actually have the Dynamic
PAMT pages-per-2MB region fixed at 2 instead of variable sized, I
checked over the docs more closely looking for anything else that might
have been missed. Spotted this 48 bit physical address bit check in the
docs, so added it.
---
.../admin-guide/kernel-parameters.txt | 7 +++++++
arch/x86/include/asm/tdx.h | 1 +
arch/x86/virt/vmx/tdx/tdx.c | 21 +++++++++++++++++--
3 files changed, 27 insertions(+), 2 deletions(-)
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index b5493a7f8f228..be4928489b02f 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -1321,6 +1321,13 @@ Kernel parameters
The filter can be disabled or changed to another
driver later using sysfs.
+ tdx_dpamt=
+ [X86] Controls whether TDX will use Dynamic PAMT
+ to save memory, when supported.
+
+ Valid parameters: "on", "off"
+ Default: "off"
+
reg_file_data_sampling=
[X86] Controls mitigation for Register File Data
Sampling (RFDS) vulnerability. RFDS is a CPU
diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h
index 9cbd250bbd39b..7910901a7ba21 100644
--- a/arch/x86/include/asm/tdx.h
+++ b/arch/x86/include/asm/tdx.h
@@ -36,6 +36,7 @@
/* Bit definitions of TDX_FEATURES0 metadata field */
#define TDX_FEATURES0_TD_PRESERVING BIT_ULL(1)
#define TDX_FEATURES0_NO_RBP_MOD BIT_ULL(18)
+#define TDX_FEATURES0_DYNAMIC_PAMT BIT_ULL(36)
#ifndef __ASSEMBLER__
diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c
index 61f48fff69df6..f397ea17248e3 100644
--- a/arch/x86/virt/vmx/tdx/tdx.c
+++ b/arch/x86/virt/vmx/tdx/tdx.c
@@ -47,6 +47,8 @@
#include "seamcall_internal.h"
#include "tdx.h"
+bool tdx_enable_dpamt __ro_after_init;
+
struct tdx_module_state {
bool initialized;
bool sysinit_done;
@@ -1028,6 +1030,8 @@ static __init int construct_tdmrs(struct list_head *tmb_list,
return ret;
}
+#define TDX_SYS_CONFIG_DYNAMIC_PAMT BIT(16)
+
static __init int config_tdx_module(struct tdmr_info_list *tdmr_list,
u64 global_keyid)
{
@@ -1056,6 +1060,12 @@ static __init int config_tdx_module(struct tdmr_info_list *tdmr_list,
args.rcx = __pa(tdmr_pa_array);
args.rdx = tdmr_list->nr_consumed_tdmrs;
args.r8 = global_keyid;
+
+ if (tdx_supports_dynamic_pamt(&tdx_sysinfo)) {
+ pr_info("Enable Dynamic PAMT\n");
+ args.r8 |= TDX_SYS_CONFIG_DYNAMIC_PAMT;
+ }
+
ret = seamcall_prerr(TDH_SYS_CONFIG, &args);
/* Free the array as it is not required anymore. */
@@ -2041,8 +2051,8 @@ EXPORT_SYMBOL_FOR_KVM(tdh_phymem_page_wbinvd_hkid);
bool tdx_supports_dynamic_pamt(const struct tdx_sys_info *sysinfo)
{
- /* To be enabled when kernel is ready. */
- return false;
+ return sysinfo->features.tdx_features0 & TDX_FEATURES0_DYNAMIC_PAMT &&
+ tdx_enable_dpamt;
}
EXPORT_SYMBOL_FOR_KVM(tdx_supports_dynamic_pamt);
@@ -2300,6 +2310,13 @@ void tdx_free_control_page(struct page *page)
}
EXPORT_SYMBOL_FOR_KVM(tdx_free_control_page);
+static int __init tdx_dpamt_setup(char *str)
+{
+ return kstrtobool(str, &tdx_enable_dpamt) == 0;
+}
+
+__setup("tdx_dpamt=", tdx_dpamt_setup);
+
void tdx_sys_disable(void)
{
struct tdx_module_args args = {};
--
2.54.0
^ permalink raw reply related
* [PATCH v7 06/11] KVM: TDX: Allocate PAMT memory for TD and vCPU control structures
From: Rick Edgecombe @ 2026-07-18 1:44 UTC (permalink / raw)
To: bp, dave.hansen, hpa, kas, kvm, linux-coco, linux-doc,
linux-kernel, mingo, nik.borisov, pbonzini, seanjc, tglx,
vannapurve, x86, chao.gao, yan.y.zhao, kai.huang, tony.lindgren,
binbin.wu
Cc: rick.p.edgecombe, Binbin Wu
In-Reply-To: <20260718014500.2231262-1-rick.p.edgecombe@intel.com>
From: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
Use control page helpers for allocating and freeing TD control structures,
such that these operations can work for Dynamic PAMT.
The TDX module tracks some state for each page of physical memory that it
might use. It calls this state the PAMT. It includes separate state for
each page size a physical page could be utilized at within the TDX module
(1GB, 2MB, 4KB). In Dynamic PAMT, only the 4KB page size state is
allocated dynamically. So the kernel must ensure PAMT backing is installed
for any 4KB page being gifted to the TDX module, and must tear down the
backing when all associated gifted pages are reclaimed.
TD scoped control pages (TDR, TDCS) and vCPU scoped control pages (TDVPR,
TDCX) are all handed to the TDX module at 4KB page size and are therefore
subject to this requirement. Replace the raw alloc_page()/__free_page()
calls for these pages with tdx_alloc/free_control_page().
Switching between special Dynamic PAMT operations or normal page
alloc/free operations is handled internally in
tdx_alloc/free_control_page(). So don't check for Dynamic PAMT around these
calls. Just call them unconditionally. Similarly, drop the NULL checks
before freeing, as tdx_free_control_page() handles NULL internally.
No functional change intended when Dynamic PAMT is not in use.
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
[sean: handle alloc+free+reclaim in one patch]
Signed-off-by: Sean Christopherson <seanjc@google.com>
[rick: enhance log, reviewing, rebase, with help from AI tooling]
Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Reviewed-by: Binbin Wu <binbin.wu@linux.intel.com>
Reviewed-by: Chao Gao <chao.gao@intel.com>
Reviewed-by: Yan Zhao <yan.y.zhao@intel.com>
Reviewed-by: Tony Lindgren <tony.lindgren@linux.intel.com>
---
v7:
- Fixup tags (Sean)
- Missing word in log (Binbin)
- Log smoothness (Yan)
---
arch/x86/kvm/vmx/tdx.c | 35 ++++++++++++++---------------------
1 file changed, 14 insertions(+), 21 deletions(-)
diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
index 989ab29b8c6fb..f6c01eab8113b 100644
--- a/arch/x86/kvm/vmx/tdx.c
+++ b/arch/x86/kvm/vmx/tdx.c
@@ -362,7 +362,7 @@ static void tdx_reclaim_control_page(struct page *ctrl_page)
if (tdx_reclaim_page(ctrl_page))
return;
- __free_page(ctrl_page);
+ tdx_free_control_page(ctrl_page);
}
struct tdx_flush_vp_arg {
@@ -589,7 +589,7 @@ static void tdx_reclaim_td_control_pages(struct kvm *kvm)
tdx_quirk_reset_paddr(page_to_phys(kvm_tdx->td.tdr_page), PAGE_SIZE);
- __free_page(kvm_tdx->td.tdr_page);
+ tdx_free_control_page(kvm_tdx->td.tdr_page);
kvm_tdx->td.tdr_page = NULL;
}
@@ -2459,7 +2459,7 @@ static int __tdx_td_init(struct kvm *kvm, struct td_params *td_params,
ret = -ENOMEM;
- tdr_page = alloc_page(GFP_KERNEL_ACCOUNT);
+ tdr_page = tdx_alloc_control_page();
if (!tdr_page)
goto free_hkid;
@@ -2472,7 +2472,7 @@ static int __tdx_td_init(struct kvm *kvm, struct td_params *td_params,
goto free_tdr;
for (i = 0; i < kvm_tdx->td.tdcs_nr_pages; i++) {
- tdcs_pages[i] = alloc_page(GFP_KERNEL_ACCOUNT);
+ tdcs_pages[i] = tdx_alloc_control_page();
if (!tdcs_pages[i])
goto free_tdcs;
}
@@ -2590,10 +2590,8 @@ static int __tdx_td_init(struct kvm *kvm, struct td_params *td_params,
teardown:
/* Only free pages not yet added, so start at 'i' */
for (; i < kvm_tdx->td.tdcs_nr_pages; i++) {
- if (tdcs_pages[i]) {
- __free_page(tdcs_pages[i]);
- tdcs_pages[i] = NULL;
- }
+ tdx_free_control_page(tdcs_pages[i]);
+ tdcs_pages[i] = NULL;
}
if (!kvm_tdx->td.tdcs_pages)
kfree(tdcs_pages);
@@ -2608,16 +2606,13 @@ static int __tdx_td_init(struct kvm *kvm, struct td_params *td_params,
free_cpumask_var(packages);
free_tdcs:
- for (i = 0; i < kvm_tdx->td.tdcs_nr_pages; i++) {
- if (tdcs_pages[i])
- __free_page(tdcs_pages[i]);
- }
+ for (i = 0; i < kvm_tdx->td.tdcs_nr_pages; i++)
+ tdx_free_control_page(tdcs_pages[i]);
kfree(tdcs_pages);
kvm_tdx->td.tdcs_pages = NULL;
free_tdr:
- if (tdr_page)
- __free_page(tdr_page);
+ tdx_free_control_page(tdr_page);
kvm_tdx->td.tdr_page = NULL;
free_hkid:
@@ -2947,7 +2942,7 @@ static int tdx_td_vcpu_init(struct kvm_vcpu *vcpu, u64 vcpu_rcx)
int ret, i;
u64 err;
- page = alloc_page(GFP_KERNEL_ACCOUNT);
+ page = tdx_alloc_control_page();
if (!page)
return -ENOMEM;
tdx->vp.tdvpr_page = page;
@@ -2967,7 +2962,7 @@ static int tdx_td_vcpu_init(struct kvm_vcpu *vcpu, u64 vcpu_rcx)
}
for (i = 0; i < kvm_tdx->td.tdcx_nr_pages; i++) {
- page = alloc_page(GFP_KERNEL_ACCOUNT);
+ page = tdx_alloc_control_page();
if (!page) {
ret = -ENOMEM;
goto free_tdcx;
@@ -2989,7 +2984,7 @@ static int tdx_td_vcpu_init(struct kvm_vcpu *vcpu, u64 vcpu_rcx)
* method, but the rest are freed here.
*/
for (; i < kvm_tdx->td.tdcx_nr_pages; i++) {
- __free_page(tdx->vp.tdcx_pages[i]);
+ tdx_free_control_page(tdx->vp.tdcx_pages[i]);
tdx->vp.tdcx_pages[i] = NULL;
}
return -EIO;
@@ -3017,16 +3012,14 @@ static int tdx_td_vcpu_init(struct kvm_vcpu *vcpu, u64 vcpu_rcx)
free_tdcx:
for (i = 0; i < kvm_tdx->td.tdcx_nr_pages; i++) {
- if (tdx->vp.tdcx_pages[i])
- __free_page(tdx->vp.tdcx_pages[i]);
+ tdx_free_control_page(tdx->vp.tdcx_pages[i]);
tdx->vp.tdcx_pages[i] = NULL;
}
kfree(tdx->vp.tdcx_pages);
tdx->vp.tdcx_pages = NULL;
free_tdvpr:
- if (tdx->vp.tdvpr_page)
- __free_page(tdx->vp.tdvpr_page);
+ tdx_free_control_page(tdx->vp.tdvpr_page);
tdx->vp.tdvpr_page = NULL;
tdx->vp.tdvpr_pa = 0;
--
2.54.0
^ permalink raw reply related
* [PATCH v7 04/11] x86/virt/tdx: Allocate refcounts for Dynamic PAMT memory
From: Rick Edgecombe @ 2026-07-18 1:44 UTC (permalink / raw)
To: bp, dave.hansen, hpa, kas, kvm, linux-coco, linux-doc,
linux-kernel, mingo, nik.borisov, pbonzini, seanjc, tglx,
vannapurve, x86, chao.gao, yan.y.zhao, kai.huang, tony.lindgren,
binbin.wu
Cc: rick.p.edgecombe, Binbin Wu
In-Reply-To: <20260718014500.2231262-1-rick.p.edgecombe@intel.com>
From: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
The PAMT memory holds metadata for all possible TDX protected memory. Each
physical address range is covered by PAMT entries at three levels (1GB,
2MB, 4KB). With Dynamic PAMT, the 4KB level of PAMT is allocated on
demand. The kernel supplies the TDX module with page pairs to store the
4KB level entries, which cover 2MB of host physical memory. The kernel must
provide this page pair before using pages from the range for TDX. If this
is not done, SEAMCALLs that give the pages to be protected by the TDX
module will fail.
Allocate reference counters for every 2MB range to track TDX memory usage.
This can be used to handle concurrent get/put callers, in order to
accurately determine when the dynamic 4KB level of Dynamic PAMT needs to
be allocated and when it can be freed.
This allocation will currently consume 2MB for every 1TB of address
space from 0 to max_pfn. The allocation size will depend on how the RAM is
physically laid out. In a worst case scenario where the entire 52 bit
address space is covered this would be 8GB. Then the Dynamic PAMT refcount
allocations could hypothetically cause the savings from Dynamic PAMT to go
negative on exotic platforms with sparse, small amounts of memory.
Future changes could reduce this refcount overhead to be only allocating
refcounts for physical ranges that contain memory that TDX can use.
However, this is left for future work.
AI was used under supervision to collect/apply feedback, review code and
workshop logs.
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Co-developed-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Reviewed-by: Binbin Wu <binbin.wu@linux.intel.com>
Reviewed-by: Chao Gao <chao.gao@intel.com>
Reviewed-by: Yan Zhao <yan.y.zhao@intel.com>
Reviewed-by: Tony Lindgren <tony.lindgren@linux.intel.com>
---
v7:
- Annote functions __init (Chao)
- Log tweaks (Yan)
- Stanardize on memory units in the text (Sohil)
- Delete unneeded comment (Sohil)
- Use vzalloc() (Sohil)
- Drop Assisted-by tag and cover AI use in log (Dave)
v6:
- Remove confusing reference to allocating PAMT memory in
pamt_refcounts comment. (Yan)
- Rename "metadata" function names that really deal with refcounts, as
metadata already has a different meaning in TDX.
- Move tdx_find_pamt_refcount() to this patch to aid in reviewability
---
arch/x86/virt/vmx/tdx/tdx.c | 53 ++++++++++++++++++++++++++++++++++++-
1 file changed, 52 insertions(+), 1 deletion(-)
diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c
index bfd9928c10249..b2ddd3c192645 100644
--- a/arch/x86/virt/vmx/tdx/tdx.c
+++ b/arch/x86/virt/vmx/tdx/tdx.c
@@ -30,6 +30,7 @@
#include <linux/suspend.h>
#include <linux/syscore_ops.h>
#include <linux/idr.h>
+#include <linux/vmalloc.h>
#include <asm/page.h>
#include <asm/special_insns.h>
#include <asm/msr-index.h>
@@ -63,6 +64,14 @@ static DEFINE_PER_CPU(bool, tdx_lp_initialized);
static struct tdmr_info_list tdx_tdmr_list;
+/*
+ * On a machine with Dynamic PAMT, the kernel maintains a reference counter
+ * for every 2MB range. The counter indicates how many users there are for
+ * the PAMT memory of the 2MB range. The kernel allocates PAMT refcounts at
+ * initialization.
+ */
+static atomic_t *pamt_refcounts;
+
/* All TDX-usable memory regions. Protected by mem_hotplug_lock. */
static LIST_HEAD(tdx_memlist);
@@ -252,6 +261,42 @@ static struct syscore tdx_syscore = {
.ops = &tdx_syscore_ops,
};
+/*
+ * Allocate PAMT reference counters for all physical memory.
+ *
+ * It consumes 2MB for every 1TB of physical memory.
+ */
+static __init int init_pamt_refcounts(void)
+{
+ size_t size = DIV_ROUND_UP(max_pfn, PTRS_PER_PTE) * sizeof(*pamt_refcounts);
+
+ if (!tdx_supports_dynamic_pamt(&tdx_sysinfo))
+ return 0;
+
+ pamt_refcounts = vzalloc(size);
+ if (!pamt_refcounts)
+ return -ENOMEM;
+
+ return 0;
+}
+
+static __init void free_pamt_refcounts(void)
+{
+ if (!tdx_supports_dynamic_pamt(&tdx_sysinfo))
+ return;
+
+ vfree(pamt_refcounts);
+ pamt_refcounts = NULL;
+}
+
+static atomic_t * __maybe_unused tdx_find_pamt_refcount(unsigned long pfn)
+{
+ /* Find which PMD a PFN is in. */
+ unsigned long index = pfn >> (PMD_SHIFT - PAGE_SHIFT);
+
+ return &pamt_refcounts[index];
+}
+
/*
* Add a memory region as a TDX memory block. The caller must make sure
* all memory regions are added in address ascending order and don't
@@ -1152,10 +1197,14 @@ static __init int init_tdx_module(void)
*/
get_online_mems();
- ret = build_tdx_memlist(&tdx_memlist);
+ ret = init_pamt_refcounts();
if (ret)
goto out_put_tdxmem;
+ ret = build_tdx_memlist(&tdx_memlist);
+ if (ret)
+ goto err_free_pamt_refcounts;
+
/* Allocate enough space for constructing TDMRs */
ret = alloc_tdmr_list(&tdx_tdmr_list, &tdx_sysinfo.tdmr);
if (ret)
@@ -1205,6 +1254,8 @@ static __init int init_tdx_module(void)
free_tdmr_list(&tdx_tdmr_list);
err_free_tdxmem:
free_tdx_memlist(&tdx_memlist);
+err_free_pamt_refcounts:
+ free_pamt_refcounts();
goto out_put_tdxmem;
}
--
2.54.0
^ permalink raw reply related
* [PATCH v7 07/11] x86/tdx: Add APIs to support Dynamic PAMT ops from KVM's fault path
From: Rick Edgecombe @ 2026-07-18 1:44 UTC (permalink / raw)
To: bp, dave.hansen, hpa, kas, kvm, linux-coco, linux-doc,
linux-kernel, mingo, nik.borisov, pbonzini, seanjc, tglx,
vannapurve, x86, chao.gao, yan.y.zhao, kai.huang, tony.lindgren,
binbin.wu
Cc: rick.p.edgecombe, Binbin Wu
In-Reply-To: <20260718014500.2231262-1-rick.p.edgecombe@intel.com>
When handling an EPT violation, KVM holds a spinlock while manipulating
the EPT. Before entering the spinlock it doesn't know how many EPT page
tables will need to be installed or whether a huge page will be used. For
this reason it allocates a worst case number of page tables that it might
need as part of servicing the EPT violation.
Under Dynamic PAMT these pre-allocated pages will potentially need to have
Dynamic PAMT backing pages installed for them. KVM already has helpers to
manage topping up page caches before taking the MMU lock, but they cannot
be passed from KVM to arch/x86 code.
The problem of how and when to install the Dynamic PAMT backing pages for
the pages given to the TDX module during the fault path has had a lot of
design attempts.
- Extracting KVM's MMU caches requires too much inlined code added to
headers.
- A few varieties of installing Dynamic PAMT backing when allocating the
S-EPT page tables. (see links)
- Using mempool_t to transfer the pages between KVM and arch/x86 doesn't
work because the component is designed more around maintaining a pool
of pages, rather than topping up a continually drained cache.
So don't do these as they all had various problems. Instead just create a
small simple data structure to use for handing a pre-allocated list of
pages between KVM and arch/x86 code. Model this on KVM's existing MMU
memory caches.
Add a tdx_pamt_cache arg to tdx_pamt_get() so it can draw pages from a
cache when needed. Not all Dynamic PAMT page installations will happen
under spinlock, for example TD and vCPU scoped control pages. So have
tdx_pamt_get() maintain the existing behavior of allocating from the page
allocator when NULL is passed for the struct tdx_pamt_cache arg. This
prevents excess allocations for cases where it can be avoided.
Export the new helpers for KVM.
AI was used under supervision to review code and workshop logs.
Co-developed-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Reviewed-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
Reviewed-by: Binbin Wu <binbin.wu@linux.intel.com>
Reviewed-by: Chao Gao <chao.gao@intel.com>
Reviewed-by: Yan Zhao <yan.y.zhao@intel.com>
Reviewed-by: Tony Lindgren <tony.lindgren@linux.intel.com>
Link: https://lore.kernel.org/kvm/aXENNKjAKTM9UJNH@google.com/
Link: https://lore.kernel.org/kvm/20260129011517.3545883-20-seanjc@google.com/
Link: https://lore.kernel.org/kvm/aYW5CbUvZrLogsWF@yzhao56-desk.sh.intel.com/
---
v7:
- Log/comment tweaks (Yan)
- Drop Assisted-by tag and cover AI use in log (Dave)
v6:
- Filled out log from Sean's series
---
arch/x86/include/asm/tdx.h | 17 ++++++++++
arch/x86/virt/vmx/tdx/tdx.c | 65 +++++++++++++++++++++++++++++++++----
2 files changed, 76 insertions(+), 6 deletions(-)
diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h
index 86bf37b15c705..9cbd250bbd39b 100644
--- a/arch/x86/include/asm/tdx.h
+++ b/arch/x86/include/asm/tdx.h
@@ -120,6 +120,23 @@ static inline bool tdx_supports_runtime_update(const struct tdx_sys_info *sysinf
bool tdx_supports_dynamic_pamt(const struct tdx_sys_info *sysinfo);
+/* Simple structure for pre-allocating Dynamic PAMT pages outside of spinlocks. */
+struct tdx_pamt_cache {
+ struct list_head page_list;
+ int cnt;
+};
+
+static inline void tdx_init_pamt_cache(struct tdx_pamt_cache *cache)
+{
+ INIT_LIST_HEAD(&cache->page_list);
+ cache->cnt = 0;
+}
+
+void tdx_free_pamt_cache(struct tdx_pamt_cache *cache);
+int tdx_topup_pamt_cache(struct tdx_pamt_cache *cache, unsigned long npages);
+int tdx_pamt_get(kvm_pfn_t pfn, struct tdx_pamt_cache *cache);
+void tdx_pamt_put(kvm_pfn_t pfn);
+
int tdx_guest_keyid_alloc(void);
u32 tdx_get_nr_guest_keyids(void);
void tdx_guest_keyid_free(unsigned int keyid);
diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c
index 343d0cccc9874..e2f11b0ba46ce 100644
--- a/arch/x86/virt/vmx/tdx/tdx.c
+++ b/arch/x86/virt/vmx/tdx/tdx.c
@@ -2045,12 +2045,33 @@ bool tdx_supports_dynamic_pamt(const struct tdx_sys_info *sysinfo)
return false;
}
-static int alloc_pamt_array(struct page **pamt_pages)
+static struct page *tdx_alloc_page_pamt_cache(struct tdx_pamt_cache *cache)
+{
+ struct page *page;
+
+ page = list_first_entry_or_null(&cache->page_list, struct page, lru);
+ if (page) {
+ list_del(&page->lru);
+ cache->cnt--;
+ }
+
+ return page;
+}
+
+static struct page *alloc_dpamt_page(struct tdx_pamt_cache *cache)
+{
+ if (cache)
+ return tdx_alloc_page_pamt_cache(cache);
+
+ return alloc_page(GFP_KERNEL_ACCOUNT);
+}
+
+static int alloc_pamt_array(struct page **pamt_pages, struct tdx_pamt_cache *cache)
{
int i, j;
for (i = 0; i < TDX_DPAMT_ENTRY_PAGE_CNT; i++) {
- pamt_pages[i] = alloc_page(GFP_KERNEL_ACCOUNT);
+ pamt_pages[i] = alloc_dpamt_page(cache);
if (!pamt_pages[i])
goto err;
}
@@ -2124,7 +2145,7 @@ static u64 tdh_phymem_pamt_remove(kvm_pfn_t pfn, struct page **pamt_pages)
static DEFINE_SPINLOCK(pamt_lock);
/* Bump PAMT refcount for the given pfn and allocate PAMT backing if needed. */
-static int tdx_pamt_get(kvm_pfn_t pfn)
+int tdx_pamt_get(kvm_pfn_t pfn, struct tdx_pamt_cache *cache)
{
struct page *pamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT];
atomic_t *pamt_refcount;
@@ -2134,7 +2155,7 @@ static int tdx_pamt_get(kvm_pfn_t pfn)
if (!tdx_supports_dynamic_pamt(&tdx_sysinfo))
return 0;
- ret = alloc_pamt_array(pamt_pages);
+ ret = alloc_pamt_array(pamt_pages, cache);
if (ret)
return ret;
@@ -2166,9 +2187,10 @@ static int tdx_pamt_get(kvm_pfn_t pfn)
return ret;
}
+EXPORT_SYMBOL_FOR_KVM(tdx_pamt_get);
/* Drop PAMT refcount for the given pfn and free PAMT backing if needed. */
-static void tdx_pamt_put(kvm_pfn_t pfn)
+void tdx_pamt_put(kvm_pfn_t pfn)
{
struct page *pamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT] = {};
atomic_t *pamt_refcount;
@@ -2209,6 +2231,37 @@ static void tdx_pamt_put(kvm_pfn_t pfn)
out_unlock:
spin_unlock(&pamt_lock);
}
+EXPORT_SYMBOL_FOR_KVM(tdx_pamt_put);
+
+void tdx_free_pamt_cache(struct tdx_pamt_cache *cache)
+{
+ struct page *page;
+
+ while ((page = tdx_alloc_page_pamt_cache(cache)))
+ __free_page(page);
+}
+EXPORT_SYMBOL_FOR_KVM(tdx_free_pamt_cache);
+
+int tdx_topup_pamt_cache(struct tdx_pamt_cache *cache, unsigned long npages)
+{
+ if (WARN_ON_ONCE(!tdx_supports_dynamic_pamt(&tdx_sysinfo)))
+ return 0;
+
+ npages *= TDX_DPAMT_ENTRY_PAGE_CNT;
+
+ while (cache->cnt < npages) {
+ struct page *page = alloc_page(GFP_KERNEL_ACCOUNT);
+
+ if (!page)
+ return -ENOMEM;
+
+ list_add(&page->lru, &cache->page_list);
+ cache->cnt++;
+ }
+
+ return 0;
+}
+EXPORT_SYMBOL_FOR_KVM(tdx_topup_pamt_cache);
/*
* Return a page that can be gifted to the TDX-Module for use as a "control"
@@ -2223,7 +2276,7 @@ struct page *tdx_alloc_control_page(void)
if (!page)
return NULL;
- if (tdx_pamt_get(page_to_pfn(page))) {
+ if (tdx_pamt_get(page_to_pfn(page), NULL)) {
__free_page(page);
return NULL;
}
--
2.54.0
^ permalink raw reply related
* [PATCH v7 05/11] x86/virt/tdx: Handle multiple callers in tdx_pamt_get/put()
From: Rick Edgecombe @ 2026-07-18 1:44 UTC (permalink / raw)
To: bp, dave.hansen, hpa, kas, kvm, linux-coco, linux-doc,
linux-kernel, mingo, nik.borisov, pbonzini, seanjc, tglx,
vannapurve, x86, chao.gao, yan.y.zhao, kai.huang, tony.lindgren,
binbin.wu
Cc: rick.p.edgecombe, Binbin Wu
In-Reply-To: <20260718014500.2231262-1-rick.p.edgecombe@intel.com>
From: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
tdx_pamt_get()/tdx_pamt_put() unconditionally add or remove Dynamic PAMT
backing for the 2MB region covering the passed page. However, multiple
callers can add or remove 4KB pages that fall within the same 2MB region
and in that scenario only a single PAMT entry is required.
Make the helpers handle only adding/removing Dynamic PAMT backing when
required, by refcounting each 2MB range. Gate the actual Dynamic PAMT add
and remove on refcount transitions (0->1 and 1->0). Serialize the refcount
check and SEAMCALL with a global spinlock so the read-decide-act sequence
is atomic. This also avoids TDX module BUSY errors, as the Dynamic PAMT add
and remove SEAMCALLs take internal TDX module locks for the 2MB ranges of
the specified PFN and the PAMT page pair PFNs. So simultaneous attempts on
the same 2MB ranges of the PFNs would otherwise encounter an error, which
would not be handleable in the put case.
The lock is global and heavyweight. Use simple conditional logic to keep
correctness obvious. This will be optimized in a later change.
The pamt_refcounts[] are atomic_t's. They do not strictly need to be
because all access is protected by pamt_lock. The overhead of an atomic_t
in this situation is minuscule compared to the global lock. Leave the
atomic_t in place to enable future optimization with minimal churn.
AI was used under supervision to collect/apply feedback, split patches,
review code and workshop logs.
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Co-developed-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Reviewed-by: Binbin Wu <binbin.wu@linux.intel.com>
Reviewed-by: Chao Gao <chao.gao@intel.com>
Reviewed-by: Yan Zhao <yan.y.zhao@intel.com>
Reviewed-by: Tony Lindgren <tony.lindgren@linux.intel.com>
---
v7:
- Convert scoped_guard() blocks to use normal spin_un/lock() for the
sake of making next patches diff cleaner
- Drop __maybe_unused from tdx_find_pamt_refcount() (Binbin)
- Switch to atomic_inc_not_zero() (Dave)
- Justify use of atomic_t in log (Sohil)
- Log/comments (Yan)
- Drop Assisted-by tag and cover AI use in log (Dave)
v6:
- Split from "x86/virt/tdx: Add tdx_alloc/free_control_page() helpers"
- Return 0 instead of ret to be clearer (Binbin)
- Clarify log (Nikolay)
- Justify why the patch is not optimized in response to comments by
(Nikolay)
- Move tdx_find_pamt_refcount() to faciliate patch re-order
- Adjustments from dropping error helper patches
- Log tweaks
---
arch/x86/virt/vmx/tdx/tdx.c | 48 +++++++++++++++++++++++++++++++++----
1 file changed, 43 insertions(+), 5 deletions(-)
diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c
index b2ddd3c192645..343d0cccc9874 100644
--- a/arch/x86/virt/vmx/tdx/tdx.c
+++ b/arch/x86/virt/vmx/tdx/tdx.c
@@ -289,7 +289,7 @@ static __init void free_pamt_refcounts(void)
pamt_refcounts = NULL;
}
-static atomic_t * __maybe_unused tdx_find_pamt_refcount(unsigned long pfn)
+static atomic_t *tdx_find_pamt_refcount(unsigned long pfn)
{
/* Find which PMD a PFN is in. */
unsigned long index = pfn >> (PMD_SHIFT - PAGE_SHIFT);
@@ -2120,10 +2120,14 @@ static u64 tdh_phymem_pamt_remove(kvm_pfn_t pfn, struct page **pamt_pages)
return 0;
}
-/* Allocate PAMT memory for the given page */
+/* Serializes adding/removing PAMT memory */
+static DEFINE_SPINLOCK(pamt_lock);
+
+/* Bump PAMT refcount for the given pfn and allocate PAMT backing if needed. */
static int tdx_pamt_get(kvm_pfn_t pfn)
{
struct page *pamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT];
+ atomic_t *pamt_refcount;
u64 tdx_status;
int ret;
@@ -2134,29 +2138,58 @@ static int tdx_pamt_get(kvm_pfn_t pfn)
if (ret)
return ret;
+ pamt_refcount = tdx_find_pamt_refcount(pfn);
+
+ spin_lock(&pamt_lock);
+
+ /*
+ * If the pamt page is already added (i.e. refcount >= 1),
+ * then just increment the refcount.
+ */
+ if (atomic_inc_not_zero(pamt_refcount))
+ goto out_free;
+
+ /* Try to add the pamt page and take the refcount 0->1. */
tdx_status = tdh_phymem_pamt_add(pfn, pamt_pages);
- if (tdx_status != TDX_SUCCESS) {
+ if (WARN_ON_ONCE(tdx_status != TDX_SUCCESS)) {
ret = -EIO;
goto out_free;
}
+ atomic_set(pamt_refcount, 1);
+ spin_unlock(&pamt_lock);
return 0;
out_free:
+ spin_unlock(&pamt_lock);
free_pamt_array(pamt_pages);
return ret;
}
-/* Free PAMT memory for the given page */
+/* Drop PAMT refcount for the given pfn and free PAMT backing if needed. */
static void tdx_pamt_put(kvm_pfn_t pfn)
{
struct page *pamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT] = {};
+ atomic_t *pamt_refcount;
u64 tdx_status;
if (!tdx_supports_dynamic_pamt(&tdx_sysinfo))
return;
+ pamt_refcount = tdx_find_pamt_refcount(pfn);
+
+ spin_lock(&pamt_lock);
+ /*
+ * If there is more than 1 reference on the pamt page, don't
+ * remove it yet. Just decrement the refcount.
+ */
+ if (atomic_read(pamt_refcount) > 1) {
+ atomic_dec(pamt_refcount);
+ goto out_unlock;
+ }
+
+ /* Try to remove the pamt page and take the refcount 1->0. */
tdx_status = tdh_phymem_pamt_remove(pfn, pamt_pages);
/*
@@ -2167,9 +2200,14 @@ static void tdx_pamt_put(kvm_pfn_t pfn)
* the dangling PAMT entry may cause future operations to fail.
*/
if (WARN_ON_ONCE(tdx_status != TDX_SUCCESS))
- return;
+ goto out_unlock;
+ atomic_set(pamt_refcount, 0);
+ spin_unlock(&pamt_lock);
free_pamt_array(pamt_pages);
+ return;
+out_unlock:
+ spin_unlock(&pamt_lock);
}
/*
--
2.54.0
^ permalink raw reply related
* [PATCH v7 03/11] x86/virt/tdx: Add tdx_alloc/free_control_page() helpers
From: Rick Edgecombe @ 2026-07-18 1:44 UTC (permalink / raw)
To: bp, dave.hansen, hpa, kas, kvm, linux-coco, linux-doc,
linux-kernel, mingo, nik.borisov, pbonzini, seanjc, tglx,
vannapurve, x86, chao.gao, yan.y.zhao, kai.huang, tony.lindgren,
binbin.wu
Cc: rick.p.edgecombe, Binbin Wu
In-Reply-To: <20260718014500.2231262-1-rick.p.edgecombe@intel.com>
From: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
Add helpers to use when allocating or preparing pages that are handed to
the TDX module for use as control/S-EPT pages, and thus need Dynamic PAMT
adjustments.
The TDX module tracks some state for each page of physical memory that it
might use. It calls this state the PAMT. It includes separate state for
each page size a physical page could be utilized at within the TDX module
(1GB, 2MB, 4KB). In Dynamic PAMT, only the 4KB page size state is
allocated dynamically.
KVM will need to hand pages to the TDX module that it will use at 4KB
granularity. So these pages will need Dynamic PAMT backing added before
they are used by the TDX module, and removed afterwards.
Add tdx_alloc_control_page() and tdx_free_control_page() to handle both
page allocation and Dynamic PAMT installation. Make them behave like
normal alloc/free functions where allocation can fail in the case of no
memory, but free (with any necessary Dynamic PAMT release) always
succeeds. Do this so they can support the existing TDX flows that require
teardowns to succeed.
Also create tdx_pamt_get/put() to handle installing Dynamic PAMT 4KB
backing for pages that are already allocated (such as KVM's use of S-EPT
page tables or guest private memory). Have them take a pfn instead of a
struct page, as future changes will want to use these helpers for guest
pages which are tracked by PFN.
Don't CLFLUSH the Dynamic PAMT pages handed to the TDX module, as is done
for some other SEAMCALLs, as the TDX docs specify that this is only
needed on "TD private memory or TD control structure page".
Since these allocations will be easily user triggerable, account the
memory.
Leave logic to handle concurrency issues for future changes.
AI was used under supervision to collect/apply feedback, split patches,
review code and workshop logs.
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Co-developed-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
Co-developed-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Reviewed-by: Chao Gao <chao.gao@intel.com>
Reviewed-by: Binbin Wu <binbin.wu@linux.intel.com>
Reviewed-by: Tony Lindgren <tony.lindgren@linux.intel.com>
---
v7:
- Comment improvements (Chao)
- Drop unneeded addition of mm.h header include (Binbin)
- Log clarity, code style nits (Sohil)
- Drop Assisted-by tag and cover AI use in log (Dave)
v6:
The major change was to split out the concurrency stuff into a future
patch. It makes it easier to explain in the log. This one is the basic
functionality. Then the simple version of the concurrency and why in the
next patch. Also, to get rid of the dynamically sized DPAMT backing
support which was not based on a formal spec.
Details:
- Split out concurrency stuff into next patch because the log was too long
- Switch to fixed size pamt page arrays (Nikolay)
- Rename tdx_alloc_page()/tdx_free_page() to tdx_alloc_control_page()/
tdx_free_control_page() to reflect control/S-EPT purpose (Sean)
- Take gfp from the caller in tdx_alloc_control_page() (Sean)
- Narrow external API: make tdx_pamt_get()/tdx_pamt_put() static and
export only tdx_alloc_control_page()/tdx_free_control_page() (note:
dropped inline helpers since the discussion on Sean's series resulted
in them not being needed)
- Switch EXPORT_SYMBOL_GPL to EXPORT_SYMBOL_FOR_KVM (Sean)
- Use WARN_ON_ONCE() instead of pr_err() for TDX module failures (Sean)
- Fold alloc_pamt_array()/free_pamt_array() helpers back in and fix the
error-unwind index bug (dpamt_pages[i] -> [j])
- Adjustments after struct page->pfn
- Adjustments from dropping error helper patches
- Make the free error paths more normal
- Drop gfp_t arg in tdx_alloc_control_page(). In the Sean mega v5, it
was really needed because the kvm_mmu_memory_cache had a gfp_t it
needed something to do with. But this was still weird because that
version didn't handle allocating the DPAMT pages as the gfp_t. And in
the end all the callers pass GFP_KERNEL_ACCOUNT. So just drop the arg.
- Log tweaks
---
arch/x86/include/asm/tdx.h | 6 ++
arch/x86/virt/vmx/tdx/tdx.c | 163 ++++++++++++++++++++++++++++++++++++
arch/x86/virt/vmx/tdx/tdx.h | 2 +
3 files changed, 171 insertions(+)
diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h
index d414064436221..86bf37b15c705 100644
--- a/arch/x86/include/asm/tdx.h
+++ b/arch/x86/include/asm/tdx.h
@@ -126,6 +126,12 @@ void tdx_guest_keyid_free(unsigned int keyid);
void tdx_quirk_reset_paddr(unsigned long base, unsigned long size);
+/* Number of PAMT pages to be provided to TDX module per 2MB region of PA */
+#define TDX_DPAMT_ENTRY_PAGE_CNT 2
+
+struct page *tdx_alloc_control_page(void);
+void tdx_free_control_page(struct page *page);
+
struct tdx_td {
/* TD root structure: */
struct page *tdr_page;
diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c
index f395f1fe95093..bfd9928c10249 100644
--- a/arch/x86/virt/vmx/tdx/tdx.c
+++ b/arch/x86/virt/vmx/tdx/tdx.c
@@ -1994,6 +1994,169 @@ bool tdx_supports_dynamic_pamt(const struct tdx_sys_info *sysinfo)
return false;
}
+static int alloc_pamt_array(struct page **pamt_pages)
+{
+ int i, j;
+
+ for (i = 0; i < TDX_DPAMT_ENTRY_PAGE_CNT; i++) {
+ pamt_pages[i] = alloc_page(GFP_KERNEL_ACCOUNT);
+ if (!pamt_pages[i])
+ goto err;
+ }
+
+ return 0;
+
+err:
+ for (j = 0; j < i; j++)
+ __free_page(pamt_pages[j]);
+
+ return -ENOMEM;
+}
+
+static void free_pamt_array(struct page **pamt_pages)
+{
+ for (int i = 0; i < TDX_DPAMT_ENTRY_PAGE_CNT; i++) {
+ /*
+ * Reset pages unconditionally to cover cases
+ * where they were passed to the TDX module.
+ */
+ tdx_quirk_reset_paddr(page_to_phys(pamt_pages[i]), PAGE_SIZE);
+
+ __free_page(pamt_pages[i]);
+ }
+}
+
+/*
+ * Calculate the arg needed for operating on the DPAMT backing for
+ * a given 4KB page.
+ */
+static u64 pamt_2mb_arg(kvm_pfn_t pfn)
+{
+ /* Arg value will specify a 2MB region of physical address space. */
+ unsigned long hpa_2mb = ALIGN_DOWN(pfn << PAGE_SHIFT, PMD_SIZE);
+
+ return hpa_2mb | TDX_PS_2M;
+}
+
+/* Add PAMT backing for the 2MB region surrounding the given pfn. */
+static u64 tdh_phymem_pamt_add(kvm_pfn_t pfn, struct page **pamt_pages)
+{
+ struct tdx_module_args args = {
+ .rcx = pamt_2mb_arg(pfn),
+ .rdx = page_to_phys(pamt_pages[0]),
+ .r8 = page_to_phys(pamt_pages[1]),
+ };
+
+ return seamcall(TDH_PHYMEM_PAMT_ADD, &args);
+}
+
+/* Remove PAMT backing for the 2MB region surrounding the given pfn. */
+static u64 tdh_phymem_pamt_remove(kvm_pfn_t pfn, struct page **pamt_pages)
+{
+ struct tdx_module_args args = {
+ .rcx = pamt_2mb_arg(pfn),
+ };
+ u64 ret;
+
+ ret = seamcall_ret(TDH_PHYMEM_PAMT_REMOVE, &args);
+ if (ret)
+ return ret;
+
+ /* Copy PAMT pages out of the struct per the TDX ABI */
+ pamt_pages[0] = phys_to_page(args.rdx);
+ pamt_pages[1] = phys_to_page(args.r8);
+
+ return 0;
+}
+
+/* Allocate PAMT memory for the given page */
+static int tdx_pamt_get(kvm_pfn_t pfn)
+{
+ struct page *pamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT];
+ u64 tdx_status;
+ int ret;
+
+ if (!tdx_supports_dynamic_pamt(&tdx_sysinfo))
+ return 0;
+
+ ret = alloc_pamt_array(pamt_pages);
+ if (ret)
+ return ret;
+
+ tdx_status = tdh_phymem_pamt_add(pfn, pamt_pages);
+ if (tdx_status != TDX_SUCCESS) {
+ ret = -EIO;
+ goto out_free;
+ }
+
+ return 0;
+
+out_free:
+ free_pamt_array(pamt_pages);
+
+ return ret;
+}
+
+/* Free PAMT memory for the given page */
+static void tdx_pamt_put(kvm_pfn_t pfn)
+{
+ struct page *pamt_pages[TDX_DPAMT_ENTRY_PAGE_CNT] = {};
+ u64 tdx_status;
+
+ if (!tdx_supports_dynamic_pamt(&tdx_sysinfo))
+ return;
+
+ tdx_status = tdh_phymem_pamt_remove(pfn, pamt_pages);
+
+ /*
+ * Don't free pamt_pages as it could hold garbage when
+ * tdh_phymem_pamt_remove() fails. Don't panic/BUG_ON(), as
+ * there is no risk of data corruption, but do yell loudly as
+ * failure indicates a kernel bug, memory is being leaked, and
+ * the dangling PAMT entry may cause future operations to fail.
+ */
+ if (WARN_ON_ONCE(tdx_status != TDX_SUCCESS))
+ return;
+
+ free_pamt_array(pamt_pages);
+}
+
+/*
+ * Return a page that can be gifted to the TDX-Module for use as a "control"
+ * page, i.e. pages that are used for control structures for a given TDX
+ * guest, and thus obtain TDX protections, including PAMT tracking.
+ */
+struct page *tdx_alloc_control_page(void)
+{
+ struct page *page;
+
+ page = alloc_page(GFP_KERNEL_ACCOUNT);
+ if (!page)
+ return NULL;
+
+ if (tdx_pamt_get(page_to_pfn(page))) {
+ __free_page(page);
+ return NULL;
+ }
+
+ return page;
+}
+EXPORT_SYMBOL_FOR_KVM(tdx_alloc_control_page);
+
+/*
+ * Free a page that was gifted to the TDX-Module for use as a control
+ * page. After this, the page is no longer protected by TDX.
+ */
+void tdx_free_control_page(struct page *page)
+{
+ if (!page)
+ return;
+
+ tdx_pamt_put(page_to_pfn(page));
+ __free_page(page);
+}
+EXPORT_SYMBOL_FOR_KVM(tdx_free_control_page);
+
void tdx_sys_disable(void)
{
struct tdx_module_args args = {};
diff --git a/arch/x86/virt/vmx/tdx/tdx.h b/arch/x86/virt/vmx/tdx/tdx.h
index bdfd0e1e337ac..a886c54decaad 100644
--- a/arch/x86/virt/vmx/tdx/tdx.h
+++ b/arch/x86/virt/vmx/tdx/tdx.h
@@ -48,6 +48,8 @@
#define TDH_SYS_CONFIG 45
#define TDH_SYS_SHUTDOWN 52
#define TDH_SYS_UPDATE 53
+#define TDH_PHYMEM_PAMT_ADD 58
+#define TDH_PHYMEM_PAMT_REMOVE 59
#define TDH_SYS_DISABLE 69
/*
--
2.54.0
^ permalink raw reply related
* [PATCH v7 02/11] x86/virt/tdx: Allocate page bitmap for Dynamic PAMT
From: Rick Edgecombe @ 2026-07-18 1:44 UTC (permalink / raw)
To: bp, dave.hansen, hpa, kas, kvm, linux-coco, linux-doc,
linux-kernel, mingo, nik.borisov, pbonzini, seanjc, tglx,
vannapurve, x86, chao.gao, yan.y.zhao, kai.huang, tony.lindgren,
binbin.wu
Cc: rick.p.edgecombe, Binbin Wu
In-Reply-To: <20260718014500.2231262-1-rick.p.edgecombe@intel.com>
From: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
The TDX Physical Address Metadata Table (PAMT) holds data about the
physical memory used by TDX, and must be allocated by the kernel during
TDX module initialization.
The exact size of the required PAMT memory is determined by the TDX module
and may vary between TDX module versions. Currently it is approximately
0.4% of the system memory. This is a significant commitment, especially if
it is not known upfront whether the machine will run any TDX guests.
Each memory region that the TDX module might use needs three separate PAMT
allocations. One for each supported page size (1GB, 2MB, 4KB). The
TDX module supports a new feature designed to reduce PAMT overhead called
Dynamic PAMT. Under Dynamic PAMT the 4KB level is allocated dynamically
during runtime, while the 1GB and 2MB levels remain allocated on TDX
module initialization.
However, in the details, Dynamic PAMT still needs some smaller per 4KB
page scoped data (currently it is 1 bit per page). The TDX module exposes
the number of bits as a separate piece of metadata than the 4KB static
allocation for normal PAMT. Although the size is enumerated differently,
it is handed to the TDX module in the same way the 4KB page size PAMT
allocation is for normal PAMT.
Begin to implement Dynamic PAMT in the kernel by reading the bits-per-page
needed for Dynamic PAMT. Calculate the size needed for the bitmap,
and use it instead of the 4KB size determined for normal PAMT, in the case
of Dynamic PAMT.
The existing metadata reading code was generated by a script, but the
current plan is to stop generating this code, as the script has continued
to need adjustments. So add manually written code and adjust the comment
about it being autogenerated to be more generic. Start to adopt a more
normal kernel code style without the ternary statements and if
conditionals assignments that the auto generated code has.
AI was used under supervision to collect/apply feedback, review code and
workshop logs.
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Co-developed-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Reviewed-by: Vishal Annapurve <vannapurve@google.com>
Reviewed-by: Binbin Wu <binbin.wu@linux.intel.com>
Reviewed-by: Tony Lindgren <tony.lindgren@linux.intel.com>
Reviewed-by: Yan Zhao <yan.y.zhao@intel.com>
---
v7:
- Re-order pamt size calculations for greater readability (Kiryl)
- Move comment to its own line in tdx_supports_dynamic_pamt() (Yan,
Sohil)
- Log tweak (Sohil)
- Make comment in metadata reading more appropriate (Sohil)
- Drop Assisted-by tag and cover AI use in log (Dave)
- Move tdx_supports_dynamic_pamt() to not static inline to reduce
churn in later changes
v6:
- Improve comment (Binbin)
- Log tweaks
- Mark tdmr_get_pamt_bitmap_sz() __init in response to upstream
changes
- Switch to more normal kernel code style, even though it differs from
the existing auto generated code.
---
arch/x86/include/asm/tdx.h | 2 ++
arch/x86/include/asm/tdx_global_metadata.h | 3 +++
arch/x86/virt/vmx/tdx/tdx.c | 29 +++++++++++++++++++--
arch/x86/virt/vmx/tdx/tdx_global_metadata.c | 23 +++++++++++++++-
4 files changed, 54 insertions(+), 3 deletions(-)
diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h
index 89e97d5761d89..d414064436221 100644
--- a/arch/x86/include/asm/tdx.h
+++ b/arch/x86/include/asm/tdx.h
@@ -118,6 +118,8 @@ static inline bool tdx_supports_runtime_update(const struct tdx_sys_info *sysinf
return sysinfo->features.tdx_features0 & TDX_FEATURES0_TD_PRESERVING;
}
+bool tdx_supports_dynamic_pamt(const struct tdx_sys_info *sysinfo);
+
int tdx_guest_keyid_alloc(void);
u32 tdx_get_nr_guest_keyids(void);
void tdx_guest_keyid_free(unsigned int keyid);
diff --git a/arch/x86/include/asm/tdx_global_metadata.h b/arch/x86/include/asm/tdx_global_metadata.h
index 41150d546589c..2a42551fc33cd 100644
--- a/arch/x86/include/asm/tdx_global_metadata.h
+++ b/arch/x86/include/asm/tdx_global_metadata.h
@@ -21,6 +21,9 @@ struct tdx_sys_info_tdmr {
u16 pamt_4k_entry_size;
u16 pamt_2m_entry_size;
u16 pamt_1g_entry_size;
+
+ /* Optional metadata, if Dynamic PAMT is supported */
+ u8 pamt_page_bitmap_entry_bits;
};
struct tdx_sys_info_td_ctrl {
diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c
index e77a5265c2c84..f395f1fe95093 100644
--- a/arch/x86/virt/vmx/tdx/tdx.c
+++ b/arch/x86/virt/vmx/tdx/tdx.c
@@ -510,6 +510,18 @@ static __init int fill_out_tdmrs(struct list_head *tmb_list,
return 0;
}
+static __init unsigned long tdmr_get_pamt_bitmap_sz(struct tdmr_info *tdmr)
+{
+ unsigned long pamt_sz, nr_pamt_entries;
+ int bits_per_entry;
+
+ bits_per_entry = tdx_sysinfo.tdmr.pamt_page_bitmap_entry_bits;
+ nr_pamt_entries = tdmr->size >> PAGE_SHIFT;
+ pamt_sz = DIV_ROUND_UP(nr_pamt_entries * bits_per_entry, BITS_PER_BYTE);
+
+ return PAGE_ALIGN(pamt_sz);
+}
+
/*
* Calculate PAMT size given a TDMR and a page size. The returned
* PAMT size is always aligned up to 4K page boundary.
@@ -578,9 +590,16 @@ static __init int tdmr_set_up_pamt(struct tdmr_info *tdmr,
* Calculate the PAMT size for each TDX supported page size
* and the total PAMT size.
*/
- tdmr->pamt_4k_size = tdmr_get_pamt_sz(tdmr, TDX_PS_4K);
- tdmr->pamt_2m_size = tdmr_get_pamt_sz(tdmr, TDX_PS_2M);
tdmr->pamt_1g_size = tdmr_get_pamt_sz(tdmr, TDX_PS_1G);
+ tdmr->pamt_2m_size = tdmr_get_pamt_sz(tdmr, TDX_PS_2M);
+
+ if (tdx_supports_dynamic_pamt(&tdx_sysinfo)) {
+ /* With Dynamic PAMT, PAMT_4K is replaced with a bitmap */
+ tdmr->pamt_4k_size = tdmr_get_pamt_bitmap_sz(tdmr);
+ } else {
+ tdmr->pamt_4k_size = tdmr_get_pamt_sz(tdmr, TDX_PS_4K);
+ }
+
tdmr_pamt_size = tdmr->pamt_4k_size + tdmr->pamt_2m_size + tdmr->pamt_1g_size;
/*
@@ -1969,6 +1988,12 @@ u64 tdh_phymem_page_wbinvd_hkid(u64 hkid, kvm_pfn_t pfn)
}
EXPORT_SYMBOL_FOR_KVM(tdh_phymem_page_wbinvd_hkid);
+bool tdx_supports_dynamic_pamt(const struct tdx_sys_info *sysinfo)
+{
+ /* To be enabled when kernel is ready. */
+ return false;
+}
+
void tdx_sys_disable(void)
{
struct tdx_module_args args = {};
diff --git a/arch/x86/virt/vmx/tdx/tdx_global_metadata.c b/arch/x86/virt/vmx/tdx/tdx_global_metadata.c
index e49c300f23d43..8393d2aa59dbe 100644
--- a/arch/x86/virt/vmx/tdx/tdx_global_metadata.c
+++ b/arch/x86/virt/vmx/tdx/tdx_global_metadata.c
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0
/*
- * Automatically generated functions to read TDX global metadata.
+ * Functions to read TDX global metadata.
*
* This file doesn't compile on its own as it lacks of inclusion
* of SEAMCALL wrapper primitive which reads global metadata.
@@ -33,6 +33,18 @@ static __init int get_tdx_sys_info_features(struct tdx_sys_info_features *sysinf
return ret;
}
+static __init int get_tdx_sys_info_tdmr_dpamt(struct tdx_sys_info_tdmr *sysinfo_tdmr)
+{
+ int ret;
+ u64 val;
+
+ ret = read_sys_metadata_field(0x9100000100000013, &val);
+ if (!ret)
+ sysinfo_tdmr->pamt_page_bitmap_entry_bits = val;
+
+ return ret;
+}
+
static __init int get_tdx_sys_info_tdmr(struct tdx_sys_info_tdmr *sysinfo_tdmr)
{
int ret = 0;
@@ -129,5 +141,14 @@ static __init int get_tdx_sys_info(struct tdx_sys_info *sysinfo)
ret = ret ?: get_tdx_sys_info_td_ctrl(&sysinfo->td_ctrl);
ret = ret ?: get_tdx_sys_info_td_conf(&sysinfo->td_conf);
+ /*
+ * The kernel supports using TDX without Dynamic PAMT, so
+ * avoid reporting failure if it's not supported. Don't try
+ * to support buggy TDX modules that advertise Dynamic PAMT
+ * but don't expose the metadata.
+ */
+ if (!ret && tdx_supports_dynamic_pamt(sysinfo))
+ ret = get_tdx_sys_info_tdmr_dpamt(&sysinfo->tdmr);
+
return ret;
}
--
2.54.0
^ permalink raw reply related
* [PATCH v7 01/11] x86/virt/tdx: Simplify PAMT layout calculation
From: Rick Edgecombe @ 2026-07-18 1:44 UTC (permalink / raw)
To: bp, dave.hansen, hpa, kas, kvm, linux-coco, linux-doc,
linux-kernel, mingo, nik.borisov, pbonzini, seanjc, tglx,
vannapurve, x86, chao.gao, yan.y.zhao, kai.huang, tony.lindgren,
binbin.wu
Cc: rick.p.edgecombe, Binbin Wu
In-Reply-To: <20260718014500.2231262-1-rick.p.edgecombe@intel.com>
For each memory region that the TDX module might use (called TDMR), three
separate traditional PAMT allocations are needed. There is one for each
supported page size (1GB, 2MB, 4KB). These store information on each page
in the TDMR. In Linux, they are allocated out of one physically contiguous
block, in order to more efficiently use some internal TDX module
bookkeeping resources. So some simple math is needed to break the single
large allocation into three smaller allocations for each page size.
There are some commonalities in the math needed to calculate the base and
size for each smaller allocation, and so an effort was made to share logic
across the three. Unfortunately doing this turned out unnaturally tortured,
with a loop iterating over the three page sizes, only to call into a
function with case statements for each page size. In the future Dynamic
PAMT will add more logic that is special to the 4KB page size, making the
benefit of the math sharing even more questionable.
Three is not a very high number, so get rid of the loop and just duplicate
the small calculation three times. In doing so, setup for future Dynamic
PAMT changes.
Since the loop that iterates over it is gone, further simplify the code by
dropping the array of intermediate size and base storage. Just store the
values to their final locations. Accept the small complication of having
to clear tdmr->pamt_4k_base in the error path, so that tdmr_do_pamt_func()
will not try to operate on the TDMR struct when attempting to free it.
AI was used under supervision to collect/apply feedback, review code and
workshop logs.
Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Reviewed-by: Binbin Wu <binbin.wu@linux.intel.com>
Reviewed-by: Kiryl Shutsemau (Meta) <kas@kernel.org>
Reviewed-by: Chao Gao <chao.gao@intel.com>
Reviewed-by: Yan Zhao <yan.y.zhao@intel.com>
Reviewed-by: Tony Lindgren <tony.lindgren@linux.intel.com>
---
v7:
- Remove accidentital whitespace changes (Kiryl)
- Drop stale sentence in log (Chao)
- Better patch subject (Yan)
- Drop Assisted-by tag and cover AI use in log (Dave)
v6:
- Drop {} by moving a comment (Binbin)
- Log tweaks
---
arch/x86/virt/vmx/tdx/tdx.c | 90 ++++++++++++-------------------------
1 file changed, 28 insertions(+), 62 deletions(-)
diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c
index 42df8ea464c47..e77a5265c2c84 100644
--- a/arch/x86/virt/vmx/tdx/tdx.c
+++ b/arch/x86/virt/vmx/tdx/tdx.c
@@ -514,31 +514,21 @@ static __init int fill_out_tdmrs(struct list_head *tmb_list,
* Calculate PAMT size given a TDMR and a page size. The returned
* PAMT size is always aligned up to 4K page boundary.
*/
-static __init unsigned long tdmr_get_pamt_sz(struct tdmr_info *tdmr, int pgsz,
- u16 pamt_entry_size)
+static __init unsigned long tdmr_get_pamt_sz(struct tdmr_info *tdmr, int pgsz)
{
unsigned long pamt_sz, nr_pamt_entries;
+ const int tdx_pg_size_shift[TDX_PS_NR] = { PAGE_SHIFT, PMD_SHIFT, PUD_SHIFT };
+ const u16 pamt_entry_size[TDX_PS_NR] = {
+ tdx_sysinfo.tdmr.pamt_4k_entry_size,
+ tdx_sysinfo.tdmr.pamt_2m_entry_size,
+ tdx_sysinfo.tdmr.pamt_1g_entry_size,
+ };
- switch (pgsz) {
- case TDX_PS_4K:
- nr_pamt_entries = tdmr->size >> PAGE_SHIFT;
- break;
- case TDX_PS_2M:
- nr_pamt_entries = tdmr->size >> PMD_SHIFT;
- break;
- case TDX_PS_1G:
- nr_pamt_entries = tdmr->size >> PUD_SHIFT;
- break;
- default:
- WARN_ON_ONCE(1);
- return 0;
- }
+ nr_pamt_entries = tdmr->size >> tdx_pg_size_shift[pgsz];
+ pamt_sz = nr_pamt_entries * pamt_entry_size[pgsz];
- pamt_sz = nr_pamt_entries * pamt_entry_size;
/* TDX requires PAMT size must be 4K aligned */
- pamt_sz = ALIGN(pamt_sz, PAGE_SIZE);
-
- return pamt_sz;
+ return PAGE_ALIGN(pamt_sz);
}
/*
@@ -576,15 +566,11 @@ static __init int tdmr_get_nid(struct tdmr_info *tdmr, struct list_head *tmb_lis
* within @tdmr, and set up PAMTs for @tdmr.
*/
static __init int tdmr_set_up_pamt(struct tdmr_info *tdmr,
- struct list_head *tmb_list,
- u16 pamt_entry_size[])
+ struct list_head *tmb_list)
{
- unsigned long pamt_base[TDX_PS_NR];
- unsigned long pamt_size[TDX_PS_NR];
- unsigned long tdmr_pamt_base;
unsigned long tdmr_pamt_size;
struct page *pamt;
- int pgsz, nid;
+ int nid;
nid = tdmr_get_nid(tdmr, tmb_list);
@@ -592,12 +578,10 @@ static __init int tdmr_set_up_pamt(struct tdmr_info *tdmr,
* Calculate the PAMT size for each TDX supported page size
* and the total PAMT size.
*/
- tdmr_pamt_size = 0;
- for (pgsz = TDX_PS_4K; pgsz < TDX_PS_NR; pgsz++) {
- pamt_size[pgsz] = tdmr_get_pamt_sz(tdmr, pgsz,
- pamt_entry_size[pgsz]);
- tdmr_pamt_size += pamt_size[pgsz];
- }
+ tdmr->pamt_4k_size = tdmr_get_pamt_sz(tdmr, TDX_PS_4K);
+ tdmr->pamt_2m_size = tdmr_get_pamt_sz(tdmr, TDX_PS_2M);
+ tdmr->pamt_1g_size = tdmr_get_pamt_sz(tdmr, TDX_PS_1G);
+ tdmr_pamt_size = tdmr->pamt_4k_size + tdmr->pamt_2m_size + tdmr->pamt_1g_size;
/*
* Allocate one chunk of physically contiguous memory for all
@@ -606,25 +590,17 @@ static __init int tdmr_set_up_pamt(struct tdmr_info *tdmr,
*/
pamt = alloc_contig_pages(tdmr_pamt_size >> PAGE_SHIFT, GFP_KERNEL,
nid, &node_online_map);
+
+ /*
+ * tdmr->pamt_4k_base is still zero so the error
+ * path of the caller will skip freeing the pamt.
+ */
if (!pamt)
return -ENOMEM;
- /*
- * Break the contiguous allocation back up into the
- * individual PAMTs for each page size.
- */
- tdmr_pamt_base = page_to_pfn(pamt) << PAGE_SHIFT;
- for (pgsz = TDX_PS_4K; pgsz < TDX_PS_NR; pgsz++) {
- pamt_base[pgsz] = tdmr_pamt_base;
- tdmr_pamt_base += pamt_size[pgsz];
- }
-
- tdmr->pamt_4k_base = pamt_base[TDX_PS_4K];
- tdmr->pamt_4k_size = pamt_size[TDX_PS_4K];
- tdmr->pamt_2m_base = pamt_base[TDX_PS_2M];
- tdmr->pamt_2m_size = pamt_size[TDX_PS_2M];
- tdmr->pamt_1g_base = pamt_base[TDX_PS_1G];
- tdmr->pamt_1g_size = pamt_size[TDX_PS_1G];
+ tdmr->pamt_4k_base = page_to_phys(pamt);
+ tdmr->pamt_2m_base = tdmr->pamt_4k_base + tdmr->pamt_4k_size;
+ tdmr->pamt_1g_base = tdmr->pamt_2m_base + tdmr->pamt_2m_size;
return 0;
}
@@ -655,10 +631,7 @@ static __init void tdmr_do_pamt_func(struct tdmr_info *tdmr,
tdmr_get_pamt(tdmr, &pamt_base, &pamt_size);
/* Do nothing if PAMT hasn't been allocated for this TDMR */
- if (!pamt_size)
- return;
-
- if (WARN_ON_ONCE(!pamt_base))
+ if (!pamt_base)
return;
pamt_func(pamt_base, pamt_size);
@@ -684,14 +657,12 @@ static __init void tdmrs_free_pamt_all(struct tdmr_info_list *tdmr_list)
/* Allocate and set up PAMTs for all TDMRs */
static __init int tdmrs_set_up_pamt_all(struct tdmr_info_list *tdmr_list,
- struct list_head *tmb_list,
- u16 pamt_entry_size[])
+ struct list_head *tmb_list)
{
int i, ret = 0;
for (i = 0; i < tdmr_list->nr_consumed_tdmrs; i++) {
- ret = tdmr_set_up_pamt(tdmr_entry(tdmr_list, i), tmb_list,
- pamt_entry_size);
+ ret = tdmr_set_up_pamt(tdmr_entry(tdmr_list, i), tmb_list);
if (ret)
goto err;
}
@@ -968,18 +939,13 @@ static __init int construct_tdmrs(struct list_head *tmb_list,
struct tdmr_info_list *tdmr_list,
struct tdx_sys_info_tdmr *sysinfo_tdmr)
{
- u16 pamt_entry_size[TDX_PS_NR] = {
- sysinfo_tdmr->pamt_4k_entry_size,
- sysinfo_tdmr->pamt_2m_entry_size,
- sysinfo_tdmr->pamt_1g_entry_size,
- };
int ret;
ret = fill_out_tdmrs(tmb_list, tdmr_list);
if (ret)
return ret;
- ret = tdmrs_set_up_pamt_all(tdmr_list, tmb_list, pamt_entry_size);
+ ret = tdmrs_set_up_pamt_all(tdmr_list, tmb_list);
if (ret)
return ret;
--
2.54.0
^ permalink raw reply related
page: next (older) | prev (newer) | latest
- recent:[subjects (threaded)|topics (new)|topics (active)]
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox