···11+// SPDX-License-Identifier: GPL-2.022+#include <linux/kernel.h>33+#include <linux/errno.h>44+#include <linux/file.h>55+#include <linux/mm.h>66+#include <linux/slab.h>77+#include <linux/io_uring.h>88+99+#include <uapi/linux/io_uring.h>1010+1111+#include "io_uring_types.h"1212+#include "io_uring.h"1313+1414+int io_file_bitmap_get(struct io_ring_ctx *ctx)1515+{1616+ struct io_file_table *table = &ctx->file_table;1717+ unsigned long nr = ctx->nr_user_files;1818+ int ret;1919+2020+ do {2121+ ret = find_next_zero_bit(table->bitmap, nr, table->alloc_hint);2222+ if (ret != nr)2323+ return ret;2424+2525+ if (!table->alloc_hint)2626+ break;2727+2828+ nr = table->alloc_hint;2929+ table->alloc_hint = 0;3030+ } while (1);3131+3232+ return -ENFILE;3333+}3434+3535+bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files)3636+{3737+ table->files = kvcalloc(nr_files, sizeof(table->files[0]),3838+ GFP_KERNEL_ACCOUNT);3939+ if (unlikely(!table->files))4040+ return false;4141+4242+ table->bitmap = bitmap_zalloc(nr_files, GFP_KERNEL_ACCOUNT);4343+ if (unlikely(!table->bitmap)) {4444+ kvfree(table->files);4545+ return false;4646+ }4747+4848+ return true;4949+}5050+5151+void io_free_file_tables(struct io_file_table *table)5252+{5353+ kvfree(table->files);5454+ bitmap_free(table->bitmap);5555+ table->files = NULL;5656+ table->bitmap = NULL;5757+}
+58
io_uring/filetable.h
···11+// SPDX-License-Identifier: GPL-2.022+#ifndef IOU_FILE_TABLE_H33+#define IOU_FILE_TABLE_H44+55+struct io_ring_ctx;66+77+/*88+ * FFS_SCM is only available on 64-bit archs, for 32-bit we just define it as 099+ * and define IO_URING_SCM_ALL. For this case, we use SCM for all files as we1010+ * can't safely always dereference the file when the task has exited and ring1111+ * cleanup is done. If a file is tracked and part of SCM, then unix gc on1212+ * process exit may reap it before __io_sqe_files_unregister() is run.1313+ */1414+#define FFS_NOWAIT 0x1UL1515+#define FFS_ISREG 0x2UL1616+#if defined(CONFIG_64BIT)1717+#define FFS_SCM 0x4UL1818+#else1919+#define IO_URING_SCM_ALL2020+#define FFS_SCM 0x0UL2121+#endif2222+#define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG|FFS_SCM)2323+2424+struct io_fixed_file {2525+ /* file * with additional FFS_* flags */2626+ unsigned long file_ptr;2727+};2828+2929+struct io_file_table {3030+ struct io_fixed_file *files;3131+ unsigned long *bitmap;3232+ unsigned int alloc_hint;3333+};3434+3535+bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files);3636+void io_free_file_tables(struct io_file_table *table);3737+int io_file_bitmap_get(struct io_ring_ctx *ctx);3838+3939+static inline void io_file_bitmap_clear(struct io_file_table *table, int bit)4040+{4141+ __clear_bit(bit, table->bitmap);4242+ table->alloc_hint = bit;4343+}4444+4545+static inline void io_file_bitmap_set(struct io_file_table *table, int bit)4646+{4747+ WARN_ON_ONCE(test_bit(bit, table->bitmap));4848+ __set_bit(bit, table->bitmap);4949+ table->alloc_hint = bit + 1;5050+}5151+5252+static inline struct io_fixed_file *5353+io_fixed_file_slot(struct io_file_table *table, unsigned i)5454+{5555+ return &table->files[i];5656+}5757+5858+#endif
-86
io_uring/io_uring.c
···146146 struct io_uring_cqe cqe;147147};148148149149-/*150150- * FFS_SCM is only available on 64-bit archs, for 32-bit we just define it as 0151151- * and define IO_URING_SCM_ALL. For this case, we use SCM for all files as we152152- * can't safely always dereference the file when the task has exited and ring153153- * cleanup is done. If a file is tracked and part of SCM, then unix gc on154154- * process exit may reap it before __io_sqe_files_unregister() is run.155155- */156156-#define FFS_NOWAIT 0x1UL157157-#define FFS_ISREG 0x2UL158158-#if defined(CONFIG_64BIT)159159-#define FFS_SCM 0x4UL160160-#else161161-#define IO_URING_SCM_ALL162162-#define FFS_SCM 0x0UL163163-#endif164164-#define FFS_MASK ~(FFS_NOWAIT|FFS_ISREG|FFS_SCM)165165-166166-struct io_fixed_file {167167- /* file * with additional FFS_* flags */168168- unsigned long file_ptr;169169-};170170-171149struct io_rsrc_put {172150 struct list_head list;173151 u64 tag;···39613983 return __io_openat_prep(req, sqe);39623984}3963398539643964-static int io_file_bitmap_get(struct io_ring_ctx *ctx)39653965-{39663966- struct io_file_table *table = &ctx->file_table;39673967- unsigned long nr = ctx->nr_user_files;39683968- int ret;39693969-39703970- do {39713971- ret = find_next_zero_bit(table->bitmap, nr, table->alloc_hint);39723972- if (ret != nr)39733973- return ret;39743974-39753975- if (!table->alloc_hint)39763976- break;39773977-39783978- nr = table->alloc_hint;39793979- table->alloc_hint = 0;39803980- } while (1);39813981-39823982- return -ENFILE;39833983-}39843984-39853986/*39863987 * Note when io_fixed_fd_install() returns error value, it will ensure39873988 * fput() is called correspondingly.···67896832 io_req_task_queue_fail(req, ret);67906833}6791683467926792-static inline struct io_fixed_file *io_fixed_file_slot(struct io_file_table *table,67936793- unsigned i)67946794-{67956795- return &table->files[i];67966796-}67976797-67986835static inline struct file *io_file_from_index(struct io_ring_ctx *ctx,67996836 int index)68006837{···78837932fail:78847933 io_rsrc_data_free(data);78857934 return ret;78867886-}78877887-78887888-static bool io_alloc_file_tables(struct io_file_table *table, unsigned nr_files)78897889-{78907890- table->files = kvcalloc(nr_files, sizeof(table->files[0]),78917891- GFP_KERNEL_ACCOUNT);78927892- if (unlikely(!table->files))78937893- return false;78947894-78957895- table->bitmap = bitmap_zalloc(nr_files, GFP_KERNEL_ACCOUNT);78967896- if (unlikely(!table->bitmap)) {78977897- kvfree(table->files);78987898- return false;78997899- }79007900-79017901- return true;79027902-}79037903-79047904-static void io_free_file_tables(struct io_file_table *table)79057905-{79067906- kvfree(table->files);79077907- bitmap_free(table->bitmap);79087908- table->files = NULL;79097909- table->bitmap = NULL;79107910-}79117911-79127912-static inline void io_file_bitmap_set(struct io_file_table *table, int bit)79137913-{79147914- WARN_ON_ONCE(test_bit(bit, table->bitmap));79157915- __set_bit(bit, table->bitmap);79167916- table->alloc_hint = bit + 1;79177917-}79187918-79197919-static inline void io_file_bitmap_clear(struct io_file_table *table, int bit)79207920-{79217921- __clear_bit(bit, table->bitmap);79227922- table->alloc_hint = bit;79237935}7924793679257937static void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
+1-6
io_uring/io_uring_types.h
···55#include <linux/task_work.h>6677#include "io-wq.h"88+#include "filetable.h"89910struct io_uring {1011 u32 head ____cacheline_aligned_in_smp;···121120 struct eventfd_ctx *cq_ev_fd;122121 unsigned int eventfd_async: 1;123122 struct rcu_head rcu;124124-};125125-126126-struct io_file_table {127127- struct io_fixed_file *files;128128- unsigned long *bitmap;129129- unsigned int alloc_hint;130123};131124132125struct io_ring_ctx {