blob: a61ceb6575ab83333f28819a9492286d539dd87d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Berkeley style UIO structures - Alan Cox 1994.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 */
David Howells607ca462012-10-13 10:46:48 +01009#ifndef __LINUX_UIO_H
10#define __LINUX_UIO_H
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Kent Overstreet92236872013-11-27 16:29:46 -080012#include <linux/kernel.h>
Al Viroaa28de22017-06-29 21:45:10 -040013#include <linux/thread_info.h>
Sagi Grimbergd05f4432018-12-03 17:52:09 -080014#include <crypto/hash.h>
David Howells607ca462012-10-13 10:46:48 +010015#include <uapi/linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Kent Overstreet92236872013-11-27 16:29:46 -080017struct page;
Al Viro241699c2016-09-22 16:33:12 -040018struct pipe_inode_info;
Jiri Slaby812ed032009-07-29 15:04:19 -070019
20struct kvec {
21 void *iov_base; /* and that should *never* hold a userland pointer */
22 size_t iov_len;
23};
24
David Howells00e23702018-10-22 13:07:28 +010025enum iter_type {
Jens Axboe875f1d02019-02-27 13:05:25 -070026 /* set if ITER_BVEC doesn't hold a bv_page ref */
27 ITER_BVEC_FLAG_NO_REF = 2,
28
29 /* iter types */
30 ITER_IOVEC = 4,
31 ITER_KVEC = 8,
32 ITER_BVEC = 16,
33 ITER_PIPE = 32,
34 ITER_DISCARD = 64,
Al Viro62a80672014-04-04 23:12:29 -040035};
36
Kent Overstreet92236872013-11-27 16:29:46 -080037struct iov_iter {
Jens Axboe875f1d02019-02-27 13:05:25 -070038 /*
39 * Bit 0 is the read/write bit, set if we're writing.
40 * Bit 1 is the BVEC_FLAG_NO_REF bit, set if type is a bvec and
41 * the caller isn't expecting to drop a page reference when done.
42 */
David Howellsaa563d72018-10-20 00:57:56 +010043 unsigned int type;
Kent Overstreet92236872013-11-27 16:29:46 -080044 size_t iov_offset;
45 size_t count;
Al Viro62a80672014-04-04 23:12:29 -040046 union {
47 const struct iovec *iov;
Al Viroa2804552014-11-27 14:48:42 -050048 const struct kvec *kvec;
Al Viro62a80672014-04-04 23:12:29 -040049 const struct bio_vec *bvec;
Al Viro241699c2016-09-22 16:33:12 -040050 struct pipe_inode_info *pipe;
Al Viro62a80672014-04-04 23:12:29 -040051 };
Al Viro241699c2016-09-22 16:33:12 -040052 union {
53 unsigned long nr_segs;
Al Viro27c0e372017-02-17 18:42:24 -050054 struct {
55 int idx;
56 int start_idx;
57 };
Al Viro241699c2016-09-22 16:33:12 -040058 };
Kent Overstreet92236872013-11-27 16:29:46 -080059};
60
David Howells00e23702018-10-22 13:07:28 +010061static inline enum iter_type iov_iter_type(const struct iov_iter *i)
62{
Ming Leif5eb4d32019-04-26 18:45:21 +080063 return i->type & ~(READ | WRITE | ITER_BVEC_FLAG_NO_REF);
David Howells00e23702018-10-22 13:07:28 +010064}
65
66static inline bool iter_is_iovec(const struct iov_iter *i)
67{
68 return iov_iter_type(i) == ITER_IOVEC;
69}
70
71static inline bool iov_iter_is_kvec(const struct iov_iter *i)
72{
73 return iov_iter_type(i) == ITER_KVEC;
74}
75
76static inline bool iov_iter_is_bvec(const struct iov_iter *i)
77{
78 return iov_iter_type(i) == ITER_BVEC;
79}
80
81static inline bool iov_iter_is_pipe(const struct iov_iter *i)
82{
83 return iov_iter_type(i) == ITER_PIPE;
84}
85
David Howells9ea9ce02018-10-20 00:57:56 +010086static inline bool iov_iter_is_discard(const struct iov_iter *i)
87{
88 return iov_iter_type(i) == ITER_DISCARD;
89}
90
David Howells00e23702018-10-22 13:07:28 +010091static inline unsigned char iov_iter_rw(const struct iov_iter *i)
92{
93 return i->type & (READ | WRITE);
94}
95
Jens Axboe875f1d02019-02-27 13:05:25 -070096static inline bool iov_iter_bvec_no_ref(const struct iov_iter *i)
97{
98 return (i->type & ITER_BVEC_FLAG_NO_REF) != 0;
99}
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/*
102 * Total number of bytes covered by an iovec.
103 *
104 * NOTE that it is not safe to use this function until all the iovec's
105 * segment lengths have been validated. Because the individual lengths can
106 * overflow a size_t when added together.
107 */
108static inline size_t iov_length(const struct iovec *iov, unsigned long nr_segs)
109{
110 unsigned long seg;
111 size_t ret = 0;
112
113 for (seg = 0; seg < nr_segs; seg++)
114 ret += iov[seg].iov_len;
115 return ret;
116}
117
Kent Overstreet92236872013-11-27 16:29:46 -0800118static inline struct iovec iov_iter_iovec(const struct iov_iter *iter)
119{
120 return (struct iovec) {
121 .iov_base = iter->iov->iov_base + iter->iov_offset,
122 .iov_len = min(iter->count,
123 iter->iov->iov_len - iter->iov_offset),
124 };
125}
126
Kent Overstreet92236872013-11-27 16:29:46 -0800127size_t iov_iter_copy_from_user_atomic(struct page *page,
128 struct iov_iter *i, unsigned long offset, size_t bytes);
Kent Overstreet92236872013-11-27 16:29:46 -0800129void iov_iter_advance(struct iov_iter *i, size_t bytes);
Al Viro27c0e372017-02-17 18:42:24 -0500130void iov_iter_revert(struct iov_iter *i, size_t bytes);
Kent Overstreet92236872013-11-27 16:29:46 -0800131int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes);
132size_t iov_iter_single_seg_count(const struct iov_iter *i);
Al Viro6e58e792014-02-03 17:07:03 -0500133size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
134 struct iov_iter *i);
Al Virof0d1bec2014-04-03 15:05:18 -0400135size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
136 struct iov_iter *i);
Al Viroaa28de22017-06-29 21:45:10 -0400137
138size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i);
139size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i);
140bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i);
141size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i);
142bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i);
143
144static __always_inline __must_check
145size_t copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
146{
147 if (unlikely(!check_copy_size(addr, bytes, true)))
Al Viroc43aeb12017-07-10 07:40:49 -0400148 return 0;
Al Viroaa28de22017-06-29 21:45:10 -0400149 else
150 return _copy_to_iter(addr, bytes, i);
151}
152
153static __always_inline __must_check
154size_t copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
155{
156 if (unlikely(!check_copy_size(addr, bytes, false)))
Al Viroc43aeb12017-07-10 07:40:49 -0400157 return 0;
Al Viroaa28de22017-06-29 21:45:10 -0400158 else
159 return _copy_from_iter(addr, bytes, i);
160}
161
162static __always_inline __must_check
163bool copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i)
164{
165 if (unlikely(!check_copy_size(addr, bytes, false)))
166 return false;
167 else
168 return _copy_from_iter_full(addr, bytes, i);
169}
170
171static __always_inline __must_check
172size_t copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
173{
174 if (unlikely(!check_copy_size(addr, bytes, false)))
Al Viroc43aeb12017-07-10 07:40:49 -0400175 return 0;
Al Viroaa28de22017-06-29 21:45:10 -0400176 else
177 return _copy_from_iter_nocache(addr, bytes, i);
178}
179
180static __always_inline __must_check
181bool copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i)
182{
183 if (unlikely(!check_copy_size(addr, bytes, false)))
184 return false;
185 else
186 return _copy_from_iter_full_nocache(addr, bytes, i);
187}
188
Dan Williams0aed55a2017-05-29 12:22:50 -0700189#ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
190/*
191 * Note, users like pmem that depend on the stricter semantics of
192 * copy_from_iter_flushcache() than copy_from_iter_nocache() must check for
193 * IS_ENABLED(CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE) before assuming that the
194 * destination is flushed from the cache on return.
195 */
Linus Torvalds6a37e942017-07-07 20:39:20 -0700196size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i);
Dan Williams0aed55a2017-05-29 12:22:50 -0700197#else
Linus Torvalds6a37e942017-07-07 20:39:20 -0700198#define _copy_from_iter_flushcache _copy_from_iter_nocache
Dan Williams0aed55a2017-05-29 12:22:50 -0700199#endif
Linus Torvalds6a37e942017-07-07 20:39:20 -0700200
Dan Williams87803562018-05-03 17:06:31 -0700201#ifdef CONFIG_ARCH_HAS_UACCESS_MCSAFE
Dan Williams522239b2018-05-22 23:17:03 -0700202size_t _copy_to_iter_mcsafe(const void *addr, size_t bytes, struct iov_iter *i);
Dan Williams87803562018-05-03 17:06:31 -0700203#else
204#define _copy_to_iter_mcsafe _copy_to_iter
205#endif
206
Linus Torvalds6a37e942017-07-07 20:39:20 -0700207static __always_inline __must_check
208size_t copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
209{
210 if (unlikely(!check_copy_size(addr, bytes, false)))
Al Viroc43aeb12017-07-10 07:40:49 -0400211 return 0;
Linus Torvalds6a37e942017-07-07 20:39:20 -0700212 else
213 return _copy_from_iter_flushcache(addr, bytes, i);
214}
215
Dan Williams87803562018-05-03 17:06:31 -0700216static __always_inline __must_check
217size_t copy_to_iter_mcsafe(void *addr, size_t bytes, struct iov_iter *i)
218{
Dave Jiangdfb06cb2018-09-05 13:31:40 -0700219 if (unlikely(!check_copy_size(addr, bytes, true)))
Dan Williams87803562018-05-03 17:06:31 -0700220 return 0;
221 else
222 return _copy_to_iter_mcsafe(addr, bytes, i);
223}
224
Matthew Wilcoxc35e0242014-08-01 09:27:22 -0400225size_t iov_iter_zero(size_t bytes, struct iov_iter *);
Al Viro886a3912014-03-05 13:50:45 -0500226unsigned long iov_iter_alignment(const struct iov_iter *i);
Al Viro357f4352016-04-08 19:05:19 -0400227unsigned long iov_iter_gap_alignment(const struct iov_iter *i);
David Howellsaa563d72018-10-20 00:57:56 +0100228void iov_iter_init(struct iov_iter *i, unsigned int direction, const struct iovec *iov,
Al Viro71d8e532014-03-05 19:28:09 -0500229 unsigned long nr_segs, size_t count);
David Howellsaa563d72018-10-20 00:57:56 +0100230void iov_iter_kvec(struct iov_iter *i, unsigned int direction, const struct kvec *kvec,
Al Viro05afcb72015-01-23 01:08:07 -0500231 unsigned long nr_segs, size_t count);
David Howellsaa563d72018-10-20 00:57:56 +0100232void iov_iter_bvec(struct iov_iter *i, unsigned int direction, const struct bio_vec *bvec,
Al Viroabb78f82014-11-24 14:46:11 -0500233 unsigned long nr_segs, size_t count);
David Howellsaa563d72018-10-20 00:57:56 +0100234void iov_iter_pipe(struct iov_iter *i, unsigned int direction, struct pipe_inode_info *pipe,
Al Viro241699c2016-09-22 16:33:12 -0400235 size_t count);
David Howells9ea9ce02018-10-20 00:57:56 +0100236void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count);
Al Viro7b2c99d2014-03-15 04:05:57 -0400237ssize_t iov_iter_get_pages(struct iov_iter *i, struct page **pages,
Miklos Szeredi2c809292014-09-24 17:09:11 +0200238 size_t maxsize, unsigned maxpages, size_t *start);
Al Viro91f79c42014-03-21 04:58:33 -0400239ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, struct page ***pages,
240 size_t maxsize, size_t *start);
Al Virof67da302014-03-19 01:16:16 -0400241int iov_iter_npages(const struct iov_iter *i, int maxpages);
Kent Overstreet92236872013-11-27 16:29:46 -0800242
Al Viro4b8164b2015-01-31 20:08:47 -0500243const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags);
244
Al Virob57332b2016-10-10 13:57:37 -0400245static inline size_t iov_iter_count(const struct iov_iter *i)
Kent Overstreet92236872013-11-27 16:29:46 -0800246{
247 return i->count;
248}
249
Omar Sandovalbd8e0ff2015-03-17 14:04:02 -0700250/*
Al Viro0b86dbf2014-06-23 08:44:40 +0100251 * Cap the iov_iter by given limit; note that the second argument is
252 * *not* the new size - it's upper limit for such. Passing it a value
253 * greater than the amount of data in iov_iter is fine - it'll just do
254 * nothing in that case.
255 */
256static inline void iov_iter_truncate(struct iov_iter *i, u64 count)
Al Viro0c949332014-03-22 06:51:37 -0400257{
Al Viro0b86dbf2014-06-23 08:44:40 +0100258 /*
259 * count doesn't have to fit in size_t - comparison extends both
260 * operands to u64 here and any value that would be truncated by
261 * conversion in assignement is by definition greater than all
262 * values of size_t, including old i->count.
263 */
Al Viro0c949332014-03-22 06:51:37 -0400264 if (i->count > count)
265 i->count = count;
266}
267
Al Virob42b15f2014-04-04 12:15:19 -0400268/*
269 * reexpand a previously truncated iterator; count must be no more than how much
270 * we had shrunk it.
271 */
272static inline void iov_iter_reexpand(struct iov_iter *i, size_t count)
273{
274 i->count = count;
275}
Sagi Grimbergcb002d02018-12-03 17:52:07 -0800276size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *csump, struct iov_iter *i);
Al Viroa604ec72014-11-24 01:08:00 -0500277size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum, struct iov_iter *i);
Al Virocbbd26b2016-11-01 22:09:04 -0400278bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum, struct iov_iter *i);
Sagi Grimbergd05f4432018-12-03 17:52:09 -0800279size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
280 struct iov_iter *i);
Al Virob42b15f2014-04-04 12:15:19 -0400281
Jens Axboe87e5e6d2019-05-14 16:02:22 -0600282ssize_t import_iovec(int type, const struct iovec __user * uvector,
Al Virobc917be2015-03-21 17:45:43 -0400283 unsigned nr_segs, unsigned fast_segs,
284 struct iovec **iov, struct iov_iter *i);
285
286#ifdef CONFIG_COMPAT
287struct compat_iovec;
Jens Axboe87e5e6d2019-05-14 16:02:22 -0600288ssize_t compat_import_iovec(int type, const struct compat_iovec __user * uvector,
Al Virobc917be2015-03-21 17:45:43 -0400289 unsigned nr_segs, unsigned fast_segs,
290 struct iovec **iov, struct iov_iter *i);
291#endif
292
293int import_single_range(int type, void __user *buf, size_t len,
294 struct iovec *iov, struct iov_iter *i);
295
Al Viro09cf6982017-02-18 01:44:03 -0500296int iov_iter_for_each_range(struct iov_iter *i, size_t bytes,
297 int (*f)(struct kvec *vec, void *context),
298 void *context);
299
Jiri Slaby812ed032009-07-29 15:04:19 -0700300#endif