Skip to content

Commit 242124e

Browse files
edumazetgregkh
authored andcommitted
net: remove sock_i_uid()
[ Upstream commit c51da3f ] Difference between sock_i_uid() and sk_uid() is that after sock_orphan(), sock_i_uid() returns GLOBAL_ROOT_UID while sk_uid() returns the last cached sk->sk_uid value. None of sock_i_uid() callers care about this. Use sk_uid() which is much faster and inlined. Note that diag/dump users are calling sock_i_ino() and can not see the full benefit yet. Signed-off-by: Eric Dumazet <edumazet@google.com> Cc: Lorenzo Colitti <lorenzo@google.com> Reviewed-by: Maciej Żenczykowski <maze@google.com> Link: https://patch.msgid.link/20250620133001.4090592-3-edumazet@google.com Signed-off-by: Jakub Kicinski <kuba@kernel.org> Stable-dep-of: 5d6b58c ("net: lockless sock_i_ino()") Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent bb13793 commit 242124e

File tree

26 files changed

+50
-66
lines changed

26 files changed

+50
-66
lines changed

include/net/sock.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2092,8 +2092,6 @@ static inline void sock_graft(struct sock *sk, struct socket *parent)
20922092
write_unlock_bh(&sk->sk_callback_lock);
20932093
}
20942094

2095-
kuid_t sock_i_uid(struct sock *sk);
2096-
20972095
static inline kuid_t sk_uid(const struct sock *sk)
20982096
{
20992097
/* Paired with WRITE_ONCE() in sockfs_setattr() */

net/appletalk/atalk_proc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ static int atalk_seq_socket_show(struct seq_file *seq, void *v)
181181
sk_wmem_alloc_get(s),
182182
sk_rmem_alloc_get(s),
183183
s->sk_state,
184-
from_kuid_munged(seq_user_ns(seq), sock_i_uid(s)));
184+
from_kuid_munged(seq_user_ns(seq), sk_uid(s)));
185185
out:
186186
return 0;
187187
}

net/bluetooth/af_bluetooth.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ static int bt_seq_show(struct seq_file *seq, void *v)
815815
refcount_read(&sk->sk_refcnt),
816816
sk_rmem_alloc_get(sk),
817817
sk_wmem_alloc_get(sk),
818-
from_kuid(seq_user_ns(seq), sock_i_uid(sk)),
818+
from_kuid(seq_user_ns(seq), sk_uid(sk)),
819819
sock_i_ino(sk),
820820
bt->parent ? sock_i_ino(bt->parent) : 0LU);
821821

net/core/sock.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2788,17 +2788,6 @@ void sock_pfree(struct sk_buff *skb)
27882788
EXPORT_SYMBOL(sock_pfree);
27892789
#endif /* CONFIG_INET */
27902790

2791-
kuid_t sock_i_uid(struct sock *sk)
2792-
{
2793-
kuid_t uid;
2794-
2795-
read_lock_bh(&sk->sk_callback_lock);
2796-
uid = sk->sk_socket ? SOCK_INODE(sk->sk_socket)->i_uid : GLOBAL_ROOT_UID;
2797-
read_unlock_bh(&sk->sk_callback_lock);
2798-
return uid;
2799-
}
2800-
EXPORT_SYMBOL(sock_i_uid);
2801-
28022791
unsigned long __sock_i_ino(struct sock *sk)
28032792
{
28042793
unsigned long ino;

net/ipv4/inet_connection_sock.c

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ static bool inet_use_bhash2_on_bind(const struct sock *sk)
168168
}
169169

170170
static bool inet_bind_conflict(const struct sock *sk, struct sock *sk2,
171-
kuid_t sk_uid, bool relax,
171+
kuid_t uid, bool relax,
172172
bool reuseport_cb_ok, bool reuseport_ok)
173173
{
174174
int bound_dev_if2;
@@ -185,20 +185,20 @@ static bool inet_bind_conflict(const struct sock *sk, struct sock *sk2,
185185
if (!relax || (!reuseport_ok && sk->sk_reuseport &&
186186
sk2->sk_reuseport && reuseport_cb_ok &&
187187
(sk2->sk_state == TCP_TIME_WAIT ||
188-
uid_eq(sk_uid, sock_i_uid(sk2)))))
188+
uid_eq(uid, sk_uid(sk2)))))
189189
return true;
190190
} else if (!reuseport_ok || !sk->sk_reuseport ||
191191
!sk2->sk_reuseport || !reuseport_cb_ok ||
192192
(sk2->sk_state != TCP_TIME_WAIT &&
193-
!uid_eq(sk_uid, sock_i_uid(sk2)))) {
193+
!uid_eq(uid, sk_uid(sk2)))) {
194194
return true;
195195
}
196196
}
197197
return false;
198198
}
199199

