From nobody Wed Apr 16 00:13:34 2025 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 E863D21CC79 for ; Tue, 1 Apr 2025 20:46:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743540420; cv=none; b=TnukqSovK2kIcfkgzgnUZegndKS25wc86OtY17lHnab4JJ+sCcW70t8qtrSIctjUQY0EpHQpZ+Ci9KUhBgGJofwFafPTl3hQjskyXWmtUpWotuJ7bSeIIKU9aV675b/av7DYQMPUNx9CLwhR3kpn5remvH6cKGXfVD7v0FpqZrE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743540420; c=relaxed/simple; bh=9Y4jQuNr81blf0kL+x4SZ577lsTpKSVZZgUjgP8+H94=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=E1IILmiQxPhyWep/icPUkMch8kESeMW0CG9YBRybMkpdpxW8RY93RsfNadJxN0okyFIt6uH/weXord9wK6sswTwlottt0DEeztvPAemFWoCkwKHdhAdUVSbS3Ypi391FOWPE7zY/haRqwLfwjp9zSGX6p4HBaBFY2udtUvqQp1c= 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=DyouEwtd; arc=none smtp.client-ip=209.85.216.74 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="DyouEwtd" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-3032ea03448so11185575a91.2 for ; Tue, 01 Apr 2025 13:46:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1743540418; x=1744145218; 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=XAZ6HcZyPVHZNs/NiQeU2YPtsx7wIdUpIqdNXxdK3gA=; b=DyouEwtdq9yHvK0FwVUiAsw9Dozv6SbcJPwqoqEuYa1kyrPC5COKuksUahOSqDZM7N wm2c6flS7j8aczykkShGZSGuR/Bea3iEqkd54eQnJQQP0uQ1tXuRZGViFsrrUJ1pdQI8 d4rYi4e5YbiRBLEdlL4nZ0hiGLfKwQ9tZqVleXm66N7rLnSMXFDhJ75vRHIp4SNSFx02 Xgk3GwQ6VmF/YsBJ7vf1zG7prF1MT7wweHeg5ic9+bJvUjgE3lbD+6ME+bEohWKHufKe ennvaLaPtuSgfuM785vTewTt9qwneaKZwIbvo6djhNCWYVNIHHsx2HTvFun0IHfPwTcY 5iCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743540418; x=1744145218; 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=XAZ6HcZyPVHZNs/NiQeU2YPtsx7wIdUpIqdNXxdK3gA=; b=JKL6PDN7oXDLCmTbrmvhGuVkpqBJzd6bDnD3KXthvf1NgoLnhL+hES4gYoBybpV0g/ 5HPLMldGM9BJ9KYCZc4+24e3VjH/spCfI9X+AKqeDxG+ZTLOXzmhvonLA5IuNp9ZX2MV SNJmC6G3SbTVsi6kH4mi9hEK4AqXVXPSmDCrawj3/5Bk8ChXvxIjMv0jMK4+FHaO7PYC MjWnwdHB9w9oGDLfenwlK+BswO4XwlHeE+sYJtcD6S8/2DluqYSRn7sn5KvRBN3qq5Jn rTGdpPxMBgwt4+uVahLu1wrIF9Uf8aHTpiSynnbWtDnoCP+aRp7ErbN95Mrzd/B9btiT JpNQ== X-Forwarded-Encrypted: i=1; AJvYcCWqp4FQYbYR8G/HctAb4fVB1qyOfpA9RyPopl9CHOaiejd8JHLfEQOdYcEpwr8JPSmy70juy6RlZhNu+8g=@vger.kernel.org X-Gm-Message-State: AOJu0Ywjn7yiMoV8iAzk2jhW3mn3vjwelvI8vPlhahP55L6qoWKENVbz XxuxflQH7x1CCiWAEFYu2rl/LthZwPRqm11gBWQZSE+2lCPYdwG/mE/eRqIrkJKfFYYTnv8zVt2 kvg== X-Google-Smtp-Source: AGHT+IFdQSCduwNTHLV/nec9/i/BvW+thtXD97niBCIJ/k+4mEqw+xFFVLw5YdqLnoDpAq+BRV3lhigqiIM= X-Received: from pjk16.prod.google.com ([2002:a17:90b:5590:b0:2ea:5084:5297]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:574b:b0:2ee:9b09:7d3d with SMTP id 98e67ed59e1d1-305320af3e0mr18907665a91.19.1743540418245; Tue, 01 Apr 2025 13:46:58 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 1 Apr 2025 13:44:18 -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-7-seanjc@google.com> Subject: [PATCH 06/12] sched/wait: Add a waitqueue helper for fully exclusive priority waiters 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" Add a waitqueue helper to add a priority waiter that requires exclusive wakeups, i.e. that requires that it be the _only_ priority waiter. The API will be used by KVM to ensure that at most one of KVM's irqfds is bound to a single eventfd (across the entire kernel). Open code the helper instead of using __add_wait_queue() so that the common path doesn't need to "handle" impossible failures. Note, the priority_exclusive() name is obviously confusing as the plain priority() API also sets WQ_FLAG_EXCLUSIVE. This will be remedied once KVM switches to add_wait_queue_priority_exclusive(), as the only other user of add_wait_queue_priority(), Xen's privcmd, doesn't actually operate in exclusive mode (more than likely, the detail was overlooked when privcmd copy-pasted (sorry, "was inspired by") KVM's implementation). Signed-off-by: Sean Christopherson --- include/linux/wait.h | 2 ++ kernel/sched/wait.c | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/include/linux/wait.h b/include/linux/wait.h index 6d90ad974408..5fe082c9e52b 100644 --- a/include/linux/wait.h +++ b/include/linux/wait.h @@ -164,6 +164,8 @@ static inline bool wq_has_sleeper(struct wait_queue_hea= d *wq_head) extern void add_wait_queue(struct wait_queue_head *wq_head, struct wait_qu= eue_entry *wq_entry); extern void add_wait_queue_exclusive(struct wait_queue_head *wq_head, stru= ct wait_queue_entry *wq_entry); extern void add_wait_queue_priority(struct wait_queue_head *wq_head, struc= t wait_queue_entry *wq_entry); +extern int add_wait_queue_priority_exclusive(struct wait_queue_head *wq_he= ad, + struct wait_queue_entry *wq_entry); extern void remove_wait_queue(struct wait_queue_head *wq_head, struct wait= _queue_entry *wq_entry); =20 static inline void __add_wait_queue(struct wait_queue_head *wq_head, struc= t wait_queue_entry *wq_entry) diff --git a/kernel/sched/wait.c b/kernel/sched/wait.c index 51e38f5f4701..80d90d1dc24d 100644 --- a/kernel/sched/wait.c +++ b/kernel/sched/wait.c @@ -47,6 +47,26 @@ void add_wait_queue_priority(struct wait_queue_head *wq_= head, struct wait_queue_ } EXPORT_SYMBOL_GPL(add_wait_queue_priority); =20 +int add_wait_queue_priority_exclusive(struct wait_queue_head *wq_head, + struct wait_queue_entry *wq_entry) +{ + struct list_head *head =3D &wq_head->head; + unsigned long flags; + int r =3D 0; + + wq_entry->flags |=3D WQ_FLAG_EXCLUSIVE | WQ_FLAG_PRIORITY; + spin_lock_irqsave(&wq_head->lock, flags); + if (!list_empty(head) && + (list_first_entry(head, typeof(*wq_entry), entry)->flags & WQ_FLAG_PR= IORITY)) + r =3D -EBUSY; + else + list_add(&wq_entry->entry, head); + spin_unlock_irqrestore(&wq_head->lock, flags); + + return r; +} +EXPORT_SYMBOL(add_wait_queue_priority_exclusive); + void remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_= entry *wq_entry) { unsigned long flags; --=20 2.49.0.504.g3bcea36a83-goog