From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4152A3955F7 for ; Thu, 23 Jul 2026 18:21:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.70 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784830915; cv=none; b=do94cwZlpH3891yd8gXRIOPINZX2B2Teo40UwEy82tlwcOrfmNheo1XtCcz4qDgu2CHyBCJ9uJnPfe9DzcKnWgW9hSbDR59l7xnCVbe3lJcAfVHVEytYhTduINFpfLbdYn5UVXPg3CUVe/EGp8BiLA9b5wf+H+HRMKqpADq5kUw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784830915; c=relaxed/simple; bh=Y8zPSkJhgEwpcGvfvuTG8GkMIhbfsd22DIIsBqUqPLk=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=uZFxQxCqQr5b+c7BFtrM4/xR2HY+eQyoEJ5jdcGj6rpat6a8zGpa7+vd16splwcBAdLXT5uhnErSdyGsQACcJYSlhy4nGmiETilCy+9VvmySwaKPaDv8x4sPbQqoAhSkTH8GTmknU8GumK+qpa5UrWnys7vJlSkJWUy6B2i0Rjg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--smostafa.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=icEULv4Y; arc=none smtp.client-ip=209.85.221.70 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--smostafa.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="icEULv4Y" Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-47f6d70223dso680334f8f.1 for ; Thu, 23 Jul 2026 11:21:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784830905; x=1785435705; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:mime-version:date:from :to:cc:subject:date:message-id:reply-to:content-type; bh=8+lHjdE+3DloA2hsk7g0E45gyAxlw5fhHa47aoUOQ2E=; b=icEULv4Y88XkJi35tbVAbsbS4VGw4eETecNeLbn1w/+ihPvk5if+17YtuHwjglsaM6 FE2dTsn0HulX2/XNafHlH4Hk1l/BqMnmuhjML0wcXLRKioQBk2h4YmzLFWVWIa8AEmnY w1mt0wzz4ohmLHwuvXluuGw8R2TtsqVpc4nkHzIMk9UTJs/d+A9MR4EQVFpO+hujMK6G Vb8TT9lGm9FQIQm6yQBHmM2kVzP4FFTDW1pNuzDNRbmeeHleyNKTUnR7dxGxQMwQh5x+ lXql5RlFF/c3LNm1FD9Mnsx1Us1zwVR+jG/2lIGigXSpqXZmj0xqs2XUVByLNDyQYzcX NIjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784830905; x=1785435705; h=content-type:cc:to:from:subject:message-id:mime-version:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=8+lHjdE+3DloA2hsk7g0E45gyAxlw5fhHa47aoUOQ2E=; b=VYNA9aLHgKd8aFd3BfR/d6I0nYKRiP9Oa/Q6VECYLZ6MN1z1wxzuPLCSb/G8kmYbFU klmnc3T8/WvkZU0Uj5JWFMQ4CNeGDXXs2rbdBxYTjw6X5YLT5ed5EUm2IN0UCWxm2iEV tbwvIdW1iochHaeloVFvvtaNpM19wa3pSF5IG1ZCUUurQvdWhJ5FZbXGuDl1vk+4FZw2 vvhUvxLDPwzK22GE8RrECrdkTToeKr7sO+gIzTnu6ZenNnE6YZgnJuWNKvLZjfHXHqhf tTn4EkKFXinox8Cmpab5dGjnChqEzDDKG33sd4tk1eeUZaprFuVZhXbUYyqnnKOP+9Wh xzbw== X-Gm-Message-State: AOJu0YzX+NCUeTvwyCjBG2KuGHnhDvETom9vDl+29Y8rLkvGhvIP4sCh 5IpJWG2QfOwzP5Ov145a6WKP6WPwaO1++h0VyfipDShstOrOn2xUuo091hR6iRFPQXWASvk9cVP 0zajMgcOk+0znpFxwYxJST8VPebue5Eo85Yt2YHrZfmB2oC+mWnXnXh0U2ixmuJwP5oz+RaYJld tbgY/jge+l+abp9FPGJ00MtwYFJWUm6JPYkAo088tJmp80Sk/8GBWZo1E= X-Received: from wrbdv10.prod.google.com ([2002:a05:6000:d8a:b0:47f:501a:8813]) (user=smostafa job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:2011:b0:46f:7d90:8121 with SMTP id ffacd0b85a97d-47f8d71c689mr5489116f8f.14.1784830904536; Thu, 23 Jul 2026 11:21:44 -0700 (PDT) Date: Thu, 23 Jul 2026 18:21:38 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.55.0.229.g6434b31f56-goog Message-ID: <20260723182140.4025575-1-smostafa@google.com> Subject: [RFC PATCH v2 0/2] KVM: arm64: Add support for BBM level 3 From: Mostafa Saleh To: linux-kernel@vger.kernel.org, kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org Cc: maz@kernel.org, oupton@kernel.org, seiden@linux.ibm.com, joey.gouly@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, catalin.marinas@arm.com, will@kernel.org, vdonnefort@google.com, tabba@google.com, sebastianene@google.com, keirf@google.com, linu.cherian@arm.com, Mostafa Saleh Content-Type: text/plain; charset="UTF-8" This patch series adds support for BBM level 3 to KVM pgtable, it depends on [1] Changes from v1: https://lore.kernel.org/all/20260717130901.2239134-1-smostafa@google.com/ - Limit BBML3 to systems with FWB and DIC and remove race check. Motivation ========== I have been looking into this for the context of: - Page table sharing between the host CPU stage-2 and the SMMUv3 for protected KVM. - Use the pagtable code to populate SMMUv3 stage-2 shadowed page table [2] However, BBM level 3 is still useful for CPU only operations as it avoids intermediately breaking translation. Design ====== Some of the conditions that BBM level 3 will be useful in (RWHZWS): 1) A change to a PTE memory type, shareability, cacheability or OA 2) Changing from block to table 3) Changing from table to block At the moment in the hyp page table code: - For #1) stage2_map_walker_try_leaf(): Replaces a leaf with another one which does not match the same OA/perms/attrs. - For #2) Switch block to table is used from: - kvm_pgtable_stage2_split(): Explicitly splitting a block (used for dirty logging), where a block is replaced by a table with the same attributes. - stage2_map_walk_leaf(): Updating a mapping that is partially part of an existing block. - #3) Does not exist in the code at the moment as coalescing is not supported. The first patch is a preparation to be able to clean up the old pte for BBML3, the second patch adds the main logic. Initially, I encapsulated the full logic of BBM in one function, which was not readable, due to different ordering and dealing with CMO, TLBI. Instead, I kept the logic into 2 functions, where BBML3 is added in the make step. One interesting case, as BBML3 will update the PTE atomically, it can only know it raced with another core at the point of the cmpxchg failing, unlike the SW implementation which locks the PTE first. And as we must issue CMOs to the new mapped page before the update, that means with BBML3 racing cores will issue redundant CMOs, to avoid this, we limit the use of BBML3 to systems with FWB and DIC. Testing ======= This was tested: - C1-Pro cores, unfortunately the version I have does not run upstream, I backported the patches to Android kernel (6.18). - mainline(7.2-rc3) kernel on a Qualcomm X1 with a hacked cpufeature as it does not support BBM, I did not see conflict aborts or TLB corruption. I tested with VHE and protected (hvhe) modes, running VMs (and protected), and running some selftests, that might exercise and stress this path tools/testing/selftests/kvm: - demand_paging_test - memslot_perf_test - memslot_modification_stress_test - dirty_log_test Future work =========== Some other changes that would be useful: - Eagerly install table on block split, we can now replace a block with a fully populated table atomically when we unmap a partial part of the block. - Use BBML3 in the hypervisor stage-1 with nvhe/protected mode to improve preformance and reduce memory usage as now it is forced to use leaf mappings. There is more to support page table sharing (such as dealing with TLB invalidation, coherency...), I submitted a talk to LPC to discuss this further. [1] https://lore.kernel.org/all/20260723044034.2983651-1-linu.cherian@arm.com/ [2] https://lore.kernel.org/linux-iommu/20260715115906.2664882-1-smostafa@google.com/ Mostafa Saleh (2): KVM: arm64: Add stage2_clean_old_pte() KVM: arm64: Support BBM level 3 arch/arm64/kvm/hyp/pgtable.c | 123 +++++++++++++++++++++++++---------- 1 file changed, 87 insertions(+), 36 deletions(-) -- 2.55.0.229.g6434b31f56-goog