From: Kuniyuki Iwashima <kuniyu@amazon.co.jp>
To: "David S. Miller" <davem@davemloft.net>,
Jakub Kicinski <kuba@kernel.org>
Cc: Kuniyuki Iwashima <kuniyu@amazon.co.jp>,
Kuniyuki Iwashima <kuni1840@gmail.com>,
Benjamin Herrenschmidt <benh@amazon.com>,
<netdev@vger.kernel.org>
Subject: [PATCH net-next 11/13] af_unix: Save hash in sk_hash.
Date: Sat, 6 Nov 2021 18:17:10 +0900 [thread overview]
Message-ID: <20211106091712.15206-12-kuniyu@amazon.co.jp> (raw)
In-Reply-To: <20211106091712.15206-1-kuniyu@amazon.co.jp>
To replace unix_table_lock with per-hash locks in the next patch, we need
to save a hash in each socket because /proc/net/unix or BPF prog iterate
sockets while holding a hash table lock and release it later in a different
function.
Currently, we store a real/pseudo hash in struct unix_address. However, we
do not allocate it to unbound sockets, nor should we do just for that. For
this purpose, we can use sk_hash. Then, we no longer use the hash field in
struct unix_address and can remove it.
Also, this patch does
- rename unix_insert_socket() to unix_insert_unbound_socket()
- remove the redundant list argument from __unix_insert_socket() and
unix_insert_unbound_socket()
- use 'unsigned int' instead of 'unsigned' in __unix_set_addr_hash()
- remove 'inline' from unix_remove_socket() and
unix_insert_unbound_socket().
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.co.jp>
---
include/net/af_unix.h | 1 -
net/unix/af_unix.c | 41 ++++++++++++++++++++++-------------------
2 files changed, 22 insertions(+), 20 deletions(-)
diff --git a/include/net/af_unix.h b/include/net/af_unix.h
index 7d142e8a0550..89049cc6c066 100644
--- a/include/net/af_unix.h
+++ b/include/net/af_unix.h
@@ -26,7 +26,6 @@ extern struct hlist_head unix_socket_table[2 * UNIX_HASH_SIZE];
struct unix_address {
refcount_t refcnt;
int len;
- unsigned int hash;
struct sockaddr_un name[];
};
diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index f95786e00723..db04dda07c39 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -278,31 +278,32 @@ static void __unix_remove_socket(struct sock *sk)
sk_del_node_init(sk);
}
-static void __unix_insert_socket(struct hlist_head *list, struct sock *sk)
+static void __unix_insert_socket(struct sock *sk)
{
WARN_ON(!sk_unhashed(sk));
- sk_add_node(sk, list);
+ sk_add_node(sk, &unix_socket_table[sk->sk_hash]);
}
-static void __unix_set_addr(struct sock *sk, struct unix_address *addr,
- unsigned hash)
+static void __unix_set_addr_hash(struct sock *sk, struct unix_address *addr,
+ unsigned int hash)
{
__unix_remove_socket(sk);
+ sk->sk_hash = hash;
smp_store_release(&unix_sk(sk)->addr, addr);
- __unix_insert_socket(&unix_socket_table[hash], sk);
+ __unix_insert_socket(sk);
}
-static inline void unix_remove_socket(struct sock *sk)
+static void unix_remove_socket(struct sock *sk)
{
spin_lock(&unix_table_lock);
__unix_remove_socket(sk);
spin_unlock(&unix_table_lock);
}
-static inline void unix_insert_socket(struct hlist_head *list, struct sock *sk)
+static void unix_insert_unbound_socket(struct sock *sk)
{
spin_lock(&unix_table_lock);
- __unix_insert_socket(list, sk);
+ __unix_insert_socket(sk);
spin_unlock(&unix_table_lock);
}
@@ -893,6 +894,7 @@ static struct sock *unix_create1(struct net *net, struct socket *sock, int kern,
sock_init_data(sock, sk);
+ sk->sk_hash = unix_unbound_hash(sk);
sk->sk_allocation = GFP_KERNEL_ACCOUNT;
sk->sk_write_space = unix_write_space;
sk->sk_max_ack_backlog = net->unx.sysctl_max_dgram_qlen;
@@ -908,7 +910,7 @@ static struct sock *unix_create1(struct net *net, struct socket *sock, int kern,
init_waitqueue_head(&u->peer_wait);
init_waitqueue_func_entry(&u->peer_wake, unix_dgram_peer_wake_relay);
memset(&u->scm_stat, 0, sizeof(struct scm_stat));
- unix_insert_socket(&unix_socket_table[unix_unbound_hash(sk)], sk);
+ unix_insert_unbound_socket(sk);
local_bh_disable();
sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
@@ -1054,6 +1056,7 @@ static int unix_autobind(struct sock *sk)
struct unix_address *addr;
unsigned int retries = 0;
static u32 ordernum = 1;
+ unsigned int new_hash;
int err;
err = mutex_lock_interruptible(&u->bindlock);
@@ -1074,12 +1077,12 @@ static int unix_autobind(struct sock *sk)
retry:
addr->len = sprintf(addr->name->sun_path + 1, "%05x", ordernum) +
offsetof(struct sockaddr_un, sun_path) + 1;
- addr->hash = unix_abstract_hash(addr->name, addr->len, sk->sk_type);
+ new_hash = unix_abstract_hash(addr->name, addr->len, sk->sk_type);
spin_lock(&unix_table_lock);
ordernum = (ordernum+1)&0xFFFFF;
- if (__unix_find_socket_byname(sock_net(sk), addr->name, addr->len, addr->hash)) {
+ if (__unix_find_socket_byname(sock_net(sk), addr->name, addr->len, new_hash)) {
spin_unlock(&unix_table_lock);
/*
* __unix_find_socket_byname() may take long time if many names
@@ -1095,7 +1098,7 @@ static int unix_autobind(struct sock *sk)
goto retry;
}
- __unix_set_addr(sk, addr, addr->hash);
+ __unix_set_addr_hash(sk, addr, new_hash);
spin_unlock(&unix_table_lock);
err = 0;
@@ -1110,9 +1113,9 @@ static int unix_bind_bsd(struct sock *sk, struct sockaddr_un *sunaddr, int addr_
struct unix_sock *u = unix_sk(sk);
struct user_namespace *ns; // barf...
struct unix_address *addr;
+ unsigned int new_hash;
struct dentry *dentry;
struct path parent;
- unsigned int hash;
int err;
unix_mkname_bsd(sunaddr, addr_len);
@@ -1147,12 +1150,11 @@ static int unix_bind_bsd(struct sock *sk, struct sockaddr_un *sunaddr, int addr_
if (u->addr)
goto out_unlock;
- addr->hash = UNIX_HASH_SIZE;
- hash = unix_bsd_hash(d_backing_inode(dentry));
+ new_hash = unix_bsd_hash(d_backing_inode(dentry));
spin_lock(&unix_table_lock);
u->path.mnt = mntget(parent.mnt);
u->path.dentry = dget(dentry);
- __unix_set_addr(sk, addr, hash);
+ __unix_set_addr_hash(sk, addr, new_hash);
spin_unlock(&unix_table_lock);
mutex_unlock(&u->bindlock);
done_path_create(&parent, dentry);
@@ -1175,6 +1177,7 @@ static int unix_bind_abstract(struct sock *sk, struct sockaddr_un *sunaddr, int
{
struct unix_sock *u = unix_sk(sk);
struct unix_address *addr;
+ unsigned int new_hash;
int err;
addr = unix_create_addr(sunaddr, addr_len);
@@ -1190,13 +1193,13 @@ static int unix_bind_abstract(struct sock *sk, struct sockaddr_un *sunaddr, int
goto out_mutex;
}
- addr->hash = unix_abstract_hash(addr->name, addr->len, sk->sk_type);
+ new_hash = unix_abstract_hash(addr->name, addr->len, sk->sk_type);
spin_lock(&unix_table_lock);
- if (__unix_find_socket_byname(sock_net(sk), addr->name, addr->len, addr->hash))
+ if (__unix_find_socket_byname(sock_net(sk), addr->name, addr->len, new_hash))
goto out_spin;
- __unix_set_addr(sk, addr, addr->hash);
+ __unix_set_addr_hash(sk, addr, new_hash);
spin_unlock(&unix_table_lock);
mutex_unlock(&u->bindlock);
return 0;
--
2.30.2
next prev parent reply other threads:[~2021-11-06 9:20 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-11-06 9:16 [PATCH net-next 00/13] af_unix: Replace unix_table_lock with per-hash locks Kuniyuki Iwashima
2021-11-06 9:17 ` [PATCH net-next 01/13] af_unix: Use offsetof() instead of sizeof() Kuniyuki Iwashima
2021-11-06 9:17 ` [PATCH net-next 02/13] af_unix: Pass struct sock to unix_autobind() Kuniyuki Iwashima
2021-11-06 9:17 ` [PATCH net-next 03/13] af_unix: Factorise unix_find_other() based on address types Kuniyuki Iwashima
2021-11-06 9:17 ` [PATCH net-next 04/13] af_unix: Return an error as a pointer in unix_find_other() Kuniyuki Iwashima
2021-11-06 9:17 ` [PATCH net-next 05/13] af_unix: Cut unix_validate_addr() out of unix_mkname() Kuniyuki Iwashima
2021-11-06 9:17 ` [PATCH net-next 06/13] af_unix: Copy unix_mkname() into unix_find_(bsd|abstract)() Kuniyuki Iwashima
2021-11-06 9:17 ` [PATCH net-next 07/13] af_unix: Remove unix_mkname() Kuniyuki Iwashima
2021-11-06 9:17 ` [PATCH net-next 08/13] af_unix: Allocate unix_address in unix_bind_(bsd|abstract)() Kuniyuki Iwashima
2021-11-06 9:17 ` [PATCH net-next 09/13] af_unix: Remove UNIX_ABSTRACT() macro and test sun_path[0] instead Kuniyuki Iwashima
2021-11-06 9:17 ` [PATCH net-next 10/13] af_unix: Add helpers to calculate hashes Kuniyuki Iwashima
2021-11-06 9:17 ` Kuniyuki Iwashima [this message]
2021-11-06 9:17 ` [PATCH net-next 12/13] af_unix: Replace the big lock with small locks Kuniyuki Iwashima
2021-11-10 6:39 ` [af_unix] 95e381b609: WARNING:possible_recursive_locking_detected kernel test robot
2021-11-06 9:17 ` [PATCH net-next 13/13] af_unix: Relax race in unix_autobind() Kuniyuki Iwashima
2021-11-08 23:10 ` Eric Dumazet
2021-11-09 2:24 ` Kuniyuki Iwashima
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=20211106091712.15206-12-kuniyu@amazon.co.jp \
--to=kuniyu@amazon.co.jp \
--cc=benh@amazon.com \
--cc=davem@davemloft.net \
--cc=kuba@kernel.org \
--cc=kuni1840@gmail.com \
--cc=netdev@vger.kernel.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox