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