]> git.hungrycats.org Git - linux/commit
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)
commitc3647e582e1563d27896c62686d88db76c6cd3a6
treebc70f99bd13d2e1579f22cbaee4dfb0d798ca4f5
parentf92d4f74de4fd6b233929a47f6bb89536c6274c8
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 <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