[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <c2s2h5hd6obrraim5u7nbqu3wcp5pm5srtf4772qxmrlaugdps@7gjdbcf6v7dx>
Date: Mon, 10 Jun 2024 18:43:44 -0400
From: Kent Overstreet <kent.overstreet@...ux.dev>
To: Kuniyuki Iwashima <kuniyu@...zon.com>
Cc: "David S. Miller" <davem@...emloft.net>,
Eric Dumazet <edumazet@...gle.com>, Jakub Kicinski <kuba@...nel.org>,
Paolo Abeni <pabeni@...hat.com>, Kuniyuki Iwashima <kuni1840@...il.com>, netdev@...r.kernel.org
Subject: Re: [PATCH v1 net-next 01/11] af_unix: Define locking order for
unix_table_double_lock().
On Mon, Jun 10, 2024 at 03:34:51PM -0700, Kuniyuki Iwashima wrote:
> When created, AF_UNIX socket is put into net->unx.table.buckets[],
> and the hash is stored in sk->sk_hash.
>
> * unbound socket : 0 <= sk_hash <= UNIX_HASH_MOD
>
> When bind() is called, the socket could be moved to another bucket.
>
> * pathname socket : 0 <= sk_hash <= UNIX_HASH_MOD
> * abstract socket : UNIX_HASH_MOD + 1 <= sk_hash <= UNIX_HASH_MOD * 2 + 1
>
> Then, we call unix_table_double_lock() which locks a single bucket
> or two.
>
> Let's define the order as unix_table_lock_cmp_fn() instead of using
> spin_lock_nested().
>
> The locking is always done in ascending order of sk->sk_hash, which
> is the index of buckets/locks array allocated by kvmalloc_array().
>
> sk_hash_A < sk_hash_B
> <=> &locks[sk_hash_A].dep_map < &locks[sk_hash_B].dep_map
>
> So, the relation of two sk->sk_hash can be derived from the addresses
> of dep_map in the array of locks.
>
> Signed-off-by: Kuniyuki Iwashima <kuniyu@...zon.com>
> ---
> net/unix/af_unix.c | 10 +++++++++-
> 1 file changed, 9 insertions(+), 1 deletion(-)
>
> diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
> index 3821f8945b1e..b0a9891c0384 100644
> --- a/net/unix/af_unix.c
> +++ b/net/unix/af_unix.c
> @@ -126,6 +126,13 @@ static spinlock_t bsd_socket_locks[UNIX_HASH_SIZE / 2];
> * hash table is protected with spinlock.
> * each socket state is protected by separate spinlock.
> */
> +#ifdef CONFIG_PROVE_LOCKING
> +static int unix_table_lock_cmp_fn(const struct lockdep_map *a,
> + const struct lockdep_map *b)
> +{
> + return a < b ? -1 : 0;
> +}
> +#endif
This should be a proper comparison function: -1 for less than, 0 for
equal, 1 for greater than.
I've got a cmp_int() macro in bcachefs that does this nicely.
Powered by blists - more mailing lists