Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 <olivier@trillion01.com>
Link: https://lore.kernel.org/r/d637fa3b437d753c0f4e44ff6a7b5bf2c2611270.1728828877.git.olivier@trillion01.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>

authored by

Olivier Langlois and committed by
Jens Axboe
a5e26f49 45b3941d

+9 -16
+6 -13
io_uring/napi.c
··· 38 38 return ns_to_ktime(t << 10); 39 39 } 40 40 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) 42 42 { 43 43 struct hlist_head *hash_list; 44 - unsigned int napi_id; 45 - struct sock *sk; 46 44 struct io_napi_entry *e; 47 - 48 - sk = sock->sk; 49 - if (!sk) 50 - return; 51 - 52 - napi_id = READ_ONCE(sk->sk_napi_id); 53 45 54 46 /* Non-NAPI IDs can be rejected. */ 55 47 if (napi_id < MIN_NAPI_ID) 56 - return; 48 + return -EINVAL; 57 49 58 50 hash_list = &ctx->napi_ht[hash_min(napi_id, HASH_BITS(ctx->napi_ht))]; 59 51 ··· 54 62 if (e) { 55 63 WRITE_ONCE(e->timeout, jiffies + NAPI_TIMEOUT); 56 64 rcu_read_unlock(); 57 - return; 65 + return -EEXIST; 58 66 } 59 67 rcu_read_unlock(); 60 68 61 69 e = kmalloc(sizeof(*e), GFP_NOWAIT); 62 70 if (!e) 63 - return; 71 + return -ENOMEM; 64 72 65 73 e->napi_id = napi_id; 66 74 e->timeout = jiffies + NAPI_TIMEOUT; ··· 69 77 if (unlikely(io_napi_hash_find(hash_list, napi_id))) { 70 78 spin_unlock(&ctx->napi_lock); 71 79 kfree(e); 72 - return; 80 + return -EEXIST; 73 81 } 74 82 75 83 hlist_add_tail_rcu(&e->node, hash_list); 76 84 list_add_tail_rcu(&e->list, &ctx->napi_list); 77 85 spin_unlock(&ctx->napi_lock); 86 + return 0; 78 87 } 79 88 80 89 static void __io_napi_remove_stale(struct io_ring_ctx *ctx)
+3 -3
io_uring/napi.h
··· 15 15 int io_register_napi(struct io_ring_ctx *ctx, void __user *arg); 16 16 int io_unregister_napi(struct io_ring_ctx *ctx, void __user *arg); 17 17 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); 19 19 20 20 void __io_napi_busy_loop(struct io_ring_ctx *ctx, struct io_wait_queue *iowq); 21 21 int io_napi_sqpoll_busy_poll(struct io_ring_ctx *ctx); ··· 48 48 return; 49 49 50 50 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)); 53 53 } 54 54 55 55 #else