Received: by 2002:a05:7412:2a8a:b0:fc:a2b0:25d7 with SMTP id u10csp48772rdh; Tue, 6 Feb 2024 18:58:42 -0800 (PST) X-Google-Smtp-Source: AGHT+IEBuXwatWuH9WdxCHdjzBdjj+oUYS8ww8g/vLSJ8qm7LB+qk3AMjFMOCRIcR/c8hZ0luoYx X-Received: by 2002:a17:906:3056:b0:a38:73f6:3561 with SMTP id d22-20020a170906305600b00a3873f63561mr234589ejd.71.1707274722490; Tue, 06 Feb 2024 18:58:42 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707274722; cv=pass; d=google.com; s=arc-20160816; b=K7wkg4juXcOMq4wncvL9e90gc4JWk3fP2febqVV7WCNXMGn5bVurRaQofEr+gcwajH OXqgdWdyZ4y2FTqoGvFO0gueoX0kEUTiNB843pWn7mKDhznIBDIlFqvyjTGn52+UKOqy bzr3eomXh6C69jeOqUnktSKrjFdh454uDTf92yLg49P6296nNcPCTll0k6mHbrLvJ47Z +IfafAy6Ly7786ceqhp6llSKbpr3Ob18CecaqZu/73OcltARPvdBKbenNwA8Svpbotk6 CLyv7hhSny2licB5r7jCD6QVXq3nBSsvh5P9stkVp8qBjkS3/dfnJLANBUSThn2hjmlt 4PdA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :in-reply-to:date:dkim-signature; bh=REgxJxxqnJ9hacBXQ6aXWZQE1YsmJHbedjyy3CiQK2A=; fh=FvX9PE2ARQm1/oGLMmVZCdsBMQRwC6UeM6D/cPZs5hM=; b=Jrdzrc9rSDUqWyM3lnBHuT57d8GBuzkJC4LzGGZMgCRIh1N9l/qwbq0azN33NyjGyD y1nmNP9a9bnRWUxb0RUn2LFuVjKvEMQd+foqFctwZZwK3Z5xQgx6rUPjkIxXrLg57XBF IMj0KVqK2azfrfIBN85uTmmkfKW0MdyFPAnItC+Ni3MnBhgJdR9U1P+0/BzLxgiFr3tI rGOAp9X2DCph4lSQ3etrEvIZwrlvbTtUk4uvYjK1DWvicultvn8i9T47i8hAmkr0wm/w AuU1TPnHK6sR9uE9JhHhXIN4cJ5oJh4k7DEaCDYOtqTcxuDWUWmRxQ0nu4iM5bx7f6m+ HtMQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=2ukTrgd4; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-55875-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55875-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCVzpSx6w8gmJux8eVwrL1n0pNwtyV4sl5L8gbESsRUO/jBIakJdCHpZgXQ9SnsTNl1mMrWmA99wOxGf7hCNr+GEYf5SMNu5oN3bAhTDCQ== Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id z11-20020a170906074b00b00a3549e68608si268396ejb.664.2024.02.06.18.58.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 18:58:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-55875-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=2ukTrgd4; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-55875-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55875-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 18FBB1F25CE2 for ; Wed, 7 Feb 2024 02:58:42 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C1C82FC05; Wed, 7 Feb 2024 02:58:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="2ukTrgd4" 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 6145E6110 for ; Wed, 7 Feb 2024 02:58:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707274706; cv=none; b=FEEhllAQAvNbzu2T/+WzlGI18TevKOXRxGX7TLeXlcV4RjWJ/RW/bvxgj1puHjL93QPjfDqNdpI7Lmamr6N5SPbVpasetib8iX2Zu/u2qCn202OeVPf6cNm7NIRJvMIWyfF51rVcB2XPbi07MX+Qc3Y4s/sBaMdcMVBx8kfBL8Q= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707274706; c=relaxed/simple; bh=q0YTkLGeLiHeoMECfeW+UDyihdTziDFAXkK/gYfa6m8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=FlEgKGJrzslQ8xNz0kRgOXHosAhy55eHGNvc8MoEHAEl/cTxhWCv6ELb2S18dbcw2YBGyI2ovxOlpRu7FpxDPtlFyhdLOdKS3f3ghnUF05y2/SpF6yr4BZBN5aiGx8H34KdzOBcurxV4EFC+XSq5frSolYcC6yQpd/EuJOhOaFU= 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=2ukTrgd4; arc=none smtp.client-ip=209.85.219.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 Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dc6b26eef6cso238016276.3 for ; Tue, 06 Feb 2024 18:58:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707274704; x=1707879504; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=REgxJxxqnJ9hacBXQ6aXWZQE1YsmJHbedjyy3CiQK2A=; b=2ukTrgd4q81UHPu3MI9HusimapHQFjEo5lBjX5bWkjctL09MKKHCjDtmMdzWfNJi/J Q9dR+lqB2TfLA+GLQlO8wIFxHHxJB1apbcM50V6I5gVINxItxNlPOByGeu1Hs38HyKrf 4GQcWpBNK/fd3jKPfiXl3njfThoMpv7/fWiCdLkaSbkrcjPJeXafmsfyQPj8ql56BH8a v5GbV8WVw1jHb3AQfeeiI23GvE60eOAbDjLufVUV2MEsqchIJ0Xn/wUgn0EL9vC51TcL 8bUfK92X3uqdT+4r/MGH7YCZw/YXA44/ULLnKSGQiFPYxwgVmB/YI7DyzvY/7BqIUOO9 JdTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707274704; x=1707879504; h=content-transfer-encoding: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=REgxJxxqnJ9hacBXQ6aXWZQE1YsmJHbedjyy3CiQK2A=; b=mxAOgGHWyx37fgybpQSw8f8P8izAcHj8x4Ov7ciBmBYqpW3zBr3NxR1t41UJ1he/In DRvCl25wVpXu/uv7gD5/TalUDu2VhLtUs+jTu/o1tS0GJ5Q401Uahw/Uue0SxXVptAL4 tgMtMKQgbUmXuumBbFWxLCm57pYGY4wVUzzrYz0i1JmqApFEmAezdUSOhUT6xiKF6Jj8 wEtaT9tsEsIbq1FgLOJGSb6uxT2Ui5IaXPEBSLJv8Isy34W9rGk+7N/OAC3Qo0q4BzQM AsHcAXMQw1/zUUetuIwUqDz5MhkWJuo3/hyQT8tiVZHQ78c+lnRXL89FG6IEsrN+tV3L lS0Q== X-Gm-Message-State: AOJu0YwTQ/Dkfcdj7TIvD6xe5Md/PnozxpErw0rstmLPlkXTFh95gb+x QoVWeO0+sOQLnHCt08sWgViktRFtf0i842y5zSGjPzLNN4uZ52LOpj48z8ozOBh9GPb1L/ce8Fc ZOA== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:2310:b0:dc2:3a02:4fc8 with SMTP id do16-20020a056902231000b00dc23a024fc8mr146257ybb.6.1707274704457; Tue, 06 Feb 2024 18:58:24 -0800 (PST) Date: Tue, 6 Feb 2024 18:58:22 -0800 In-Reply-To: <19a1ac538e6cb1b479122df677909fb49fedbb28.camel@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <19a1ac538e6cb1b479122df677909fb49fedbb28.camel@infradead.org> Message-ID: Subject: Re: [PATCH v3] KVM: x86: Use fast path for Xen timer delivery From: Sean Christopherson To: David Woodhouse Cc: kvm , Paul Durrant , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable On Tue, Feb 06, 2024, David Woodhouse wrote: > On Tue, 2024-02-06 at 10:41 -0800, Sean Christopherson wrote: > >=20 > > This has an obvious-in-hindsight recursive deadlock bug.=C2=A0 If KVM a= ctually needs > > to inject a timer IRQ, and the fast path fails, i.e. the gpc is invalid= , > > kvm_xen_set_evtchn() will attempt to acquire xen.xen_lock, which is alr= eady held >=20 > Hm, right. In fact, kvm_xen_set_evtchn() shouldn't actually *need* the > xen_lock in an ideal world; it's only taking it in order to work around > the fact that the gfn_to_pfn_cache doesn't have its *own* self- > sufficient locking. I have patches for that... >=20 > I think the *simplest* of the "patches for that" approaches is just to > use the gpc->refresh_lock to cover all activate, refresh and deactivate > calls. I was waiting for Paul's series to land before sending that one, > but I'll work on it today, and double-check my belief that we can then > just drop xen_lock from kvm_xen_set_evtchn(). While I definitely want to get rid of arch.xen.xen_lock, I don't want to ad= dress the deadlock by relying on adding more locking to the gpc code. I want a t= eeny tiny patch that is easy to review and backport. Y'all are *proably* the on= ly folks that care about Xen emulation, but even so, that's not a valid reason= for taking a roundabout way to fixing a deadlock. Can't we simply not take xen_lock in kvm_xen_vcpu_get_attr() It holds vcpu= ->mutex so it's mutually exclusive with kvm_xen_vcpu_set_attr(), and I don't see an= y other flows other than vCPU destruction that deactivate (or change) the gpc. And the worst case scenario is that if _userspace_ is being stupid, userspa= ce gets a stale GPA. diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index 4b4e738c6f1b..50aa28b9ffc4 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -973,8 +973,6 @@ int kvm_xen_vcpu_get_attr(struct kvm_vcpu *vcpu, struct= kvm_xen_vcpu_attr *data) { int r =3D -ENOENT; =20 - mutex_lock(&vcpu->kvm->arch.xen.xen_lock); - switch (data->type) { case KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO: if (vcpu->arch.xen.vcpu_info_cache.active) @@ -1083,7 +1081,6 @@ int kvm_xen_vcpu_get_attr(struct kvm_vcpu *vcpu, stru= ct kvm_xen_vcpu_attr *data) break; } =20 - mutex_unlock(&vcpu->kvm->arch.xen.xen_lock); return r; } =20 =20 If that seems to risky, we could go with an ugly and hacky, but conservativ= e: diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index 4b4e738c6f1b..456d05c5b18a 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -1052,7 +1052,9 @@ int kvm_xen_vcpu_get_attr(struct kvm_vcpu *vcpu, stru= ct kvm_xen_vcpu_attr *data) */ if (vcpu->arch.xen.timer_expires) { hrtimer_cancel(&vcpu->arch.xen.timer); + mutex_unlock(&vcpu->kvm->arch.xen.xen_lock); kvm_xen_inject_timer_irqs(vcpu); + mutex_lock(&vcpu->kvm->arch.xen.xen_lock); } =20 data->u.timer.port =3D vcpu->arch.xen.timer_virq;