From patchwork Thu Feb 29 02:57:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Stevens X-Patchwork-Id: 208182 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2097:b0:108:e6aa:91d0 with SMTP id gs23csp143120dyb; Wed, 28 Feb 2024 19:00:11 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVKhn4q6Ts0iSH2NFrZwTeVbMAqzZz60n/XE7ENAqS+aA3aSWZsNsIF+TGxIqxmtXpwAhIHiVMjcuRDi5QM6FUy+7mrAQ== X-Google-Smtp-Source: AGHT+IHGcd3nohgcuzbmimVwKme6mM4d0KGvBF++2nlFxK3ceLkQ9SNz0a03WglX9OlroVtZelYo X-Received: by 2002:a05:6a20:1a94:b0:1a1:2a5b:a75b with SMTP id ci20-20020a056a201a9400b001a12a5ba75bmr372009pzb.12.1709175611547; Wed, 28 Feb 2024 19:00:11 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709175611; cv=pass; d=google.com; s=arc-20160816; b=qYtbh+L8JYGabJK5crQQ/dPu4GmNc2i+Gx4Lb5iR4w/fhf8YRtweIb0DXp6YWhxeky MRD5Dus/3+ZNbk1ljUDETgGxc/ybBhndAB7bJ04GX/cZh1KsiEqa258j3h+sFTsiQkMd 7qHqXZmtaGKZRtb3kYbjpgdk4Mi4bZuDu9tXt5pBAPKdIAxVKJWcX35MJmjR4U1q9Ve7 XHAizx2rTNjqkrhoaHWm79oceCTaaT6WAhf7XvAS2yk9Mq8INP1ke7HIKCioJBiksdO/ TwihDnrXKIOVdHkfrokcfXGhWYtbv3rBAxDSSUfm5OqT6tmIa/wvLWe2REmWrNQyrh8J VM4Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=Ni2jT8j4ual03uOxDJftxXgB5iCIPPNSKtQ7Mr+vnE0=; fh=tNnHSUJEi69o8gcBdrccfCF3vxqgU7FosT0aZccnYf4=; b=qoc26w0xPbnwNiLJfWuzlTxsD5jJ8N8guQH3oXr8IdXkm6kWeLn2JcKAL8Au9nSywk uplhGHIAF5wQYkfOYsMwRgzZZoNjgxCXttHbFk0/gx8T+oj6VwPloe/9ifQuV8C91hGx d0GNbtzyqZTu5/CzPc/P0ybCLwa5maxMEM8WCA7kltVvNcEwAxjtDf7+Lvjqw5hzcfDk A5vpKRwusk31lhL3iWyEC8glOg3LS97JqcUwly5Ns+WWhXBXB8iWXWmrKY+YFMEtCZjJ XRAg6FRJL/2MJOhfEnjEN1wuk/3xjj/4GTqvEcCu7/ZFtBtjIqQ2gDrdZHdGWijNF63a mYog==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=J0QDgUnR; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-86069-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-86069-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id y6-20020a63ce06000000b005e19d15689bsi429237pgf.819.2024.02.28.19.00.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Feb 2024 19:00:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-86069-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=J0QDgUnR; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-86069-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-86069-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 49D1A2860DA for ; Thu, 29 Feb 2024 03:00:11 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E79CE41C84; Thu, 29 Feb 2024 02:58:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="J0QDgUnR" Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) (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 871093D38C for ; Thu, 29 Feb 2024 02:58:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709175512; cv=none; b=UeLehDhpMPrP1I+HvbimY2kJqQ0GrEcujUOw/xW54dyivUKk2kjUw2Fs/zdjCbF+mNtfupHUm8oiFcPcxPIyfESJYT1/jzx4vYArr6ZY7Z6IDyNVdBs7Q8yivj+VKwlias5N847XiGVeQ42tGmdXewR7v+5IUd9ygew97V2EuaQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709175512; c=relaxed/simple; bh=ntmqS432o2IecDkbveS88A7jvkU9H2eMDxulzMNm4uA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SR2RoSp9JnWYjLbL5ySciOUrm0RvGPmk3ZqvozJFHp9tNFKA0xPx8T/XoTm2jL3o+cC95o6BmERbSyu2nwFQ6OY2BCQ43xX4zXiPiYgosMVblF30Sqrdk3fd+qfUP/xX2SQIm5+h9of164ViP3DkSKF5NXCw44je+soYtgwLkO4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=J0QDgUnR; arc=none smtp.client-ip=209.85.210.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-6e581ba25e1so334855b3a.1 for ; Wed, 28 Feb 2024 18:58:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1709175510; x=1709780310; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Ni2jT8j4ual03uOxDJftxXgB5iCIPPNSKtQ7Mr+vnE0=; b=J0QDgUnR/OeRtU1ByzEXvqoTR5PYVh3RUK9jBS1tOfws8S1szIMaZRyDiVs0rr0iH/ nwpSRRKmq79Fo67RgPTkzk1X8T0ksPR5oCvPFX9gLjJahSEEOahzeZRcjoaz2hdZYU9e hdgEB1OZIYjHwfILSifxU9lPSTLXxfy1R35oQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709175510; x=1709780310; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ni2jT8j4ual03uOxDJftxXgB5iCIPPNSKtQ7Mr+vnE0=; b=SOpAevWY8t6yfCJRjD8x4RFdWarED3VhuWkmRv3Ky7wxYH14Ph05S9rMBl+M4ssFOK zfXlx+MW0cAduskjOxsLfVXDRfpHAVWiGWd+SNEe04iYjKK2IGE3LIGQiBVr6udbgB5F wjwrm9SfyXUx0/Bu+AooDjNebJkSx3xV8bNzueAn2pnFtOPfxeyoiLklu9eD+JmJi38n qSjoIMwDwlpIr+6TT+vGCkxaeR4P2QKHbQWJiXgrOHB5iaXZfTsCXblQ+SMNznK71sOz OzEyfepgOqnmfAxmOE1LT1NbA3jm/sRzJioszaX1XIS71YQRpMfm8McCRDzdfI11LRSx eDaw== X-Forwarded-Encrypted: i=1; AJvYcCUMJRo35G4l8OLHg+NykjjWRTStzynjuAHHP8SlLZ0zXpMQSqC527tokHRfBv8txFeeyPHsVH2Zkf323/S3rZdyqby+G2viLrgCXJVZ X-Gm-Message-State: AOJu0YwCDKWQgDt2bCEfVJznvDSLNfwyobFl2yR53XAZ72aWwemjzThi Dzf7oX+BR/eO70eP/7DiIztHZMhnd+qm2bgrJO6Os1+9IRVl0Y/L/kZluJK0Ww== X-Received: by 2002:a05:6a00:cc4:b0:6e5:37cb:64c4 with SMTP id b4-20020a056a000cc400b006e537cb64c4mr952284pfv.9.1709175509796; Wed, 28 Feb 2024 18:58:29 -0800 (PST) Received: from localhost ([2401:fa00:8f:203:f51:e79e:9056:77ea]) by smtp.gmail.com with UTF8SMTPSA id x65-20020a626344000000b006da96503d9fsm167784pfb.109.2024.02.28.18.58.27 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 28 Feb 2024 18:58:29 -0800 (PST) From: David Stevens X-Google-Original-From: David Stevens To: Sean Christopherson , Paolo Bonzini Cc: Yu Zhang , Isaku Yamahata , Zhi Wang , Maxim Levitsky , kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, David Stevens Subject: [PATCH v11 5/8] KVM: Migrate kvm_vcpu_map() to kvm_follow_pfn() Date: Thu, 29 Feb 2024 11:57:56 +0900 Message-ID: <20240229025759.1187910-6-stevensd@google.com> X-Mailer: git-send-email 2.44.0.rc1.240.g4c46232300-goog In-Reply-To: <20240229025759.1187910-1-stevensd@google.com> References: <20240229025759.1187910-1-stevensd@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792200526058735194 X-GMAIL-MSGID: 1792200526058735194 From: David Stevens Migrate kvm_vcpu_map() to kvm_follow_pfn(). Track is_refcounted_page so that kvm_vcpu_unmap() know whether or not it needs to release the page. Signed-off-by: David Stevens --- include/linux/kvm_host.h | 2 +- virt/kvm/kvm_main.c | 24 ++++++++++++++---------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 66516088bb0a..59dc9fbafc08 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -295,6 +295,7 @@ struct kvm_host_map { void *hva; kvm_pfn_t pfn; kvm_pfn_t gfn; + bool is_refcounted_page; }; /* @@ -1270,7 +1271,6 @@ void kvm_release_pfn_dirty(kvm_pfn_t pfn); void kvm_set_pfn_dirty(kvm_pfn_t pfn); void kvm_set_pfn_accessed(kvm_pfn_t pfn); -void kvm_release_pfn(kvm_pfn_t pfn, bool dirty); int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset, int len); int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 984bcf8511e7..17bf9fd6774e 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3184,24 +3184,22 @@ struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn) } EXPORT_SYMBOL_GPL(gfn_to_page); -void kvm_release_pfn(kvm_pfn_t pfn, bool dirty) -{ - if (dirty) - kvm_release_pfn_dirty(pfn); - else - kvm_release_pfn_clean(pfn); -} - int kvm_vcpu_map(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map) { kvm_pfn_t pfn; void *hva = NULL; struct page *page = KVM_UNMAPPED_PAGE; + struct kvm_follow_pfn kfp = { + .slot = gfn_to_memslot(vcpu->kvm, gfn), + .gfn = gfn, + .flags = FOLL_GET | FOLL_WRITE, + .allow_non_refcounted_struct_page = true, + }; if (!map) return -EINVAL; - pfn = gfn_to_pfn(vcpu->kvm, gfn); + pfn = kvm_follow_pfn(&kfp); if (is_error_noslot_pfn(pfn)) return -EINVAL; @@ -3221,6 +3219,7 @@ int kvm_vcpu_map(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map) map->hva = hva; map->pfn = pfn; map->gfn = gfn; + map->is_refcounted_page = !!kfp.refcounted_page; return 0; } @@ -3244,7 +3243,12 @@ void kvm_vcpu_unmap(struct kvm_vcpu *vcpu, struct kvm_host_map *map, bool dirty) if (dirty) kvm_vcpu_mark_page_dirty(vcpu, map->gfn); - kvm_release_pfn(map->pfn, dirty); + if (map->is_refcounted_page) { + if (dirty) + kvm_release_page_dirty(map->page); + else + kvm_release_page_clean(map->page); + } map->hva = NULL; map->page = NULL;