All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: tglx@linutronix.de, axboe@kernel.dk
Cc: linux-kernel@vger.kernel.org, peterz@infradead.org,
	mingo@redhat.com, dvhart@infradead.org, dave@stgolabs.net,
	andrealmeid@igalia.com, Andrew Morton <akpm@linux-foundation.org>,
	urezki@gmail.com, hch@infradead.org, lstoakes@gmail.com,
	Arnd Bergmann <arnd@arndb.de>,
	linux-api@vger.kernel.org, linux-mm@kvack.org,
	linux-arch@vger.kernel.org, malteskarupke@web.de
Subject: [PATCH  v2 13/14] futex: Enable FUTEX2_{8,16}
Date: Mon, 07 Aug 2023 14:18:56 +0200	[thread overview]
Message-ID: <20230807123323.641470179@infradead.org> (raw)
In-Reply-To: 20230807121843.710612856@infradead.org

When futexes are no longer u32 aligned, the lower offset bits are no
longer available to put type info in. However, since offset is the
offset within a page, there are plenty bits available on the top end.

After that, pass flags into futex_get_value_locked() for WAIT and
disallow FUTEX2_SIZE_U64 instead of mandating FUTEX2_SIZE_U32.

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
 include/linux/futex.h   |   11 ++++++-----
 kernel/futex/core.c     |    9 +++++++++
 kernel/futex/futex.h    |    4 ++--
 kernel/futex/waitwake.c |    5 +++--
 4 files changed, 20 insertions(+), 9 deletions(-)

--- a/include/linux/futex.h
+++ b/include/linux/futex.h
@@ -16,18 +16,19 @@ struct task_struct;
  * The key type depends on whether it's a shared or private mapping.
  * Don't rearrange members without looking at hash_futex().
  *
- * offset is aligned to a multiple of sizeof(u32) (== 4) by definition.
- * We use the two low order bits of offset to tell what is the kind of key :
+ * offset is the position within a page and is in the range [0, PAGE_SIZE).
+ * The high bits of the offset indicate what kind of key this is:
  *  00 : Private process futex (PTHREAD_PROCESS_PRIVATE)
  *       (no reference on an inode or mm)
  *  01 : Shared futex (PTHREAD_PROCESS_SHARED)
  *	mapped on a file (reference on the underlying inode)
  *  10 : Shared futex (PTHREAD_PROCESS_SHARED)
  *       (but private mapping on an mm, and reference taken on it)
-*/
+ */
 
