KVM: arm64: Fast fail transparent_hugepage_adjust() for non-PMD folios

transparent_hugepage_adjust() must walk the userspace stage-1 to detect
if a PMD-level mapping is installed and whether a stage-2 block mapping
can be used. Even though this stage-1 walk is most likely cache hot
(in most cases, the mapping has just been installed), walking a
page-table is still costly.

While we can't be sure by solely looking at the folio if the stage-1 is
a block mapping as userspace has multiple ways to split it, we can infer
from its size if the stage-1 mapping is smaller than PMD_SIZE. Anonymous
memory or memfd faults allocate a big enough folio, as do khugepaged or
MADV_COLLAPSE.

Add a fast fail based on the folio size: whenever a struct page is
present and its folio is not PMD-mappable, bail out early and do not
walk stage-1. This helps when THPs are disabled or under memory
pressure, while adding a folio check on the PMD-hit path.

To measure this walk on 100% PMD misses, function_graph traced
user_mem_abort() and get_user_mapping_size() with THP disabled while a
guest faulted PAGE_SIZE anonymous pages:

  Platform            Setup                get_user_mapping_size()
  ------------------  -------------------  -----------------------
  Apple M2 (big CPU)  nested L1->L2, perf  mean  5.25%, med  2.70%
  Ampere Altra        non-NV, VHE          mean 13.69%, med 12.28%

On non-NV VHE Ampere Altra, get_user_mapping_size() takes 0.26 us mean
(0.24 us median) out of 2.32 us in user_mem_abort(). On nested L1->L2
Apple M2, longer L1 user_mem_abort() handling shrinks the relative
share.

Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
Link: https://patch.msgid.link/20261112113729.2858513-1-vdonnefort@google.com
Signed-off-by: Oliver Upton <oupton@kernel.org>
1 file changed