Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp135733lqb; Thu, 23 May 2024 13:08:41 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXmw+bKByKQ4yDXujtSQNbFpuEgGGg/Ayo9YaSdjp24RUVU7jELeauD9QOkhx24ooZJz5KekCI6OTSjYWqhGEmFB9fRQjhmSd3AOscqRw== X-Google-Smtp-Source: AGHT+IHXEheU7LI6qVk3AhzvleC9q0+cK14Gi6CO3gHERDKUJnnOlWeupoopKkrf86U2fZrtcSJs X-Received: by 2002:a05:6a00:4006:b0:6f3:eb71:af9d with SMTP id d2e1a72fcca58-6f8f45d8a37mr231020b3a.28.1716494920901; Thu, 23 May 2024 13:08:40 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716494920; cv=pass; d=google.com; s=arc-20160816; b=qFovDtPzxlu8W13O2iyBc+3NYbeFcyhfXJLVn1FlORaeDzaDOV8bFbpySYmcSkNkej 3LJjW7aZc/LmqBdGqPomDD08kRJFWMFj/wRopX2kGXD4haG5IeLmOBb9jn0MESJTxK3y dscMAEhM8L0fnoCaYXY5Unt68ub1Z+xLm+HA0WixGlHXKR6J+EriK/zuQMiPF3NEqRPu nV8nlCL4Ywab8Mj/QPgLbUHZ+uZK7sSbhlkbo12g5vpB7G4RGpVQjwuTe/55gpikBJ+c JVEPcQvl/fDLLXf+cBkIWqUgrMVpau3+q0JCHahUOBA7JZB/HcfRyQRd6h7Y98Hogp92 tT0g== 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=dGvuPgGsg0VxczO9LN+XhOWYu9n5IUtz1G620Nf/Nuk=; fh=mrSudHu1cNdwDef6YMqMmE4TOkN75ugFhIln0OhjIBA=; b=dtDtiHwCkEu81LvjMSbsVtO0YjUmp1QW2tB1Pn5wsf6Rw16S5ymHVaK4FzSrz75bV7 EO3VCTrPMdUUkSdrcWWLAXzQpFYPHL/bLsQvCM6y3V3uXcY//I8hxBLd0AJA5B81lIf+ 7xAeBpXMgyiWgR8co5BxCq12010YnoPu0WPrXgpIsVq34xTGbhRPAMDbrgjLTfEA80Ei EGZJC8tL6eYdi3CFDJ4Z/GzPpb8oMKaSYNbeb5Q+ROjcAmrmvOmTkzLxQnSgoAvX0cSv M5gdcGqY/Ds4//gIksp2ZS9FWBe+x/qqwHJKgSeSehlV2CH4XlVUhOLQiSS38vKWPlTp F/hg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=fail header.i=@igalia.com header.s=20170329 header.b=PWEQh5uT; arc=pass (i=1 spf=pass spfdomain=igalia.com dkim=pass dkdomain=igalia.com); spf=pass (google.com: domain of linux-kernel+bounces-187956-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-187956-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id d2e1a72fcca58-6f8fd9fe610si4912b3a.371.2024.05.23.13.08.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 May 2024 13:08:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-187956-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=fail header.i=@igalia.com header.s=20170329 header.b=PWEQh5uT; arc=pass (i=1 spf=pass spfdomain=igalia.com dkim=pass dkdomain=igalia.com); spf=pass (google.com: domain of linux-kernel+bounces-187956-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-187956-linux.lists.archive=gmail.com@vger.kernel.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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 2F13DB23AB5 for ; Thu, 23 May 2024 20:07:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D022784D3E; Thu, 23 May 2024 20:07:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=igalia.com header.i=@igalia.com header.b="PWEQh5uT" Received: from fanzine2.igalia.com (fanzine.igalia.com [178.60.130.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C1051535D4; Thu, 23 May 2024 20:07:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=178.60.130.6 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716494860; cv=none; b=E5JloG92r+Z5LuMsPDsp1FCD7N8lIRgWppt3dY8pKjA+Nvm25PZmz8s0CkMpaobNi4avcgsUIsGuRXzlscbUAEHGswxXFLZmILq2mPzzGEvi0Y9NPQqO0+ldA2ASwGLPldnx8hd1jr9H7MnAVIk43V3DuUzwAeUSqvNTkGfQjMc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716494860; c=relaxed/simple; bh=Opd2bSlYcIyMBwk1KoGA72+CQf9BDRA2NiY+NSNklQw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=aQVJzZgyqpd/j39Sc88dvfi2xQqF3sJo462/afFWvRwE/153qdkgVTpH5CGz0tmH5nMEbkBzA3uSIl6AdZGjemgQdkEFEO7mXZ7E3u0GjOBsvTqfnlnrI/9FkhCeruIwP6A88uAgViVMCClRbBMHyRNAdGl+WHoM6Eg04Lfd3do= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=igalia.com; spf=pass smtp.mailfrom=igalia.com; dkim=pass (2048-bit key) header.d=igalia.com header.i=@igalia.com header.b=PWEQh5uT; arc=none smtp.client-ip=178.60.130.6 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=igalia.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=igalia.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=dGvuPgGsg0VxczO9LN+XhOWYu9n5IUtz1G620Nf/Nuk=; b=PWEQh5uThVxdrbuQH4ohp4U9R8 gnq4XROotBE3zo3BSuC2NAqC1tvDbOXX8B+8HgaoDEKshIodHRP9L/bWiHPhEF7j4d/c4p+9EMaqm Ed27V8n7UYu32NgVaoYbaiyZ/JQuxnJnXzSU5c/2oJNanXl4zZqjkBrVT31WHxJe4rb2v6SVZGgRX 4e8AIEdeySt/Ec28+DTslnjz6yWOYt0L7cY4uuxQHJc8nJ9VBbCknW72msAaYvWPMbKF3XXm1x1A1 j4Xv4yiEO2RdSwdZ+C0/wWxDfAZvzMzRL5Zw2LIH1vrNZcunF+K4/Tu4qJ7kiv6RIB6NF7P3mfNxM j3AQWYfQ==; Received: from [191.8.29.37] (helo=localhost.localdomain) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1sAEig-00BhS0-Oi; Thu, 23 May 2024 22:07:23 +0200 From: =?UTF-8?q?Andr=C3=A9=20Almeida?= To: Mathieu Desnoyers , Peter Zijlstra Cc: linux-kernel@vger.kernel.org, "Thomas Gleixner" , "Paul E . McKenney" , "Boqun Feng" , "H . Peter Anvin" , "Paul Turner" , linux-api@vger.kernel.org, "Christian Brauner" , "Florian Weimer" , David.Laight@ACULAB.COM, carlos@redhat.com, "Peter Oskolkov" , "Alexander Mikhalitsyn" , "Chris Kennelly" , "Ingo Molnar" , "Darren Hart" , "Davidlohr Bueso" , =?UTF-8?q?Andr=C3=A9=20Almeida?= , libc-alpha@sourceware.org, "Steven Rostedt" , "Jonathan Corbet" , "Noah Goldstein" , "Daniel Colascione" , longman@redhat.com, kernel-dev@igalia.com Subject: [PATCH v2 1/1] futex: Add FUTEX_SPIN operation Date: Thu, 23 May 2024 17:07:04 -0300 Message-ID: <20240523200704.281514-2-andrealmeid@igalia.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240523200704.281514-1-andrealmeid@igalia.com> References: <20240523200704.281514-1-andrealmeid@igalia.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a new mode for futex wait, the futex spin. Given the FUTEX2_SPIN flag, parse the futex value as the TID of the lock owner. Then, before going to the normal wait path, spins while the lock owner is running in a different CPU, to avoid the whole context switch operation and to quickly return to userspace. If the lock owner is not running, just sleep as the normal futex wait path. The user value is masked with FUTEX_TID_MASK, to allow some bits for future use. The check for the owner to be running or not is important to avoid spinning for something that won't be released quickly. Userspace is responsible on providing the proper TID, the kernel does a basic check. Signed-off-by: André Almeida --- include/uapi/linux/futex.h | 2 +- kernel/futex/futex.h | 6 ++- kernel/futex/waitwake.c | 78 +++++++++++++++++++++++++++++++++++++- 3 files changed, 82 insertions(+), 4 deletions(-) diff --git a/include/uapi/linux/futex.h b/include/uapi/linux/futex.h index d2ee625ea189..d77d692ffac2 100644 --- a/include/uapi/linux/futex.h +++ b/include/uapi/linux/futex.h @@ -63,7 +63,7 @@ #define FUTEX2_SIZE_U32 0x02 #define FUTEX2_SIZE_U64 0x03 #define FUTEX2_NUMA 0x04 - /* 0x08 */ +#define FUTEX2_SPIN 0x08 /* 0x10 */ /* 0x20 */ /* 0x40 */ diff --git a/kernel/futex/futex.h b/kernel/futex/futex.h index 8b195d06f4e8..180c1c10dc81 100644 --- a/kernel/futex/futex.h +++ b/kernel/futex/futex.h @@ -37,6 +37,7 @@ #define FLAGS_HAS_TIMEOUT 0x0040 #define FLAGS_NUMA 0x0080 #define FLAGS_STRICT 0x0100 +#define FLAGS_SPIN 0x0200 /* FUTEX_ to FLAGS_ */ static inline unsigned int futex_to_flags(unsigned int op) @@ -52,7 +53,7 @@ static inline unsigned int futex_to_flags(unsigned int op) return flags; } -#define FUTEX2_VALID_MASK (FUTEX2_SIZE_MASK | FUTEX2_PRIVATE) +#define FUTEX2_VALID_MASK (FUTEX2_SIZE_MASK | FUTEX2_PRIVATE | FUTEX2_SPIN) /* FUTEX2_ to FLAGS_ */ static inline unsigned int futex2_to_flags(unsigned int flags2) @@ -65,6 +66,9 @@ static inline unsigned int futex2_to_flags(unsigned int flags2) if (flags2 & FUTEX2_NUMA) flags |= FLAGS_NUMA; + if (flags2 & FUTEX2_SPIN) + flags |= FLAGS_SPIN; + return flags; } diff --git a/kernel/futex/waitwake.c b/kernel/futex/waitwake.c index 3a10375d9521..276c03804b92 100644 --- a/kernel/futex/waitwake.c +++ b/kernel/futex/waitwake.c @@ -372,6 +372,73 @@ void futex_wait_queue(struct futex_hash_bucket *hb, struct futex_q *q, __set_current_state(TASK_RUNNING); } +static inline bool task_on_cpu(struct task_struct *p) +{ +#ifdef CONFIG_SMP + return !!(p->on_cpu); +#else + return false; +#endif +} + +static int futex_spin(struct futex_hash_bucket *hb, struct futex_q *q, + struct hrtimer_sleeper *timeout, u32 uval) +{ + struct task_struct *p; + pid_t pid = uval & FUTEX_TID_MASK; + + p = find_get_task_by_vpid(pid); + + /* no task found, maybe it already exited */ + if (!p) { + futex_q_unlock(hb); + return -EAGAIN; + } + + /* can't spin in a kernel task */ + if (unlikely(p->flags & PF_KTHREAD)) { + put_task_struct(p); + futex_q_unlock(hb); + return -EPERM; + } + + futex_queue(q, hb); + + if (timeout) + hrtimer_sleeper_start_expires(timeout, HRTIMER_MODE_ABS); + + while (1) { + if (likely(!plist_node_empty(&q->list))) { + if (timeout && !timeout->task) + goto exit; + + if (task_on_cpu(p)) { + /* spin */ + continue; + } else { + /* task is not running, sleep */ + break; + } + } else { + goto exit; + } + } + + /* spinning didn't work, go to the normal path */ + set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE); + + if (likely(!plist_node_empty(&q->list))) { + if (!timeout || timeout->task) + schedule(); + } + + __set_current_state(TASK_RUNNING); + +exit: + put_task_struct(p); + return 0; +} + /** * futex_unqueue_multiple - Remove various futexes from their hash bucket * @v: The list of futexes to unqueue @@ -665,8 +732,15 @@ int __futex_wait(u32 __user *uaddr, unsigned int flags, u32 val, if (ret) return ret; - /* futex_queue and wait for wakeup, timeout, or a signal. */ - futex_wait_queue(hb, &q, to); + if (flags & FLAGS_SPIN) { + ret = futex_spin(hb, &q, to, val); + + if (ret) + return ret; + } else { + /* futex_queue and wait for wakeup, timeout, or a signal. */ + futex_wait_queue(hb, &q, to); + } /* If we were woken (and unqueued), we succeeded, whatever. */ if (!futex_unqueue(&q)) -- 2.45.1