Commit 1f59bc0f authored by Pavel Begunkov's avatar Pavel Begunkov Committed by Jens Axboe

io_uring: don't scm-account for non af_unix sockets

io_uring deals with file reference loops by registering all fixed files
in the SCM/GC infrastrucure. However, only a small subset of all file
types can keep long-term references to other files and those that don't
are not interesting for the garbage collector as they can't be in a
reference loop. They neither can be directly recycled by GC nor affect
loop searching.

Let's skip io_uring SCM accounting for loop-less files, i.e. all but
af_unix sockets, quite imroving fixed file updates performance and
greatly helpnig with memory footprint.
Signed-off-by: default avatarPavel Begunkov <asml.silence@gmail.com>
Link: https://lore.kernel.org/r/9c44ecf6e89d69130a8c4360cce2183ffc5ddd6f.1649277098.git.asml.silence@gmail.comSigned-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent b4f20bb4
...@@ -1223,6 +1223,18 @@ struct sock *io_uring_get_socket(struct file *file) ...@@ -1223,6 +1223,18 @@ struct sock *io_uring_get_socket(struct file *file)
} }
EXPORT_SYMBOL(io_uring_get_socket); EXPORT_SYMBOL(io_uring_get_socket);
#if defined(CONFIG_UNIX)
static inline bool io_file_need_scm(struct file *filp)
{
return !!unix_get_socket(filp);
}
#else
static inline bool io_file_need_scm(struct file *filp)
{
return 0;
}
#endif
static void io_ring_submit_unlock(struct io_ring_ctx *ctx, unsigned issue_flags) static void io_ring_submit_unlock(struct io_ring_ctx *ctx, unsigned issue_flags)
{ {
lockdep_assert_held(&ctx->uring_lock); lockdep_assert_held(&ctx->uring_lock);
...@@ -8426,6 +8438,17 @@ static void io_free_file_tables(struct io_file_table *table) ...@@ -8426,6 +8438,17 @@ static void io_free_file_tables(struct io_file_table *table)
static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
{ {
int i;
for (i = 0; i < ctx->nr_user_files; i++) {
struct file *file = io_file_from_index(ctx, i);
if (!file || io_file_need_scm(file))
continue;
io_fixed_file_slot(&ctx->file_table, i)->file_ptr = 0;
fput(file);
}
#if defined(CONFIG_UNIX) #if defined(CONFIG_UNIX)
if (ctx->ring_sock) { if (ctx->ring_sock) {
struct sock *sock = ctx->ring_sock->sk; struct sock *sock = ctx->ring_sock->sk;
...@@ -8434,16 +8457,6 @@ static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) ...@@ -8434,16 +8457,6 @@ static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
kfree_skb(skb); kfree_skb(skb);
} }
#else
int i;
for (i = 0; i < ctx->nr_user_files; i++) {
struct file *file;
file = io_file_from_index(ctx, i);
if (file)
fput(file);
}
#endif #endif
io_free_file_tables(&ctx->file_table); io_free_file_tables(&ctx->file_table);
io_rsrc_data_free(ctx->file_data); io_rsrc_data_free(ctx->file_data);
...@@ -8592,7 +8605,9 @@ static struct io_sq_data *io_get_sq_data(struct io_uring_params *p, ...@@ -8592,7 +8605,9 @@ static struct io_sq_data *io_get_sq_data(struct io_uring_params *p,
/* /*
* Ensure the UNIX gc is aware of our file set, so we are certain that * Ensure the UNIX gc is aware of our file set, so we are certain that
* the io_uring can be safely unregistered on process exit, even if we have * the io_uring can be safely unregistered on process exit, even if we have
* loops in the file referencing. * loops in the file referencing. We account only files that can hold other
* files because otherwise they can't form a loop and so are not interesting
* for GC.
*/ */
static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
{ {
...@@ -8618,8 +8633,9 @@ static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) ...@@ -8618,8 +8633,9 @@ static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
for (i = 0; i < nr; i++) { for (i = 0; i < nr; i++) {
struct file *file = io_file_from_index(ctx, i + offset); struct file *file = io_file_from_index(ctx, i + offset);
if (!file) if (!file || !io_file_need_scm(file))
continue; continue;
fpl->fp[nr_files] = get_file(file); fpl->fp[nr_files] = get_file(file);
unix_inflight(fpl->user, fpl->fp[nr_files]); unix_inflight(fpl->user, fpl->fp[nr_files]);
nr_files++; nr_files++;
...@@ -8636,7 +8652,7 @@ static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) ...@@ -8636,7 +8652,7 @@ static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset)
for (i = 0; i < nr; i++) { for (i = 0; i < nr; i++) {
struct file *file = io_file_from_index(ctx, i + offset); struct file *file = io_file_from_index(ctx, i + offset);
if (file) if (file && io_file_need_scm(file))
fput(file); fput(file);
} }
} else { } else {
...@@ -8678,6 +8694,7 @@ static int io_sqe_files_scm(struct io_ring_ctx *ctx) ...@@ -8678,6 +8694,7 @@ static int io_sqe_files_scm(struct io_ring_ctx *ctx)
if (file) if (file)
fput(file); fput(file);
io_fixed_file_slot(&ctx->file_table, total)->file_ptr = 0;
total++; total++;
} }
...@@ -8699,6 +8716,11 @@ static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc) ...@@ -8699,6 +8716,11 @@ static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
struct sk_buff *skb; struct sk_buff *skb;
int i; int i;
if (!io_file_need_scm(file)) {
fput(file);
return;
}
__skb_queue_head_init(&list); __skb_queue_head_init(&list);
/* /*
...@@ -8893,6 +8915,9 @@ static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file, ...@@ -8893,6 +8915,9 @@ static int io_sqe_file_register(struct io_ring_ctx *ctx, struct file *file,
struct sk_buff_head *head = &sock->sk_receive_queue; struct sk_buff_head *head = &sock->sk_receive_queue;
struct sk_buff *skb; struct sk_buff *skb;
if (!io_file_need_scm(file))
return 0;
/* /*
* See if we can merge this file into an existing skb SCM_RIGHTS * See if we can merge this file into an existing skb SCM_RIGHTS
* file set. If there's no room, fall back to allocating a new skb * file set. If there's no room, fall back to allocating a new skb
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment