It is cheap to take tdp_mmu_pages_lock in all write-side critical sections.
We already do it all the time when zapping with read_lock(), so it is not
a problem to do it from the kvm_tdp_mmu_zap_all() path (aka
kvm_arch_flush_shadow_all(), aka VM destruction and MMU notifier release).
Signed-off-by: Paolo Bonzini <[email protected]>
---
Documentation/virt/kvm/locking.rst | 6 ++----
arch/x86/kvm/mmu/tdp_mmu.c | 17 ++++++-----------
2 files changed, 8 insertions(+), 15 deletions(-)
diff --git a/Documentation/virt/kvm/locking.rst b/Documentation/virt/kvm/locking.rst
index 3a034db5e55f..381eb0e7d947 100644
--- a/Documentation/virt/kvm/locking.rst
+++ b/Documentation/virt/kvm/locking.rst
@@ -43,10 +43,8 @@ On x86:
- vcpu->mutex is taken outside kvm->arch.hyperv.hv_lock and kvm->arch.xen.xen_lock
-- kvm->arch.mmu_lock is an rwlock. kvm->arch.tdp_mmu_pages_lock and
- kvm->arch.mmu_unsync_pages_lock are taken inside kvm->arch.mmu_lock, and
- cannot be taken without already holding kvm->arch.mmu_lock (typically with
- ``read_lock`` for the TDP MMU, thus the need for additional spinlocks).
+- kvm->arch.mmu_lock is an rwlock and is taken outside
+ kvm->arch.tdp_mmu_pages_lock and kvm->arch.mmu_unsync_pages_lock
Everything else is a leaf: no other lock is taken inside the critical
sections.
diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c
index b9abfa78808a..f61bc842067f 100644
--- a/arch/x86/kvm/mmu/tdp_mmu.c
+++ b/arch/x86/kvm/mmu/tdp_mmu.c
@@ -285,24 +285,19 @@ static void tdp_unaccount_mmu_page(struct kvm *kvm, struct kvm_mmu_page *sp)
* the MMU lock and the operation must synchronize with other
* threads that might be adding or removing pages.
*/
-static void tdp_mmu_unlink_sp(struct kvm *kvm, struct kvm_mmu_page *sp,
- bool shared)
+static void tdp_mmu_unlink_sp(struct kvm *kvm, struct kvm_mmu_page *sp)
{
+ lockdep_assert_held(&kvm->mmu_lock);
+
tdp_unaccount_mmu_page(kvm, sp);
if (!sp->nx_huge_page_disallowed)
return;
- if (shared)
- spin_lock(&kvm->arch.tdp_mmu_pages_lock);
- else
- lockdep_assert_held_write(&kvm->mmu_lock);
-
+ spin_lock(&kvm->arch.tdp_mmu_pages_lock);
sp->nx_huge_page_disallowed = false;
untrack_possible_nx_huge_page(kvm, sp);
-
- if (shared)
- spin_unlock(&kvm->arch.tdp_mmu_pages_lock);
+ spin_unlock(&kvm->arch.tdp_mmu_pages_lock);
}
/**
@@ -331,7 +326,7 @@ static void handle_removed_pt(struct kvm *kvm, tdp_ptep_t pt, bool shared)
trace_kvm_mmu_prepare_zap_page(sp);
- tdp_mmu_unlink_sp(kvm, sp, shared);
+ tdp_mmu_unlink_sp(kvm, sp);
for (i = 0; i < SPTE_ENT_PER_PAGE; i++) {
tdp_ptep_t sptep = pt + i;
--
2.39.1
Hi Paolo,
kernel test robot noticed the following build warnings:
[auto build test WARNING on kvm/queue]
[also build test WARNING on linus/master v6.6-rc3 next-20230929]
[cannot apply to mst-vhost/linux-next kvm/linux-next]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Paolo-Bonzini/KVM-x86-mmu-remove-unnecessary-bool-shared-argument-from-functions/20230929-003259
base: https://git.kernel.org/pub/scm/virt/kvm/kvm.git queue
patch link: https://lore.kernel.org/r/20230928162959.1514661-4-pbonzini%40redhat.com
patch subject: [PATCH 3/3] KVM: x86/mmu: always take tdp_mmu_pages_lock
config: x86_64-buildonly-randconfig-004-20230929 (https://download.01.org/0day-ci/archive/20230929/[email protected]/config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20230929/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> arch/x86/kvm/mmu/tdp_mmu.c:289: warning: Excess function parameter 'shared' description in 'tdp_mmu_unlink_sp'
vim +289 arch/x86/kvm/mmu/tdp_mmu.c
43a063cab325ee7 Yosry Ahmed 2022-08-23 278
a9442f594147f95 Ben Gardon 2021-02-02 279 /**
c298a30c2821cb0 David Matlack 2022-01-19 280 * tdp_mmu_unlink_sp() - Remove a shadow page from the list of used pages
a9442f594147f95 Ben Gardon 2021-02-02 281 *
a9442f594147f95 Ben Gardon 2021-02-02 282 * @kvm: kvm instance
a9442f594147f95 Ben Gardon 2021-02-02 283 * @sp: the page to be removed
9a77daacc87dee9 Ben Gardon 2021-02-02 284 * @shared: This operation may not be running under the exclusive use of
9a77daacc87dee9 Ben Gardon 2021-02-02 285 * the MMU lock and the operation must synchronize with other
9a77daacc87dee9 Ben Gardon 2021-02-02 286 * threads that might be adding or removing pages.
a9442f594147f95 Ben Gardon 2021-02-02 287 */
44f1ce87ebc1ca1 Paolo Bonzini 2023-09-28 288 static void tdp_mmu_unlink_sp(struct kvm *kvm, struct kvm_mmu_page *sp)
a9442f594147f95 Ben Gardon 2021-02-02 @289 {
44f1ce87ebc1ca1 Paolo Bonzini 2023-09-28 290 lockdep_assert_held(&kvm->mmu_lock);
44f1ce87ebc1ca1 Paolo Bonzini 2023-09-28 291
43a063cab325ee7 Yosry Ahmed 2022-08-23 292 tdp_unaccount_mmu_page(kvm, sp);
d25ceb9264364dc Sean Christopherson 2022-10-19 293
d25ceb9264364dc Sean Christopherson 2022-10-19 294 if (!sp->nx_huge_page_disallowed)
d25ceb9264364dc Sean Christopherson 2022-10-19 295 return;
d25ceb9264364dc Sean Christopherson 2022-10-19 296
9a77daacc87dee9 Ben Gardon 2021-02-02 297 spin_lock(&kvm->arch.tdp_mmu_pages_lock);
61f94478547bb4f Sean Christopherson 2022-10-19 298 sp->nx_huge_page_disallowed = false;
61f94478547bb4f Sean Christopherson 2022-10-19 299 untrack_possible_nx_huge_page(kvm, sp);
9a77daacc87dee9 Ben Gardon 2021-02-02 300 spin_unlock(&kvm->arch.tdp_mmu_pages_lock);
a9442f594147f95 Ben Gardon 2021-02-02 301 }
a9442f594147f95 Ben Gardon 2021-02-02 302
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
On Fri, Sep 29, 2023, kernel test robot wrote:
> Hi Paolo,
>
> kernel test robot noticed the following build warnings:
>
> [auto build test WARNING on kvm/queue]
> [also build test WARNING on linus/master v6.6-rc3 next-20230929]
> [cannot apply to mst-vhost/linux-next kvm/linux-next]
> [If your patch is applied to the wrong git tree, kindly drop us a note.
> And when submitting patch, we suggest to use '--base' as documented in
> https://git-scm.com/docs/git-format-patch#_base_tree_information]
>
> url: https://github.com/intel-lab-lkp/linux/commits/Paolo-Bonzini/KVM-x86-mmu-remove-unnecessary-bool-shared-argument-from-functions/20230929-003259
> base: https://git.kernel.org/pub/scm/virt/kvm/kvm.git queue
> patch link: https://lore.kernel.org/r/20230928162959.1514661-4-pbonzini%40redhat.com
> patch subject: [PATCH 3/3] KVM: x86/mmu: always take tdp_mmu_pages_lock
> config: x86_64-buildonly-randconfig-004-20230929 (https://download.01.org/0day-ci/archive/20230929/[email protected]/config)
> compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
> reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20230929/[email protected]/reproduce)
>
> If you fix the issue in a separate patch/commit (i.e. not just a new version of
> the same patch/commit), kindly add following tags
> | Reported-by: kernel test robot <[email protected]>
> | Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
>
> All warnings (new ones prefixed by >>):
>
> >> arch/x86/kvm/mmu/tdp_mmu.c:289: warning: Excess function parameter 'shared' description in 'tdp_mmu_unlink_sp'
>
>
> vim +289 arch/x86/kvm/mmu/tdp_mmu.c
>
> 43a063cab325ee7 Yosry Ahmed 2022-08-23 278
> a9442f594147f95 Ben Gardon 2021-02-02 279 /**
> c298a30c2821cb0 David Matlack 2022-01-19 280 * tdp_mmu_unlink_sp() - Remove a shadow page from the list of used pages
> a9442f594147f95 Ben Gardon 2021-02-02 281 *
> a9442f594147f95 Ben Gardon 2021-02-02 282 * @kvm: kvm instance
> a9442f594147f95 Ben Gardon 2021-02-02 283 * @sp: the page to be removed
> 9a77daacc87dee9 Ben Gardon 2021-02-02 284 * @shared: This operation may not be running under the exclusive use of
> 9a77daacc87dee9 Ben Gardon 2021-02-02 285 * the MMU lock and the operation must synchronize with other
> 9a77daacc87dee9 Ben Gardon 2021-02-02 286 * threads that might be adding or removing pages.
> a9442f594147f95 Ben Gardon 2021-02-02 287 */
The bot is complaining about the kernel doc, i.e. the above @shared documentation
needs to be deleted. Took me a few seconds to understand what the complaint was
about...