Skip to content

Commit d131f00

Browse files
committed
KVM: Assert slots_lock is held when setting memory regions
Add proper lockdep assertions in __kvm_set_memory_region() and __x86_set_memory_region() instead of relying comments. Opportunistically delete __kvm_set_memory_region()'s entire function comment as the API doesn't allocate memory or select a gfn, and the "mostly for framebuffers" comment hasn't been true for a very long time. Cc: Tao Su <tao1.su@linux.intel.com> Reviewed-by: Xiaoyao Li <xiaoyao.li@intel.com> Reviewed-by: Claudio Imbrenda <imbrenda@linux.ibm.com> Acked-by: Christoph Schlameuss <schlameuss@linux.ibm.com> Link: https://lore.kernel.org/r/20250111002022.1230573-3-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent f81a6d1 commit d131f00

File tree

2 files changed

+4
-9
lines changed

2 files changed

+4
-9
lines changed

arch/x86/kvm/x86.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12793,7 +12793,8 @@ void __user * __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa,
1279312793
struct kvm_memslots *slots = kvm_memslots(kvm);
1279412794
struct kvm_memory_slot *slot;
1279512795

12796-
/* Called with kvm->slots_lock held. */
12796+
lockdep_assert_held(&kvm->slots_lock);
12797+
1279712798
if (WARN_ON(id >= KVM_MEM_SLOTS_NUM))
1279812799
return ERR_PTR_USR(-EINVAL);
1279912800

virt/kvm/kvm_main.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,14 +1926,6 @@ static bool kvm_check_memslot_overlap(struct kvm_memslots *slots, int id,
19261926
return false;
19271927
}
19281928

1929-
/*
1930-
* Allocate some memory and give it an address in the guest physical address
1931-
* space.
1932-
*
1933-
* Discontiguous memory is allowed, mostly for framebuffers.
1934-
*
1935-
* Must be called holding kvm->slots_lock for write.
1936-
*/
19371929
int __kvm_set_memory_region(struct kvm *kvm,
19381930
const struct kvm_userspace_memory_region2 *mem)
19391931
{
@@ -1945,6 +1937,8 @@ int __kvm_set_memory_region(struct kvm *kvm,
19451937
int as_id, id;
19461938
int r;
19471939

1940+
lockdep_assert_held(&kvm->slots_lock);
1941+
19481942
r = check_memory_region_flags(kvm, mem);
19491943
if (r)
19501944
return r;

0 commit comments

Comments
 (0)