]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
KVM: x86: Use fls() instead of ffs() for rmaps histogram bucketing
authorLi RongQing <lirongqing@baidu.com>
Thu, 28 May 2026 12:40:43 +0000 (08:40 -0400)
committerSean Christopherson <seanjc@google.com>
Thu, 28 May 2026 13:09:24 +0000 (06:09 -0700)
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 <lirongqing@baidu.com>
Link: https://patch.msgid.link/20260528124043.2153-1-lirongqing@baidu.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
arch/x86/kvm/debugfs.c

index 999227fc7c6659158051525bcf819ae2f9edc5f0..0074a56e45b44ba8c931278a1ba68fc054eb9e9c 100644 (file)
@@ -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]++;