From nobody Mon Nov 25 18:23:54 2024 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (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 A0F2F18BC0B; Fri, 25 Oct 2024 09:41:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729849272; cv=none; b=ty7QKAqiBXb7h91vsjauA0XmDJpMSE8uMw05l1uwK9uAC1mdi4+JwGBMtf0r6LtFkxTsNZBr/z65ir/hymrf4qaTVHK7xsXHl9kM0F8Mbttmiuy9wZzeP8sH+0KC+bna3HmprNOa+HrvdFVmrjU2JtBrKrkzE8OnF+86YyGN9SE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729849272; c=relaxed/simple; bh=Hah2EZUe7CgPQAjXHt6b7wT95EAGMHPfyzvXP0UEAKw=; h=Message-Id:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=j5owjbsUJMDwoJHkeMC1DBobyOHJwg+wvskMMgtE9zDa3uR3tKdGA9JLHNftVX/l72GHSyekdY6tdDXQyTNkMtmLE/NmOsJGKqrqetvF2B0l3+NQvVgFBrP7Ib9QiQHD3ZFxnAlzEdo/orrXwB9/WLmccKsA7iMS5sFn9hCigFs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=McNtDpCw; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="McNtDpCw" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-Id:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=0AEHQ3fQNKjih+WCnZnELy/0uqQ93GQb79KeQAZqpzY=; b=McNtDpCw6pjIEqx1QyIATJefZK jqyWF8iz8+9HX4WnJpzMD8QeilUseTQ/PQxpt024ZVD/RTbhE2tzhZ+M/+sMzWodfZvKGmRh7vTDY D945hn22VNwRJmsnRB3v9+4WCSKNbd4TATVzNAbtBJt9+x4jkyi5nOYLY1wNL/WNVQVfXYEuvvG2O hPH4rkssuZZjpoq8BUzAO3IwaAYFV0c+9uEE4NgB5vt9Opiyc99u2bFWew65H/TLWbqdFct9PK3lN zEzc6m0D1tLFmVcsU/VQR5xdkfd2KZ5i6yRkA2knj8/7wCTsbUcl606O1YdIYtnxPvDFZ+buX759U aNbpbWRg==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1t4GoU-000000054PN-00f2; Fri, 25 Oct 2024 09:40:58 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 0) id CDA73300ABE; Fri, 25 Oct 2024 11:40:57 +0200 (CEST) Message-Id: <20241025093944.372391936@infradead.org> User-Agent: quilt/0.65 Date: Fri, 25 Oct 2024 11:03:48 +0200 From: Peter Zijlstra To: tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, peterz@infradead.org, mingo@redhat.com, dvhart@infradead.org, dave@stgolabs.net, andrealmeid@igalia.com, Andrew Morton , urezki@gmail.com, hch@infradead.org, lstoakes@gmail.com, Arnd Bergmann , linux-api@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, malteskarupke@web.de, cl@linux.com, llong@redhat.com, Christoph Hellwig Subject: [PATCH 1/6] mm: Add vmalloc_huge_node() References: <20241025090347.244183920@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" To enable node specific hash-tables. Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Christoph Hellwig Reviewed-by: Christoph Lameter Reviewed-by: Davidlohr Bueso Reviewed-by: Uladzislau Rezki (Sony) --- include/linux/vmalloc.h | 3 +++ mm/vmalloc.c | 7 +++++++ 2 files changed, 10 insertions(+) --- a/include/linux/vmalloc.h +++ b/include/linux/vmalloc.h @@ -177,6 +177,9 @@ void *__vmalloc_node_noprof(unsigned lon void *vmalloc_huge_noprof(unsigned long size, gfp_t gfp_mask) __alloc_size= (1); #define vmalloc_huge(...) alloc_hooks(vmalloc_huge_noprof(__VA_ARGS__)) =20 +void *vmalloc_huge_node_noprof(unsigned long size, gfp_t gfp_mask, int nod= e) __alloc_size(1); +#define vmalloc_huge_node(...) alloc_hooks(vmalloc_huge_node_noprof(__VA_A= RGS__)) + extern void *__vmalloc_array_noprof(size_t n, size_t size, gfp_t flags) __= alloc_size(1, 2); #define __vmalloc_array(...) alloc_hooks(__vmalloc_array_noprof(__VA_ARGS_= _)) =20 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -3948,6 +3948,13 @@ void *vmalloc_huge_noprof(unsigned long } EXPORT_SYMBOL_GPL(vmalloc_huge_noprof); =20 +void *vmalloc_huge_node_noprof(unsigned long size, gfp_t gfp_mask, int nod= e) +{ + return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END, + gfp_mask, PAGE_KERNEL, VM_ALLOW_HUGE_VMAP, + node, __builtin_return_address(0)); +} + /** * vzalloc - allocate virtually contiguous memory with zero fill * @size: allocation size From nobody Mon Nov 25 18:23:54 2024 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (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 A0EC9175A6; Fri, 25 Oct 2024 09:41:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729849273; cv=none; b=CWAVcvW2SJ85Ej4m+86GgWn+Ro9uZlQctcp/2/pwXYe19qNBeK94rAINw3NVUZzJ75WFQ+3lZ04DTbPKNd+o3YS41Yw24CN1eiq7elM/EtJcf51TI/2UhI8ME07rnZxJd2IdPZZXN/wBxBs9GB+kZnUGrJgsZH9ezJut3xk/xco= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729849273; c=relaxed/simple; bh=KDCxAA2t4R126zWcHSt54xfK4tfXCDSdh3BN1NDkNf0=; h=Message-Id:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=ZwpKNAknv3mNsMMGZLIv8ngzUwUyXX5hUcg9qrQmOEK85/t4DrLAeQrn0A1l0+0dExBq3ftLzzizYiQ+2O+PlukoTM22ZTyTqbKkolJTyF6P8cZO5UmGBjpcyF4kXPKOhwbonHpsgEnJ9yhiNeBMnjIpImVYhL5LJUz7N68iX/Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=Y2cyR1HP; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="Y2cyR1HP" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-Id:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=RZyeqL/sDawSYArfjik+bzTHUaH0yRhT/D7WDZYPYBA=; b=Y2cyR1HPqEIEF7pPOyzphBJXKY 5m6VIXPj2PJRcDkKRF8+NVUBugBJk6WNVYlLYSmQQ8F33FYEpW4Sqh1ioL5UUNbrSkDCQCbIt/mbU C/qEC6Q4O77K8rlLfhA2cwul6CNsYEDCKf+19iOiRxXrJKjsKXtCfJQ0V6j0mI8qWRPvGZrutb1gE zagsCUxVojU8zDApLn4H/SbdldGgPTcoKed5Jc0C4QZBFaI3hUx6A7I7jJKSX4MyKbb19A9SlktQ6 RSeJ9D7aHgah0fGVuSgl3kyVxp1uzgnGQjtHjauc7Fqsg12U6Wwvi0k8UOmvnJUXnhpMSDBg3E45S vRnXCBzQ==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1t4GoU-000000054PO-00xQ; Fri, 25 Oct 2024 09:40:58 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 0) id D1B7C300B40; Fri, 25 Oct 2024 11:40:57 +0200 (CEST) Message-Id: <20241025093944.485691531@infradead.org> User-Agent: quilt/0.65 Date: Fri, 25 Oct 2024 11:03:49 +0200 From: Peter Zijlstra To: tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, peterz@infradead.org, mingo@redhat.com, dvhart@infradead.org, dave@stgolabs.net, andrealmeid@igalia.com, Andrew Morton , urezki@gmail.com, hch@infradead.org, lstoakes@gmail.com, Arnd Bergmann , linux-api@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, malteskarupke@web.de, cl@linux.com, llong@redhat.com Subject: [PATCH 2/6] futex: Implement FUTEX2_NUMA References: <20241025090347.244183920@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Extend the futex2 interface to be numa aware. When FUTEX2_NUMA is specified for a futex, the user value is extended to two words (of the same size). The first is the user value we all know, the second one will be the node to place this futex on. struct futex_numa_32 { u32 val; u32 node; }; When node is set to ~0, WAIT will set it to the current node_id such that WAKE knows where to find it. If userspace corrupts the node value between WAIT and WAKE, the futex will not be found and no wakeup will happen. When FUTEX2_NUMA is not set, the node is simply an extention of the hash, such that traditional futexes are still interleaved over the nodes. This is done to avoid having to have a separate !numa hash-table. Signed-off-by: Peter Zijlstra (Intel) --- include/linux/futex.h | 3 + include/uapi/linux/futex.h | 8 ++ kernel/futex/core.c | 128 ++++++++++++++++++++++++++++++++++++----= ----- kernel/futex/futex.h | 17 +++++ 4 files changed, 131 insertions(+), 25 deletions(-) --- a/include/linux/futex.h +++ b/include/linux/futex.h @@ -34,6 +34,7 @@ union futex_key { u64 i_seq; unsigned long pgoff; unsigned int offset; + /* unsigned int node; */ } shared; struct { union { @@ -42,11 +43,13 @@ union futex_key { }; unsigned long address; unsigned int offset; + /* unsigned int node; */ } private; struct { u64 ptr; unsigned long word; unsigned int offset; + unsigned int node; /* NOT hashed! */ } both; }; =20 --- a/include/uapi/linux/futex.h +++ b/include/uapi/linux/futex.h @@ -74,6 +74,14 @@ /* do not use */ #define FUTEX_32 FUTEX2_SIZE_U32 /* historical accident :-( */ =20 + +/* + * When FUTEX2_NUMA doubles the futex word, the second word is a node valu= e. + * The special value -1 indicates no-node. This is the same value as + * NUMA_NO_NODE, except that value is not ABI, this is. + */ +#define FUTEX_NO_NODE (-1) + /* * Max numbers of elements in a futex_waitv array */ --- a/kernel/futex/core.c +++ b/kernel/futex/core.c @@ -36,7 +36,8 @@ #include #include #include -#include +#include +#include #include #include =20 @@ -49,12 +50,14 @@ * reside in the same cacheline. */ static struct { - struct futex_hash_bucket *queues; unsigned long hashsize; + unsigned int hashshift; + struct futex_hash_bucket *queues[MAX_NUMNODES]; } __futex_data __read_mostly __aligned(2*sizeof(long)); -#define futex_queues (__futex_data.queues) -#define futex_hashsize (__futex_data.hashsize) =20 +#define futex_hashsize (__futex_data.hashsize) +#define futex_hashshift (__futex_data.hashshift) +#define futex_queues (__futex_data.queues) =20 /* * Fault injections for futexes. @@ -107,6 +110,26 @@ late_initcall(fail_futex_debugfs); =20 #endif /* CONFIG_FAIL_FUTEX */ =20 +static int futex_get_value(u32 *val, u32 __user *from, unsigned int flags) +{ + switch (futex_size(flags)) { + case 1: return __get_user(*val, (u8 __user *)from); + case 2: return __get_user(*val, (u16 __user *)from); + case 4: return __get_user(*val, (u32 __user *)from); + default: BUG(); + } +} + +static int futex_put_value(u32 val, u32 __user *to, unsigned int flags) +{ + switch (futex_size(flags)) { + case 1: return __put_user(val, (u8 __user *)to); + case 2: return __put_user(val, (u16 __user *)to); + case 4: return __put_user(val, (u32 __user *)to); + default: BUG(); + } +} + /** * futex_hash - Return the hash bucket in the global hash * @key: Pointer to the futex key for which the hash is calculated @@ -116,10 +139,29 @@ late_initcall(fail_futex_debugfs); */ struct futex_hash_bucket *futex_hash(union futex_key *key) { - u32 hash =3D jhash2((u32 *)key, offsetof(typeof(*key), both.offset) / 4, + u32 hash =3D jhash2((u32 *)key, + offsetof(typeof(*key), both.offset) / sizeof(u32), key->both.offset); + int node =3D key->both.node; =20 - return &futex_queues[hash & (futex_hashsize - 1)]; + if (node =3D=3D FUTEX_NO_NODE) { + /* + * In case of !FLAGS_NUMA, use some unused hash bits to pick a + * node -- this ensures regular futexes are interleaved across + * the nodes and avoids having to allocate multiple + * hash-tables. + * + * NOTE: this isn't perfectly uniform, but it is fast and + * handles sparse node masks. + */ + node =3D (hash >> futex_hashshift) % nr_node_ids; + if (!node_possible(node)) { + node =3D find_next_bit_wrap(node_possible_map.bits, + nr_node_ids, node); + } + } + + return &futex_queues[node][hash & (futex_hashsize - 1)]; } =20 =20 @@ -219,7 +261,7 @@ static u64 get_inode_sequence_number(str * * lock_page() might sleep, the caller should not hold a spinlock. */ -int get_futex_key(u32 __user *uaddr, unsigned int flags, union futex_key *= key, +int get_futex_key(void __user *uaddr, unsigned int flags, union futex_key = *key, enum futex_access rw) { unsigned long address =3D (unsigned long)uaddr; @@ -227,25 +269,49 @@ int get_futex_key(u32 __user *uaddr, uns struct page *page; struct folio *folio; struct address_space *mapping; - int err, ro =3D 0; + int node, err, size, ro =3D 0; bool fshared; =20 fshared =3D flags & FLAGS_SHARED; + size =3D futex_size(flags); + if (flags & FLAGS_NUMA) + size *=3D 2; =20 /* * The futex address must be "naturally" aligned. */ key->both.offset =3D address % PAGE_SIZE; - if (unlikely((address % sizeof(u32)) !=3D 0)) + if (unlikely((address % size) !=3D 0)) return -EINVAL; address -=3D key->both.offset; =20 - if (unlikely(!access_ok(uaddr, sizeof(u32)))) + if (unlikely(!access_ok(uaddr, size))) return -EFAULT; =20 if (unlikely(should_fail_futex(fshared))) return -EFAULT; =20 + if (flags & FLAGS_NUMA) { + void __user *naddr =3D uaddr + size / 2; + + if (futex_get_value(&node, naddr, flags)) + return -EFAULT; + + if (node =3D=3D FUTEX_NO_NODE) { + node =3D numa_node_id(); + if (futex_put_value(node, naddr, flags)) + return -EFAULT; + + } else if (node >=3D MAX_NUMNODES || !node_possible(node)) { + return -EINVAL; + } + + key->both.node =3D node; + + } else { + key->both.node =3D FUTEX_NO_NODE; + } + /* * PROCESS_PRIVATE futexes are fast. * As the mm cannot disappear under us and the 'key' only needs @@ -1148,26 +1214,42 @@ void futex_exit_release(struct task_stru =20 static int __init futex_init(void) { - unsigned int futex_shift; - unsigned long i; + unsigned int order, n; + unsigned long size, i; =20 #ifdef CONFIG_BASE_SMALL futex_hashsize =3D 16; #else - futex_hashsize =3D roundup_pow_of_two(256 * num_possible_cpus()); + futex_hashsize =3D 256 * num_possible_cpus(); + futex_hashsize /=3D num_possible_nodes(); + futex_hashsize =3D roundup_pow_of_two(futex_hashsize); #endif + futex_hashshift =3D ilog2(futex_hashsize); + size =3D sizeof(struct futex_hash_bucket) * futex_hashsize; + order =3D get_order(size); + + for_each_node(n) { + struct futex_hash_bucket *table; + + if (order > MAX_ORDER) + table =3D vmalloc_huge_node(size, GFP_KERNEL, n); + else + table =3D alloc_pages_exact_nid(n, size, GFP_KERNEL); + + BUG_ON(!table); + + for (i =3D 0; i < futex_hashsize; i++) { + atomic_set(&table[i].waiters, 0); + spin_lock_init(&table[i].lock); + plist_head_init(&table[i].chain); + } =20 - futex_queues =3D alloc_large_system_hash("futex", sizeof(*futex_queues), - futex_hashsize, 0, 0, - &futex_shift, NULL, - futex_hashsize, futex_hashsize); - futex_hashsize =3D 1UL << futex_shift; - - for (i =3D 0; i < futex_hashsize; i++) { - atomic_set(&futex_queues[i].waiters, 0); - plist_head_init(&futex_queues[i].chain); - spin_lock_init(&futex_queues[i].lock); + futex_queues[n] =3D table; } + pr_info("futex hash table, %d nodes, %ld entries (order: %d, %lu bytes)\n= ", + num_possible_nodes(), + futex_hashsize, order, + sizeof(struct futex_hash_bucket) * futex_hashsize); =20 return 0; } --- a/kernel/futex/futex.h +++ b/kernel/futex/futex.h @@ -52,7 +52,7 @@ static inline unsigned int futex_to_flag return flags; } =20 -#define FUTEX2_VALID_MASK (FUTEX2_SIZE_MASK | FUTEX2_PRIVATE) +#define FUTEX2_VALID_MASK (FUTEX2_SIZE_MASK | FUTEX2_NUMA | FUTEX2_PRIVATE) =20 /* FUTEX2_ to FLAGS_ */ static inline unsigned int futex2_to_flags(unsigned int flags2) @@ -85,6 +85,19 @@ static inline bool futex_flags_valid(uns if ((flags & FLAGS_SIZE_MASK) !=3D FLAGS_SIZE_32) return false; =20 + /* + * Must be able to represent both FUTEX_NO_NODE and every valid nodeid + * in a futex word. + */ + if (flags & FLAGS_NUMA) { + int bits =3D 8 * futex_size(flags); + u64 max =3D ~0ULL; + + max >>=3D 64 - bits; + if (nr_node_ids >=3D max) + return false; + } + return true; } =20 @@ -193,7 +206,7 @@ enum futex_access { FUTEX_WRITE }; =20 -extern int get_futex_key(u32 __user *uaddr, unsigned int flags, union fute= x_key *key, +extern int get_futex_key(void __user *uaddr, unsigned int flags, union fut= ex_key *key, enum futex_access rw); =20 extern struct hrtimer_sleeper * From nobody Mon Nov 25 18:23:54 2024 Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (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 5B1B51D90B4; Fri, 25 Oct 2024 09:41:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.92.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729849279; cv=none; b=fbdrSYqf4iiGeW3vi6NGAsWoQ4QPLuK9S8I2qPen0sT32MxmKh6ApGOyS75Dx/Y8PgBF4YRPTRG3B85k7LiZA4EEd0lxtELL6MnQGD3/RvNp69+TSX78sUd9mq5jxp96g/8Zvjbi4mQJ97a1/77aj8oyhFdclB9+n6jbnSekE9o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729849279; c=relaxed/simple; bh=WKchFVXnj6SKM18yijVrqOA+u2bmi/cCI+xywo0KwFk=; h=Message-Id:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=URTjYbtGYGr/TxY6vKJqLq9U3ysXyfjcSaWFWw/9Tdl11Hgmlas7Zt9OCJyFbLq/tTOJ48ot10zXLPQnwhWFsGdM7ptpBRZ9qlQcaTZc6lREhtDb2hxok45pdqNeDIXqwsHmJ4xJ11WB/qwWqTadQJMCQ6GJ60rPJNrZw+eGLx8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=mHf3VaB+; arc=none smtp.client-ip=90.155.92.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="mHf3VaB+" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-Id:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=U0D/97Jan/1h5/mHQt4BdsZRHtXtzjdUFE/GVa1R21M=; b=mHf3VaB+HmQHmxesCbnKkAMi2d sZG3k9IRWCQW3O2HIyPWhAEPdZw/T3W1b/yG1wTuewo6QaSkAYvR++sMpfAZVuGx3gAFXcYqQzSQT j4xVtIecla88uLIdFVu1LiFvwB4Cq782IvCH+uJzMi+7MndvdPq+OAGDQKLipcXDSxxMKBmDn/vdg hZG4GTC4vYB7tQPNZKR62OMPaydbPwqbxB3+jfYTpnV+ZwwlH3kRgwWBYGKfLduwn6uwoNeHSNsHi bSqf1vVyNKE51O8hMxkxgID8XJZpxqWOz/FjDNFHXb7tgBbBOe6zscEWeuaj1ox6NMer/4J3zvV/9 pd4m9big==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1t4GoU-00000008sa8-1mSU; Fri, 25 Oct 2024 09:40:59 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 0) id D5DAD301171; Fri, 25 Oct 2024 11:40:57 +0200 (CEST) Message-Id: <20241025093944.598921704@infradead.org> User-Agent: quilt/0.65 Date: Fri, 25 Oct 2024 11:03:50 +0200 From: Peter Zijlstra To: tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, peterz@infradead.org, mingo@redhat.com, dvhart@infradead.org, dave@stgolabs.net, andrealmeid@igalia.com, Andrew Morton , urezki@gmail.com, hch@infradead.org, lstoakes@gmail.com, Arnd Bergmann , linux-api@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, malteskarupke@web.de, cl@linux.com, llong@redhat.com Subject: [PATCH 3/6] futex: Propagate flags into futex_get_value_locked() References: <20241025090347.244183920@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In order to facilitate variable sized futexes propagate the flags into futex_get_value_locked(). No functional change intended. Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Thomas Gleixner --- kernel/futex/core.c | 4 ++-- kernel/futex/futex.h | 2 +- kernel/futex/pi.c | 8 ++++---- kernel/futex/requeue.c | 4 ++-- kernel/futex/waitwake.c | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-) --- a/kernel/futex/core.c +++ b/kernel/futex/core.c @@ -528,12 +528,12 @@ int futex_cmpxchg_value_locked(u32 *curv return ret; } =20 -int futex_get_value_locked(u32 *dest, u32 __user *from) +int futex_get_value_locked(u32 *dest, u32 __user *from, unsigned int flags) { int ret; =20 pagefault_disable(); - ret =3D __get_user(*dest, from); + ret =3D futex_get_value(dest, from, flags); pagefault_enable(); =20 return ret ? -EFAULT : 0; --- a/kernel/futex/futex.h +++ b/kernel/futex/futex.h @@ -239,7 +239,7 @@ extern void futex_wake_mark(struct wake_ =20 extern int fault_in_user_writeable(u32 __user *uaddr); extern int futex_cmpxchg_value_locked(u32 *curval, u32 __user *uaddr, u32 = uval, u32 newval); -extern int futex_get_value_locked(u32 *dest, u32 __user *from); +extern int futex_get_value_locked(u32 *dest, u32 __user *from, unsigned in= t flags); extern struct futex_q *futex_top_waiter(struct futex_hash_bucket *hb, unio= n futex_key *key); =20 extern void __futex_unqueue(struct futex_q *q); --- a/kernel/futex/pi.c +++ b/kernel/futex/pi.c @@ -240,7 +240,7 @@ static int attach_to_pi_state(u32 __user * still is what we expect it to be, otherwise retry the entire * operation. */ - if (futex_get_value_locked(&uval2, uaddr)) + if (futex_get_value_locked(&uval2, uaddr, FLAGS_SIZE_32)) goto out_efault; =20 if (uval !=3D uval2) @@ -359,7 +359,7 @@ static int handle_exit_race(u32 __user * * The same logic applies to the case where the exiting task is * already gone. */ - if (futex_get_value_locked(&uval2, uaddr)) + if (futex_get_value_locked(&uval2, uaddr, FLAGS_SIZE_32)) return -EFAULT; =20 /* If the user space value has changed, try again. */ @@ -527,7 +527,7 @@ int futex_lock_pi_atomic(u32 __user *uad * Read the user space value first so we can validate a few * things before proceeding further. */ - if (futex_get_value_locked(&uval, uaddr)) + if (futex_get_value_locked(&uval, uaddr, FLAGS_SIZE_32)) return -EFAULT; =20 if (unlikely(should_fail_futex(true))) @@ -750,7 +750,7 @@ static int __fixup_pi_state_owner(u32 __ if (!pi_state->owner) newtid |=3D FUTEX_OWNER_DIED; =20 - err =3D futex_get_value_locked(&uval, uaddr); + err =3D futex_get_value_locked(&uval, uaddr, FLAGS_SIZE_32); if (err) goto handle_err; =20 --- a/kernel/futex/requeue.c +++ b/kernel/futex/requeue.c @@ -275,7 +275,7 @@ futex_proxy_trylock_atomic(u32 __user *p u32 curval; int ret; =20 - if (futex_get_value_locked(&curval, pifutex)) + if (futex_get_value_locked(&curval, pifutex, FLAGS_SIZE_32)) return -EFAULT; =20 if (unlikely(should_fail_futex(true))) @@ -453,7 +453,7 @@ int futex_requeue(u32 __user *uaddr1, un if (likely(cmpval !=3D NULL)) { u32 curval; =20 - ret =3D futex_get_value_locked(&curval, uaddr1); + ret =3D futex_get_value_locked(&curval, uaddr1, FLAGS_SIZE_32); =20 if (unlikely(ret)) { double_unlock_hb(hb1, hb2); --- a/kernel/futex/waitwake.c +++ b/kernel/futex/waitwake.c @@ -453,7 +453,7 @@ int futex_wait_multiple_setup(struct fut u32 val =3D vs[i].w.val; =20 hb =3D futex_q_lock(q); - ret =3D futex_get_value_locked(&uval, uaddr); + ret =3D futex_get_value_locked(&uval, uaddr, FLAGS_SIZE_32); =20 if (!ret && uval =3D=3D val) { /* @@ -621,7 +621,7 @@ int futex_wait_setup(u32 __user *uaddr, retry_private: *hb =3D futex_q_lock(q); =20 - ret =3D futex_get_value_locked(&uval, uaddr); + ret =3D futex_get_value_locked(&uval, uaddr, FLAGS_SIZE_32); =20 if (ret) { futex_q_unlock(*hb); From nobody Mon Nov 25 18:23:54 2024 Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (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 4297C1D3633; Fri, 25 Oct 2024 09:41:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.92.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729849278; cv=none; b=eNp/6kIeYXIPt8IvBRnminlSrw5TRnvPau+jLwiMwDa9Aix/WpldB35vy/Bq+uw8upMnVIokkgewTi0enwcxGRq6Z4qSZvrnih31IAgfK688sjwMZWCPuNaXRzju9F69B2LJtfiiAVHmwY0CbG2//PsqGr4blKBb6qCcRO64IoY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729849278; c=relaxed/simple; bh=sXXkT7pRvKDIBsE6m3hBBKOdqF8sUJ05K7oWBaA6SwI=; h=Message-Id:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=EqQxQP8rHC+bBBrG2AypXsQjZlU/tM8lkwIbH+XklkSZexsMcNVdzD4QkK5DgikVCs8+AyDek96v6Wg0rxQbZ4VSCiSlMjOdF0ca4JHqOWrFAM198tG9N64PGjcuNMhGtdtFCxEJMAAB/hqz545W9zcugAsbF+HoyicB/3ZaFMA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=oToK9ZTc; arc=none smtp.client-ip=90.155.92.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="oToK9ZTc" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-Id:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=dbMjdL9P+u2qdFG5YZQzJ7hdT9tBr5pG921aWEix044=; b=oToK9ZTcwKqnYN5RC0kkFefevU u8/ilYOenOsPM+FVeWvDHPVxcg4GGhL6k6STW5NP7C9ct6oVDyUl76zIlqlnzLWUBUkJwh89WFj7Q 5XoeELdhk1ILEt2BpbvKy9ZschyFkZsKVD1/SWt707/NGdhazJYXZP7lmTBDuBWBZX+dxZghMUjIu soy3pCQ3Jo0kqETLQTyhcOX7TsZEFBb6dJhn04uR/LOTn/nyWxdHHxFlw8/P6zSIBS+wVR6aqZu6z ReO4sVqRqBLg0ND9nBe1PXNugWLsBasiG0DRiZKxiKN/M5TGkEehurCM7uqSL5D9qs1MXVLEGrose SG7oVy5w==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1t4GoU-00000008sa9-1l5D; Fri, 25 Oct 2024 09:41:00 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 0) id DA229301D03; Fri, 25 Oct 2024 11:40:57 +0200 (CEST) Message-Id: <20241025093944.707639534@infradead.org> User-Agent: quilt/0.65 Date: Fri, 25 Oct 2024 11:03:51 +0200 From: Peter Zijlstra To: tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, peterz@infradead.org, mingo@redhat.com, dvhart@infradead.org, dave@stgolabs.net, andrealmeid@igalia.com, Andrew Morton , urezki@gmail.com, hch@infradead.org, lstoakes@gmail.com, Arnd Bergmann , linux-api@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, malteskarupke@web.de, cl@linux.com, llong@redhat.com Subject: [PATCH 4/6] futex: Enable FUTEX2_{8,16} References: <20241025090347.244183920@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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) Reviewed-by: Thomas Gleixner --- 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) (=3D=3D 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) -*/ + */ =20 -#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) =20 union futex_key { struct { --- a/kernel/futex/core.c +++ b/kernel/futex/core.c @@ -313,6 +313,15 @@ int get_futex_key(void __user *uaddr, un } =20 /* + * 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. + */ + key->both.offset |=3D 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 @@ -81,8 +81,8 @@ static inline bool futex_flags_valid(uns return false; } =20 - /* Only 32bit futexes are implemented -- for now */ - if ((flags & FLAGS_SIZE_MASK) !=3D FLAGS_SIZE_32) + /* 64bit futexes aren't implemented -- yet */ + if ((flags & FLAGS_SIZE_MASK) =3D=3D FLAGS_SIZE_64) return false; =20 /* --- a/kernel/futex/waitwake.c +++ b/kernel/futex/waitwake.c @@ -449,11 +449,12 @@ int futex_wait_multiple_setup(struct fut =20 for (i =3D 0; i < count; i++) { u32 __user *uaddr =3D (u32 __user *)(unsigned long)vs[i].w.uaddr; + unsigned int flags =3D vs[i].w.flags; struct futex_q *q =3D &vs[i].q; u32 val =3D vs[i].w.val; =20 hb =3D futex_q_lock(q); - ret =3D futex_get_value_locked(&uval, uaddr, FLAGS_SIZE_32); + ret =3D futex_get_value_locked(&uval, uaddr, flags); =20 if (!ret && uval =3D=3D val) { /* @@ -621,7 +622,7 @@ int futex_wait_setup(u32 __user *uaddr, retry_private: *hb =3D futex_q_lock(q); =20 - ret =3D futex_get_value_locked(&uval, uaddr, FLAGS_SIZE_32); + ret =3D futex_get_value_locked(&uval, uaddr, flags); =20 if (ret) { futex_q_unlock(*hb); From nobody Mon Nov 25 18:23:54 2024 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (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 085701D27A4; Fri, 25 Oct 2024 09:41:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729849273; cv=none; b=oGWTkT3jvJsu3Lcm8un0NFEZDURNO5ws+T/RdslJbgyYDdb6paZXlK7gC+5OPCOihCeDFY+/kJjLj52MsDtZ4jhqvAfNkwxyNST2Zr0qeMkM+oAp1DTVmZPgct9pu+dQSul29dW6/hoofJSF1y9NCoxVaf2HiBBVX57ivR24fek= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729849273; c=relaxed/simple; bh=u9cC+VIflPY+XWujgQFWfKRUU/IWFOqABA383V31UD8=; h=Message-Id:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=rK6chcwUNxJOiaMehpaNTvyDGXJP/TMreMxVan5A4LpLH1WIamkbnB7TVZw1jQWtbZJakcUMJ2hPIbmT5sUM86c0im9kZ1F8kPoexAZrnujglMjUv3u766ffsrBs4LS3i/9sigmJNiWNezz9IdlzVYHR+LuCy+GHNim5v6zeQwI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=oh40u7qw; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="oh40u7qw" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-Id:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=BvjuRtWo050xPXsgTFFwYa3Pxgz0X14YXS2BCPIBTTw=; b=oh40u7qwkY/JURIVsPemCwfDPJ 0eudseN8r74Sr1VTVdGipoDt0WYw8UT03IDAnHpPTvt7oBzEXuC2//Wvl+vImQ1HELoQMuxdysCM2 RdJiYvRjjckM+MZJLWlymoVVf+vwSQIHbiR/kr+RjhYsTCxoU5LT6Q2BEIVzVAqRKWoFA2BIvtbOg DiL7E10pWE1c0nvV+KUlqg8oxq19vBO1JlGwEIPYE34CFH37MXGBhd3aaQ21LImZil8hVJYrrP/q+ QqfuzOV956MaNMFpymQ3/oNLOPhXV43/EGa1ta+OwDIld1Nk2ozSq5gNULhLjn+YWQWPqYRTrKtzW SBqZIetQ==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1t4GoU-000000054PT-2Eki; Fri, 25 Oct 2024 09:40:59 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 0) id DE930301D99; Fri, 25 Oct 2024 11:40:57 +0200 (CEST) Message-Id: <20241025093944.817031866@infradead.org> User-Agent: quilt/0.65 Date: Fri, 25 Oct 2024 11:03:52 +0200 From: Peter Zijlstra To: tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, peterz@infradead.org, mingo@redhat.com, dvhart@infradead.org, dave@stgolabs.net, andrealmeid@igalia.com, Andrew Morton , urezki@gmail.com, hch@infradead.org, lstoakes@gmail.com, Arnd Bergmann , linux-api@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, malteskarupke@web.de, cl@linux.com, llong@redhat.com Subject: [PATCH 5/6] futex,selftests: Extend the futex selftests References: <20241025090347.244183920@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Extend the wait/requeue selftests to also cover the futex2 syscalls. Signed-off-by: Peter Zijlstra (Intel) --- tools/testing/selftests/futex/functional/futex_requeue.c | 100 ++= +++++++- tools/testing/selftests/futex/functional/futex_wait.c | 56 ++= ++- tools/testing/selftests/futex/functional/futex_wait_timeout.c | 16 + tools/testing/selftests/futex/functional/futex_wait_wouldblock.c | 28 ++ tools/testing/selftests/futex/functional/futex_waitv.c | 15 - tools/testing/selftests/futex/functional/run.sh | 6=20 tools/testing/selftests/futex/include/futex2test.h | 52 ++= +++ 7 files changed, 243 insertions(+), 30 deletions(-) --- a/tools/testing/selftests/futex/functional/futex_requeue.c +++ b/tools/testing/selftests/futex/functional/futex_requeue.c @@ -7,8 +7,10 @@ =20 #include #include +#include #include "logging.h" #include "futextest.h" +#include "futex2test.h" =20 #define TEST_NAME "futex-requeue" #define timeout_ns 30000000 @@ -16,24 +18,58 @@ =20 volatile futex_t *f1; =20 +bool futex2 =3D 0; +bool mixed =3D 0; + void usage(char *prog) { printf("Usage: %s\n", prog); printf(" -c Use color\n"); + printf(" -n Use futex2 interface\n"); + printf(" -x Use mixed size futex\n"); printf(" -h Display this help message\n"); printf(" -v L Verbosity level: %d=3DQUIET %d=3DCRITICAL %d=3DINFO\n", VQUIET, VCRITICAL, VINFO); } =20 -void *waiterfn(void *arg) +static void *waiterfn(void *arg) { + unsigned int flags =3D 0; struct timespec to; =20 - to.tv_sec =3D 0; - to.tv_nsec =3D timeout_ns; + if (futex2) { + unsigned long mask; + + if (clock_gettime(CLOCK_MONOTONIC, &to)) { + printf("clock_gettime() failed errno %d", errno); + return NULL; + } + + to.tv_nsec +=3D timeout_ns; + if (to.tv_nsec >=3D 1000000000) { + to.tv_sec++; + to.tv_nsec -=3D 1000000000; + } + + if (mixed) { + flags |=3D FUTEX2_SIZE_U16; + mask =3D (unsigned short)(~0U); + } else { + flags |=3D FUTEX2_SIZE_U32; + mask =3D (unsigned int)(~0U); + } + + if (futex2_wait(f1, *f1, mask, flags, + &to, CLOCK_MONOTONIC)) + printf("waiter failed errno %d\n", errno); + } else { + + to.tv_sec =3D 0; + to.tv_nsec =3D timeout_ns; =20 - if (futex_wait(f1, *f1, &to, 0)) - printf("waiter failed errno %d\n", errno); + if (futex_wait(f1, *f1, &to, flags)) + printf("waiter failed errno %d\n", errno); + } =20 return NULL; } @@ -48,7 +84,7 @@ int main(int argc, char *argv[]) =20 f1 =3D &_f1; =20 - while ((c =3D getopt(argc, argv, "cht:v:")) !=3D -1) { + while ((c =3D getopt(argc, argv, "xncht:v:")) !=3D -1) { switch (c) { case 'c': log_color(1); @@ -59,6 +95,12 @@ int main(int argc, char *argv[]) case 'v': log_verbosity(atoi(optarg)); break; + case 'x': + mixed=3D1; + /* fallthrough */ + case 'n': + futex2=3D1; + break; default: usage(basename(argv[0])); exit(1); @@ -79,7 +121,22 @@ int main(int argc, char *argv[]) usleep(WAKE_WAIT_US); =20 info("Requeuing 1 futex from f1 to f2\n"); - res =3D futex_cmp_requeue(f1, 0, &f2, 0, 1, 0); + if (futex2) { + struct futex_waitv futexes[2] =3D { + { + .val =3D 0, + .uaddr =3D (unsigned long)f1, + .flags =3D mixed ? FUTEX2_SIZE_U16 : FUTEX2_SIZE_U32, + }, + { + .uaddr =3D (unsigned long)&f2, + .flags =3D FUTEX2_SIZE_U32, + }, + }; + res =3D futex2_requeue(futexes, 0, 0, 1); + } else { + res =3D futex_cmp_requeue(f1, 0, &f2, 0, 1, 0); + } if (res !=3D 1) { ksft_test_result_fail("futex_requeue simple returned: %d %s\n", res ? errno : res, @@ -89,7 +146,11 @@ int main(int argc, char *argv[]) =20 =20 info("Waking 1 futex at f2\n"); - res =3D futex_wake(&f2, 1, 0); + if (futex2) { + res =3D futex2_wake(&f2, ~0U, 1, FUTEX2_SIZE_U32); + } else { + res =3D futex_wake(&f2, 1, 0); + } if (res !=3D 1) { ksft_test_result_fail("futex_requeue simple returned: %d %s\n", res ? errno : res, @@ -112,7 +173,22 @@ int main(int argc, char *argv[]) usleep(WAKE_WAIT_US); =20 info("Waking 3 futexes at f1 and requeuing 7 futexes from f1 to f2\n"); - res =3D futex_cmp_requeue(f1, 0, &f2, 3, 7, 0); + if (futex2) { + struct futex_waitv futexes[2] =3D { + { + .val =3D 0, + .uaddr =3D (unsigned long)f1, + .flags =3D mixed ? FUTEX2_SIZE_U16 : FUTEX2_SIZE_U32, + }, + { + .uaddr =3D (unsigned long)&f2, + .flags =3D FUTEX2_SIZE_U32, + }, + }; + res =3D futex2_requeue(futexes, 0, 3, 7); + } else { + res =3D futex_cmp_requeue(f1, 0, &f2, 3, 7, 0); + } if (res !=3D 10) { ksft_test_result_fail("futex_requeue many returned: %d %s\n", res ? errno : res, @@ -121,7 +197,11 @@ int main(int argc, char *argv[]) } =20 info("Waking INT_MAX futexes at f2\n"); - res =3D futex_wake(&f2, INT_MAX, 0); + if (futex2) { + res =3D futex2_wake(&f2, ~0U, INT_MAX, FUTEX2_SIZE_U32); + } else { + res =3D futex_wake(&f2, INT_MAX, 0); + } if (res !=3D 7) { ksft_test_result_fail("futex_requeue many returned: %d %s\n", res ? errno : res, --- a/tools/testing/selftests/futex/functional/futex_wait.c +++ b/tools/testing/selftests/futex/functional/futex_wait.c @@ -9,8 +9,10 @@ #include #include #include +#include #include "logging.h" #include "futextest.h" +#include "futex2test.h" =20 #define TEST_NAME "futex-wait" #define timeout_ns 30000000 @@ -19,10 +21,13 @@ =20 void *futex; =20 +bool futex2 =3D 0; + void usage(char *prog) { printf("Usage: %s\n", prog); printf(" -c Use color\n"); + printf(" -n Use futex2 interface\n"); printf(" -h Display this help message\n"); printf(" -v L Verbosity level: %d=3DQUIET %d=3DCRITICAL %d=3DINFO\n", VQUIET, VCRITICAL, VINFO); @@ -30,17 +35,35 @@ void usage(char *prog) =20 static void *waiterfn(void *arg) { - struct timespec to; unsigned int flags =3D 0; + struct timespec to; =20 if (arg) flags =3D *((unsigned int *) arg); =20 - to.tv_sec =3D 0; - to.tv_nsec =3D timeout_ns; + if (futex2) { + if (clock_gettime(CLOCK_MONOTONIC, &to)) { + printf("clock_gettime() failed errno %d", errno); + return NULL; + } =20 - if (futex_wait(futex, 0, &to, flags)) - printf("waiter failed errno %d\n", errno); + to.tv_nsec +=3D timeout_ns; + if (to.tv_nsec >=3D 1000000000) { + to.tv_sec++; + to.tv_nsec -=3D 1000000000; + } + + if (futex2_wait(futex, 0, ~0U, flags | FUTEX2_SIZE_U32, + &to, CLOCK_MONOTONIC)) + printf("waiter failed errno %d\n", errno); + } else { + + to.tv_sec =3D 0; + to.tv_nsec =3D timeout_ns; + + if (futex_wait(futex, 0, &to, flags)) + printf("waiter failed errno %d\n", errno); + } =20 return NULL; } @@ -55,7 +78,7 @@ int main(int argc, char *argv[]) =20 futex =3D &f_private; =20 - while ((c =3D getopt(argc, argv, "cht:v:")) !=3D -1) { + while ((c =3D getopt(argc, argv, "ncht:v:")) !=3D -1) { switch (c) { case 'c': log_color(1); @@ -66,6 +89,9 @@ int main(int argc, char *argv[]) case 'v': log_verbosity(atoi(optarg)); break; + case 'n': + futex2=3D1; + break; default: usage(basename(argv[0])); exit(1); @@ -84,7 +110,11 @@ int main(int argc, char *argv[]) usleep(WAKE_WAIT_US); =20 info("Calling private futex_wake on futex: %p\n", futex); - res =3D futex_wake(futex, 1, FUTEX_PRIVATE_FLAG); + if (futex2) { + res =3D futex2_wake(futex, ~0U, 1, FUTEX2_SIZE_U32 | FUTEX2_PRIVATE); + } else { + res =3D futex_wake(futex, 1, FUTEX_PRIVATE_FLAG); + } if (res !=3D 1) { ksft_test_result_fail("futex_wake private returned: %d %s\n", errno, strerror(errno)); @@ -112,7 +142,11 @@ int main(int argc, char *argv[]) usleep(WAKE_WAIT_US); =20 info("Calling shared (page anon) futex_wake on futex: %p\n", futex); - res =3D futex_wake(futex, 1, 0); + if (futex2) { + res =3D futex2_wake(futex, ~0U, 1, FUTEX2_SIZE_U32); + } else { + res =3D futex_wake(futex, 1, 0); + } if (res !=3D 1) { ksft_test_result_fail("futex_wake shared (page anon) returned: %d %s\n", errno, strerror(errno)); @@ -151,7 +185,11 @@ int main(int argc, char *argv[]) usleep(WAKE_WAIT_US); =20 info("Calling shared (file backed) futex_wake on futex: %p\n", futex); - res =3D futex_wake(shm, 1, 0); + if (futex2) { + res =3D futex2_wake(shm, ~0U, 1, FUTEX2_SIZE_U32); + } else { + res =3D futex_wake(shm, 1, 0); + } if (res !=3D 1) { ksft_test_result_fail("futex_wake shared (file backed) returned: %d %s\n= ", errno, strerror(errno)); --- a/tools/testing/selftests/futex/functional/futex_wait_timeout.c +++ b/tools/testing/selftests/futex/functional/futex_wait_timeout.c @@ -103,7 +103,7 @@ int main(int argc, char *argv[]) struct futex_waitv waitv =3D { .uaddr =3D (uintptr_t)&f1, .val =3D f1, - .flags =3D FUTEX_32, + .flags =3D FUTEX2_SIZE_U32, .__reserved =3D 0 }; =20 @@ -128,7 +128,7 @@ int main(int argc, char *argv[]) } =20 ksft_print_header(); - ksft_set_plan(9); + ksft_set_plan(11); ksft_print_msg("%s: Block on a futex and wait for timeout\n", basename(argv[0])); ksft_print_msg("\tArguments: timeout=3D%ldns\n", timeout_ns); @@ -201,6 +201,18 @@ int main(int argc, char *argv[]) res =3D futex_waitv(&waitv, 1, 0, &to, CLOCK_REALTIME); test_timeout(res, &ret, "futex_waitv realtime", ETIMEDOUT); =20 + /* futex2_wait with CLOCK_MONOTONIC */ + if (futex_get_abs_timeout(CLOCK_MONOTONIC, &to, timeout_ns)) + return RET_FAIL; + res =3D futex2_wait(&f1, f1, 1, FUTEX2_SIZE_U32, &to, CLOCK_MONOTONIC); + test_timeout(res, &ret, "futex2_wait monotonic", ETIMEDOUT); + + /* futex2_wait with CLOCK_REALTIME */ + if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns)) + return RET_FAIL; + res =3D futex2_wait(&f1, f1, 1, FUTEX2_SIZE_U32, &to, CLOCK_REALTIME); + test_timeout(res, &ret, "futex2_wait realtime", ETIMEDOUT); + ksft_print_cnts(); return ret; } --- a/tools/testing/selftests/futex/functional/futex_wait_wouldblock.c +++ b/tools/testing/selftests/futex/functional/futex_wait_wouldblock.c @@ -46,7 +46,7 @@ int main(int argc, char *argv[]) struct futex_waitv waitv =3D { .uaddr =3D (uintptr_t)&f1, .val =3D f1+1, - .flags =3D FUTEX_32, + .flags =3D FUTEX2_SIZE_U32 | FUTEX2_PRIVATE, .__reserved =3D 0 }; =20 @@ -68,7 +68,7 @@ int main(int argc, char *argv[]) } =20 ksft_print_header(); - ksft_set_plan(2); + ksft_set_plan(3); ksft_print_msg("%s: Test the unexpected futex value in FUTEX_WAIT\n", basename(argv[0])); =20 @@ -106,6 +106,30 @@ int main(int argc, char *argv[]) ksft_test_result_pass("futex_waitv\n"); } =20 + if (clock_gettime(CLOCK_MONOTONIC, &to)) { + error("clock_gettime failed\n", errno); + return errno; + } + + to.tv_nsec +=3D timeout_ns; + + if (to.tv_nsec >=3D 1000000000) { + to.tv_sec++; + to.tv_nsec -=3D 1000000000; + } + + info("Calling futex2_wait on f1: %u @ %p with val=3D%u\n", f1, &f1, f1+1); + res =3D futex2_wait(&f1, f1+1, ~0U, FUTEX2_SIZE_U32 | FUTEX2_PRIVATE, + &to, CLOCK_MONOTONIC); + if (!res || errno !=3D EWOULDBLOCK) { + ksft_test_result_pass("futex2_wait returned: %d %s\n", + res ? errno : res, + res ? strerror(errno) : ""); + ret =3D RET_FAIL; + } else { + ksft_test_result_pass("futex2_wait\n"); + } + ksft_print_cnts(); return ret; } --- a/tools/testing/selftests/futex/functional/futex_waitv.c +++ b/tools/testing/selftests/futex/functional/futex_waitv.c @@ -88,7 +88,7 @@ int main(int argc, char *argv[]) =20 for (i =3D 0; i < NR_FUTEXES; i++) { waitv[i].uaddr =3D (uintptr_t)&futexes[i]; - waitv[i].flags =3D FUTEX_32 | FUTEX_PRIVATE_FLAG; + waitv[i].flags =3D FUTEX2_SIZE_U32 | FUTEX2_PRIVATE; waitv[i].val =3D 0; waitv[i].__reserved =3D 0; } @@ -99,7 +99,8 @@ int main(int argc, char *argv[]) =20 usleep(WAKE_WAIT_US); =20 - res =3D futex_wake(u64_to_ptr(waitv[NR_FUTEXES - 1].uaddr), 1, FUTEX_PRIV= ATE_FLAG); + res =3D futex2_wake(u64_to_ptr(waitv[NR_FUTEXES - 1].uaddr), ~0U, 1, + FUTEX2_PRIVATE | FUTEX2_SIZE_U32); if (res !=3D 1) { ksft_test_result_fail("futex_wake private returned: %d %s\n", res ? errno : res, @@ -122,7 +123,7 @@ int main(int argc, char *argv[]) =20 *shared_data =3D 0; waitv[i].uaddr =3D (uintptr_t)shared_data; - waitv[i].flags =3D FUTEX_32; + waitv[i].flags =3D FUTEX2_SIZE_U32; waitv[i].val =3D 0; waitv[i].__reserved =3D 0; } @@ -145,8 +146,8 @@ int main(int argc, char *argv[]) for (i =3D 0; i < NR_FUTEXES; i++) shmdt(u64_to_ptr(waitv[i].uaddr)); =20 - /* Testing a waiter without FUTEX_32 flag */ - waitv[0].flags =3D FUTEX_PRIVATE_FLAG; + /* Testing a waiter without FUTEX2_SIZE_U32 flag */ + waitv[0].flags =3D FUTEX2_PRIVATE; =20 if (clock_gettime(CLOCK_MONOTONIC, &to)) error("gettime64 failed\n", errno); @@ -160,11 +161,11 @@ int main(int argc, char *argv[]) res ? strerror(errno) : ""); ret =3D RET_FAIL; } else { - ksft_test_result_pass("futex_waitv without FUTEX_32\n"); + ksft_test_result_pass("futex_waitv without FUTEX2_SIZE_U32\n"); } =20 /* Testing a waiter with an unaligned address */ - waitv[0].flags =3D FUTEX_PRIVATE_FLAG | FUTEX_32; + waitv[0].flags =3D FUTEX2_PRIVATE | FUTEX2_SIZE_U32; waitv[0].uaddr =3D 1; =20 if (clock_gettime(CLOCK_MONOTONIC, &to)) --- a/tools/testing/selftests/futex/functional/run.sh +++ b/tools/testing/selftests/futex/functional/run.sh @@ -76,9 +76,15 @@ echo =20 echo ./futex_wait $COLOR +echo +./futex_wait -n $COLOR =20 echo ./futex_requeue $COLOR +echo +./futex_requeue -n $COLOR +echo +./futex_requeue -x $COLOR =20 echo ./futex_waitv $COLOR --- a/tools/testing/selftests/futex/include/futex2test.h +++ b/tools/testing/selftests/futex/include/futex2test.h @@ -8,6 +8,41 @@ =20 #define u64_to_ptr(x) ((void *)(uintptr_t)(x)) =20 +#ifndef __NR_futex_waitv +#define __NR_futex_waitv 449 + +struct futex_waitv { + __u64 val; + __u64 uaddr; + __u32 flags; + __u32 __reserved; +}; +#endif + +#ifndef __NR_futex_wake +#define __NR_futex_wake 454 +#define __NR_futex_wait 455 +#define __NR_futex_requeue 456 +#endif + +#ifndef FUTEX2_SIZE_U8 +/* + * Flags for futex2 syscalls. + */ +#define FUTEX2_SIZE_U8 0x00 +#define FUTEX2_SIZE_U16 0x01 +#define FUTEX2_SIZE_U32 0x02 +#define FUTEX2_SIZE_U64 0x03 +#define FUTEX2_NUMA 0x04 + /* 0x08 */ + /* 0x10 */ + /* 0x20 */ + /* 0x40 */ +#define FUTEX2_PRIVATE FUTEX_PRIVATE_FLAG +#endif + +#define FUTEX_NO_NODE (-1) + /** * futex_waitv - Wait at multiple futexes, wake on any * @waiters: Array of waiters @@ -20,3 +55,20 @@ static inline int futex_waitv(volatile s { return syscall(__NR_futex_waitv, waiters, nr_waiters, flags, timo, clocki= d); } + +static inline int futex2_wake(volatile void *uaddr, unsigned long mask, in= t nr, unsigned int flags) +{ + return syscall(__NR_futex_wake, uaddr, mask, nr, flags); +} + +static inline int futex2_wait(volatile void *uaddr, unsigned long val, uns= igned long mask, + unsigned int flags, struct timespec *timo, clockid_t clockid) +{ + return syscall(__NR_futex_wait, uaddr, val, mask, flags, timo, clockid); +} + +static inline int futex2_requeue(struct futex_waitv *futexes, unsigned int= flags, + int nr_wake, int nr_requeue) +{ + return syscall(__NR_futex_requeue, futexes, flags, nr_wake, nr_requeue); +} From nobody Mon Nov 25 18:23:54 2024 Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (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 04E7D1D364C; Fri, 25 Oct 2024 09:41:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.92.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729849279; cv=none; b=AYK/x3Qu4JnoYpvikn29PiAU06dFoLnxSeDagH9BZUalg9Vuiey3qY5faUb7LHZ/XP3g6zN4mIrK1RcacXfWrjQ7+Tv8xXvXHup3sqA0tgKxNSb960nPc68yGQAZDrJlnVdHuu5EcvdRPSvEud/1k+vzK6wg652Mp/YqV95louk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729849279; c=relaxed/simple; bh=zKcGzEVnkm9CSp7Mz/W/XRrnpAxAW7lFFQ1dztupItk=; h=Message-Id:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=fNu3EZEEc5xolHb5RHe0Y9TjlsUrExiw8/QBXfkBVGVkeaKF58qI0Myqrd1oBXPFjG1FtekRJOa60Cbd0GYobWwz28qQTkmcTzud2rncHVqkOmpS2pkLtsQL+bHryzeOpQZyVY3FNnRGwa3QmamuNLeIjvnYip85YcdyGxzzWAA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=EWoI2kF0; arc=none smtp.client-ip=90.155.92.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="EWoI2kF0" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-Id:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=WBCThp+Q/6jOaBGTrPx+/LzFcfDXMvuZTpT311NKNDE=; b=EWoI2kF0smf1s0JbGY03zJ6pds 7+1TyI0uc8qyQpyMesLwBt8XOHhmPlGYE6EGGOJ73mlBKdBT1KX37ZFqsrABYBwNA94a91/FQ42E0 qEU/HMdu1bqB3bmHqSxuYrPpUtnWTkeUecZHvkEGzT2NST2LbR0+7RMTxFAPnPz2fjV7vmnBwTS5g jVIMPypgqKWNQJGkWzRSb3hMHT4ailPDfYV4mvfJ9mkvV5AFzSxGTSsRSCkhQ8AYpy1JO1U57kNLQ HPpriKCzCoE2UyasLLSWp8bGqlpPR/6bYSadxpDjWiFR6elaqL2Mg6ZY8wuFm4p7WIT7slUwynCVh ouy8S96w==; Received: from j130084.upc-j.chello.nl ([24.132.130.84] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1t4GoV-00000008saA-0f0B; Fri, 25 Oct 2024 09:40:59 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 0) id E2BAE302179; Fri, 25 Oct 2024 11:40:57 +0200 (CEST) Message-Id: <20241025093944.922683354@infradead.org> User-Agent: quilt/0.65 Date: Fri, 25 Oct 2024 11:03:53 +0200 From: Peter Zijlstra To: tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, peterz@infradead.org, mingo@redhat.com, dvhart@infradead.org, dave@stgolabs.net, andrealmeid@igalia.com, Andrew Morton , urezki@gmail.com, hch@infradead.org, lstoakes@gmail.com, Arnd Bergmann , linux-api@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, malteskarupke@web.de, cl@linux.com, llong@redhat.com Subject: [PATCH 6/6] futex,selftests: Extend the futex selftests for NUMA References: <20241025090347.244183920@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" XXX Signed-off-by: Peter Zijlstra (Intel) --- tools/testing/selftests/futex/functional/Makefile | 3=20 tools/testing/selftests/futex/functional/futex_numa.c | 262 +++++++++++++= +++++ 2 files changed, 264 insertions(+), 1 deletion(-) --- a/tools/testing/selftests/futex/functional/Makefile +++ b/tools/testing/selftests/futex/functional/Makefile @@ -17,7 +17,8 @@ TEST_GEN_PROGS :=3D \ futex_wait_private_mapped_file \ futex_wait \ futex_requeue \ - futex_waitv + futex_waitv \ + futex_numa =20 TEST_PROGS :=3D run.sh =20 --- /dev/null +++ b/tools/testing/selftests/futex/functional/futex_numa.c @@ -0,0 +1,262 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include +#include +#include +#include +#include "logging.h" +#include "futextest.h" +#include "futex2test.h" + +typedef u_int32_t u32; +typedef int32_t s32; +typedef u_int64_t u64; + +static int fflags =3D (FUTEX2_SIZE_U32 | FUTEX2_PRIVATE); +static int fnode =3D FUTEX_NO_NODE; + +/* fairly stupid test-and-set lock with a waiter flag */ + +#define N_LOCK 0x0000001 +#define N_WAITERS 0x0001000 + +struct futex_numa_32 { + union { + u64 full; + struct { + u32 val; + u32 node; + }; + }; +}; + +void futex_numa_32_lock(struct futex_numa_32 *lock) +{ + for (;;) { + struct futex_numa_32 new, old =3D { + .full =3D __atomic_load_n(&lock->full, __ATOMIC_RELAXED), + }; + + for (;;) { + new =3D old; + if (old.val =3D=3D 0) { + /* no waiter, no lock -> first lock, set no-node */ + new.node =3D fnode; + } + if (old.val & N_LOCK) { + /* contention, set waiter */ + new.val |=3D N_WAITERS; + } + new.val |=3D N_LOCK; + + /* nothing changed, ready to block */ + if (old.full =3D=3D new.full) + break; + + /* + * Use u64 cmpxchg to set the futex value and node in a + * consistent manner. + */ + if (__atomic_compare_exchange_n(&lock->full, + &old.full, new.full, + /* .weak */ false, + __ATOMIC_ACQUIRE, + __ATOMIC_RELAXED)) { + + /* if we just set N_LOCK, we own it */ + if (!(old.val & N_LOCK)) + return; + + /* go block */ + break; + } + } + + futex2_wait(lock, new.val, ~0U, fflags, NULL, 0); + } +} + +void futex_numa_32_unlock(struct futex_numa_32 *lock) +{ + u32 val =3D __atomic_sub_fetch(&lock->val, N_LOCK, __ATOMIC_RELEASE); + assert((s32)val >=3D 0); + if (val & N_WAITERS) { + int woken =3D futex2_wake(lock, ~0U, 1, fflags); + assert(val =3D=3D N_WAITERS); + if (!woken) { + __atomic_compare_exchange_n(&lock->val, &val, 0U, + false, __ATOMIC_RELAXED, + __ATOMIC_RELAXED); + } + } +} + +static long nanos =3D 50000; + +struct thread_args { + pthread_t tid; + volatile int * done; + struct futex_numa_32 *lock; + int val; + int *val1, *val2; + int node; +}; + +static void *threadfn(void *_arg) +{ + struct thread_args *args =3D _arg; + struct timespec ts =3D { + .tv_nsec =3D nanos, + }; + int node; + + while (!*args->done) { + + futex_numa_32_lock(args->lock); + args->val++; + + assert(*args->val1 =3D=3D *args->val2); + (*args->val1)++; + nanosleep(&ts, NULL); + (*args->val2)++; + + node =3D args->lock->node; + futex_numa_32_unlock(args->lock); + + if (node !=3D args->node) { + args->node =3D node; + printf("node: %d\n", node); + } + + nanosleep(&ts, NULL); + } + + return NULL; +} + +static void *contendfn(void *_arg) +{ + struct thread_args *args =3D _arg; + + while (!*args->done) { + /* + * futex2_wait() will take hb-lock, verify *var =3D=3D val and + * queue/abort. By knowingly setting val 'wrong' this will + * abort and thereby generate hb-lock contention. + */ + futex2_wait(&args->lock->val, ~0U, ~0U, fflags, NULL, 0); + args->val++; + } + + return NULL; +} + +static volatile int done =3D 0; +static struct futex_numa_32 lock =3D { .val =3D 0, }; +static int val1, val2; + +int main(int argc, char *argv[]) +{ + struct thread_args *tas[512], *cas[512]; + int c, t, threads =3D 2, contenders =3D 0; + int sleeps =3D 10; + int total =3D 0; + + while ((c =3D getopt(argc, argv, "c:t:s:n:N::")) !=3D -1) { + switch (c) { + case 'c': + contenders =3D atoi(optarg); + break; + case 't': + threads =3D atoi(optarg); + break; + case 's': + sleeps =3D atoi(optarg); + break; + case 'n': + nanos =3D atoi(optarg); + break; + case 'N': + fflags |=3D FUTEX2_NUMA; + if (optarg) + fnode =3D atoi(optarg); + break; + default: + exit(1); + break; + } + } + + for (t =3D 0; t < contenders; t++) { + struct thread_args *args =3D calloc(1, sizeof(*args)); + if (!args) { + perror("thread_args"); + exit(-1); + } + + args->done =3D &done; + args->lock =3D &lock; + args->val1 =3D &val1; + args->val2 =3D &val2; + args->node =3D -1; + + if (pthread_create(&args->tid, NULL, contendfn, args)) { + perror("pthread_create"); + exit(-1); + } + + cas[t] =3D args; + } + + for (t =3D 0; t < threads; t++) { + struct thread_args *args =3D calloc(1, sizeof(*args)); + if (!args) { + perror("thread_args"); + exit(-1); + } + + args->done =3D &done; + args->lock =3D &lock; + args->val1 =3D &val1; + args->val2 =3D &val2; + args->node =3D -1; + + if (pthread_create(&args->tid, NULL, threadfn, args)) { + perror("pthread_create"); + exit(-1); + } + + tas[t] =3D args; + } + + sleep(sleeps); + + done =3D true; + + for (t =3D 0; t < threads; t++) { + struct thread_args *args =3D tas[t]; + + pthread_join(args->tid, NULL); + total +=3D args->val; +// printf("tval: %d\n", args->val); + } + printf("total: %d\n", total); + + if (contenders) { + total =3D 0; + for (t =3D 0; t < contenders; t++) { + struct thread_args *args =3D cas[t]; + + pthread_join(args->tid, NULL); + total +=3D args->val; + // printf("tval: %d\n", args->val); + } + printf("contenders: %d\n", total); + } + + return 0; +} +