From: Li RongQing Date: Thu, 28 May 2026 12:40:43 +0000 (-0400) Subject: KVM: x86: Use fls() instead of ffs() for rmaps histogram bucketing X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c3647e582e1563d27896c62686d88db76c6cd3a6;p=thirdparty%2Flinux.git KVM: x86: Use fls() instead of ffs() for rmaps histogram bucketing The kvm_mmu_rmaps_stat_show() function implements a logarithmic histogram to collect statistics on the distribution of pte_list_count sizes. However, it currently uses ffs() (Find First Set), which looks for the least significant set bit. Using ffs() leads to severe statistical distortion for any count that is not a power of two. For example, if a rmap has a pte_list_count of 6 (binary 0110), ffs(6) returns 2, forcing this entry to be erroneously counted towards the bucket representing a count of 2. In contrast, it conceptually belongs to the bucket spanning the 4 to 7 range. Replace ffs() with fls() (Find Last Set) to correctly identify the most significant set bit (effectively computing floor(log2(count)) + 1). This Ensure that intermediate counts are correctly categorized into their appropriate power-of-two order-of-magnitude buckets rather than being severely under-reported. Fixes: 3bcd0662d66f ("KVM: X86: Introduce mmu_rmaps_stat per-vm debugfs file") Signed-off-by: Li RongQing Link: https://patch.msgid.link/20260528124043.2153-1-lirongqing@baidu.com Signed-off-by: Sean Christopherson --- diff --git a/arch/x86/kvm/debugfs.c b/arch/x86/kvm/debugfs.c index 999227fc7c66..0074a56e45b4 100644 --- a/arch/x86/kvm/debugfs.c +++ b/arch/x86/kvm/debugfs.c @@ -121,7 +121,7 @@ static int kvm_mmu_rmaps_stat_show(struct seq_file *m, void *v) lpage_size = kvm_mmu_slot_lpages(slot, k + 1); cur = log[k]; for (l = 0; l < lpage_size; l++) { - index = ffs(pte_list_count(&rmap[l])); + index = fls(pte_list_count(&rmap[l])); if (WARN_ON_ONCE(index >= RMAP_LOG_SIZE)) index = RMAP_LOG_SIZE - 1; cur[index]++;