io_uring/rsrc: keep cached refs per node
[platform/kernel/linux-starfive.git] / io_uring / rsrc.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/errno.h>
4 #include <linux/fs.h>
5 #include <linux/file.h>
6 #include <linux/mm.h>
7 #include <linux/slab.h>
8 #include <linux/nospec.h>
9 #include <linux/hugetlb.h>
10 #include <linux/compat.h>
11 #include <linux/io_uring.h>
12
13 #include <uapi/linux/io_uring.h>
14
15 #include "io_uring.h"
16 #include "openclose.h"
17 #include "rsrc.h"
18
19 struct io_rsrc_update {
20         struct file                     *file;
21         u64                             arg;
22         u32                             nr_args;
23         u32                             offset;
24 };
25
26 static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
27                                   struct io_mapped_ubuf **pimu,
28                                   struct page **last_hpage);
29
30 #define IO_RSRC_REF_BATCH       100
31
32 /* only define max */
33 #define IORING_MAX_FIXED_FILES  (1U << 20)
34 #define IORING_MAX_REG_BUFFERS  (1U << 14)
35
36 void io_rsrc_refs_drop(struct io_ring_ctx *ctx)
37         __must_hold(&ctx->uring_lock)
38 {
39         struct io_rsrc_node *node = ctx->rsrc_node;
40
41         if (node && node->cached_refs) {
42                 io_rsrc_put_node(node, node->cached_refs);
43                 node->cached_refs = 0;
44         }
45 }
46
47 int __io_account_mem(struct user_struct *user, unsigned long nr_pages)
48 {
49         unsigned long page_limit, cur_pages, new_pages;
50
51         if (!nr_pages)
52                 return 0;
53
54         /* Don't allow more pages than we can safely lock */
55         page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
56
57         cur_pages = atomic_long_read(&user->locked_vm);
58         do {
59                 new_pages = cur_pages + nr_pages;
60                 if (new_pages > page_limit)
61                         return -ENOMEM;
62         } while (!atomic_long_try_cmpxchg(&user->locked_vm,
63                                           &cur_pages, new_pages));
64         return 0;
65 }
66
67 static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
68 {
69         if (ctx->user)
70                 __io_unaccount_mem(ctx->user, nr_pages);
71
72         if (ctx->mm_account)
73                 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
74 }
75
76 static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
77 {
78         int ret;
79
80         if (ctx->user) {
81                 ret = __io_account_mem(ctx->user, nr_pages);
82                 if (ret)
83                         return ret;
84         }
85
86         if (ctx->mm_account)
87                 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
88
89         return 0;
90 }
91
92 static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
93                        void __user *arg, unsigned index)
94 {
95         struct iovec __user *src;
96
97 #ifdef CONFIG_COMPAT
98         if (ctx->compat) {
99                 struct compat_iovec __user *ciovs;
100                 struct compat_iovec ciov;
101
102                 ciovs = (struct compat_iovec __user *) arg;
103                 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
104                         return -EFAULT;
105
106                 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
107                 dst->iov_len = ciov.iov_len;
108                 return 0;
109         }
110 #endif
111         src = (struct iovec __user *) arg;
112         if (copy_from_user(dst, &src[index], sizeof(*dst)))
113                 return -EFAULT;
114         return 0;
115 }
116
117 static int io_buffer_validate(struct iovec *iov)
118 {
119         unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1);
120
121         /*
122          * Don't impose further limits on the size and buffer
123          * constraints here, we'll -EINVAL later when IO is
124          * submitted if they are wrong.
125          */
126         if (!iov->iov_base)
127                 return iov->iov_len ? -EFAULT : 0;
128         if (!iov->iov_len)
129                 return -EFAULT;
130
131         /* arbitrary limit, but we need something */
132         if (iov->iov_len > SZ_1G)
133                 return -EFAULT;
134
135         if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp))
136                 return -EOVERFLOW;
137
138         return 0;
139 }
140
141 static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot)
142 {
143         struct io_mapped_ubuf *imu = *slot;
144         unsigned int i;
145
146         if (imu != ctx->dummy_ubuf) {
147                 for (i = 0; i < imu->nr_bvecs; i++)
148                         unpin_user_page(imu->bvec[i].bv_page);
149                 if (imu->acct_pages)
150                         io_unaccount_mem(ctx, imu->acct_pages);
151                 kvfree(imu);
152         }
153         *slot = NULL;
154 }
155
156 void io_rsrc_refs_refill(struct io_ring_ctx *ctx, struct io_rsrc_node *node)
157         __must_hold(&ctx->uring_lock)
158 {
159         node->cached_refs += IO_RSRC_REF_BATCH;
160         refcount_add(IO_RSRC_REF_BATCH, &node->refs);
161 }
162
163 static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
164 {
165         struct io_rsrc_data *rsrc_data = ref_node->rsrc_data;
166         struct io_ring_ctx *ctx = rsrc_data->ctx;
167         struct io_rsrc_put *prsrc, *tmp;
168
169         list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
170                 list_del(&prsrc->list);
171
172                 if (prsrc->tag) {
173                         if (ctx->flags & IORING_SETUP_IOPOLL) {
174                                 mutex_lock(&ctx->uring_lock);
175                                 io_post_aux_cqe(ctx, prsrc->tag, 0, 0);
176                                 mutex_unlock(&ctx->uring_lock);
177                         } else {
178                                 io_post_aux_cqe(ctx, prsrc->tag, 0, 0);
179                         }
180                 }
181
182                 rsrc_data->do_put(ctx, prsrc);
183                 kfree(prsrc);
184         }
185
186         io_rsrc_node_destroy(ref_node);
187         if (atomic_dec_and_test(&rsrc_data->refs))
188                 complete(&rsrc_data->done);
189 }
190
191 void io_rsrc_put_work(struct work_struct *work)
192 {
193         struct io_ring_ctx *ctx;
194         struct llist_node *node;
195
196         ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
197         node = llist_del_all(&ctx->rsrc_put_llist);
198
199         while (node) {
200                 struct io_rsrc_node *ref_node;
201                 struct llist_node *next = node->next;
202
203                 ref_node = llist_entry(node, struct io_rsrc_node, llist);
204                 __io_rsrc_put_work(ref_node);
205                 node = next;
206         }
207 }
208
209 void io_rsrc_put_tw(struct callback_head *cb)
210 {
211         struct io_ring_ctx *ctx = container_of(cb, struct io_ring_ctx,
212                                                rsrc_put_tw);
213
214         io_rsrc_put_work(&ctx->rsrc_put_work.work);
215 }
216
217 void io_wait_rsrc_data(struct io_rsrc_data *data)
218 {
219         if (data && !atomic_dec_and_test(&data->refs))
220                 wait_for_completion(&data->done);
221 }
222
223 void io_rsrc_node_destroy(struct io_rsrc_node *ref_node)
224 {
225         kfree(ref_node);
226 }
227
228 __cold void io_rsrc_node_ref_zero(struct io_rsrc_node *node)
229 {
230         struct io_ring_ctx *ctx = node->rsrc_data->ctx;
231         unsigned long flags;
232         bool first_add = false;
233         unsigned long delay = HZ;
234
235         spin_lock_irqsave(&ctx->rsrc_ref_lock, flags);
236         node->done = true;
237
238         /* if we are mid-quiesce then do not delay */
239         if (node->rsrc_data->quiesce)
240                 delay = 0;
241
242         while (!list_empty(&ctx->rsrc_ref_list)) {
243                 node = list_first_entry(&ctx->rsrc_ref_list,
244                                             struct io_rsrc_node, node);
245                 /* recycle ref nodes in order */
246                 if (!node->done)
247                         break;
248                 list_del(&node->node);
249                 first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist);
250         }
251         spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags);
252
253         if (!first_add)
254                 return;
255
256         if (ctx->submitter_task) {
257                 if (!task_work_add(ctx->submitter_task, &ctx->rsrc_put_tw,
258                                    ctx->notify_method))
259                         return;
260         }
261         mod_delayed_work(system_wq, &ctx->rsrc_put_work, delay);
262 }
263
264 static struct io_rsrc_node *io_rsrc_node_alloc(void)
265 {
266         struct io_rsrc_node *ref_node;
267
268         ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
269         if (!ref_node)
270                 return NULL;
271
272         refcount_set(&ref_node->refs, 1);
273         INIT_LIST_HEAD(&ref_node->node);
274         INIT_LIST_HEAD(&ref_node->rsrc_list);
275         ref_node->done = false;
276         return ref_node;
277 }
278
279 void io_rsrc_node_switch(struct io_ring_ctx *ctx,
280                          struct io_rsrc_data *data_to_kill)
281         __must_hold(&ctx->uring_lock)
282 {
283         WARN_ON_ONCE(!ctx->rsrc_backup_node);
284         WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node);
285
286         io_rsrc_refs_drop(ctx);
287
288         if (data_to_kill) {
289                 struct io_rsrc_node *rsrc_node = ctx->rsrc_node;
290
291                 rsrc_node->rsrc_data = data_to_kill;
292                 spin_lock_irq(&ctx->rsrc_ref_lock);
293                 list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list);
294                 spin_unlock_irq(&ctx->rsrc_ref_lock);
295
296                 atomic_inc(&data_to_kill->refs);
297                 /* put master ref */
298                 io_rsrc_put_node(rsrc_node, 1);
299                 ctx->rsrc_node = NULL;
300         }
301
302         if (!ctx->rsrc_node) {
303                 ctx->rsrc_node = ctx->rsrc_backup_node;
304                 ctx->rsrc_backup_node = NULL;
305                 ctx->rsrc_node->cached_refs = 0;
306         }
307 }
308
309 int io_rsrc_node_switch_start(struct io_ring_ctx *ctx)
310 {
311         if (ctx->rsrc_backup_node)
312                 return 0;
313         ctx->rsrc_backup_node = io_rsrc_node_alloc();
314         return ctx->rsrc_backup_node ? 0 : -ENOMEM;
315 }
316
317 __cold static int io_rsrc_ref_quiesce(struct io_rsrc_data *data,
318                                       struct io_ring_ctx *ctx)
319 {
320         int ret;
321
322         /* As we may drop ->uring_lock, other task may have started quiesce */
323         if (data->quiesce)
324                 return -ENXIO;
325         ret = io_rsrc_node_switch_start(ctx);
326         if (ret)
327                 return ret;
328         io_rsrc_node_switch(ctx, data);
329
330         /* kill initial ref, already quiesced if zero */
331         if (atomic_dec_and_test(&data->refs))
332                 return 0;
333
334         data->quiesce = true;
335         mutex_unlock(&ctx->uring_lock);
336         do {
337                 ret = io_run_task_work_sig(ctx);
338                 if (ret < 0) {
339                         atomic_inc(&data->refs);
340                         /* wait for all works potentially completing data->done */
341                         flush_delayed_work(&ctx->rsrc_put_work);
342                         reinit_completion(&data->done);
343                         mutex_lock(&ctx->uring_lock);
344                         break;
345                 }
346
347                 flush_delayed_work(&ctx->rsrc_put_work);
348                 ret = wait_for_completion_interruptible(&data->done);
349                 if (!ret) {
350                         mutex_lock(&ctx->uring_lock);
351                         if (atomic_read(&data->refs) <= 0)
352                                 break;
353                         /*
354                          * it has been revived by another thread while
355                          * we were unlocked
356                          */
357                         mutex_unlock(&ctx->uring_lock);
358                 }
359         } while (1);
360         data->quiesce = false;
361
362         return ret;
363 }
364
365 static void io_free_page_table(void **table, size_t size)
366 {
367         unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
368
369         for (i = 0; i < nr_tables; i++)
370                 kfree(table[i]);
371         kfree(table);
372 }
373
374 static void io_rsrc_data_free(struct io_rsrc_data *data)
375 {
376         size_t size = data->nr * sizeof(data->tags[0][0]);
377
378         if (data->tags)
379                 io_free_page_table((void **)data->tags, size);
380         kfree(data);
381 }
382
383 static __cold void **io_alloc_page_table(size_t size)
384 {
385         unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
386         size_t init_size = size;
387         void **table;
388
389         table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT);
390         if (!table)
391                 return NULL;
392
393         for (i = 0; i < nr_tables; i++) {
394                 unsigned int this_size = min_t(size_t, size, PAGE_SIZE);
395
396                 table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT);
397                 if (!table[i]) {
398                         io_free_page_table(table, init_size);
399                         return NULL;
400                 }
401                 size -= this_size;
402         }
403         return table;
404 }
405
406 __cold static int io_rsrc_data_alloc(struct io_ring_ctx *ctx,
407                                      rsrc_put_fn *do_put, u64 __user *utags,
408                                      unsigned nr, struct io_rsrc_data **pdata)
409 {
410         struct io_rsrc_data *data;
411         int ret = 0;
412         unsigned i;
413
414         data = kzalloc(sizeof(*data), GFP_KERNEL);
415         if (!data)
416                 return -ENOMEM;
417         data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0]));
418         if (!data->tags) {
419                 kfree(data);
420                 return -ENOMEM;
421         }
422
423         data->nr = nr;
424         data->ctx = ctx;
425         data->do_put = do_put;
426         if (utags) {
427                 ret = -EFAULT;
428                 for (i = 0; i < nr; i++) {
429                         u64 *tag_slot = io_get_tag_slot(data, i);
430
431                         if (copy_from_user(tag_slot, &utags[i],
432                                            sizeof(*tag_slot)))
433                                 goto fail;
434                 }
435         }
436
437         atomic_set(&data->refs, 1);
438         init_completion(&data->done);
439         *pdata = data;
440         return 0;
441 fail:
442         io_rsrc_data_free(data);
443         return ret;
444 }
445
446 static int __io_sqe_files_update(struct io_ring_ctx *ctx,
447                                  struct io_uring_rsrc_update2 *up,
448                                  unsigned nr_args)
449 {
450         u64 __user *tags = u64_to_user_ptr(up->tags);
451         __s32 __user *fds = u64_to_user_ptr(up->data);
452         struct io_rsrc_data *data = ctx->file_data;
453         struct io_fixed_file *file_slot;
454         struct file *file;
455         int fd, i, err = 0;
456         unsigned int done;
457         bool needs_switch = false;
458
459         if (!ctx->file_data)
460                 return -ENXIO;
461         if (up->offset + nr_args > ctx->nr_user_files)
462                 return -EINVAL;
463
464         for (done = 0; done < nr_args; done++) {
465                 u64 tag = 0;
466
467                 if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) ||
468                     copy_from_user(&fd, &fds[done], sizeof(fd))) {
469                         err = -EFAULT;
470                         break;
471                 }
472                 if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) {
473                         err = -EINVAL;
474                         break;
475                 }
476                 if (fd == IORING_REGISTER_FILES_SKIP)
477                         continue;
478
479                 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
480                 file_slot = io_fixed_file_slot(&ctx->file_table, i);
481
482                 if (file_slot->file_ptr) {
483                         file = (struct file *)(file_slot->file_ptr & FFS_MASK);
484                         err = io_queue_rsrc_removal(data, i, ctx->rsrc_node, file);
485                         if (err)
486                                 break;
487                         file_slot->file_ptr = 0;
488                         io_file_bitmap_clear(&ctx->file_table, i);
489                         needs_switch = true;
490                 }
491                 if (fd != -1) {
492                         file = fget(fd);
493                         if (!file) {
494                                 err = -EBADF;
495                                 break;
496                         }
497                         /*
498                          * Don't allow io_uring instances to be registered. If
499                          * UNIX isn't enabled, then this causes a reference
500                          * cycle and this instance can never get freed. If UNIX
501                          * is enabled we'll handle it just fine, but there's
502                          * still no point in allowing a ring fd as it doesn't
503                          * support regular read/write anyway.
504                          */
505                         if (io_is_uring_fops(file)) {
506                                 fput(file);
507                                 err = -EBADF;
508                                 break;
509                         }
510                         err = io_scm_file_account(ctx, file);
511                         if (err) {
512                                 fput(file);
513                                 break;
514                         }
515                         *io_get_tag_slot(data, i) = tag;
516                         io_fixed_file_set(file_slot, file);
517                         io_file_bitmap_set(&ctx->file_table, i);
518                 }
519         }
520
521         if (needs_switch)
522                 io_rsrc_node_switch(ctx, data);
523         return done ? done : err;
524 }
525
526 static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
527                                    struct io_uring_rsrc_update2 *up,
528                                    unsigned int nr_args)
529 {
530         u64 __user *tags = u64_to_user_ptr(up->tags);
531         struct iovec iov, __user *iovs = u64_to_user_ptr(up->data);
532         struct page *last_hpage = NULL;
533         bool needs_switch = false;
534         __u32 done;
535         int i, err;
536
537         if (!ctx->buf_data)
538                 return -ENXIO;
539         if (up->offset + nr_args > ctx->nr_user_bufs)
540                 return -EINVAL;
541
542         for (done = 0; done < nr_args; done++) {
543                 struct io_mapped_ubuf *imu;
544                 int offset = up->offset + done;
545                 u64 tag = 0;
546
547                 err = io_copy_iov(ctx, &iov, iovs, done);
548                 if (err)
549                         break;
550                 if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) {
551                         err = -EFAULT;
552                         break;
553                 }
554                 err = io_buffer_validate(&iov);
555                 if (err)
556                         break;
557                 if (!iov.iov_base && tag) {
558                         err = -EINVAL;
559                         break;
560                 }
561                 err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage);
562                 if (err)
563                         break;
564
565                 i = array_index_nospec(offset, ctx->nr_user_bufs);
566                 if (ctx->user_bufs[i] != ctx->dummy_ubuf) {
567                         err = io_queue_rsrc_removal(ctx->buf_data, i,
568                                                     ctx->rsrc_node, ctx->user_bufs[i]);
569                         if (unlikely(err)) {
570                                 io_buffer_unmap(ctx, &imu);
571                                 break;
572                         }
573                         ctx->user_bufs[i] = ctx->dummy_ubuf;
574                         needs_switch = true;
575                 }
576
577                 ctx->user_bufs[i] = imu;
578                 *io_get_tag_slot(ctx->buf_data, offset) = tag;
579         }
580
581         if (needs_switch)
582                 io_rsrc_node_switch(ctx, ctx->buf_data);
583         return done ? done : err;
584 }
585
586 static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
587                                      struct io_uring_rsrc_update2 *up,
588                                      unsigned nr_args)
589 {
590         __u32 tmp;
591         int err;
592
593         if (check_add_overflow(up->offset, nr_args, &tmp))
594                 return -EOVERFLOW;
595         err = io_rsrc_node_switch_start(ctx);
596         if (err)
597                 return err;
598
599         switch (type) {
600         case IORING_RSRC_FILE:
601                 return __io_sqe_files_update(ctx, up, nr_args);
602         case IORING_RSRC_BUFFER:
603                 return __io_sqe_buffers_update(ctx, up, nr_args);
604         }
605         return -EINVAL;
606 }
607
608 int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
609                              unsigned nr_args)
610 {
611         struct io_uring_rsrc_update2 up;
612
613         if (!nr_args)
614                 return -EINVAL;
615         memset(&up, 0, sizeof(up));
616         if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update)))
617                 return -EFAULT;
618         if (up.resv || up.resv2)
619                 return -EINVAL;
620         return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args);
621 }
622
623 int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
624                             unsigned size, unsigned type)
625 {
626         struct io_uring_rsrc_update2 up;
627
628         if (size != sizeof(up))
629                 return -EINVAL;
630         if (copy_from_user(&up, arg, sizeof(up)))
631                 return -EFAULT;
632         if (!up.nr || up.resv || up.resv2)
633                 return -EINVAL;
634         return __io_register_rsrc_update(ctx, type, &up, up.nr);
635 }
636
637 __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
638                             unsigned int size, unsigned int type)
639 {
640         struct io_uring_rsrc_register rr;
641
642         /* keep it extendible */
643         if (size != sizeof(rr))
644                 return -EINVAL;
645
646         memset(&rr, 0, sizeof(rr));
647         if (copy_from_user(&rr, arg, size))
648                 return -EFAULT;
649         if (!rr.nr || rr.resv2)
650                 return -EINVAL;
651         if (rr.flags & ~IORING_RSRC_REGISTER_SPARSE)
652                 return -EINVAL;
653
654         switch (type) {
655         case IORING_RSRC_FILE:
656                 if (rr.flags & IORING_RSRC_REGISTER_SPARSE && rr.data)
657                         break;
658                 return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data),
659                                              rr.nr, u64_to_user_ptr(rr.tags));
660         case IORING_RSRC_BUFFER:
661                 if (rr.flags & IORING_RSRC_REGISTER_SPARSE && rr.data)
662                         break;
663                 return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data),
664                                                rr.nr, u64_to_user_ptr(rr.tags));
665         }
666         return -EINVAL;
667 }
668
669 int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
670 {
671         struct io_rsrc_update *up = io_kiocb_to_cmd(req, struct io_rsrc_update);
672
673         if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
674                 return -EINVAL;
675         if (sqe->rw_flags || sqe->splice_fd_in)
676                 return -EINVAL;
677
678         up->offset = READ_ONCE(sqe->off);
679         up->nr_args = READ_ONCE(sqe->len);
680         if (!up->nr_args)
681                 return -EINVAL;
682         up->arg = READ_ONCE(sqe->addr);
683         return 0;
684 }
685
686 static int io_files_update_with_index_alloc(struct io_kiocb *req,
687                                             unsigned int issue_flags)
688 {
689         struct io_rsrc_update *up = io_kiocb_to_cmd(req, struct io_rsrc_update);
690         __s32 __user *fds = u64_to_user_ptr(up->arg);
691         unsigned int done;
692         struct file *file;
693         int ret, fd;
694
695         if (!req->ctx->file_data)
696                 return -ENXIO;
697
698         for (done = 0; done < up->nr_args; done++) {
699                 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
700                         ret = -EFAULT;
701                         break;
702                 }
703
704                 file = fget(fd);
705                 if (!file) {
706                         ret = -EBADF;
707                         break;
708                 }
709                 ret = io_fixed_fd_install(req, issue_flags, file,
710                                           IORING_FILE_INDEX_ALLOC);
711                 if (ret < 0)
712                         break;
713                 if (copy_to_user(&fds[done], &ret, sizeof(ret))) {
714                         __io_close_fixed(req->ctx, issue_flags, ret);
715                         ret = -EFAULT;
716                         break;
717                 }
718         }
719
720         if (done)
721                 return done;
722         return ret;
723 }
724
725 int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
726 {
727         struct io_rsrc_update *up = io_kiocb_to_cmd(req, struct io_rsrc_update);
728         struct io_ring_ctx *ctx = req->ctx;
729         struct io_uring_rsrc_update2 up2;
730         int ret;
731
732         up2.offset = up->offset;
733         up2.data = up->arg;
734         up2.nr = 0;
735         up2.tags = 0;
736         up2.resv = 0;
737         up2.resv2 = 0;
738
739         if (up->offset == IORING_FILE_INDEX_ALLOC) {
740                 ret = io_files_update_with_index_alloc(req, issue_flags);
741         } else {
742                 io_ring_submit_lock(ctx, issue_flags);
743                 ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
744                                                 &up2, up->nr_args);
745                 io_ring_submit_unlock(ctx, issue_flags);
746         }
747
748         if (ret < 0)
749                 req_set_fail(req);
750         io_req_set_res(req, ret, 0);
751         return IOU_OK;
752 }
753
754 int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
755                           struct io_rsrc_node *node, void *rsrc)
756 {
757         u64 *tag_slot = io_get_tag_slot(data, idx);
758         struct io_rsrc_put *prsrc;
759
760         prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
761         if (!prsrc)
762                 return -ENOMEM;
763
764         prsrc->tag = *tag_slot;
765         *tag_slot = 0;
766         prsrc->rsrc = rsrc;
767         list_add(&prsrc->list, &node->rsrc_list);
768         return 0;
769 }
770
771 void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
772 {
773         int i;
774
775         for (i = 0; i < ctx->nr_user_files; i++) {
776                 struct file *file = io_file_from_index(&ctx->file_table, i);
777
778                 /* skip scm accounted files, they'll be freed by ->ring_sock */
779                 if (!file || io_file_need_scm(file))
780                         continue;
781                 io_file_bitmap_clear(&ctx->file_table, i);
782                 fput(file);
783         }
784
785 #if defined(CONFIG_UNIX)
786         if (ctx->ring_sock) {
787                 struct sock *sock = ctx->ring_sock->sk;
788                 struct sk_buff *skb;
789
790                 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
791                         kfree_skb(skb);
792         }
793 #endif
794         io_free_file_tables(&ctx->file_table);
795         io_file_table_set_alloc_range(ctx, 0, 0);
796         io_rsrc_data_free(ctx->file_data);
797         ctx->file_data = NULL;
798         ctx->nr_user_files = 0;
799 }
800
801 int io_sqe_files_unregister(struct io_ring_ctx *ctx)
802 {
803         unsigned nr = ctx->nr_user_files;
804         int ret;
805
806         if (!ctx->file_data)
807                 return -ENXIO;
808
809         /*
810          * Quiesce may unlock ->uring_lock, and while it's not held
811          * prevent new requests using the table.
812          */
813         ctx->nr_user_files = 0;
814         ret = io_rsrc_ref_quiesce(ctx->file_data, ctx);
815         ctx->nr_user_files = nr;
816         if (!ret)
817                 __io_sqe_files_unregister(ctx);
818         return ret;
819 }
820
821 /*
822  * Ensure the UNIX gc is aware of our file set, so we are certain that
823  * the io_uring can be safely unregistered on process exit, even if we have
824  * loops in the file referencing. We account only files that can hold other
825  * files because otherwise they can't form a loop and so are not interesting
826  * for GC.
827  */
828 int __io_scm_file_account(struct io_ring_ctx *ctx, struct file *file)
829 {
830 #if defined(CONFIG_UNIX)
831         struct sock *sk = ctx->ring_sock->sk;
832         struct sk_buff_head *head = &sk->sk_receive_queue;
833         struct scm_fp_list *fpl;
834         struct sk_buff *skb;
835
836         if (likely(!io_file_need_scm(file)))
837                 return 0;
838
839         /*
840          * See if we can merge this file into an existing skb SCM_RIGHTS
841          * file set. If there's no room, fall back to allocating a new skb
842          * and filling it in.
843          */
844         spin_lock_irq(&head->lock);
845         skb = skb_peek(head);
846         if (skb && UNIXCB(skb).fp->count < SCM_MAX_FD)
847                 __skb_unlink(skb, head);
848         else
849                 skb = NULL;
850         spin_unlock_irq(&head->lock);
851
852         if (!skb) {
853                 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
854                 if (!fpl)
855                         return -ENOMEM;
856
857                 skb = alloc_skb(0, GFP_KERNEL);
858                 if (!skb) {
859                         kfree(fpl);
860                         return -ENOMEM;
861                 }
862
863                 fpl->user = get_uid(current_user());
864                 fpl->max = SCM_MAX_FD;
865                 fpl->count = 0;
866
867                 UNIXCB(skb).fp = fpl;
868                 skb->sk = sk;
869                 skb->scm_io_uring = 1;
870                 skb->destructor = unix_destruct_scm;
871                 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
872         }
873
874         fpl = UNIXCB(skb).fp;
875         fpl->fp[fpl->count++] = get_file(file);
876         unix_inflight(fpl->user, file);
877         skb_queue_head(head, skb);
878         fput(file);
879 #endif
880         return 0;
881 }
882
883 static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
884 {
885         struct file *file = prsrc->file;
886 #if defined(CONFIG_UNIX)
887         struct sock *sock = ctx->ring_sock->sk;
888         struct sk_buff_head list, *head = &sock->sk_receive_queue;
889         struct sk_buff *skb;
890         int i;
891
892         if (!io_file_need_scm(file)) {
893                 fput(file);
894                 return;
895         }
896
897         __skb_queue_head_init(&list);
898
899         /*
900          * Find the skb that holds this file in its SCM_RIGHTS. When found,
901          * remove this entry and rearrange the file array.
902          */
903         skb = skb_dequeue(head);
904         while (skb) {
905                 struct scm_fp_list *fp;
906
907                 fp = UNIXCB(skb).fp;
908                 for (i = 0; i < fp->count; i++) {
909                         int left;
910
911                         if (fp->fp[i] != file)
912                                 continue;
913
914                         unix_notinflight(fp->user, fp->fp[i]);
915                         left = fp->count - 1 - i;
916                         if (left) {
917                                 memmove(&fp->fp[i], &fp->fp[i + 1],
918                                                 left * sizeof(struct file *));
919                         }
920                         fp->count--;
921                         if (!fp->count) {
922                                 kfree_skb(skb);
923                                 skb = NULL;
924                         } else {
925                                 __skb_queue_tail(&list, skb);
926                         }
927                         fput(file);
928                         file = NULL;
929                         break;
930                 }
931
932                 if (!file)
933                         break;
934
935                 __skb_queue_tail(&list, skb);
936
937                 skb = skb_dequeue(head);
938         }
939
940         if (skb_peek(&list)) {
941                 spin_lock_irq(&head->lock);
942                 while ((skb = __skb_dequeue(&list)) != NULL)
943                         __skb_queue_tail(head, skb);
944                 spin_unlock_irq(&head->lock);
945         }
946 #else
947         fput(file);
948 #endif
949 }
950
951 int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
952                           unsigned nr_args, u64 __user *tags)
953 {
954         __s32 __user *fds = (__s32 __user *) arg;
955         struct file *file;
956         int fd, ret;
957         unsigned i;
958
959         if (ctx->file_data)
960                 return -EBUSY;
961         if (!nr_args)
962                 return -EINVAL;
963         if (nr_args > IORING_MAX_FIXED_FILES)
964                 return -EMFILE;
965         if (nr_args > rlimit(RLIMIT_NOFILE))
966                 return -EMFILE;
967         ret = io_rsrc_node_switch_start(ctx);
968         if (ret)
969                 return ret;
970         ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args,
971                                  &ctx->file_data);
972         if (ret)
973                 return ret;
974
975         if (!io_alloc_file_tables(&ctx->file_table, nr_args)) {
976                 io_rsrc_data_free(ctx->file_data);
977                 ctx->file_data = NULL;
978                 return -ENOMEM;
979         }
980
981         for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
982                 struct io_fixed_file *file_slot;
983
984                 if (fds && copy_from_user(&fd, &fds[i], sizeof(fd))) {
985                         ret = -EFAULT;
986                         goto fail;
987                 }
988                 /* allow sparse sets */
989                 if (!fds || fd == -1) {
990                         ret = -EINVAL;
991                         if (unlikely(*io_get_tag_slot(ctx->file_data, i)))
992                                 goto fail;
993                         continue;
994                 }
995
996                 file = fget(fd);
997                 ret = -EBADF;
998                 if (unlikely(!file))
999                         goto fail;
1000
1001                 /*
1002                  * Don't allow io_uring instances to be registered. If UNIX
1003                  * isn't enabled, then this causes a reference cycle and this
1004                  * instance can never get freed. If UNIX is enabled we'll
1005                  * handle it just fine, but there's still no point in allowing
1006                  * a ring fd as it doesn't support regular read/write anyway.
1007                  */
1008                 if (io_is_uring_fops(file)) {
1009                         fput(file);
1010                         goto fail;
1011                 }
1012                 ret = io_scm_file_account(ctx, file);
1013                 if (ret) {
1014                         fput(file);
1015                         goto fail;
1016                 }
1017                 file_slot = io_fixed_file_slot(&ctx->file_table, i);
1018                 io_fixed_file_set(file_slot, file);
1019                 io_file_bitmap_set(&ctx->file_table, i);
1020         }
1021
1022         /* default it to the whole table */
1023         io_file_table_set_alloc_range(ctx, 0, ctx->nr_user_files);
1024         io_rsrc_node_switch(ctx, NULL);
1025         return 0;
1026 fail:
1027         __io_sqe_files_unregister(ctx);
1028         return ret;
1029 }
1030
1031 static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
1032 {
1033         io_buffer_unmap(ctx, &prsrc->buf);
1034         prsrc->buf = NULL;
1035 }
1036
1037 void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
1038 {
1039         unsigned int i;
1040
1041         for (i = 0; i < ctx->nr_user_bufs; i++)
1042                 io_buffer_unmap(ctx, &ctx->user_bufs[i]);
1043         kfree(ctx->user_bufs);
1044         io_rsrc_data_free(ctx->buf_data);
1045         ctx->user_bufs = NULL;
1046         ctx->buf_data = NULL;
1047         ctx->nr_user_bufs = 0;
1048 }
1049
1050 int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
1051 {
1052         unsigned nr = ctx->nr_user_bufs;
1053         int ret;
1054
1055         if (!ctx->buf_data)
1056                 return -ENXIO;
1057
1058         /*
1059          * Quiesce may unlock ->uring_lock, and while it's not held
1060          * prevent new requests using the table.
1061          */
1062         ctx->nr_user_bufs = 0;
1063         ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx);
1064         ctx->nr_user_bufs = nr;
1065         if (!ret)
1066                 __io_sqe_buffers_unregister(ctx);
1067         return ret;
1068 }
1069
1070 /*
1071  * Not super efficient, but this is just a registration time. And we do cache
1072  * the last compound head, so generally we'll only do a full search if we don't
1073  * match that one.
1074  *
1075  * We check if the given compound head page has already been accounted, to
1076  * avoid double accounting it. This allows us to account the full size of the
1077  * page, not just the constituent pages of a huge page.
1078  */
1079 static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
1080                                   int nr_pages, struct page *hpage)
1081 {
1082         int i, j;
1083
1084         /* check current page array */
1085         for (i = 0; i < nr_pages; i++) {
1086                 if (!PageCompound(pages[i]))
1087                         continue;
1088                 if (compound_head(pages[i]) == hpage)
1089                         return true;
1090         }
1091
1092         /* check previously registered pages */
1093         for (i = 0; i < ctx->nr_user_bufs; i++) {
1094                 struct io_mapped_ubuf *imu = ctx->user_bufs[i];
1095
1096                 for (j = 0; j < imu->nr_bvecs; j++) {
1097                         if (!PageCompound(imu->bvec[j].bv_page))
1098                                 continue;
1099                         if (compound_head(imu->bvec[j].bv_page) == hpage)
1100                                 return true;
1101                 }
1102         }
1103
1104         return false;
1105 }
1106
1107 static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
1108                                  int nr_pages, struct io_mapped_ubuf *imu,
1109                                  struct page **last_hpage)
1110 {
1111         int i, ret;
1112
1113         imu->acct_pages = 0;
1114         for (i = 0; i < nr_pages; i++) {
1115                 if (!PageCompound(pages[i])) {
1116                         imu->acct_pages++;
1117                 } else {
1118                         struct page *hpage;
1119
1120                         hpage = compound_head(pages[i]);
1121                         if (hpage == *last_hpage)
1122                                 continue;
1123                         *last_hpage = hpage;
1124                         if (headpage_already_acct(ctx, pages, i, hpage))
1125                                 continue;
1126                         imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
1127                 }
1128         }
1129
1130         if (!imu->acct_pages)
1131                 return 0;
1132
1133         ret = io_account_mem(ctx, imu->acct_pages);
1134         if (ret)
1135                 imu->acct_pages = 0;
1136         return ret;
1137 }
1138
1139 struct page **io_pin_pages(unsigned long ubuf, unsigned long len, int *npages)
1140 {
1141         unsigned long start, end, nr_pages;
1142         struct vm_area_struct **vmas = NULL;
1143         struct page **pages = NULL;
1144         int i, pret, ret = -ENOMEM;
1145
1146         end = (ubuf + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1147         start = ubuf >> PAGE_SHIFT;
1148         nr_pages = end - start;
1149
1150         pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
1151         if (!pages)
1152                 goto done;
1153
1154         vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
1155                               GFP_KERNEL);
1156         if (!vmas)
1157                 goto done;
1158
1159         ret = 0;
1160         mmap_read_lock(current->mm);
1161         pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
1162                               pages, vmas);
1163         if (pret == nr_pages) {
1164                 struct file *file = vmas[0]->vm_file;
1165
1166                 /* don't support file backed memory */
1167                 for (i = 0; i < nr_pages; i++) {
1168                         if (vmas[i]->vm_file != file) {
1169                                 ret = -EINVAL;
1170                                 break;
1171                         }
1172                         if (!file)
1173                                 continue;
1174                         if (!vma_is_shmem(vmas[i]) && !is_file_hugepages(file)) {
1175                                 ret = -EOPNOTSUPP;
1176                                 break;
1177                         }
1178                 }
1179                 *npages = nr_pages;
1180         } else {
1181                 ret = pret < 0 ? pret : -EFAULT;
1182         }
1183         mmap_read_unlock(current->mm);
1184         if (ret) {
1185                 /*
1186                  * if we did partial map, or found file backed vmas,
1187                  * release any pages we did get
1188                  */
1189                 if (pret > 0)
1190                         unpin_user_pages(pages, pret);
1191                 goto done;
1192         }
1193         ret = 0;
1194 done:
1195         kvfree(vmas);
1196         if (ret < 0) {
1197                 kvfree(pages);
1198                 pages = ERR_PTR(ret);
1199         }
1200         return pages;
1201 }
1202
1203 static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
1204                                   struct io_mapped_ubuf **pimu,
1205                                   struct page **last_hpage)
1206 {
1207         struct io_mapped_ubuf *imu = NULL;
1208         struct page **pages = NULL;
1209         unsigned long off;
1210         size_t size;
1211         int ret, nr_pages, i;
1212         struct folio *folio = NULL;
1213
1214         *pimu = ctx->dummy_ubuf;
1215         if (!iov->iov_base)
1216                 return 0;
1217
1218         ret = -ENOMEM;
1219         pages = io_pin_pages((unsigned long) iov->iov_base, iov->iov_len,
1220                                 &nr_pages);
1221         if (IS_ERR(pages)) {
1222                 ret = PTR_ERR(pages);
1223                 pages = NULL;
1224                 goto done;
1225         }
1226
1227         /* If it's a huge page, try to coalesce them into a single bvec entry */
1228         if (nr_pages > 1) {
1229                 folio = page_folio(pages[0]);
1230                 for (i = 1; i < nr_pages; i++) {
1231                         if (page_folio(pages[i]) != folio) {
1232                                 folio = NULL;
1233                                 break;
1234                         }
1235                 }
1236                 if (folio) {
1237                         /*
1238                          * The pages are bound to the folio, it doesn't
1239                          * actually unpin them but drops all but one reference,
1240                          * which is usually put down by io_buffer_unmap().
1241                          * Note, needs a better helper.
1242                          */
1243                         unpin_user_pages(&pages[1], nr_pages - 1);
1244                         nr_pages = 1;
1245                 }
1246         }
1247
1248         imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL);
1249         if (!imu)
1250                 goto done;
1251
1252         ret = io_buffer_account_pin(ctx, pages, nr_pages, imu, last_hpage);
1253         if (ret) {
1254                 unpin_user_pages(pages, nr_pages);
1255                 goto done;
1256         }
1257
1258         off = (unsigned long) iov->iov_base & ~PAGE_MASK;
1259         size = iov->iov_len;
1260         /* store original address for later verification */
1261         imu->ubuf = (unsigned long) iov->iov_base;
1262         imu->ubuf_end = imu->ubuf + iov->iov_len;
1263         imu->nr_bvecs = nr_pages;
1264         *pimu = imu;
1265         ret = 0;
1266
1267         if (folio) {
1268                 bvec_set_page(&imu->bvec[0], pages[0], size, off);
1269                 goto done;
1270         }
1271         for (i = 0; i < nr_pages; i++) {
1272                 size_t vec_len;
1273
1274                 vec_len = min_t(size_t, size, PAGE_SIZE - off);
1275                 bvec_set_page(&imu->bvec[i], pages[i], vec_len, off);
1276                 off = 0;
1277                 size -= vec_len;
1278         }
1279 done:
1280         if (ret)
1281                 kvfree(imu);
1282         kvfree(pages);
1283         return ret;
1284 }
1285
1286 static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
1287 {
1288         ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL);
1289         return ctx->user_bufs ? 0 : -ENOMEM;
1290 }
1291
1292 int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
1293                             unsigned int nr_args, u64 __user *tags)
1294 {
1295         struct page *last_hpage = NULL;
1296         struct io_rsrc_data *data;
1297         int i, ret;
1298         struct iovec iov;
1299
1300         BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16));
1301
1302         if (ctx->user_bufs)
1303                 return -EBUSY;
1304         if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS)
1305                 return -EINVAL;
1306         ret = io_rsrc_node_switch_start(ctx);
1307         if (ret)
1308                 return ret;
1309         ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data);
1310         if (ret)
1311                 return ret;
1312         ret = io_buffers_map_alloc(ctx, nr_args);
1313         if (ret) {
1314                 io_rsrc_data_free(data);
1315                 return ret;
1316         }
1317
1318         for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) {
1319                 if (arg) {
1320                         ret = io_copy_iov(ctx, &iov, arg, i);
1321                         if (ret)
1322                                 break;
1323                         ret = io_buffer_validate(&iov);
1324                         if (ret)
1325                                 break;
1326                 } else {
1327                         memset(&iov, 0, sizeof(iov));
1328                 }
1329
1330                 if (!iov.iov_base && *io_get_tag_slot(data, i)) {
1331                         ret = -EINVAL;
1332                         break;
1333                 }
1334
1335                 ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i],
1336                                              &last_hpage);
1337                 if (ret)
1338                         break;
1339         }
1340
1341         WARN_ON_ONCE(ctx->buf_data);
1342
1343         ctx->buf_data = data;
1344         if (ret)
1345                 __io_sqe_buffers_unregister(ctx);
1346         else
1347                 io_rsrc_node_switch(ctx, NULL);
1348         return ret;
1349 }
1350
1351 int io_import_fixed(int ddir, struct iov_iter *iter,
1352                            struct io_mapped_ubuf *imu,
1353                            u64 buf_addr, size_t len)
1354 {
1355         u64 buf_end;
1356         size_t offset;
1357
1358         if (WARN_ON_ONCE(!imu))
1359                 return -EFAULT;
1360         if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end)))
1361                 return -EFAULT;
1362         /* not inside the mapped region */
1363         if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end))
1364                 return -EFAULT;
1365
1366         /*
1367          * Might not be a start of buffer, set size appropriately
1368          * and advance us to the beginning.
1369          */
1370         offset = buf_addr - imu->ubuf;
1371         iov_iter_bvec(iter, ddir, imu->bvec, imu->nr_bvecs, offset + len);
1372
1373         if (offset) {
1374                 /*
1375                  * Don't use iov_iter_advance() here, as it's really slow for
1376                  * using the latter parts of a big fixed buffer - it iterates
1377                  * over each segment manually. We can cheat a bit here, because
1378                  * we know that:
1379                  *
1380                  * 1) it's a BVEC iter, we set it up
1381                  * 2) all bvecs are PAGE_SIZE in size, except potentially the
1382                  *    first and last bvec
1383                  *
1384                  * So just find our index, and adjust the iterator afterwards.
1385                  * If the offset is within the first bvec (or the whole first
1386                  * bvec, just use iov_iter_advance(). This makes it easier
1387                  * since we can just skip the first segment, which may not
1388                  * be PAGE_SIZE aligned.
1389                  */
1390                 const struct bio_vec *bvec = imu->bvec;
1391
1392                 if (offset <= bvec->bv_len) {
1393                         /*
1394                          * Note, huge pages buffers consists of one large
1395                          * bvec entry and should always go this way. The other
1396                          * branch doesn't expect non PAGE_SIZE'd chunks.
1397                          */
1398                         iter->bvec = bvec;
1399                         iter->nr_segs = bvec->bv_len;
1400                         iter->count -= offset;
1401                         iter->iov_offset = offset;
1402                 } else {
1403                         unsigned long seg_skip;
1404
1405                         /* skip first vec */
1406                         offset -= bvec->bv_len;
1407                         seg_skip = 1 + (offset >> PAGE_SHIFT);
1408
1409                         iter->bvec = bvec + seg_skip;
1410                         iter->nr_segs -= seg_skip;
1411                         iter->count -= bvec->bv_len + offset;
1412                         iter->iov_offset = offset & ~PAGE_MASK;
1413                 }
1414         }
1415
1416         return 0;
1417 }