Skip to content

Commit a5e26f4

Browse files
Olivier Langloisaxboe
authored andcommitted
io_uring/napi: improve __io_napi_add
1. move the sock->sk pointer validity test outside the function to avoid the function call overhead and to make the function more more reusable 2. change its name to __io_napi_add_id to be more precise about it is doing 3. return an error code to report errors Signed-off-by: Olivier Langlois <[email protected]> Link: https://lore.kernel.org/r/d637fa3b437d753c0f4e44ff6a7b5bf2c2611270.1728828877.git.olivier@trillion01.com Signed-off-by: Jens Axboe <[email protected]>
1 parent 45b3941 commit a5e26f4

File tree

2 files changed

+9
-16
lines changed

2 files changed

+9
-16
lines changed

io_uring/napi.c

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,14 @@ static inline ktime_t net_to_ktime(unsigned long t)
3838
return ns_to_ktime(t << 10);
3939
}
4040

41-
void __io_napi_add(struct io_ring_ctx *ctx, struct socket *sock)
41+
int __io_napi_add_id(struct io_ring_ctx *ctx, unsigned int napi_id)
4242
{
4343
struct hlist_head *hash_list;
44-
unsigned int napi_id;
45-
struct sock *sk;
4644
struct io_napi_entry *e;
4745

48-
sk = sock->sk;
49-
if (!sk)
50-
return;
51-
52-
napi_id = READ_ONCE(sk->sk_napi_id);
53-
5446
/* Non-NAPI IDs can be rejected. */
5547
if (napi_id < MIN_NAPI_ID)
56-
return;
48+
return -EINVAL;
5749

5850
hash_list = &ctx->napi_ht[hash_min(napi_id, HASH_BITS(ctx->napi_ht))];
5951

@@ -62,13 +54,13 @@ void __io_napi_add(struct io_ring_ctx *ctx, struct socket *sock)
6254
if (e) {
6355
WRITE_ONCE(e->timeout, jiffies + NAPI_TIMEOUT);
6456
rcu_read_unlock();
65-
return;
57+
return -EEXIST;
6658
}
6759
rcu_read_unlock();
6860

6961
e = kmalloc(sizeof(*e), GFP_NOWAIT);
7062
if (!e)
71-
return;
63+
return -ENOMEM;
7264

7365
e->napi_id = napi_id;
7466
e->timeout = jiffies + NAPI_TIMEOUT;
@@ -77,12 +69,13 @@ void __io_napi_add(struct io_ring_ctx *ctx, struct socket *sock)
7769
if (unlikely(io_napi_hash_find(hash_list, napi_id))) {
7870
spin_unlock(&ctx->napi_lock);
7971
kfree(e);
80-
return;
72+
return -EEXIST;
8173
}
8274

8375
hlist_add_tail_rcu(&e->node, hash_list);
8476
list_add_tail_rcu(&e->list, &ctx->napi_list);
8577
spin_unlock(&ctx->napi_lock);
78+
return 0;
8679
}
8780

8881
static void __io_napi_remove_stale(struct io_ring_ctx *ctx)

io_uring/napi.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ void io_napi_free(struct io_ring_ctx *ctx);
1515
int io_register_napi(struct io_ring_ctx *ctx, void __user *arg);
1616
int io_unregister_napi(struct io_ring_ctx *ctx, void __user *arg);
1717

18-
void __io_napi_add(struct io_ring_ctx *ctx, struct socket *sock);
18+
int __io_napi_add_id(struct io_ring_ctx *ctx, unsigned int napi_id);
1919

2020
void __io_napi_busy_loop(struct io_ring_ctx *ctx, struct io_wait_queue *iowq);
2121
int io_napi_sqpoll_busy_poll(struct io_ring_ctx *ctx);
@@ -48,8 +48,8 @@ static inline void io_napi_add(struct io_kiocb *req)
4848
return;
4949

5050
sock = sock_from_file(req->file);
51-
if (sock)
52-
__io_napi_add(ctx, sock);
51+
if (sock && sock->sk)
52+
__io_napi_add_id(ctx, READ_ONCE(sock->sk->sk_napi_id));
5353
}
5454

5555
#else

0 commit comments

Comments
 (0)