200200
static bool __inet_bhash2_conflict(const struct sock *sk, struct sock *sk2,
201-
kuid_t sk_uid, bool relax,
201+
kuid_t uid, bool relax,
202202
bool reuseport_cb_ok, bool reuseport_ok)
203203
{
204204
if (ipv6_only_sock(sk2)) {
@@ -211,20 +211,20 @@ static bool __inet_bhash2_conflict(const struct sock *sk, struct sock *sk2,
211211
#endif
212212
}
213213

214-
return inet_bind_conflict(sk, sk2, sk_uid, relax,
214+
return inet_bind_conflict(sk, sk2, uid, relax,
215215
reuseport_cb_ok, reuseport_ok);
216216
}
217217

218218
static bool inet_bhash2_conflict(const struct sock *sk,
219219
const struct inet_bind2_bucket *tb2,
220-
kuid_t sk_uid,
220+
kuid_t uid,
221221
bool relax, bool reuseport_cb_ok,
222222
bool reuseport_ok)
223223
{
224224
struct sock *sk2;
225225

226226
sk_for_each_bound(sk2, &tb2->owners) {
227-
if (__inet_bhash2_conflict(sk, sk2, sk_uid, relax,
227+
if (__inet_bhash2_conflict(sk, sk2, uid, relax,
228228
reuseport_cb_ok, reuseport_ok))
229229
return true;
230230
}
@@ -242,8 +242,8 @@ static int inet_csk_bind_conflict(const struct sock *sk,
242242
const struct inet_bind2_bucket *tb2, /* may be null */
243243
bool relax, bool reuseport_ok)
244244
{
245-
kuid_t uid = sock_i_uid((struct sock *)sk);
246245
struct sock_reuseport *reuseport_cb;
246+
kuid_t uid = sk_uid(sk);
247247
bool reuseport_cb_ok;
248248
struct sock *sk2;
249249

@@ -287,11 +287,11 @@ static int inet_csk_bind_conflict(const struct sock *sk,
287287
static bool inet_bhash2_addr_any_conflict(const struct sock *sk, int port, int l3mdev,
288288
bool relax, bool reuseport_ok)
289289
{
290-
kuid_t uid = sock_i_uid((struct sock *)sk);
291290
const struct net *net = sock_net(sk);
292291
struct sock_reuseport *reuseport_cb;
293292
struct inet_bind_hashbucket *head2;
294293
struct inet_bind2_bucket *tb2;
294+
kuid_t uid = sk_uid(sk);
295295
bool conflict = false;
296296
bool reuseport_cb_ok;
297297

@@ -425,15 +425,13 @@ inet_csk_find_open_port(const struct sock *sk, struct inet_bind_bucket **tb_ret,
425425
static inline int sk_reuseport_match(struct inet_bind_bucket *tb,
426426
struct sock *sk)
427427
{
428-
kuid_t uid = sock_i_uid(sk);
429-
430428
if (tb->fastreuseport <= 0)
431429
return 0;
432430
if (!sk->sk_reuseport)
433431
return 0;
434432
if (rcu_access_pointer(sk->sk_reuseport_cb))
435433
return 0;
436-
if (!uid_eq(tb->fastuid, uid))
434+
if (!uid_eq(tb->fastuid, sk_uid(sk)))
437435
return 0;
438436
/* We only need to check the rcv_saddr if this tb was once marked
439437
* without fastreuseport and then was reset, as we can only know that
@@ -458,14 +456,13 @@ static inline int sk_reuseport_match(struct inet_bind_bucket *tb,
458456
void inet_csk_update_fastreuse(struct inet_bind_bucket *tb,
459457
struct sock *sk)
460458
{
461-
kuid_t uid = sock_i_uid(sk);
462459
bool reuse = sk->sk_reuse && sk->sk_state != TCP_LISTEN;
463460

464461
if (hlist_empty(&tb->bhash2)) {
465462
tb->fastreuse = reuse;
466463
if (sk->sk_reuseport) {
467464
tb->fastreuseport = FASTREUSEPORT_ANY;
468-
tb->fastuid = uid;
465+
tb->fastuid = sk_uid(sk);
469466
tb->fast_rcv_saddr = sk->sk_rcv_saddr;
470467
tb->fast_ipv6_only = ipv6_only_sock(sk);
471468
tb->fast_sk_family = sk->sk_family;
@@ -492,7 +489,7 @@ void inet_csk_update_fastreuse(struct inet_bind_bucket *tb,
492489
*/
493490
if (!sk_reuseport_match(tb, sk)) {
494491
tb->fastreuseport = FASTREUSEPORT_STRICT;
495-
tb->fastuid = uid;
492+
tb->fastuid = sk_uid(sk);
496493
tb->fast_rcv_saddr = sk->sk_rcv_saddr;
497494
tb->fast_ipv6_only = ipv6_only_sock(sk);
498495
tb->fast_sk_family = sk->sk_family;

net/ipv4/inet_diag.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ int inet_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb,
181181
goto errout;
182182
#endif
183183

184-
r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
184+
r->idiag_uid = from_kuid_munged(user_ns, sk_uid(sk));
185185
r->idiag_inode = sock_i_ino(sk);
186186

187187
memset(&inet_sockopt, 0, sizeof(inet_sockopt));

net/ipv4/inet_hashtables.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,16 +721,16 @@ static int inet_reuseport_add_sock(struct sock *sk,
721721
{
722722
struct inet_bind_bucket *tb = inet_csk(sk)->icsk_bind_hash;
723723
const struct hlist_nulls_node *node;
724+
kuid_t uid = sk_uid(sk);
724725
struct sock *sk2;
725-
kuid_t uid = sock_i_uid(sk);
726726

727727
sk_nulls_for_each_rcu(sk2, node, &ilb->nulls_head) {
728728
if (sk2 != sk &&
729729
sk2->sk_family == sk->sk_family &&
730730
ipv6_only_sock(sk2) == ipv6_only_sock(sk) &&
731731
sk2->sk_bound_dev_if == sk->sk_bound_dev_if &&
732732
inet_csk(sk2)->icsk_bind_hash == tb &&
733-
sk2->sk_reuseport && uid_eq(uid, sock_i_uid(sk2)) &&
733+
sk2->sk_reuseport && uid_eq(uid, sk_uid(sk2)) &&
734734
inet_rcv_saddr_equal(sk, sk2, false))
735735
return reuseport_add_sock(sk, sk2,
736736
inet_rcv_saddr_any(sk));

net/ipv4/ping.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,7 @@ static void ping_v4_format_sock(struct sock *sp, struct seq_file *f,
11161116
sk_wmem_alloc_get(sp),
11171117
sk_rmem_alloc_get(sp),
11181118
0, 0L, 0,
1119-
from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)),
1119+
from_kuid_munged(seq_user_ns(f), sk_uid(sp)),
11201120
0, sock_i_ino(sp),
11211121
refcount_read(&sp->sk_refcnt), sp,
11221122
atomic_read(&sp->sk_drops));

net/ipv4/raw.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ static void raw_sock_seq_show(struct seq_file *seq, struct sock *sp, int i)
10431043
sk_wmem_alloc_get(sp),
10441044
sk_rmem_alloc_get(sp),
10451045
0, 0L, 0,
1046-
from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)),
1046+
from_kuid_munged(seq_user_ns(seq), sk_uid(sp)),
10471047
0, sock_i_ino(sp),
10481048
refcount_read(&sp->sk_refcnt), sp, atomic_read(&sp->sk_drops));
10491049
}

net/ipv4/tcp_ipv4.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2896,7 +2896,7 @@ static void get_openreq4(const struct request_sock *req,
28962896
jiffies_delta_to_clock_t(delta),
28972897
req->num_timeout,
28982898
from_kuid_munged(seq_user_ns(f),
2899-
sock_i_uid(req->rsk_listener)),
2899+
sk_uid(req->rsk_listener)),
29002900
0, /* non standard timer */
29012901
0, /* open_requests have no inode */
29022902
0,
@@ -2954,7 +2954,7 @@ static void get_tcp4_sock(struct sock *sk, struct seq_file *f, int i)
29542954
timer_active,
29552955
jiffies_delta_to_clock_t(timer_expires - jiffies),
29562956
icsk->icsk_retransmits,
2957-
from_kuid_munged(seq_user_ns(f), sock_i_uid(sk)),
2957+
from_kuid_munged(seq_user_ns(f), sk_uid(sk)),
29582958
icsk->icsk_probes_out,
29592959
sock_i_ino(sk),
29602960
refcount_read(&sk->sk_refcnt), sk,
@@ -3246,9 +3246,9 @@ static int bpf_iter_tcp_seq_show(struct seq_file *seq, void *v)
32463246
const struct request_sock *req = v;
32473247

32483248
uid = from_kuid_munged(seq_user_ns(seq),
3249-
sock_i_uid(req->rsk_listener));
3249+
sk_uid(req->rsk_listener));
32503250
} else {
3251-
uid = from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk));
3251+
uid = from_kuid_munged(seq_user_ns(seq), sk_uid(sk));
32523252
}
32533253

32543254
meta.seq = seq;

0 commit comments

Comments
 (0)