Skip to content

Commit 38e7571

Browse files
committed
Merge tag 'io_uring-2019-03-06' of git://git.kernel.dk/linux-block
Pull io_uring IO interface from Jens Axboe: "Second attempt at adding the io_uring interface. Since the first one, we've added basic unit testing of the three system calls, that resides in liburing like the other unit tests that we have so far. It'll take a while to get full coverage of it, but we're working towards it. I've also added two basic test programs to tools/io_uring. One uses the raw interface and has support for all the various features that io_uring supports outside of standard IO, like fixed files, fixed IO buffers, and polled IO. The other uses the liburing API, and is a simplified version of cp(1). This adds support for a new IO interface, io_uring. io_uring allows an application to communicate with the kernel through two rings, the submission queue (SQ) and completion queue (CQ) ring. This allows for very efficient handling of IOs, see the v5 posting for some basic numbers: https://lore.kernel.org/linux-block/20190116175003.17880-1-axboe@kernel.dk/ Outside of just efficiency, the interface is also flexible and extendable, and allows for future use cases like the upcoming NVMe key-value store API, networked IO, and so on. It also supports async buffered IO, something that we've always failed to support in the kernel. Outside of basic IO features, it supports async polled IO as well. This particular feature has already been tested at Facebook months ago for flash storage boxes, with 25-33% improvements. It makes polled IO actually useful for real world use cases, where even basic flash sees a nice win in terms of efficiency, latency, and performance. These boxes were IOPS bound before, now they are not. This series adds three new system calls. One for setting up an io_uring instance (io_uring_setup(2)), one for submitting/completing IO (io_uring_enter(2)), and one for aux functions like registrating file sets, buffers, etc (io_uring_register(2)). Through the help of Arnd, I've coordinated the syscall numbers so merge on that front should be painless. Jon did a writeup of the interface a while back, which (except for minor details that have been tweaked) is still accurate. Find that here: https://lwn.net/Articles/776703/ Huge thanks to Al Viro for helping getting the reference cycle code correct, and to Jann Horn for his extensive reviews focused on both security and bugs in general. There's a userspace library that provides basic functionality for applications that don't need or want to care about how to fiddle with the rings directly. It has helpers to allow applications to easily set up an io_uring instance, and submit/complete IO through it without knowing about the intricacies of the rings. It also includes man pages (thanks to Jeff Moyer), and will continue to grow support helper functions and features as time progresses. Find it here: git://git.kernel.dk/liburing Fio has full support for the raw interface, both in the form of an IO engine (io_uring), but also with a small test application (t/io_uring) that can exercise and benchmark the interface" * tag 'io_uring-2019-03-06' of git://git.kernel.dk/linux-block: io_uring: add a few test tools io_uring: allow workqueue item to handle multiple buffered requests io_uring: add support for IORING_OP_POLL io_uring: add io_kiocb ref count io_uring: add submission polling io_uring: add file set registration net: split out functions related to registering inflight socket files io_uring: add support for pre-mapped user IO buffers block: implement bio helper to add iter bvec pages to bio io_uring: batch io_kiocb allocation io_uring: use fget/fput_many() for file references fs: add fget_many() and fput_many() io_uring: support for IO polling io_uring: add fsync support Add io_uring IO interface
2 parents 80201fe + 21b4aa5 commit 38e7571

32 files changed

Lines changed: 4783 additions & 146 deletions

‎arch/x86/entry/syscalls/syscall_32.tbl‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,6 @@
429429
421 i386 rt_sigtimedwait_time64 sys_rt_sigtimedwait __ia32_compat_sys_rt_sigtimedwait_time64
430430
422 i386 futex_time64 sys_futex __ia32_sys_futex
431431
423 i386 sched_rr_get_interval_time64 sys_sched_rr_get_interval __ia32_sys_sched_rr_get_interval
432+
425 i386 io_uring_setup sys_io_uring_setup __ia32_sys_io_uring_setup
433+
426 i386 io_uring_enter sys_io_uring_enter __ia32_sys_io_uring_enter
434+
427 i386 io_uring_register sys_io_uring_register __ia32_sys_io_uring_register

