From patchwork Fri Aug 2 20:49:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13751983 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (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 8282461FE9 for ; Fri, 2 Aug 2024 20:50:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722631809; cv=none; b=OmUhEfQrcq89RJCL5bOe+6K9LWg7l2TFjtuISB/j8JvODyKCpmGMZDF0a1SekKD43AT1mv+cdl3Rb8Qy9q29sTZ06gw8Iqev9T+FMcUookrz1gijqsiC30QjEcCnPMxIEmlUPHxNtB6xnkZnjE4oQaZeGOxE3iugeYWz+/F9hUE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722631809; c=relaxed/simple; bh=sBOo69kX0YSryhxxIcP7p5qc/h2v/Z1JZ3udeP11lhk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=YZCUMRmBXy486sqZbCX3Ot8lnFK3tpGX+VaaHxm4/QWGXWhtiZv+MVtGCxets07AeRvAYtfxl7eh6CQQV9x4sn1cJEBC78hnGWK/Mx+ll3TVDf+IQrdDHtZy8NSDG3oMTaMa+vVvpMWAQ9fxHs7JV6QcmcOIlfs1nH2rwOuVXbM= 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=Q2vQpciJ; arc=none smtp.client-ip=209.85.215.202 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="Q2vQpciJ" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-721d20a0807so8648546a12.1 for ; Fri, 02 Aug 2024 13:50:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722631808; x=1723236608; 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=O9gMVDnHqOtk8HQWACGShA2WLrj6CrVEJQ1Rnn6+n04=; b=Q2vQpciJXWKeda903RP1ApRBHHnwXCeZMC0TnkqRpsPVO7XOGehAlGOYqEXCO+ReaJ 5/ofGa0ihuARQPbJoRNYAVdr9HDaJSq6Od5jkNayPu1sRqOGtIMZSFcEMai+Qt2uRnJb 9n7usvWB5NXdavwFQBAh84iBQBS8UQY3351a2oyK9SKB4Wft6Hp+7CRjAKDs5klHYUn1 zceg95trXDojyAZf7L3DY8qmBFzpMJxYng9FHe5ZEnrypbP2Z8BmE4XA/UVyaqJd3OpI imTRGj1Q2sDf20jLYvhGpec82BAieRXjq5/TnzqBnQJG8G2g6AvaXmoq9BHtW4MBNw0F Uh8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722631808; x=1723236608; 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=O9gMVDnHqOtk8HQWACGShA2WLrj6CrVEJQ1Rnn6+n04=; b=ehV4wD5zuUkpxJ0UgeeqPU0YP9hJjVjI8EAhSducxxlC0JegKdvWSKE3fCAwAsSN+i boEwwaLcCAhSxXMw4PasS35zh1+/2hzVF0tJC11KxIrP5xlAHdD8fu+PHEGBoToVtUFR 757G5zF9/B9lWvWmfDHxp+OQNGLUl/q/o96UfF609kSZoNsW4mbgz6O6cvi3nK+qFE67 AfSEVhKgMrTSJOLCuGFYW4RJXykA8kahHq3EeQWfYuFHy1zJQFXLC52/ozxUXHK+Yrss t4ORzDla5KhLx4PJ8dLS2PIWOeIcrvCxEL/iIIGIWtN7cPJCWbIn/LuVE7ieP1ogOttP QJ6g== X-Gm-Message-State: AOJu0Yz0BBFFEgNDBdGCIUYiibv6xTgZa81C0P7ngYjpQKglLqmbkXw0 pQKrxaim4Ud4Mcg6bblian/j7klWh/2KheO8ytTTOMRMXq+vWpdIvFP1gQkvAd5ExbUh3vXTJVT PVA== X-Google-Smtp-Source: AGHT+IFIO4UD2Q8xM+v/fcdA/e1KXIqTtO1/9sFBl4VhJt9M8ln8Nh08MMSytma4Tf1IuwJIQtRi3c99cHA= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a63:798f:0:b0:7a1:4462:412e with SMTP id 41be03b00d2f7-7b749235e63mr8632a12.9.1722631807730; Fri, 02 Aug 2024 13:50:07 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 2 Aug 2024 13:49:58 -0700 In-Reply-To: <20240802205003.353672-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240802205003.353672-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc2.264.g509ed76dc8-goog Message-ID: <20240802205003.353672-2-seanjc@google.com> Subject: [PATCH 1/6] 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 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. 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 689e8be873a7..b341d00aae37 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1179,8 +1179,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 d0788d0a72cc..0557d663b69b 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2105,25 +2105,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 Fri Aug 2 20:49:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13751984 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (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 80D0D78C60 for ; Fri, 2 Aug 2024 20:50:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722631811; cv=none; b=HhQqjaFE7WmxHK7FTLnixk9nFUvt5a/tu1D1agjuYKUz1LBuT9y6NHTsTi1RIthtcrTpoEWJ5gv8J5+qQv0yuG1VVGyoJJXtKr7QKcOOy6kJoi6eQUgSh0T5MYP98U2KlKSZtR88WQ/pdXGm5WFp4PrWHWC3vhdC8/fe+/UB8wk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722631811; c=relaxed/simple; bh=+/UuIDHcRQ8bBgQBKufiTQ6/NBfl7hp7XC+v3gwl3TA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=P1Y/573H/DX/t/+7l+LuGJq/nnJEy1monFF0lMT55u1yJAao5UnAaL83+dNUdQuEmlidvxYcjvjMVpQDru2j4/laV+xqhZQhQXL5XD0v0lorOybBB/FB2sbILb50Itd/KALNXB3f/h7QMsLSwOnf20t4LOsc01BLZE1R4GhqMfU= 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=xx0tKsh8; arc=none smtp.client-ip=209.85.219.202 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="xx0tKsh8" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e035f7b5976so9622906276.0 for ; Fri, 02 Aug 2024 13:50:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722631809; x=1723236609; 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=vhgy4am0UvUSmvRzeDG6pp/0Zly8gx1T1Ec+frofWT0=; b=xx0tKsh8e+50cNuu6P6wuUE/Ded+CesEjVmTUyuWnSnPhGAXRUKY1TktSE+pagsYjo NM2ZFW5CB7criwjQMDMjsmP3NWJneMwhvBTTglGrwudMQbx8LsLVlcpd/OhTEJyXaQZD 2JCflIkZ6i9QJCAwApZzrBSqR/byxVO+0aq0DW6+ieLYqwpNKlZpGkFBeByGofw7Je72 XIhks/TazxLoMJJcJp8ne1H5heW2X+OQDXg+V3W0guIbuEp41JFgn7dbhJdFJtqd1DpL VD4EPFPwx8pvNjHJR2EbNByqfuEzlq1VOhKTd3mKgaWMhL0mOry8EQj98OSo3UjAd1zr Fw+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722631809; x=1723236609; 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=vhgy4am0UvUSmvRzeDG6pp/0Zly8gx1T1Ec+frofWT0=; b=FwM9/rgeQLgaSx72M1wKfmNUt7vz7+6TpZTQQA03jg0hWZoy+G4JRxp0bbuUN6niCE Tfk3wiUr0RlLHjsWzpqz56IB95CxZsh7byI//Rf5ppaWMoGHRNLG0PNc866RqXSX1GXP lM/USwdc/wYI6sc21CyAop6PTa6X7F+ADkIGkhhgrOOSrPhV/uPACMwVAZh6EeUDXofB PTu2LevCyR4vwiLd7rtQ7w3Ja1crR7Vh4PHp6ORKe8P1ZOJao8YQy6uMUGAq5UUOgWjx cb3MWonZ0oRmXyvTjNMgCXqnRFwNbcoDE7rDwHHiGZPSA6x3DemQ66MMuaPdngAUnfTc Zw9Q== X-Gm-Message-State: AOJu0Yw8YUjls9gSYw0USpIhhmd162NGF83JPybcKgDsTcwrrtkTtRBI 09OPuH9tcnVrmgq5Rd0pRDXu3wHNhqugH9LSu9tUGS3kB/8UHfhf8/4M/46Q3jx1P2tggMQY7xl ULA== X-Google-Smtp-Source: AGHT+IFaG/omRB7AizNm6kEMBOCZAmcvGUoqJ2zKlTWS8jxj1A+cn3Aj6JsUZ4pDJBVCXzSy0rcuzzTc+Cc= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:86:0:b0:e0b:a34d:f223 with SMTP id 3f1490d57ef6-e0bd5b96ecemr44386276.5.1722631809493; Fri, 02 Aug 2024 13:50:09 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 2 Aug 2024 13:49:59 -0700 In-Reply-To: <20240802205003.353672-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240802205003.353672-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc2.264.g509ed76dc8-goog Message-ID: <20240802205003.353672-3-seanjc@google.com> Subject: [PATCH 2/6] KVM: Assert slots_lock is held in __kvm_set_memory_region() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Add a proper lockdep assertion in __kvm_set_memory_region() instead of relying on a function comment. Opportunistically delete the 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. Signed-off-by: Sean Christopherson --- virt/kvm/kvm_main.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 0557d663b69b..f202bdbfca9e 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1973,14 +1973,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) { @@ -1992,6 +1984,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 Fri Aug 2 20:50:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13751985 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.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 3FA001304BA for ; Fri, 2 Aug 2024 20:50:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722631813; cv=none; b=QRXK12RZctBIUVH2KjY7bh/ZnQ49KEiWuQ35tAUAoVb7x0tUlol+2NzEhgg2ZOsEwcVDToa754J5rM/UyoJ3Q8MlCZXAlk++BDCHz/UAo4UebAlneSJLPSpWBa1w2GM2RC+sqnWpJmrQ4rre/0/Nz2TUgTBfJMxglC/R9wTOKjs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722631813; c=relaxed/simple; bh=ryVUMI2g0m5dQwWIHvd3/K7VFFhleb/XIi+fC2p2RwQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=udEv7gtZDXeALpYBBwHmoxGU7LT9nvAUCSMEReJ21xOIeye2yhiH26rWfXhLf3PUQlB3BNNFAM1UDS011A6jihhpGwq9WKaw1QrgAx725E5QJsEPY7ihCGQorLOmqlkZnBwmX2s8SH+n8R2jqj9D46y2d9LNqr+Z1WJ66v+GUXA= 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=eTt1onKw; arc=none smtp.client-ip=209.85.128.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="eTt1onKw" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-688777c95c4so50252307b3.1 for ; Fri, 02 Aug 2024 13:50:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722631811; x=1723236611; 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=cNufUBVkEFtXyyJFRWhFP/HQuDK1gq9WLgQ7TUopaSE=; b=eTt1onKwprdYca0HS/2aoQgL1qcZHnLaADOYW31SCv1QRWhyqXAsOhVFnScDPoV0/y EEelP2CqjJbFlpDq0ywyW29rgkjnjWpZdlavEFPeb2RRrh6O22sLV00cqvT7KicxCYwv sWdJeSbaS51zFbyexNYRcWUuDsHWSVE+koaDiThmYZBR54b35GB6ocBm6ca0++2L4LCl VxTxl+9K1QQfl+xMnzFHaJFiNJX3/EZ/L7ydSN5zLha44GQntOj9BICEdKkigmsHJq6w 7oahG+preuaNXGUlV93UaDiyFP6XfQ7SW9YDD4+5DSiszCFWGE9vHSUbV+qzc3uHj2XX 4y+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722631811; x=1723236611; 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=cNufUBVkEFtXyyJFRWhFP/HQuDK1gq9WLgQ7TUopaSE=; b=rQSAYAYthJ79iVYQo/2jDJvcsQX95gUdQlqlqiNQ0c/UblOKZwW5tZFnW9nfgzdqcg FKPbqp2N8Az5+W+kq9vk4590RkRS4lSJMrUg1Mu4KUI0O8xuZwjG+TYmKfhYlcMHs8wF LIbeo4UPzxk2o8KnSiAj/0r/NWEbG+GnTu2kk37v4iqO1pGFHTfyAF19PjzWqmLWhNQr 7EJcQbxHjk40T5Ovw98Z5WRRn/aFKmLDNrnRsrlmgNQziVkYB85pK1ozPz6yuP3Q3v8L T9OrK5Njubo7wkGkHdMdRrHD1R5Hf4Bf6ecirnzaqSaJZ45fakzf/viAs1sYXIayhMoP NF7Q== X-Gm-Message-State: AOJu0Yx9BKeyYQGSQZ8atpwgWst2y8hcMxz+HO/Diyj47tjtJC8yYbb+ DSE6JPfQhDUNmwUJOBPyDt1gDQdylnNFNp76igmlWTQrAJ0WDr6YWffTas0arkxPUgAjeKA2BwO FWQ== X-Google-Smtp-Source: AGHT+IEVd++gjv5JjjXYdrIQrBE6X+Wf+379il6l16mOh+D6tVdPpj/zSVq1nLbIYd7hetKBqRUwhCVXO90= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:112:b0:62c:fb55:aeab with SMTP id 00721157ae682-6896458f799mr3223987b3.8.1722631811375; Fri, 02 Aug 2024 13:50:11 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 2 Aug 2024 13:50:00 -0700 In-Reply-To: <20240802205003.353672-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240802205003.353672-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc2.264.g509ed76dc8-goog Message-ID: <20240802205003.353672-4-seanjc@google.com> Subject: [PATCH 3/6] 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 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. 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 af6c8cf6a37a..77949fee13f7 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -12794,7 +12794,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 b341d00aae37..cefa274c0852 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1179,8 +1179,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 f202bdbfca9e..63b43644ed9f 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1973,8 +1973,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; @@ -2097,7 +2097,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 Fri Aug 2 20:50:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13751986 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (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 08C99136326 for ; Fri, 2 Aug 2024 20:50:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722631815; cv=none; b=n0vwVui1UpKPTwlU6vVunLLLDu2hxPmAJHTpT1LbnsDWBl1irXa6SuroqFpNVEnD2FUwlF+gOTZ6RLNYo6en+GmrLHM0DiT3Ea2LSVzEXTuSzgq3/hI43elMAkJsPTu0VIiGBtpgux2ixnsNR6191NeAWHzp+TwpQLB5sv61Ipk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722631815; c=relaxed/simple; bh=gCqaKijnA/XXSCKJJY3H14sUdvU41wCk5WOtCSYXq78=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Bk8xonw4Rn/5G0AaTPWwVJMgH25gFNQ1DZuN048+Irt7zCyfXkPrOtDsHVx9z0zIU+sMOu0Hh9GxVOeyTh17qmOrubExJ071YiR93qxjTG0oaHQG3Q935KtIjdp4lN0mnXnwnQkuzLJ51Flp7LK4kBRvchaIqpz6UVTaOwpIsxY= 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=WXiTJZE9; arc=none smtp.client-ip=209.85.215.202 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="WXiTJZE9" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-7b04199911bso3496321a12.1 for ; Fri, 02 Aug 2024 13:50:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722631813; x=1723236613; 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=c5Nq/OFr3yVwVK6otXPmP+p3Am8dAjZp5v9yRhPUjYo=; b=WXiTJZE9B7GKNKQc/dtGTvDxiROqAkt3qJFVuM3EitWEs8TW3mwQbBcQaVQp22ah55 XIDH5jZWugny4n9kftF9iUZ8GZYOoGs9I4UH8OuLikx9tukjFZPPRneV/Z9bDd79KfGA 7ylVmvulze1fHy1N+xmfaogsNpgIo6pNDR6EzdBY6gdRHo3KJYns63TRA/Riqrb0ziYz WSieyEFvgnXbBDQSKkffmXV7o5Jq3kZUMp23WFqg3O3aAqRRzVNNRnsH96I01q/T7qe/ r7IHzNeNvLX8o6BIGNMqDPMG/TZarnjMj/FAxic6cXNCzdRWtiBm2dGEEGyTDkm35qGt cI/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722631813; x=1723236613; 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=c5Nq/OFr3yVwVK6otXPmP+p3Am8dAjZp5v9yRhPUjYo=; b=B8kmntFbOQn0S0UWlASsAfNr/ISMaWQQ7ne6TZM4ycPX1ElZ5E27ZPOU169eF73+vI 5AbeW/iyUWSDrMBzv0ofO9z73rnnhvLPV/DnTaZJ/O/xB56CS+BqJOOqXoXUkTeCtT0P dCwDxel42J3pwSkoaoRdQXl/jlbSG0lG8jP9Z94/TQyuQYekWQBCC97pOd1PJVRO+Tym mLqTzPQSKIv17CkgKJmXfZ78kzfwBh7uUCqAoPG5Zl2iw56YwvqjwpTwHXiGsGkgfxwt gB0WrtJtAgWoU9Ckt2j/OiE1lyZNhHvCjWr6bQW5cPj+CNNyqL3Lf9loB/vkN2I9YpCL 0RMQ== X-Gm-Message-State: AOJu0YxFTngw4SxQq6scJ+p4VgA5LMFdgfGGc3/EXOTI/SxOHX6o/Yq9 wqWewB1NRpkO7U/ou1VhizRVH6ozbvjxJTDFrxocFUwREbOZcmmUH+NxMdzJhKANHA4E9xYvPG8 vmw== X-Google-Smtp-Source: AGHT+IFCCb4cjN7vw4l+DdM80k46FcB95ke4PJ/4aZuNN0SLLSK30zfFovXw2fZA2crCh5pBwalD0Ra5Fa8= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:a617:b0:2c5:2b19:4218 with SMTP id 98e67ed59e1d1-2cffa2728a2mr37588a91.3.1722631813176; Fri, 02 Aug 2024 13:50:13 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 2 Aug 2024 13:50:01 -0700 In-Reply-To: <20240802205003.353672-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240802205003.353672-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc2.264.g509ed76dc8-goog Message-ID: <20240802205003.353672-5-seanjc@google.com> Subject: [PATCH 4/6] 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 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. 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 77949fee13f7..bd365fb8ab6e 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -12895,7 +12895,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 cefa274c0852..b5c048858fc4 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1170,7 +1170,7 @@ static inline bool kvm_memslot_iter_is_valid(struct kvm_memslot_iter *iter, 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 63b43644ed9f..42ec817d6a7e 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1973,8 +1973,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; @@ -2104,7 +2104,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); @@ -2115,7 +2115,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 Fri Aug 2 20:50:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13751987 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.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 DBB78139587 for ; Fri, 2 Aug 2024 20:50:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722631817; cv=none; b=cOumjstcWQwg/KQy/0rE/+kK8vHt4Z4NWF0RJmeJ8QGayRc2q3eWTEO3uSS+xWKpoMzzDqFsOu3tT/n1/E2Met+ytqxS5DjtfHUtxzZpWHXQumziN8mbp5laKgfF6Kxi/ZaTMi6AGQBM52OwuCIkz2rWd1m2o7NJXsCQ4o9VCO8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722631817; c=relaxed/simple; bh=96WKGVZv6MYMzFgE5HIqk4A3cYSg0BZ1Uq2YbSu01Vc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=slb/ElnhNpIkoovKZy0DB5+UT3RAbbzgAwpys8WJmq06oFO8khpYReSF//exlSBDnOOLdRRJrIafVR4POy8DLA5jQNToRK8J5Rs6Tu62yB51+fJbtcF6GWqKs4HJQHXMBT0n7KeEnY8++T5zk2JcYQR0rHHJ1wNvAT2vXytloaA= 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=tbEXVM5M; arc=none smtp.client-ip=209.85.210.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="tbEXVM5M" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-70e923f6632so8366310b3a.3 for ; Fri, 02 Aug 2024 13:50:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722631815; x=1723236615; 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=9TDEuE7b7M7Fc+GA7/BL6fAQx5gD/oVWr+JWhTN6LC8=; b=tbEXVM5MUJ/l0SizyJmWvJysjPUAHEPogxkJjnLn39aznWnN3dbfn2lwwC145tQrkL EDIu4yyEyGZk/Fm8XUXlqpoiieN1jl05x/u2YxdBWD5VEOVOmstxTET7XvaJhFfyP+KJ nJ0LRJToFlNK8k5Yu2orl1MQ+FATmQLR64uhyo7B2D4Mhci8fy7A1HQI5Mj8+fGq1gAU hk+GBYAQLwrTjb+6O4cs6qkXtbax7a5aF0xZ9uPM8neRSARc/cAm1zskEuEFCyKDTmN9 nA69N14E9U2WWp643we01/D0yckIarwS5jCu+Vviza39hfKZeTNl8p1iboVnW9yliKK4 Iqqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722631815; x=1723236615; 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=9TDEuE7b7M7Fc+GA7/BL6fAQx5gD/oVWr+JWhTN6LC8=; b=IAk6BUU+yyffmOBw6ZYHLb3GR8QXViJkLXjeg3C8NZSwfcshtgnWlY+ATKTH7gNeTa /H7ZwSXozaQgwnJLMjJBFK77isNH9P8oX76HOc1tVtDqhviHlaT0zWFbWwBOiiyy1JQ1 gOTMv+IasUDVYfAd0Q88GmDfmYaPGMA/X9qqhMaXusLiG9o7geGOgVCdhQ6eQZYvKhq7 YbsISHBQcbPA4sdM2hGF0EPeDnwIQogMw0mAvAYpV78lsuhOp7IW66jVhmORhLxvMAIq vqQxVb+/KanLhGL14g98QuLkKbImrgH9vwckoucP3TtrTwY60t2/UjBqmTTVD6FYpvtI +MXg== X-Gm-Message-State: AOJu0Yzbrwh6AHXehyrpIog82mBQzEoepZrw4E44Q+8G+dCcyUIIFJtD ArfpmZE8wxr/YR/AP7zPqvJ13f3nCF4uPn/WxVMzDdJnkgeZyaoaM66gCs8gBdgiiG66Ihpux7z mSg== X-Google-Smtp-Source: AGHT+IGmstI5tuapD/d9fVDrQq8mrfrk57GkjBrOBNxVboWhlaf2L+9JkUsv0iGblKu/0wbgzPE1ESYXn7U= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:914c:b0:710:4d3a:6bc9 with SMTP id d2e1a72fcca58-7106d04604dmr86509b3a.3.1722631815035; Fri, 02 Aug 2024 13:50:15 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 2 Aug 2024 13:50:02 -0700 In-Reply-To: <20240802205003.353672-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240802205003.353672-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc2.264.g509ed76dc8-goog Message-ID: <20240802205003.353672-6-seanjc@google.com> Subject: [PATCH 5/6] 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 Disallow all flags for KVM-internal memslots as all existing flags require some amount of userspace interaction to have any meaning. This will allow moving the flags checking from __kvm_set_memory_region() to kvm_vm_ioctl_set_memory_region() without creating a hole where a KVM bug could silently succeed and create a bogus memslot. 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 42ec817d6a7e..84fcb20e3e1c 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2104,6 +2104,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); From patchwork Fri Aug 2 20:50:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13751988 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.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 05D2113A411 for ; Fri, 2 Aug 2024 20:50:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722631819; cv=none; b=bSq6MNYFwitI2xj4iJc5yLayDEStMOsTLUDb9CgYGD1W4BGok6GV8RBcJrZaZojlmp9loQF2syE56NGwVhrJus3C0nDSD8uxSsxceAb4hePh7O9CuqzH2/5PaZZtkDOZE2EDzSaG6frbAkDfN/OysyrNuvtKg8iq6LIW/MnROYw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722631819; c=relaxed/simple; bh=KEGZdr470mbN39TUkKhvxHtOKwGWSbX3eH+Pxhz6IxE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XRKfVlZFYsXjVguVtx1B2wieSwKkkrSNDRFxeVM2oC4YBdS5BSsQkF446DAsd1qH4g7SyDVedFtOMwOTP/2fQJGm0viVvTrF2XtWqcZC1KB/6ubkmYJDy6hK8jhgRAt5/8T9yazGGN0OdOCgLwua5ZHCmY0zAjrSN0WLu3Hogdg= 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=Nmr/ypcQ; arc=none smtp.client-ip=209.85.215.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="Nmr/ypcQ" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-778702b9f8fso2778436a12.1 for ; Fri, 02 Aug 2024 13:50:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722631817; x=1723236617; 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=sjrf6LZp0RLJx3l68AWkRp+t7dxqDnRfk/l7MG9GSDc=; b=Nmr/ypcQRV8rLYJ2ynANvyYB3nrBPYJCMtYzatnQVi+njMFTluNJkFnxCsfI5fmkDs HHTwa10ArAfBE1qNBf2LfzuLYP8bJGqdzl1XtTi3cICZVk+C4cNkoASl7qZz+eVTaBCg /me7rE21Yay+LEepOPlqvPiQojNXvXtzU1dg2Og9p6O0vAOsfRJkfwWO8MlZFhQV6iC+ uug3LdVIQdF8rtIOYR3d3DSZfcXBphBCuB4sUYVoSekou7RM1dfuzICCHsjv5UHco8nC hmLTRtjPHrxr77IYvK+T/DqFXC6OO27AljzI5QbhbQoQlpUq058DYiekEgrLdOCkv/mA e79g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722631817; x=1723236617; 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=sjrf6LZp0RLJx3l68AWkRp+t7dxqDnRfk/l7MG9GSDc=; b=fj9qIyrm2Iyi3jHZ9v9FJ6SFfLKOpdugGpzhqNUtrUdGg5Q8adJkYAfOIo3l1MIu9A cTzBcfRRRBqbRFED7bIGNDBmQMjfbqalryOXhibcB5c6lyDFcnZY+6MQvVrtYmZreaap e0U8qwUp1kHx7paha6f7bprpGYHJytt8/HDjl57Sx0F5il4sym/j5BbgASZ22UwysENO tN3wIYQtIlBcD0387jC515YF9wMpJvw4tO19HGq62bhwcR8wISPEg1OL7fOolgi5mZvz BDwTzryoauDSuFZp8WQXyui7w3/J5F44ldFBq/QaiR8AG8psKsTuquRL4yrfUrMKgpTd sYfQ== X-Gm-Message-State: AOJu0YwG9mOwWbSBPSKmljUM4Lpt3+tSluiq+68EiZM176C4hAA9Nm1P iGWqwUPXFgdNh9lrhsYWq7haM5yv4vtqitSKt831HxNVUV90TelQCoVqm7enK9xNPrAF8xcEq90 KUw== X-Google-Smtp-Source: AGHT+IF6C7pZ4NUGocFOOu/UQeiI5UJqzb1YswLOQBQxbJtWu5WEPTvztRxCrWwXOvBTc4lrrFA0biwyW7Q= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a63:6704:0:b0:7b8:b174:3200 with SMTP id 41be03b00d2f7-7b8b1743d58mr2218a12.5.1722631817192; Fri, 02 Aug 2024 13:50:17 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 2 Aug 2024 13:50:03 -0700 In-Reply-To: <20240802205003.353672-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240802205003.353672-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc2.264.g509ed76dc8-goog Message-ID: <20240802205003.353672-7-seanjc@google.com> Subject: [PATCH 6/6] KVM: Move flags check for user memory regions to the ioctl() specific API From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Move the check on memory region flags to kvm_vm_ioctl_set_memory_region() now that the internal API, kvm_set_internal_memslot(), disallows any and all flags. No functional change intended. Signed-off-by: Sean Christopherson --- virt/kvm/kvm_main.c | 54 ++++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 32 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 84fcb20e3e1c..09cc261b080a 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1566,34 +1566,6 @@ static void kvm_replace_memslot(struct kvm *kvm, #define KVM_SET_USER_MEMORY_REGION_V1_FLAGS \ (KVM_MEM_LOG_DIRTY_PAGES | KVM_MEM_READONLY) -static int check_memory_region_flags(struct kvm *kvm, - const struct kvm_userspace_memory_region2 *mem) -{ - u32 valid_flags = KVM_MEM_LOG_DIRTY_PAGES; - - if (kvm_arch_has_private_mem(kvm)) - valid_flags |= KVM_MEM_GUEST_MEMFD; - - /* Dirty logging private memory is not currently supported. */ - if (mem->flags & KVM_MEM_GUEST_MEMFD) - valid_flags &= ~KVM_MEM_LOG_DIRTY_PAGES; - -#ifdef CONFIG_HAVE_KVM_READONLY_MEM - /* - * GUEST_MEMFD is incompatible with read-only memslots, as writes to - * read-only memslots have emulated MMIO, not page fault, semantics, - * and KVM doesn't allow emulated MMIO for private memory. - */ - if (!(mem->flags & KVM_MEM_GUEST_MEMFD)) - valid_flags |= KVM_MEM_READONLY; -#endif - - if (mem->flags & ~valid_flags) - return -EINVAL; - - return 0; -} - static void kvm_swap_active_memslots(struct kvm *kvm, int as_id) { struct kvm_memslots *slots = kvm_get_inactive_memslots(kvm, as_id); @@ -1986,10 +1958,6 @@ static int kvm_set_memory_region(struct kvm *kvm, lockdep_assert_held(&kvm->slots_lock); - r = check_memory_region_flags(kvm, mem); - if (r) - return r; - as_id = mem->slot >> 16; id = (u16)mem->slot; @@ -2114,6 +2082,28 @@ EXPORT_SYMBOL_GPL(kvm_set_internal_memslot); static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm, struct kvm_userspace_memory_region2 *mem) { + u32 valid_flags = KVM_MEM_LOG_DIRTY_PAGES; + + if (kvm_arch_has_private_mem(kvm)) + valid_flags |= KVM_MEM_GUEST_MEMFD; + + /* Dirty logging private memory is not currently supported. */ + if (mem->flags & KVM_MEM_GUEST_MEMFD) + valid_flags &= ~KVM_MEM_LOG_DIRTY_PAGES; + +#ifdef CONFIG_HAVE_KVM_READONLY_MEM + /* + * GUEST_MEMFD is incompatible with read-only memslots, as writes to + * read-only memslots have emulated MMIO, not page fault, semantics, + * and KVM doesn't allow emulated MMIO for private memory. + */ + if (!(mem->flags & KVM_MEM_GUEST_MEMFD)) + valid_flags |= KVM_MEM_READONLY; +#endif + + if (mem->flags & ~valid_flags) + return -EINVAL; + if ((u16)mem->slot >= KVM_USER_MEM_SLOTS) return -EINVAL;