From patchwork Sat Jan 11 00:20:18 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13935629 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 D7BCE1C27 for ; Sat, 11 Jan 2025 00:20:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736554828; cv=none; b=qd5IOgKdfEbZWkcCd3Cx5zaXWZ323IuOjHPHTcM8xFzE/19XJZcEJ+dkJpuidqgPoF9bVVRSVXQiQPXazPA4FxwzBs8MCY8TxEw/5WDuiOI0rr7BXm2lvdjoWkT7VQ1j6Fs+fuT2rxsPwbSXJLge4V8+0CLgUxiEggfSs3oEliU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736554828; c=relaxed/simple; bh=9bLabAX9qJpvoDdnVlnPIlO/ydni9rNkwiea7ti7JxI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=B7coM0AoQaK0jSxR8LWM+KkrOWhgDHxwcXwX/d72vNE6u4mcyMfzd6Gv6VkD/JQaZHhvu5dzzgd3sOjl33ELEn4rnhOfy3+5lbHSu5MtuonYLebPdfvsVj7NrjVKlJpqgbckp+cNJPCVmDpsjRDb6vHyhiNcB0Vx9Si82+Eidsk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=WbLDyZHq; arc=none smtp.client-ip=209.85.216.74 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--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="WbLDyZHq" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ef79d9c692so6703427a91.0 for ; Fri, 10 Jan 2025 16:20:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736554826; x=1737159626; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=1z0qvvRjGkbjMSPVvMkJ6g5fzjiEL9xgqcuZ1X7Vulw=; b=WbLDyZHqg9eNJFOrH6BAEcN6g1Np/l8p/M5LlnC5fNOWqjZQXy264bLhNLKycWpsAT HqaNvMOMdh6gfLcoRExQR4gsfFNNKbLKX+f9HrLxWu+6nhtfC8KSKoWHjnetwsfQ8ogn xtVZ/fTkDy3rX/Jyzcx8gQKmY85nKhh43dPIRBeYwpZ994FW7uxYK7KZ0ffup1MP/ksf N8A1ntT59nOcMK/y4ZkKqm181MB6PU59viQy50QLTAohD4dvLZm/tmNHUO0+c8wEyloc BY5YDZ0vN21Ppk1FTUHVFN/6qO7EuOQ0WSmjeNPCC8ph4o9B7LXfq6Am0qF+zyWw6zDQ SpCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736554826; x=1737159626; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=1z0qvvRjGkbjMSPVvMkJ6g5fzjiEL9xgqcuZ1X7Vulw=; b=wINIcZdQSFiUa/fh+9Sh2+p4dSdMKXVBz/UOQ3DYtjdx4dxiZDRJleFbHcsJP/SpAm aIKByNkZphSay8uGOzM/8CFPHniR6UrHZ1MRDtt89LSvpC+jCV46uciBLFMZl5Katq0t a+Js0SgxMLT7zPvmdFH1oqPpw8fRSvUKZA+naCwEUID+/k3y1pAA9NP6blYbYVzT0Dly 7MosBr9jb85wdiPbr8kd5uv+XfcuGgR+8v6bWu8FHWV59BvMmJGUy7X8/0wmVppSV+8d 9hLRfEOPP2CztLUvd2OymSDprvc2NkchcAUrL4B6YiSouwVTNGjibp9YZlKuTTuAd6Xo GhEw== X-Gm-Message-State: AOJu0Yyi6AHQEmDwKYVUgugHdmbJQ9qBKkJFGkC6Sbsj6Gaq69G8vazQ T907M+GG0qdCxkJRP7wXzW1WJV0Yu4pDGyvHvSFaiFljZM/Z6ztV1m+PXP8lMYLHzKoRxVqcyiY +jg== X-Google-Smtp-Source: AGHT+IHWbfZBHGSMZfSSplPGo1MbCjmySgtVr8uQK2bZeENb0UAKX/k6hcjnMICqiEw6LKHg1epjouIsGUk= X-Received: from pjbrs12.prod.google.com ([2002:a17:90b:2b8c:b0:2ea:6aa8:c4ad]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2e10:b0:2ea:4a6b:79d1 with SMTP id 98e67ed59e1d1-2f548eb3213mr19097571a91.11.1736554826286; Fri, 10 Jan 2025 16:20:26 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 16:20:18 -0800 In-Reply-To: <20250111002022.1230573-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250111002022.1230573-1-seanjc@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250111002022.1230573-2-seanjc@google.com> Subject: [PATCH v2 1/5] KVM: Open code kvm_set_memory_region() into its sole caller (ioctl() API) From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Tao Su , Claudio Imbrenda , Christian Borntraeger , Xiaoyao Li Open code kvm_set_memory_region() into its sole caller in preparation for adding a dedicated API for setting internal memslots. Oppurtunistically use the fancy new guard(mutex) to avoid a local 'r' variable. Cc: Tao Su Signed-off-by: Sean Christopherson --- include/linux/kvm_host.h | 2 -- virt/kvm/kvm_main.c | 15 ++------------- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 401439bb21e3..7443de24b1d9 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1192,8 +1192,6 @@ enum kvm_mr_change { KVM_MR_FLAGS_ONLY, }; -int kvm_set_memory_region(struct kvm *kvm, - const struct kvm_userspace_memory_region2 *mem); int __kvm_set_memory_region(struct kvm *kvm, const struct kvm_userspace_memory_region2 *mem); void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index de2c11dae231..eb3d0a385077 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2058,25 +2058,14 @@ int __kvm_set_memory_region(struct kvm *kvm, } EXPORT_SYMBOL_GPL(__kvm_set_memory_region); -int kvm_set_memory_region(struct kvm *kvm, - const struct kvm_userspace_memory_region2 *mem) -{ - int r; - - mutex_lock(&kvm->slots_lock); - r = __kvm_set_memory_region(kvm, mem); - mutex_unlock(&kvm->slots_lock); - return r; -} -EXPORT_SYMBOL_GPL(kvm_set_memory_region); - static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, struct kvm_userspace_memory_region2 *mem) { if ((u16)mem->slot >= KVM_USER_MEM_SLOTS) return -EINVAL; - return kvm_set_memory_region(kvm, mem); + guard(mutex)(&kvm->slots_lock); + return __kvm_set_memory_region(kvm, mem); } #ifndef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT From patchwork Sat Jan 11 00:20:19 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13935630 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (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 917479454 for ; Sat, 11 Jan 2025 00:20:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736554830; cv=none; b=O44GmZ3NjmSgd2cFFnGMjITm3BYOEZ68unBpBdyaSxHhc4XL/UcFPpXKR9edoHmwnvJ/4Nm4UF9wHewVopdKw/wwdpkAs1cAuC7o0HE9SduP9WTaO1boHhPpOj27n3GQNzs4jGt1K3m6b1zhO6OX+ZFeSBtcbB6vS9e/L/jezQI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736554830; c=relaxed/simple; bh=TPYIUhk66icLn4klFzPb+dLqocxeMl6d1T8cmhRTu/I=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=BG89Gv9pnOPBKTZsJBuFmlDivAOhURXSo/IGPOO1Ak08Zpa+IRlaQ3CiOuMOdD4CGkLpPc/2U6Tk/0tzjcMNbsr7u8CsPsHf1+nDiY52fgwqJjmdLJVj9Hmb49HEU6+0edb/hp1U67stBJEYZ/iw/9XiojpwnRTcNz7OMR5OYAc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=GrrCx9Ns; arc=none smtp.client-ip=209.85.214.201 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--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="GrrCx9Ns" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-21648ddd461so50546405ad.0 for ; Fri, 10 Jan 2025 16:20:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736554828; x=1737159628; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=895F/8HOIoZ/1RnH4XNznGAUJfeM7NK0SPEGQvgpER8=; b=GrrCx9NsvhM8oo8APdth4Pd8sEjnkDyGQ9LaG+tqk2jn9zgnuUNkYeNZ4keEO9HXHG tshnaSH+WxWNDAC1jzlObwhRJhB5P4xf85z0FFeoGehs7bE95/wbaf3jdeqfio4vZHia SA7LinFBIQjf5JyA/uUx3IQcY4C0WF2z9YU/87vy8klX/zlbbSbiP9toeoURbP4EDYF6 M5ubTtfu4K00DV2qWcyy4AuEJPwvJtaIYvOkPAif8CSDTL2iyOdzS7a0LJmL77uDKcEd Texddq4QwsHcgDBIf3OcLCRGD05bUGXxaFgAOb/krTeIT0dceD3nVRaJ/E4quxek7Yuc QoqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736554828; x=1737159628; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=895F/8HOIoZ/1RnH4XNznGAUJfeM7NK0SPEGQvgpER8=; b=rdt+nLga+pw1EkmkmipUaa6kzP4GTMzKJUNYdghV7wl4NxLe48AKXafA6L0Y/nualS Dk+Lz4hwocbdmun8ILPHFzKe8AISNkzD6eA9uT3e6oDKFDpcF3Q0AxE7KxloQYpl9NtS PQKOxqbsJbGodkXGAIjxzmRPqEuzkNBKGKLmGFz324TWtwzZ42sc48pyMW9uh4Ear49n OsyNhXkB0FmT/GwWubzkIbXPlGlCpFCH3hG6M6gbWWDIaMj6T2I7eF5dY4Oj6OR1L6pc TJL/qDRtu7EZbPNQU1jfCEFaiefy2Rg3maXFy9fWrZVQaPcDGNwqVXumN5287vtvyTIh GSJQ== X-Gm-Message-State: AOJu0YzTdrxUeBfJTuf4TfJBibExYNts0jv1tnVJAX/HXxAJ7/2qprEe 2l0qhT5CmI6b/PHm0k29WxUENBpS/osA5/X/wUwmAfFiEWCAVrx1I3xuwFoBp7FgTuc1v3nab4D 4UA== X-Google-Smtp-Source: AGHT+IHCA/ETl/3gcC8jDf9XvD+nlajF/u7s1VZSF3c8VsdM1o/ROmNiXPwd+PqoNaqHkoH9I4bjiYtp/o4= X-Received: from pfbfa27.prod.google.com ([2002:a05:6a00:2d1b:b0:725:d033:af87]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:918d:b0:1e0:f05b:e727 with SMTP id adf61e73a8af0-1e88d0e63a6mr20310025637.2.1736554827806; Fri, 10 Jan 2025 16:20:27 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 16:20:19 -0800 In-Reply-To: <20250111002022.1230573-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250111002022.1230573-1-seanjc@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250111002022.1230573-3-seanjc@google.com> Subject: [PATCH v2 2/5] KVM: Assert slots_lock is held when setting memory regions From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Tao Su , Claudio Imbrenda , Christian Borntraeger , Xiaoyao Li 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 Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 3 ++- virt/kvm/kvm_main.c | 10 ++-------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 1b04092ec76a..a861287a67bd 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -12793,7 +12793,8 @@ void __user * __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, struct kvm_memslots *slots = kvm_memslots(kvm); struct kvm_memory_slot *slot; - /* Called with kvm->slots_lock held. */ + lockdep_assert_held(&kvm->slots_lock); + if (WARN_ON(id >= KVM_MEM_SLOTS_NUM)) return ERR_PTR_USR(-EINVAL); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index eb3d0a385077..7d25b50cb298 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1926,14 +1926,6 @@ static bool kvm_check_memslot_overlap(struct kvm_memslots *slots, int id, return false; } -/* - * Allocate some memory and give it an address in the guest physical address - * space. - * - * Discontiguous memory is allowed, mostly for framebuffers. - * - * Must be called holding kvm->slots_lock for write. - */ int __kvm_set_memory_region(struct kvm *kvm, const struct kvm_userspace_memory_region2 *mem) { @@ -1945,6 +1937,8 @@ int __kvm_set_memory_region(struct kvm *kvm, int as_id, id; int r; + lockdep_assert_held(&kvm->slots_lock); + r = check_memory_region_flags(kvm, mem); if (r) return r; From patchwork Sat Jan 11 00:20:20 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13935631 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (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 470971BC20 for ; Sat, 11 Jan 2025 00:20:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736554831; cv=none; b=eZH9mIeUjrsSkcMIvPZRpQYu+tnOMlrWMGUvwtf8DTKAs8uyBeu+oCE+0YOSoVKNQ+fs0wUBnKrxgYFq+hjVDYCJNqmYUld1Pfqt8g/ROWqioxJev3Dx1jogMMxhXAu4tMAaWVUHemR/hZjImRiiw5A3Pe20mA7OlCcyEsaN/yU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736554831; c=relaxed/simple; bh=zx4dqWjk0j1kl0acCQQPeLTqubWJGVskp3jgLunn0Fk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=WSUkdO6+7VKhyPC1WpPf0er9+EP3jjFEg80HnOh8eF2rRjuCgah5qwWdTs+y99GOIhxKrI7sw+Wt7iqmByHyMFxCz8opDR5eyftKqCaqAQby4qwY0p7eQ7ZSiJeknoFLp0lqJLCT61RjTYiB0XEb/rAW1FGPJpRrtYUV+JpTYrU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=V5qmcVK9; arc=none smtp.client-ip=209.85.214.201 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--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="V5qmcVK9" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2164fad3792so44085425ad.0 for ; Fri, 10 Jan 2025 16:20:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736554829; x=1737159629; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=eB8gBJuxbpe5X48d9PLQPVBWlx8r3t3ZJrmp2uj4RCY=; b=V5qmcVK91kTOaaFafo+noRd7hFTFGWYgRGNy6TrYaZonLEafH4g5n9sJ3a1sSHz499 oHUYvpLiLAU9rA+oXinLWea8SaxhtvR0t+CGvWNj1bxde4rc7Vxn8Wr3c92um6nbf6Id 9NztDY5t/5lRrFMHSOEtS+rTLGpbpYLpKI6PLFbVkqG+mL9CmDCsiBe0JSqMpYmgZhII btCR2xRz9EA31tgso4nXr4tU+YYGZOww00d4wDB79ySLi6yxXTzF/13rxT7drAq474/q 6LtjAsEy++7z0MtvbkZ/+3C3Cs91oxozAC6ttVv/VvB84zMWC3Of2a8XCKm5oKvTyONe R40g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736554829; x=1737159629; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=eB8gBJuxbpe5X48d9PLQPVBWlx8r3t3ZJrmp2uj4RCY=; b=mpoj9IlHZoH56aexEdcZwwoTSLLQl5+8j/AJ9gR83nqsIS5ggyqMz+h+8GkcznvwFV M/i86+LEwRYvtrpR3tROju5hkNS6jWZqSjrOLx8O8k9wFAjLczohu9vloQvOQWWEWV8F 2yOGBR4s2sgLA/7CXXwC/oA+V5vA3JevB2SNVaLN1Ag0BgNek//ufrVbgy6WROyT9Lfl WE02eQyw5Ni08qxfU4GKQ/R5Fbh8mBvZFzc/xzOWCPqYeM+5PN//wbucv9H+RGf293FY vMaIAecBTNWt2MVfIWsKrhHG4o8UQhUMzckmqZig8WkNJN8u7K/vusmAUYTm8/Dr4jQ2 HbfQ== X-Gm-Message-State: AOJu0YwP0/fVSHn/IfI+V238zcO9GO7VviK1iU6LPCxvnKvdHmqnyMo9 9fYIIWGCKG3nMRcA5z6j/d+ZQqjLn5gmjZJ9JNrEA5ifC3b9MCfySUgeT144YtrXOyk+/2yFxLO KIA== X-Google-Smtp-Source: AGHT+IGNsgJP7koT39cFwI4Yg+3wXDCAQbpwSVw/Id0arVP5ZQq/8yJYYSBpdXsIn/H3/U0UxdATkYQDmUo= X-Received: from pfjf21.prod.google.com ([2002:a05:6a00:22d5:b0:725:f1d9:f706]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:c88b:b0:1e1:a693:d5fd with SMTP id adf61e73a8af0-1e88d128f5dmr21173841637.25.1736554829616; Fri, 10 Jan 2025 16:20:29 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 16:20:20 -0800 In-Reply-To: <20250111002022.1230573-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250111002022.1230573-1-seanjc@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250111002022.1230573-4-seanjc@google.com> Subject: [PATCH v2 3/5] KVM: Add a dedicated API for setting KVM-internal memslots From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Tao Su , Claudio Imbrenda , Christian Borntraeger , Xiaoyao Li Add a dedicated API for setting internal memslots, and have it explicitly disallow setting userspace memslots. Setting a userspace memslots without a direct command from userspace would result in all manner of issues. No functional change intended. Cc: Tao Su Cc: Claudio Imbrenda Cc: Christian Borntraeger Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 2 +- include/linux/kvm_host.h | 4 ++-- virt/kvm/kvm_main.c | 15 ++++++++++++--- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index a861287a67bd..36b5d06e3904 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -12827,7 +12827,7 @@ void __user * __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, m.guest_phys_addr = gpa; m.userspace_addr = hva; m.memory_size = size; - r = __kvm_set_memory_region(kvm, &m); + r = kvm_set_internal_memslot(kvm, &m); if (r < 0) return ERR_PTR_USR(r); } diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 7443de24b1d9..8707d25a2e5b 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1192,8 +1192,8 @@ enum kvm_mr_change { KVM_MR_FLAGS_ONLY, }; -int __kvm_set_memory_region(struct kvm *kvm, - const struct kvm_userspace_memory_region2 *mem); +int kvm_set_internal_memslot(struct kvm *kvm, + const struct kvm_userspace_memory_region2 *mem); void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot); void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen); int kvm_arch_prepare_memory_region(struct kvm *kvm, diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 7d25b50cb298..e1be2e4e6c9f 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1926,8 +1926,8 @@ static bool kvm_check_memslot_overlap(struct kvm_memslots *slots, int id, return false; } -int __kvm_set_memory_region(struct kvm *kvm, - const struct kvm_userspace_memory_region2 *mem) +static int __kvm_set_memory_region(struct kvm *kvm, + const struct kvm_userspace_memory_region2 *mem) { struct kvm_memory_slot *old, *new; struct kvm_memslots *slots; @@ -2050,7 +2050,16 @@ int __kvm_set_memory_region(struct kvm *kvm, kfree(new); return r; } -EXPORT_SYMBOL_GPL(__kvm_set_memory_region); + +int kvm_set_internal_memslot(struct kvm *kvm, + const struct kvm_userspace_memory_region2 *mem) +{ + if (WARN_ON_ONCE(mem->slot < KVM_USER_MEM_SLOTS)) + return -EINVAL; + + return __kvm_set_memory_region(kvm, mem); +} +EXPORT_SYMBOL_GPL(kvm_set_internal_memslot); static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, struct kvm_userspace_memory_region2 *mem) From patchwork Sat Jan 11 00:20:21 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13935632 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (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 2F2944438B for ; Sat, 11 Jan 2025 00:20:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736554833; cv=none; b=WRD77LMV1p+T4eC78tkdx+JoXk3UbPVS8SR3wX5G3BO0hkRoLM5wNL5leNk6JFFg1UCS3Sqsc3FMRsm3HFGU86409U7yk+Y83g0OVGMyrE2oNl790ux1DeBczO5KzSlvKs7HykKrf5kJgXSixWS/WF3mTyt++MMFFTLPUwLAY7I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736554833; c=relaxed/simple; bh=srxRFWXBvHrISysQpsmByXcQcCBsfGALuZmTGX0HrTU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=vFOGX4VwxElZFaCWRhkVetVsg2q/kB7fs1dK08SCU5qGqwXjpuR/CDo4CxjDWkQfp50MMaP54nSFEmawCqNU99qj7ItOS/7apXXmQ5sDJerecxJtHxpnvm3UEUHAM9l/Q9eNark5PktY+OZBOf7tz1P3Z8PBLPC2NeLFQsI6jg8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=o6FhCsFs; arc=none smtp.client-ip=209.85.214.201 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--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="o6FhCsFs" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-216430a88b0so49519255ad.0 for ; Fri, 10 Jan 2025 16:20:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736554831; x=1737159631; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=D5gnrYLh2bOfd2KfhYLYfK3u+PPOZNNTRLHmwhfYjRg=; b=o6FhCsFsJh7FiBLK1FRCey6c6hE4Kaakc7Xx3xIxqg3dA9OuUl+oAUaZFsDWI9GFJr 84vDdH0whrEsq5zp1RKlt/hm77VwS2mDcIDmOx35AZpH20v7VKY9OoW/DHwmQxwA+5s6 wLz/Hpoo/0SJZ5Ge1s5vB3Zo3qQAvkBQJilbwYRicZvhJjJFT0jIt2TCvMKojTQg3eiW wLznb5+IQSxAJNxxFYgistnSdhZcdHmu7peO7iA6dJreAAXecI7WksrpE0pkAgraWmO0 VM+m9eqDKMZbAzlZ2pFq+5neS2emZE277L3KlE9kaIft7uIxAryZo59KJLVn0e/89I/o YQqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736554831; x=1737159631; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=D5gnrYLh2bOfd2KfhYLYfK3u+PPOZNNTRLHmwhfYjRg=; b=LmE+XDuDGqs4fSBUMdu/CAu9gBCPzjF1M1UfbrMYWkS6UbIyDsQt3OtSYv+STWj5Ze UJvqafM+nDtiYIBK40xfqMDeDbQ1aCjNFDgbeTZJ6SDUSe6Y9akUErvdzh8J4MWpD9Jz LwgRiSmqOFH0R/KTwZiOudSdINuyquMkhh13vY3JDOPmaMURksRfNSLjCWrQIfKrmE9X HkZz5aOXYgMdOKjw2q3Msdv8fFjMxgNhFDhfhS8xeTIOnqwniMqlllkvA8/1g3mlOKB6 VF6VIPK29jkLIuPIOJH/n7TM1ZDsMQ5e5+4qFN4BhZwjrU2HVmvAL9fvqN3FoXk6HkAp AZaA== X-Gm-Message-State: AOJu0YzqNZuKVSdupIyt5N5uctwnO+J2uRVs4Eb2wr99XfNfD0P+NnhO XywQPi/jK903AOqTF+RjJjAFFLyK50MKx+obaTrbLzVNIMahg/x90FFOcR/WCANs5CJsFViVB/v +Jg== X-Google-Smtp-Source: AGHT+IGxsPV3oLiaWtBJNyEMPx1oG/v/eJjoOZVmnLqym7xeU5cTCoGfqlvryySu/bDBti/qJyF0gohE5Dk= X-Received: from pgbfe25.prod.google.com ([2002:a05:6a02:2899:b0:801:e378:a64a]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:78a0:b0:1e1:e2d9:44d9 with SMTP id adf61e73a8af0-1e88cfd3cccmr17252164637.27.1736554831472; Fri, 10 Jan 2025 16:20:31 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 16:20:21 -0800 In-Reply-To: <20250111002022.1230573-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250111002022.1230573-1-seanjc@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250111002022.1230573-5-seanjc@google.com> Subject: [PATCH v2 4/5] KVM: x86: Drop double-underscores from __kvm_set_memory_region() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Tao Su , Claudio Imbrenda , Christian Borntraeger , Xiaoyao Li Now that there's no outer wrapper for __kvm_set_memory_region() and it's static, drop its double-underscore prefix. No functional change intended. Cc: Tao Su Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 2 +- include/linux/kvm_host.h | 2 +- virt/kvm/kvm_main.c | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 36b5d06e3904..82f389e3910d 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -12928,7 +12928,7 @@ static int kvm_alloc_memslot_metadata(struct kvm *kvm, /* * Clear out the previous array pointers for the KVM_MR_MOVE case. The - * old arrays will be freed by __kvm_set_memory_region() if installing + * old arrays will be freed by kvm_set_memory_region() if installing * the new memslot is successful. */ memset(&slot->arch, 0, sizeof(slot->arch)); diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 8707d25a2e5b..dcb59d6e8acb 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1183,7 +1183,7 @@ struct kvm_memory_slot *kvm_vcpu_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn * -- just change its flags * * Since flags can be changed by some of these operations, the following - * differentiation is the best we can do for __kvm_set_memory_region(): + * differentiation is the best we can do for kvm_set_memory_region(): */ enum kvm_mr_change { KVM_MR_CREATE, diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index e1be2e4e6c9f..ecd4a66b22f3 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1926,8 +1926,8 @@ static bool kvm_check_memslot_overlap(struct kvm_memslots *slots, int id, return false; } -static int __kvm_set_memory_region(struct kvm *kvm, - const struct kvm_userspace_memory_region2 *mem) +static int kvm_set_memory_region(struct kvm *kvm, + const struct kvm_userspace_memory_region2 *mem) { struct kvm_memory_slot *old, *new; struct kvm_memslots *slots; @@ -2057,7 +2057,7 @@ int kvm_set_internal_memslot(struct kvm *kvm, if (WARN_ON_ONCE(mem->slot < KVM_USER_MEM_SLOTS)) return -EINVAL; - return __kvm_set_memory_region(kvm, mem); + return kvm_set_memory_region(kvm, mem); } EXPORT_SYMBOL_GPL(kvm_set_internal_memslot); @@ -2068,7 +2068,7 @@ static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, return -EINVAL; guard(mutex)(&kvm->slots_lock); - return __kvm_set_memory_region(kvm, mem); + return kvm_set_memory_region(kvm, mem); } #ifndef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT From patchwork Sat Jan 11 00:20:22 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13935633 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (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 C0EF280BEC for ; Sat, 11 Jan 2025 00:20:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736554835; cv=none; b=TeQYopl4V/rBbZsron4VTwoteT3LaYUJI5mXmN8g2ZnxPjzm9c6E2AxULGb8uR1gvia5+nAFp7SbuEYu1QmImIE/4WZpNVzn4gpl7rzlioyab0vYOKN29gtr2Npoh9cygexTimb7AGoQh/L48ByaxfcfbVodV/FSChY2hrfvFvU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736554835; c=relaxed/simple; bh=kRQEs5EcMwZCxgmlCwNEQYT3iaJsSfh9i/u7XBtRt2g=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fFU6Le1u9IKBzJM+BA7ibKDn3pIaaMgMHR4xRoLk+zXmFR5LUMVmCFCKPRcla3D9XXA1uU3RkE05i2ajtySDWFUSrDXrEJw+fThIY7RZhnChR5Wo6pHupokayIaQZUepb1ybeYXCQzOVnqx5/OAvs3slP+8W/VdaTZMcQj9NSzI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Voj7EdZN; arc=none smtp.client-ip=209.85.214.201 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--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Voj7EdZN" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2163dc0f5dbso49682185ad.2 for ; Fri, 10 Jan 2025 16:20:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736554833; x=1737159633; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=OdVnAyDt165LR+waupi1EXnTBJ4UpDIfS7w0uoWpBks=; b=Voj7EdZNk9C17Ts9aIiBgSWs4X++4RnelQ7knR/vyGTky5JRlkewnVRU1Y6ki57gdc kGvtDIHs7laaes681IcWLZcHnKerbgnmWLAo8G7PfUcCsTa5yDF8150ykJQ7U3DbNy1h FwdcuJnb/csxTWuSGqoieHxPn20QdQdjbNL2XXc6ovajspLy6DJNb3R8UFD9Re7XtXCA 7RDPYquzrNn8+lxhrJBBABGdA+AtQSgWbS78i+PfF40e/NjC/pcUJa/VXsdm3nVTEIlo P8hnoIR/Xm+Sk3Yg1LXuKd3IV5JCNusPVY9KxTHiYdR2GHJAbaE2DDbNTiLvwqDImgt1 Ad8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736554833; x=1737159633; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=OdVnAyDt165LR+waupi1EXnTBJ4UpDIfS7w0uoWpBks=; b=n26toyc3eOvoJP7BPDrXkcwNbC7jiMuoOyFNfantdbzA/baOkF/aosfU6TQc1+ETEi dwzP3Q/KJuTS3TKyT7fW6ORHG5KKOtOR+pSfSnq77GRQUWv3jR1dGVGDLworj1WpyZbj hiTTBjy4ndOQEvFuCsoUke3b/Vh/iZPPX73fUF1WQJAQxywmIlG3oo9FR0ptRwchOOHU sz0wJgRIwc0ya+6tL7t/CI/nq1w8qXbu+Wk1Bs3nx2rJjGeDaeJDCVLVGPfZjFMhAzTH nCyM+j1DyTIg6/ZcB9gn5EUBgflULZWduTC6uXiBTGVEjB+WeHoRCbJ4vPRtDzotWh8q JB4w== X-Gm-Message-State: AOJu0YyHUJ6vM/JYjO6U/5KJQV2DA77DtmXiykFMG3MtZDlCOzV45KFt cWc1aMejvHS6Qm57DCmG2xp//VqTfHAo3Idi0+7ntHm4CW/lPCcGxtKWSuruuxQbTkJWqm5UjEd RJQ== X-Google-Smtp-Source: AGHT+IH+/qlBvGc3CjvrIEEaolrvw2YpHtzofLqwTRvUtZ1gocbkP6ns6eR/fLbBFKKAUO79Mci9eOI/HTQ= X-Received: from pfgu31.prod.google.com ([2002:a05:6a00:99f:b0:725:ceac:b481]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:928c:b0:727:3935:dc83 with SMTP id d2e1a72fcca58-72d21fb1e07mr17528652b3a.10.1736554832973; Fri, 10 Jan 2025 16:20:32 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Jan 2025 16:20:22 -0800 In-Reply-To: <20250111002022.1230573-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250111002022.1230573-1-seanjc@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250111002022.1230573-6-seanjc@google.com> Subject: [PATCH v2 5/5] KVM: Disallow all flags for KVM-internal memslots From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Tao Su , Claudio Imbrenda , Christian Borntraeger , Xiaoyao Li Disallow all flags for KVM-internal memslots as all existing flags require some amount of userspace interaction to have any meaning. In addition to guarding against KVM goofs, explicitly disallowing dirty logging of KVM- internal memslots will (hopefully) allow exempting KVM-internal memslots from the KVM_MEM_MAX_NR_PAGES limit, which appears to exist purely because the dirty bitmap operations use a 32-bit index. Cc: Xiaoyao Li Cc: Claudio Imbrenda Cc: Christian Borntraeger Signed-off-by: Sean Christopherson --- virt/kvm/kvm_main.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index ecd4a66b22f3..a8a84bf450f9 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2057,6 +2057,9 @@ int kvm_set_internal_memslot(struct kvm *kvm, if (WARN_ON_ONCE(mem->slot < KVM_USER_MEM_SLOTS)) return -EINVAL; + if (WARN_ON_ONCE(mem->flags)) + return -EINVAL; + return kvm_set_memory_region(kvm, mem); } EXPORT_SYMBOL_GPL(kvm_set_internal_memslot);