‎arch/x86/entry/syscalls/syscall_64.tbl‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,9 @@
345345
334 common rseq __x64_sys_rseq
346346
# don't use numbers 387 through 423, add new calls after the last
347347
# 'common' entry
348+
425 common io_uring_setup __x64_sys_io_uring_setup
349+
426 common io_uring_enter __x64_sys_io_uring_enter
350+
427 common io_uring_register __x64_sys_io_uring_register
348351

349352
#
350353
# x32-specific system call numbers start at 512 to avoid cache impact

‎block/bio.c‎

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,40 @@ int bio_add_page(struct bio *bio, struct page *page,
836836
}
837837
EXPORT_SYMBOL(bio_add_page);
838838

839+
static int __bio_iov_bvec_add_pages(struct bio *bio, struct iov_iter *iter)
840+
{
841+
const struct bio_vec *bv = iter->bvec;
842+
unsigned int len;
843+
size_t size;
844+
845+
if (WARN_ON_ONCE(iter->iov_offset > bv->bv_len))
846+
return -EINVAL;
847+
848+
len = min_t(size_t, bv->bv_len - iter->iov_offset, iter->count);
849+
size = bio_add_page(bio, bv->bv_page, len,
850+
bv->bv_offset + iter->iov_offset);
851+
if (size == len) {
852+
struct page *page;
853+
int i;
854+
855+
/*
856+
* For the normal O_DIRECT case, we could skip grabbing this
857+
* reference and then not have to put them again when IO
858+
* completes. But this breaks some in-kernel users, like
859+
* splicing to/from a loop device, where we release the pipe
860+
* pages unconditionally. If we can fix that case, we can
861+
* get rid of the get here and the need to call
862+
* bio_release_pages() at IO completion time.
863+
*/
864+
mp_bvec_for_each_page(page, bv, i)
865+
get_page(page);
866+
iov_iter_advance(iter, size);
867+
return 0;
868+
}
869+
870+
return -EINVAL;
871+
}
872+
839873
#define PAGE_PTRS_PER_BVEC (sizeof(struct bio_vec) / sizeof(struct page *))
840874