-#define FUT_OFF_INODE    1 /* We set bit 0 if key has a reference on inode */
-#define FUT_OFF_MMSHARED 2 /* We set bit 1 if key has a reference on mm */
+#define FUT_OFF_INODE    (PAGE_SIZE << 0)
+#define FUT_OFF_MMSHARED (PAGE_SIZE << 1)
+#define FUT_OFF_SIZE	 (PAGE_SIZE << 2)
 
 union futex_key {
 	struct {
--- a/kernel/futex/core.c
+++ b/kernel/futex/core.c
@@ -308,6 +308,15 @@ int get_futex_key(void __user *uaddr, un
 	}
 
 	/*
+	 * Encode the futex size in the offset. This makes cross-size
+	 * wake-wait fail -- see futex_match().
+	 *
+	 * NOTE that cross-size wake-wait is fundamentally broken wrt
+	 * FLAGS_NUMA but could possibly work for !NUMA.
+	 */
+	key->both.offset |= FUT_OFF_SIZE * (flags & FLAGS_SIZE_MASK);
+
+	/*
 	 * PROCESS_PRIVATE futexes are fast.
 	 * As the mm cannot disappear under us and the 'key' only needs
 	 * virtual address, we dont even have to find the underlying vma.
--- a/kernel/futex/futex.h
+++ b/kernel/futex/futex.h
@@ -79,8 +79,8 @@ static inline bool futex_flags_valid(uns
 			return false;
 	}
 
-	/* Only 32bit futexes are implemented -- for now */
-	if ((flags & FLAGS_SIZE_MASK) != FLAGS_SIZE_32)
+	/* 64bit futexes aren't implemented -- yet */
+	if ((flags & FLAGS_SIZE_MASK) == FLAGS_SIZE_64)
 		return false;
 
 	/*
--- a/kernel/futex/waitwake.c
+++ b/kernel/futex/waitwake.c
@@ -434,11 +434,12 @@ static int futex_wait_multiple_setup(str
 
 	for (i = 0; i < count; i++) {
 		u32 __user *uaddr = (u32 __user *)(unsigned long)vs[i].w.uaddr;
+		unsigned int flags = vs[i].w.flags;
 		struct futex_q *q = &vs[i].q;
 		u32 val = vs[i].w.val;
 
 		hb = futex_q_lock(q);
-		ret = futex_get_value_locked(&uval, uaddr, FLAGS_SIZE_32);
+		ret = futex_get_value_locked(&uval, uaddr, flags);
 
 		if (!ret && uval == val) {
 			/*
@@ -606,7 +607,7 @@ int futex_wait_setup(u32 __user *uaddr,
 retry_private:
 	*hb = futex_q_lock(q);
 
-	ret = futex_get_value_locked(&uval, uaddr, FLAGS_SIZE_32);
+	ret = futex_get_value_locked(&uval, uaddr, flags);
 
 	if (ret) {
 		futex_q_unlock(*hb);



  parent reply	other threads:[~2023-08-07 12:37 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-07 12:18 [PATCH v2 00/14] futex: More futex2 bits Peter Zijlstra
2023-08-07 12:18 ` [PATCH v2 01/14] futex: Clarify FUTEX2 flags Peter Zijlstra
2023-08-07 23:51   ` André Almeida
2023-08-07 12:18 ` [PATCH v2 02/14] futex: Extend the " Peter Zijlstra
2023-08-07 18:56   ` Thomas Gleixner
2023-08-08  0:02   ` André Almeida
2023-08-07 12:18 ` [PATCH v2 03/14] futex: Flag conversion Peter Zijlstra
2023-08-07 18:58   ` Thomas Gleixner
2023-08-08  0:52   ` André Almeida
2023-08-07 12:18 ` [PATCH v2 04/14] futex: Validate futex value against futex size Peter Zijlstra
2023-08-07 12:18 ` [PATCH v2 05/14] futex: Add sys_futex_wake() Peter Zijlstra
2023-08-07 19:00   ` Thomas Gleixner
2023-08-09 22:25   ` André Almeida
2023-08-10 12:13     ` Peter Zijlstra
2023-08-10 17:01       ` André Almeida
2023-08-07 12:18 ` [PATCH v2 06/14] futex: Add sys_futex_wait() Peter Zijlstra
2023-08-07 12:18 ` [PATCH v2 07/14] futex: Propagate flags into get_futex_key() Peter Zijlstra
2023-08-07 12:18 ` [PATCH v2 08/14] futex: Add flags2 argument to futex_requeue() Peter Zijlstra
2023-08-07 19:01   ` Thomas Gleixner
2023-08-07 12:18 ` [PATCH v2 09/14] futex: Add sys_futex_requeue() Peter Zijlstra
2023-08-07 19:05   ` Thomas Gleixner
2023-08-07 12:18 ` [PATCH v2 10/14] mm: Add vmalloc_huge_node() Peter Zijlstra
2023-08-07 12:18 ` [PATCH v2 11/14] futex: Implement FUTEX2_NUMA Peter Zijlstra
2023-08-07 21:11   ` Thomas Gleixner
2023-08-08  8:54     ` Peter Zijlstra
2023-08-08  9:12       ` Thomas Gleixner
2023-08-07 12:18 ` [PATCH v2 12/14] futex: Propagate flags into futex_get_value_locked() Peter Zijlstra
2023-08-07 21:12   ` Thomas Gleixner
2023-08-07 12:18 ` Peter Zijlstra [this message]
2023-08-07 21:15   ` [PATCH v2 13/14] futex: Enable FUTEX2_{8,16} Thomas Gleixner
2023-08-07 12:18 ` [PATCH v2 14/14] futex,selftests: Extend the futex selftests Peter Zijlstra
2023-08-08 20:32 ` [PATCH v2 00/14] futex: More futex2 bits Jens Axboe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230807123323.641470179@infradead.org \
    --to=peterz@infradead.org \
    --cc=akpm@linux-foundation.org \
    --cc=andrealmeid@igalia.com \
    --cc=arnd@arndb.de \
    --cc=axboe@kernel.dk \
    --cc=dave@stgolabs.net \
    --cc=dvhart@infradead.org \
    --cc=hch@infradead.org \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=lstoakes@gmail.com \
    --cc=malteskarupke@web.de \
    --cc=mingo@redhat.com \
    --cc=tglx@linutronix.de \
    --cc=urezki@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.