Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp2408639rdd; Fri, 12 Jan 2024 08:32:04 -0800 (PST) X-Google-Smtp-Source: AGHT+IGMgbM6OthFW/Kf6lSj4quOeCQdkgCo+sTmmX4uvCb4YsbA7ADFqQn+Cjd0yeaAV3T33fPJ X-Received: by 2002:a05:6358:d598:b0:175:d08a:dbf with SMTP id ms24-20020a056358d59800b00175d08a0dbfmr998936rwb.39.1705077124428; Fri, 12 Jan 2024 08:32:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1705077124; cv=none; d=google.com; s=arc-20160816; b=eT91Kv56Za4vKayptFBX9Xo2FZ/7Q/T34LnrbKFOD5f22y0lXKNkcN7VXVBLSMxE9N u+xh9sB7PSPRzJWnHJ0gaSPyuOewS9qaVGx2DP33ghgOD13VCX0dkvyLHB4Jnv830Ygv AngC2VddmFKERKshv8oiWgf7G5/oOxGXBwa5Tc2mJEREcraYJjI6I6mk8jAjC1iDNqCh odr0Z2O6VRRlS151nfkRssz+42b2F02LDJJ7tOHb7VBHsfnUoIBF1DeeNqqhvbcUSU00 tb5Mx4fG7UkosbZG0N8aFytGOdSUprQa14pAO2ulNssCfLIAdkECj6+X+1rQcqH3L/cy Yc7A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=bLTJ+wysBuqnwQSsCZnj62r1zQVMEhODC8W/mhh0SgY=; fh=eaE3SI/c/+UsIE/UWBETb2QwUmMvi8brlLO/YzO2jbo=; b=cwwuS0PL+xGXozrbk8hZeZeLPNUeN59GAOT2JByIE1TBfa0BNtxWXVi60Zgwvm8bNY LJXc8054W9mUvHeWzpAO8cDKq8EIZBUbqxCLuFDYHnQjd8wycrRT8feOM4Gjedo5p2Mk BBK0xHWZmNBuvASkxOufUg4Qku+lacsiY9T4TdbNuOMR2ciW5bk70Ir9dRIgLx1tjw36 S9rlYSZRkYJ9LcsCEP+BRs3QF7EjV34cUopbltE4DMn5Hsxd4Dd29RGhNsueveMAhr4h IAM3N+CCh566TudqWappBzLvqURzKohaMfo1mvz8lw/i8dnQkT8jn76LIFQ5N/eXZ/hk tbZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=pjODapA9; spf=pass (google.com: domain of linux-kernel+bounces-24859-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-24859-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id fd14-20020a056a002e8e00b006dab36291d7si3605500pfb.4.2024.01.12.08.32.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Jan 2024 08:32:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-24859-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=pjODapA9; spf=pass (google.com: domain of linux-kernel+bounces-24859-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-24859-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com 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 A7AEE2858F5 for ; Fri, 12 Jan 2024 16:32:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C546974E3B; Fri, 12 Jan 2024 16:31:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="pjODapA9" Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.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 CBD8F59172 for ; Fri, 12 Jan 2024 16:31:50 +0000 (UTC) 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 Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dbe9dacc912so8223411276.2 for ; Fri, 12 Jan 2024 08:31:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1705077110; x=1705681910; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=bLTJ+wysBuqnwQSsCZnj62r1zQVMEhODC8W/mhh0SgY=; b=pjODapA98ShcyayfivGfEND9IVouZI3Jq0ER+sgg8EQDpmn9yy1bWIlLSGoS03Q7mx rT+1bqsjBWbYZBKmxiWMnfB/dhKB+GfLfag3/LkMsEBD5KDWL336s6RlTdLk6o0JIUBT k5MZVmjhEEWZCfLISsDRla4ZPyrbiKTXbWJxh94iq1NnYKiBtoqnaKBEPrWjyeQZP5o1 jwI4AJoXKbAgTNINOsn3MoumwGREY1M+8eRfICgBJB2xFEAAHLO1NKjooWR/+VuLqk63 t96blPWe/DK1tFlDEvEyZ6kJjTC4LypghABYXBFP8OC0QYYD3F8+T74R4o08qm/wXvKN sS7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705077110; x=1705681910; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=bLTJ+wysBuqnwQSsCZnj62r1zQVMEhODC8W/mhh0SgY=; b=MTLr+tXJzzzYgdbfLv/lP8W9lp9svLLQFHLMcEbmMAYNN31CcGGrK5D+TozucQP+ZK Dt8KpmxGL+eNWt/V2VTxbkHis14vAplICQNqqIIn1XILrOY93c/2ixvyxMe/DrFTrdDY ECTmko4zfUETilPSJWvZbEFmFszps1YBldHC8ej2XCLbxZnanHSu1jcGOGbh2ch49jph 1lE6HjXarXX5iVAH8+ghzBUgXzKTYLUUyYSMHPPExfzcc0S7ek2s3g9E0amo3S8HTqZF X/gEaySDpE5UQo8RSUX3OLSzee0Y6jefSMvsAxSd1lGK1Ta9vpB0UGp8201KWMEXm7hm vCMw== X-Gm-Message-State: AOJu0Yx5G+fzl6F5jG/wY3PP0SZCeoQYpn4DH+IiIQ9j2fEP9VY/WUVj N/W7b26zdX8Tdeg2VIN6C71ILOwZwjn9ktdx3Q== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1364:b0:dbd:7149:a389 with SMTP id bt4-20020a056902136400b00dbd7149a389mr47116ybb.11.1705077109868; Fri, 12 Jan 2024 08:31:49 -0800 (PST) Date: Fri, 12 Jan 2024 08:31:48 -0800 In-Reply-To: <20240112074839.waglpqqgs772m4a3@yy-desk-7060> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240110012045.505046-1-seanjc@google.com> <20240112074839.waglpqqgs772m4a3@yy-desk-7060> Message-ID: Subject: Re: [PATCH v2] KVM: x86/mmu: Retry fault before acquiring mmu_lock if mapping is changing From: Sean Christopherson To: Yuan Yao Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Yan Zhao , Kai Huang Content-Type: text/plain; charset="us-ascii" On Fri, Jan 12, 2024, Yuan Yao wrote: > > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > > index 3c844e428684..92f51540c4a7 100644 > > --- a/arch/x86/kvm/mmu/mmu.c > > +++ b/arch/x86/kvm/mmu/mmu.c > > @@ -4415,6 +4415,22 @@ static int kvm_faultin_pfn(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault, > > if (unlikely(!fault->slot)) > > return kvm_handle_noslot_fault(vcpu, fault, access); > > > > + /* > > + * Pre-check for a relevant mmu_notifier invalidation event prior to > > + * acquiring mmu_lock. If there is an in-progress invalidation and the > > + * kernel allows preemption, the invalidation task may drop mmu_lock > > + * and yield in response to mmu_lock being contended, which is *very* > > + * counter-productive as this vCPU can't actually make forward progress > > + * until the invalidation completes. This "unsafe" check can get false > > + * negatives, i.e. KVM needs to re-check after acquiring mmu_lock. Do > > + * the pre-check even for non-preemtible kernels, i.e. even if KVM will > > + * never yield mmu_lock in response to contention, as this vCPU ob > > + * *guaranteed* to need to retry, i.e. waiting until mmu_lock is held > > + * to detect retry guarantees the worst case latency for the vCPU. > > + */ > > + if (mmu_invalidate_retry_gfn_unsafe(vcpu->kvm, fault->mmu_seq, fault->gfn)) > > + return RET_PF_RETRY; > > This breaks the contract of kvm_faultin_pfn(), i.e. the pfn's refcount > increased after resolved from gfn, but its caller won't decrease it. Oof, good catch. > How about call kvm_release_pfn_clean() just before return RET_PF_RETRY here, > so we don't need to duplicate it in 3 different places. Hrm, yeah, that does seem to be the best option. Thanks! > > + > > return RET_PF_CONTINUE; > > } > > > > diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h > > index 7e7fd25b09b3..179df96b20f8 100644 > > --- a/include/linux/kvm_host.h > > +++ b/include/linux/kvm_host.h > > @@ -2031,6 +2031,32 @@ static inline int mmu_invalidate_retry_gfn(struct kvm *kvm, > > return 1; > > return 0; > > } > > + > > +/* > > + * This lockless version of the range-based retry check *must* be paired with a > > s/lockess/lockless Heh, unless mine eyes deceive me, that's what I wrote :-)