841875
/**
@@ -884,23 +918,35 @@ static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
884918
}
885919

886920
/**
887-
* bio_iov_iter_get_pages - pin user or kernel pages and add them to a bio
921+
* bio_iov_iter_get_pages - add user or kernel pages to a bio
888922
* @bio: bio to add pages to
889-
* @iter: iov iterator describing the region to be mapped
923+
* @iter: iov iterator describing the region to be added
924+
*
925+
* This takes either an iterator pointing to user memory, or one pointing to
926+
* kernel pages (BVEC iterator). If we're adding user pages, we pin them and
927+
* map them into the kernel. On IO completion, the caller should put those
928+
* pages. For now, when adding kernel pages, we still grab a reference to the
929+
* page. This isn't strictly needed for the common case, but some call paths
930+
* end up releasing pages from eg a pipe and we can't easily control these.
931+
* See comment in __bio_iov_bvec_add_pages().
890932
*
891-
* Pins pages from *iter and appends them to @bio's bvec array. The
892-
* pages will have to be released using put_page() when done.
893933
* The function tries, but does not guarantee, to pin as many pages as
894-
* fit into the bio, or are requested in *iter, whatever is smaller.
895-
* If MM encounters an error pinning the requested pages, it stops.
896-
* Error is returned only if 0 pages could be pinned.
934+
* fit into the bio, or are requested in *iter, whatever is smaller. If
935+
* MM encounters an error pinning the requested pages, it stops. Error
936+
* is returned only if 0 pages could be pinned.
897937
*/
898938
int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)
899939
{
940+
const bool is_bvec = iov_iter_is_bvec(iter);
900941
unsigned short orig_vcnt = bio->bi_vcnt;
901942

902943
do {
903-
int ret = __bio_iov_iter_get_pages(bio, iter);
944+
int ret;
945+
946+
if (is_bvec)
947+
ret = __bio_iov_bvec_add_pages(bio, iter);
948+
else
949+
ret = __bio_iov_iter_get_pages(bio, iter);
904950

905951
if (unlikely(ret))
906952
return bio->bi_vcnt > orig_vcnt ? 0 : ret;

‎fs/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ obj-$(CONFIG_TIMERFD) += timerfd.o
3131
obj-$(CONFIG_EVENTFD) += eventfd.o
3232
obj-$(CONFIG_USERFAULTFD) += userfaultfd.o
3333
obj-$(CONFIG_AIO) += aio.o
34+
obj-$(CONFIG_IO_URING) += io_uring.o
3435
obj-$(CONFIG_FS_DAX) += dax.o
3536
obj-$(CONFIG_FS_ENCRYPTION) += crypto/
3637
obj-$(CONFIG_FILE_LOCKING) += locks.o

‎fs/file.c‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ void do_close_on_exec(struct files_struct *files)
706706
spin_unlock(&files->file_lock);
707707
}
708708

709-
static struct file *__fget(unsigned int fd, fmode_t mask)
709+
static struct file *__fget(unsigned int fd, fmode_t mask, unsigned int refs)
710710
{
711711
struct files_struct *files = current->files;
712712
struct file *file;
@@ -721,23 +721,28 @@ static struct file *__fget(unsigned int fd, fmode_t mask)
721721
*/
722722
if (file->f_mode & mask)
723723
file = NULL;
724-
else if (!get_file_rcu(file))
724+
else if (!get_file_rcu_many(file, refs))
725725
goto loop;
726726
}
727727
rcu_read_unlock();
728728

729729
return file;
730730
}
731731

732+
struct file *fget_many(unsigned int fd, unsigned int refs)
733+
{
734+
return __fget(fd, FMODE_PATH, refs);
735+
}
736+
732737
struct file *fget(unsigned int fd)
733738
{
734-
return __fget(fd, FMODE_PATH);
739+
return __fget(fd, FMODE_PATH, 1);
735740
}
736741
EXPORT_SYMBOL(fget);
737742

738743
struct file *fget_raw(unsigned int fd)
739744
{
740-
return __fget(fd, 0);
745+
return __fget(fd, 0, 1);
741746
}
742747
EXPORT_SYMBOL(fget_raw);
743748

@@ -768,7 +773,7 @@ static unsigned long __fget_light(unsigned int fd, fmode_t mask)
768773
return 0;
769774
return (unsigned long)file;
770775
} else {
771-
file = __fget(fd, mask);
776+
file = __fget(fd, mask, 1);
772777
if (!file)
773778
return 0;
774779
return FDPUT_FPUT | (unsigned long)file;

‎fs/file_table.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,9 @@ void flush_delayed_fput(void)
326326

327327
static DECLARE_DELAYED_WORK(delayed_fput_work, delayed_fput);
328328

329-
void fput(struct file *file)
329+
void fput_many(struct file *file, unsigned int refs)
330330
{
331-
if (atomic_long_dec_and_test(&file->f_count)) {
331+
if (atomic_long_sub_and_test(refs, &file->f_count)) {
332332
struct task_struct *task = current;
333333

334334
if (likely(!in_interrupt() && !(task->flags & PF_KTHREAD))) {
@@ -347,6 +347,11 @@ void fput(struct file *file)
347347
}
348348
}
349349

350+
void fput(struct file *file)
351+
{
352+
fput_many(file, 1);
353+
}
354+
350355
/*
351356
* synchronous analog of fput(); for kernel threads that might be needed
352357
* in some umount() (and thus can't use flush_delayed_fput() without

0 commit comments

Comments
 (0)