KVM: arm64: Fixes for hyp_trace
Subsystems (3)
cover KVM: arm64: Fixes for hyp_trace
Here's a couple of fixes for hyp_trace:
The first one fixes a potential 64 bits overflow in the hypervisor trace clock.
The second may prevent tracing loading for certain buffer_size_kb configuration. This one depends on the allocator, so I have based the series on kvmarm/next.
Vincent Donnefort (2):
KVM: arm64: Prevent 64-bit overflow when computing hyp clock rate
KVM: arm64: Fix bpages backing allocation size in hyp_trace
arch/arm64/kvm/hyp_trace.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-)
base-commit: 8ed245078003cc5f61ca4c8f9920374086f2515c
Signature · 1 line
2.56.0.360.g66cac248cb-goog
PATCH 1/2 KVM: arm64: Prevent 64-bit overflow when computing hyp clock rate
The hypervisor clock rate is delta_cycles * NSEC_PER_SEC / delta_boot. It is computed whenever a deviation occurs. If the delta between two rate updates is big enough, delta_cycles * NSEC_PER_SEC will overflow.
Prevent overflow with mul_u64_u64_div_u64(). And as a bonus, simplify the clock estimation with mul_u64_u32_shr().
--- arch/arm64/kvm/hyp_trace.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-)
arch/arm64/kvm/hyp_trace.c +3 -9
@@ -60,14 +60,8 @@ static void __hyp_clock_work(struct work_struct *work)if (hyp_clock->mult) {u64 err, cur = delta_cycles; -if (WARN_ON_ONCE (cur >= hyp_clock->cyc_overflow64)) { -__uint128_t tmp = (__uint128_t )cur * hyp_clock->mult; - - cur = tmp >> hyp_clock->shift; - }else { - cur *= hyp_clock->mult; - cur >>= hyp_clock->shift; - } +WARN_ON_ONCE (cur >= hyp_clock->cyc_overflow64); + cur =mul_u64_u32_shr (cur, hyp_clock->mult, hyp_clock->shift); cur += hyp_clock->boot; err =abs_diff (cur, boot);
@@ -85,7 +79,7 @@ static void __hyp_clock_work(struct work_struct *work) err / NSEC_PER_USEC); } - rate =div64_u64 (delta_cycles *NSEC_PER_SEC , delta_boot); + rate =mul_u64_u64_div_u64 (delta_cycles,NSEC_PER_SEC , delta_boot);clocks_calc_mult_shift (&hyp_clock->mult, &hyp_clock->shift, rate,NSEC_PER_SEC ,CLOCK_MAX_CONVERSION_S );
Signature · 1 line
2.56.0.360.g66cac248cb-goog
Re: [PATCH v1 1/2] KVM: arm64: Prevent 64-bit overflow when computing hyp clock rate
Quoted 11 lines · Vincent Donnefort' via kernel-team
The hypervisor clock rate is
delta_cycles*NSEC_PER_SEC/delta_boot. It is computed whenever a deviation occurs. If the delta between two rate updates is big enough,delta_cycles*NSEC_PER_SECwill overflow.Prevent overflow with
mul_u64_u64_div_u64(). And as a bonus, simplify the clock estimation withmul_u64_u32_shr().Fixes b22888917fa4 ("KVM: arm64: Sync boot clock with the nVHE/pKVM hyp")Signed-off-by Vincent Donnefort <[email protected]>
Cheers,
/fuad
PATCH 2/2 KVM: arm64: Fix bpages backing allocation size in hyp_trace
In reply to Vincent Donneforthyp_trace_buffer_alloc_bpages_backing() computes nr_bpages as (PAGE_ALIGN(size) / PAGE_SIZE) + 1, whereas trace_remote_alloc_buffer() takes into account the buffer page header.
Align the bpages backing allocation by using __calc_nr_pages_ring_buffer_desc(). There's no need to validate nr_bpages again, it has been before with trace_buffer_desc_size().
--- arch/arm64/kvm/hyp_trace.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/kvm/hyp_trace.c +2 -2
@@ -146,8 +146,8 @@ static void __unshare_page(unsigned long va)static int hyp_trace_buffer_alloc_bpages_backing (struct hyp_trace_buffer *trace_buffer,size_t size) { +unsigned long nr_bpages;size_t backing_size; -int nr_bpages;void *start;int ret;
@@ -157,7 +157,7 @@ static int hyp_trace_buffer_alloc_bpages_backing(struct hyp_trace_buffer *trace_return 0 ; } - nr_bpages = (PAGE_ALIGN (size) /PAGE_SIZE ) +1 ; + nr_bpages =__calc_nr_pages_ring_buffer_desc (size); backing_size =PAGE_ALIGN (sizeof (struct simple_buffer_page ) * nr_bpages *num_possible_cpus ());
Signature · 1 line
2.56.0.360.g66cac248cb-goog
[email protected]
AI reviewRe: [PATCH v1 2/2] KVM: arm64: Fix bpages backing allocation size in hyp_trace
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] tracing: Integer overflow in trace buffer size calculation bypasses capacity check --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] tracing: Integer overflow in trace buffer size calculation bypasses capacity check --
commit 652836a529b433a8c0e0cb4f4308fa9b9556763d
Author: Vincent Donnefort <[email protected]>
KVM: arm64: Fix bpages backing allocation size in hyp_trace
This commit updates the trace buffer backing allocation in hyp_trace to calculate the required number of pages using __calc_nr_pages_ring_buffer_desc(). This appropriately accounts for the buffer page header and relies on trace_buffer_desc_size() for validation.
Quoted 4 lines
arch/arm64/kvm/hyp_trace.c +0
-0
[ ... ]
arch/arm64/kvm/hyp_trace.c static int hyp_trace_buffer_alloc_bpages_backing(struct hyp_trace_buffer *trace_ · line 160
Quoted 6 lines
arch/arm64/kvm/hyp_trace.c +1
-1@@ -157,7 +157,7 @@ static int hyp_trace_buffer_alloc_bpages_backing(struct hyp_trace_buffer *trace_return 0 ; } - nr_bpages = (PAGE_ALIGN (size) /PAGE_SIZE ) +1 ; + nr_bpages =__calc_nr_pages_ring_buffer_desc (size);
[Severity: Low]
Does this calculation overflow if size is near SIZE_MAX?
Looking at __calc_nr_pages_ring_buffer_desc() in include/linux/ring_buffer.h:
static inline unsigned long __calc_nr_pages_ring_buffer_desc(size_t size)
{
/* Takes into account the reader page */
return max(DIV_ROUND_UP(size, PAGE_SIZE - BUF_PAGE_HDR_SIZE), 2UL) + 1;
}If size is extremely large, the DIV_ROUND_UP macro wraps around, causing the function to erroneously return a very small page count (e.g. 0 wrapping to 3 pages).
The commit message notes there is no need to validate nr_bpages again because it was validated by trace_buffer_desc_size(), but could this overflow bypass that check?
Looking at trace_buffer_desc_size() in include/linux/ring_buffer.h:
/* Capped by ring_buffer_desc::nr_page_va */
if (nr_pages > UINT_MAX)
return SIZE_MAX;Because the wrapped nr_pages is small, this capacity check would be circumvented, bypassing the intended -E2BIG rejection. Downstream consumers like hyp_trace_buffer_alloc_bpages_backing(), trace_remote_alloc_buffer(), and __tracing_load() would then operate safely on this truncated page count. This results in a silent failure where the kernel allocates a tiny functional trace buffer instead of returning an error for the impossibly large allocation.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v1 2/2] KVM: arm64: Fix bpages backing allocation size in hyp_trace
In reply to Vincent DonnefortQuoted 10 lines · Vincent Donnefort
hyp_trace_buffer_alloc_bpages_backing()computesnr_bpagesas (PAGE_ALIGN(size)/PAGE_SIZE) + 1, whereastrace_remote_alloc_buffer()takes into account the buffer page header.Align the bpages backing allocation by using
__calc_nr_pages_ring_buffer_desc(). There's no need to validatenr_bpagesagain, it has been before withtrace_buffer_desc_size().Fixes 3aed038aac8d ("KVM: arm64: Add trace remote for the nVHE/pKVM hyp")
Should this have Cc: stable too, like patch 1? The undercount is there since 3aed038aac8d for small buffers, and 442ffa742daa6 ("tracing/remotes: Account for ring buffer page header in size calculation") makes it reachable at ordinary sizes.
Cheers,
/fuad
Re: [PATCH v1 2/2] KVM: arm64: Fix bpages backing allocation size in hyp_trace
Quoted 17 lines · Fuad Tabba
Quoted 10 lines · Vincent Donnefort
hyp_trace_buffer_alloc_bpages_backing()computesnr_bpagesas (PAGE_ALIGN(size)/PAGE_SIZE) + 1, whereastrace_remote_alloc_buffer()takes into account the buffer page header.Align the bpages backing allocation by using
__calc_nr_pages_ring_buffer_desc(). There's no need to validatenr_bpagesagain, it has been before withtrace_buffer_desc_size().Fixes 3aed038aac8d ("KVM: arm64: Add trace remote for the nVHE/pKVM hyp")Should this have Cc: stable too, like patch 1? The undercount is there since
3aed038aac8dfor small buffers, and442ffa742daa6("tracing/remotes: Account for ring buffer page header in size calculation") makes it reachable at ordinary sizes.
Quoted 8 lines
Reviewed-by Fuad Tabba <[email protected]>Tested-by Fuad Tabba <[email protected]>Cheers,
/fuadTo unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
Yeah I wasn't sure, because __calc_nr_pages_ring_buffer_desc() was introduced later.
But I can figure it out once the patch fails to apply and provide the appropriate backport.
Thanks for the review.
Signature · 1 line
Vincent
Quoted 10 lines · Vincent Donnefort
Here's a couple of fixes for
hyp_trace:The first one fixes a potential 64 bits overflow in the hypervisor trace clock.
The second may prevent tracing loading for certain
buffer_size_kbconfiguration. This one depends on the allocator, so I have based the series on kvmarm/next.[...]
Applied to next, thanks!
[1/2] KVM: arm64: Prevent 64-bit overflow when computing hyp clock rate
commit: 6164ce3491bcc3a27a2e2e9260a286eb2df2301e
[2/2] KVM: arm64: Fix bpages backing allocation size in hyp_trace
commit: aac4560e1287c36196fd2d78bf04d0c1c2559e3eCheers,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.