From nobody Wed Apr 16 00:13:33 2025 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 C8C4321420C for ; Tue, 1 Apr 2025 20:46:51 +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=1743540413; cv=none; b=nDgVImP9UG9hfM9UJ/65UqjqWh9xJ1Y7sqdgrlhJ0NAFVz2/KXc642CuHkxWY4xuH3RY0akckXLWrTP0leYZmeAJ07Cu2hEjZ1fG1dVqeFwraWeF+tU8oV6iveV/QqHvgncIywEmfn/g+HntVbA27ZbfXqB00YwD6GylWFFfavo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743540413; c=relaxed/simple; bh=NEcVsWkrtlEatK+Y5SErb33iB70xl7AybVaIH/Yq9FU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Mq4UuTKbcHv2/LOtlTpJEyBZ6/Qnz+9IHL8ITas8d7AQFxxDD3V9jOPUonnEKzvobdsY/nVeBbc2EyMWe5FE81nWZVxwnEmyCa8VROJuVLN8I/h2sqKe1k6bVL+AiQDiSL2KNPx2qJbqP24MmAUWTnhdpti9Mcyk1BUab8NEXwI= 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=QJRmlX0m; 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="QJRmlX0m" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-227ed471999so95586995ad.3 for ; Tue, 01 Apr 2025 13:46:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743540411; x=1744145211; 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=j8aM8OQNKmiMYC4fpWY1Tcz4TDYVMXdrg7MtQvLe+/k=; b=QJRmlX0mTrHvzBAE9nceVI/BKw2gf4+m9Ju2Ej37vUwGOO3w0G7DT9WiDAoNQM/ZYU PanPZy7adQotXtjpoPJhz6s57JNS7Lar+KWzOxlgIwGyFmQHfeXglTOnDZlyjZf7k+vC Wd5jJbMniavzjs8OiUFteiqP6r8HTKPjDnnyf0t1ZfGyz6ylGBYbq5hHn10lK18OCl42 xBYOsZYpDicGhS4Cw1KGPZ5qEKoYf2yVuqG9pW3htw8beAHvkKWKuOpAiD/Ym9n6TWwa Tmbi7bGfN9mNsOvgh8U4kPFFD2uc7hDYywN58D8QgzV4C0cBinQT91jabx7BLjYzfRoj zjfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743540411; x=1744145211; 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=j8aM8OQNKmiMYC4fpWY1Tcz4TDYVMXdrg7MtQvLe+/k=; b=A6mqBRDB9RzDvtVyawHq7ijG15XXRTki907ugPDRrwDJI9//n257lOUOY3k+8+rBTR L9MLiMSE0htJQGAsNxE/U8w1rGJKGIpIk/wdslRwOU0Tw6N4x90HrWMBr3Cqe6uC92b5 tMaQQd8f+ev1yfQZe8Y5aSYjilTXZlEbEkVop7otzkB04zDKjPrhUXHCHEa/GD+/vXCf jhg6khEWNJtdpudl4HXgi64/l5Xi/FINGJjh+G+eORV+MfporXaS0QNPY+iFM8t6igdW 3uEfs9bJbUIG147CdsyrRkJdFIOTWUH9bdFGAaZkfRhJQd4IS6DMZdPhwzFwnohjZsGJ wg6w== X-Forwarded-Encrypted: i=1; AJvYcCUTJSTS2uxTAIkFtp3iDv3Alw5JxURN4oygmmPc9A2foNBBxAVcMUuUogP9E4+8BaG9nqjRUmnkX1/IkmY=@vger.kernel.org X-Gm-Message-State: AOJu0Yw216CqlmUu/FjJz6yN9oWmnPOrdRbVe6YtYRbw7aIDdLrDz69/ 0gajeBcaWiroGIEjaaToSm3ZJsNz0BiDlgybF+WeZN9qAE6sD0prTF5Xd6AxD/FUW9uqiYJoPHc wVA== X-Google-Smtp-Source: AGHT+IHk4c/1IjO1MwiDzaYInvb39GIpeBXBReykLE1mG0z4QyNEUAZvNPkX1TcPikaWCjInx7JyJFujPu0= X-Received: from pfbcw10.prod.google.com ([2002:a05:6a00:450a:b0:736:3a40:5df5]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:c411:b0:220:f7bb:842 with SMTP id d9443c01a7336-2295c0ed243mr67188835ad.42.1743540411361; Tue, 01 Apr 2025 13:46:51 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 1 Apr 2025 13:44:14 -0700 In-Reply-To: <20250401204425.904001-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250401204425.904001-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.504.g3bcea36a83-goog Message-ID: <20250401204425.904001-3-seanjc@google.com> Subject: [PATCH 02/12] KVM: Acquire SCRU lock outside of irqfds.lock during assignment From: Sean Christopherson To: Paolo Bonzini , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Marc Zyngier , Oliver Upton , Sean Christopherson , Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-riscv@lists.infradead.org, David Matlack , Juergen Gross , Stefano Stabellini , Oleksandr Tyshchenko Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Acquire SRCU outside of irqfds.lock so that the locking is symmetrical, and add a comment explaining why on earth KVM holds SRCU for so long. Signed-off-by: Sean Christopherson --- virt/kvm/eventfd.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/virt/kvm/eventfd.c b/virt/kvm/eventfd.c index 01c6eb4dceb8..e47b7b6df94f 100644 --- a/virt/kvm/eventfd.c +++ b/virt/kvm/eventfd.c @@ -401,6 +401,18 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *ar= gs) */ init_waitqueue_func_entry(&irqfd->wait, irqfd_wakeup); =20 + /* + * Set the irqfd routing and add it to KVM's list before registering + * the irqfd with the eventfd, so that the routing information is valid + * and stays valid, e.g. if there are GSI routing changes, prior to + * making the irqfd visible, i.e. before it might be signaled. + * + * Note, holding SRCU ensures a stable read of routing information, and + * also prevents irqfd_shutdown() from freeing the irqfd before it's + * fully initialized. + */ + idx =3D srcu_read_lock(&kvm->irq_srcu); + spin_lock_irq(&kvm->irqfds.lock); =20 ret =3D 0; @@ -409,11 +421,9 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *ar= gs) continue; /* This fd is used for another irq already. */ ret =3D -EBUSY; - spin_unlock_irq(&kvm->irqfds.lock); - goto fail; + goto fail_duplicate; } =20 - idx =3D srcu_read_lock(&kvm->irq_srcu); irqfd_update(kvm, irqfd); =20 list_add_tail(&irqfd->list, &kvm->irqfds.items); @@ -449,6 +459,9 @@ kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *arg= s) srcu_read_unlock(&kvm->irq_srcu, idx); return 0; =20 +fail_duplicate: + spin_unlock_irq(&kvm->irqfds.lock); + srcu_read_unlock(&kvm->irq_srcu, idx); fail: if (irqfd->resampler) irqfd_resampler_shutdown(irqfd); --=20 2.49.0.504.g3bcea36a83-goog