gup: Introduce FOLL_NOFAULT flag to disable page faults
[linux-block.git] / include / linux / uio.h
CommitLineData
2874c5fd 1/* SPDX-License-Identifier: GPL-2.0-or-later */
1da177e4
LT
2/*
3 * Berkeley style UIO structures - Alan Cox 1994.
1da177e4 4 */
607ca46e
DH
5#ifndef __LINUX_UIO_H
6#define __LINUX_UIO_H
1da177e4 7
92236878 8#include <linux/kernel.h>
aa28de27 9#include <linux/thread_info.h>
607ca46e 10#include <uapi/linux/uio.h>
1da177e4 11
92236878 12struct page;
241699cd 13struct pipe_inode_info;
812ed032
JS
14
15struct kvec {
16 void *iov_base; /* and that should *never* hold a userland pointer */
17 size_t iov_len;
18};
19
00e23707 20enum iter_type {
875f1d07 21 /* iter types */
8cd54c1c
AV
22 ITER_IOVEC,
23 ITER_KVEC,
24 ITER_BVEC,
25 ITER_PIPE,
26 ITER_XARRAY,
27 ITER_DISCARD,
62a8067a
AV
28};
29
8fb0f47a
JA
30struct iov_iter_state {
31 size_t iov_offset;
32 size_t count;
33 unsigned long nr_segs;
34};
35
92236878 36struct iov_iter {
8cd54c1c
AV
37 u8 iter_type;
38 bool data_source;
92236878
KO
39 size_t iov_offset;
40 size_t count;
62a8067a
AV
41 union {
42 const struct iovec *iov;
a280455f 43 const struct kvec *kvec;
62a8067a 44 const struct bio_vec *bvec;
7ff50620 45 struct xarray *xarray;
241699cd
AV
46 struct pipe_inode_info *pipe;
47 };
48 union {
49 unsigned long nr_segs;
27c0e374 50 struct {
8cefc107
DH
51 unsigned int head;
52 unsigned int start_head;
27c0e374 53 };
7ff50620 54 loff_t xarray_start;
62a8067a 55 };
92236878
KO
56};
57
00e23707
DH
58static inline enum iter_type iov_iter_type(const struct iov_iter *i)
59{
8cd54c1c 60 return i->iter_type;
00e23707
DH
61}
62
8fb0f47a
JA
63static inline void iov_iter_save_state(struct iov_iter *iter,
64 struct iov_iter_state *state)
65{
66 state->iov_offset = iter->iov_offset;
67 state->count = iter->count;
68 state->nr_segs = iter->nr_segs;
69}
70
00e23707
DH
71static inline bool iter_is_iovec(const struct iov_iter *i)
72{
73 return iov_iter_type(i) == ITER_IOVEC;
74}
75
76static inline bool iov_iter_is_kvec(const struct iov_iter *i)
77{
78 return iov_iter_type(i) == ITER_KVEC;
79}
80
81static inline bool iov_iter_is_bvec(const struct iov_iter *i)
82{
83 return iov_iter_type(i) == ITER_BVEC;
84}
85
86static inline bool iov_iter_is_pipe(const struct iov_iter *i)
87{
88 return iov_iter_type(i) == ITER_PIPE;
89}
90
9ea9ce04
DH
91static inline bool iov_iter_is_discard(const struct iov_iter *i)
92{
93 return iov_iter_type(i) == ITER_DISCARD;
94}
95
7ff50620
DH
96static inline bool iov_iter_is_xarray(const struct iov_iter *i)
97{
98 return iov_iter_type(i) == ITER_XARRAY;
99}
100
00e23707
DH
101static inline unsigned char iov_iter_rw(const struct iov_iter *i)
102{
8cd54c1c 103 return i->data_source ? WRITE : READ;
00e23707
DH
104}
105
1da177e4
LT
106/*
107 * Total number of bytes covered by an iovec.
108 *
109 * NOTE that it is not safe to use this function until all the iovec's
110 * segment lengths have been validated. Because the individual lengths can
111 * overflow a size_t when added together.
112 */
113static inline size_t iov_length(const struct iovec *iov, unsigned long nr_segs)
114{
115 unsigned long seg;
116 size_t ret = 0;
117
118 for (seg = 0; seg < nr_segs; seg++)
119 ret += iov[seg].iov_len;
120 return ret;
121}
122
92236878
KO
123static inline struct iovec iov_iter_iovec(const struct iov_iter *iter)
124{
125 return (struct iovec) {
126 .iov_base = iter->iov->iov_base + iter->iov_offset,
127 .iov_len = min(iter->count,
128 iter->iov->iov_len - iter->iov_offset),
129 };
130}
131
f0b65f39
AV
132size_t copy_page_from_iter_atomic(struct page *page, unsigned offset,
133 size_t bytes, struct iov_iter *i);
92236878 134void iov_iter_advance(struct iov_iter *i, size_t bytes);
27c0e374 135void iov_iter_revert(struct iov_iter *i, size_t bytes);
a6294593 136size_t fault_in_iov_iter_readable(const struct iov_iter *i, size_t bytes);
cdd591fc 137size_t fault_in_iov_iter_writeable(const struct iov_iter *i, size_t bytes);
92236878 138size_t iov_iter_single_seg_count(const struct iov_iter *i);
6e58e79d
AV
139size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
140 struct iov_iter *i);
f0d1bec9
AV
141size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
142 struct iov_iter *i);
aa28de27
AV
143
144size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i);
145size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i);
aa28de27 146size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i);
aa28de27
AV
147
148static __always_inline __must_check
149size_t copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
150{
151 if (unlikely(!check_copy_size(addr, bytes, true)))
c43aeb19 152 return 0;
aa28de27
AV
153 else
154 return _copy_to_iter(addr, bytes, i);
155}
156
157static __always_inline __must_check
158size_t copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
159{
160 if (unlikely(!check_copy_size(addr, bytes, false)))
c43aeb19 161 return 0;
aa28de27
AV
162 else
163 return _copy_from_iter(addr, bytes, i);
164}
165
166static __always_inline __must_check
167bool copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i)
168{
4b6c132b
AV
169 size_t copied = copy_from_iter(addr, bytes, i);
170 if (likely(copied == bytes))
171 return true;
172 iov_iter_revert(i, copied);
173 return false;
aa28de27
AV
174}
175
176static __always_inline __must_check
177size_t copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
178{
179 if (unlikely(!check_copy_size(addr, bytes, false)))
c43aeb19 180 return 0;
aa28de27
AV
181 else
182 return _copy_from_iter_nocache(addr, bytes, i);
183}
184
185static __always_inline __must_check
186bool copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i)
187{
4b6c132b
AV
188 size_t copied = copy_from_iter_nocache(addr, bytes, i);
189 if (likely(copied == bytes))
190 return true;
191 iov_iter_revert(i, copied);
192 return false;
aa28de27
AV
193}
194
0aed55af
DW
195#ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
196/*
197 * Note, users like pmem that depend on the stricter semantics of
198 * copy_from_iter_flushcache() than copy_from_iter_nocache() must check for
199 * IS_ENABLED(CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE) before assuming that the
200 * destination is flushed from the cache on return.
201 */
6a37e940 202size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i);
0aed55af 203#else
6a37e940
LT
204#define _copy_from_iter_flushcache _copy_from_iter_nocache
205#endif
206
ec6347bb
DW
207#ifdef CONFIG_ARCH_HAS_COPY_MC
208size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i);
8780356e 209#else
ec6347bb 210#define _copy_mc_to_iter _copy_to_iter
8780356e
DW
211#endif
212
6a37e940
LT
213static __always_inline __must_check
214size_t copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
0aed55af 215{
6a37e940 216 if (unlikely(!check_copy_size(addr, bytes, false)))
c43aeb19 217 return 0;
6a37e940
LT
218 else
219 return _copy_from_iter_flushcache(addr, bytes, i);
0aed55af 220}
6a37e940 221
8780356e 222static __always_inline __must_check
ec6347bb 223size_t copy_mc_to_iter(void *addr, size_t bytes, struct iov_iter *i)
8780356e 224{
dfb06cba 225 if (unlikely(!check_copy_size(addr, bytes, true)))
8780356e
DW
226 return 0;
227 else
ec6347bb 228 return _copy_mc_to_iter(addr, bytes, i);
8780356e
DW
229}
230
c35e0248 231size_t iov_iter_zero(size_t bytes, struct iov_iter *);
886a3911 232unsigned long iov_iter_alignment(const struct iov_iter *i);
357f435d 233unsigned long iov_iter_gap_alignment(const struct iov_iter *i);
aa563d7b 234void iov_iter_init(struct iov_iter *i, unsigned int direction, const struct iovec *iov,
71d8e532 235 unsigned long nr_segs, size_t count);
aa563d7b 236void iov_iter_kvec(struct iov_iter *i, unsigned int direction, const struct kvec *kvec,
05afcb77 237 unsigned long nr_segs, size_t count);
aa563d7b 238void iov_iter_bvec(struct iov_iter *i, unsigned int direction, const struct bio_vec *bvec,
abb78f87 239 unsigned long nr_segs, size_t count);
aa563d7b 240void iov_iter_pipe(struct iov_iter *i, unsigned int direction, struct pipe_inode_info *pipe,
241699cd 241 size_t count);
9ea9ce04 242void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count);
7ff50620
DH
243void iov_iter_xarray(struct iov_iter *i, unsigned int direction, struct xarray *xarray,
244 loff_t start, size_t count);
7b2c99d1 245ssize_t iov_iter_get_pages(struct iov_iter *i, struct page **pages,
2c80929c 246 size_t maxsize, unsigned maxpages, size_t *start);
91f79c43
AV
247ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, struct page ***pages,
248 size_t maxsize, size_t *start);
f67da30c 249int iov_iter_npages(const struct iov_iter *i, int maxpages);
8fb0f47a 250void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state);
92236878 251
4b8164b9
AV
252const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags);
253
b57332b4 254static inline size_t iov_iter_count(const struct iov_iter *i)
92236878
KO
255{
256 return i->count;
257}
258
0b86dbf6
AV
259/*
260 * Cap the iov_iter by given limit; note that the second argument is
261 * *not* the new size - it's upper limit for such. Passing it a value
262 * greater than the amount of data in iov_iter is fine - it'll just do
263 * nothing in that case.
264 */
265static inline void iov_iter_truncate(struct iov_iter *i, u64 count)
0c949334 266{
0b86dbf6
AV
267 /*
268 * count doesn't have to fit in size_t - comparison extends both
269 * operands to u64 here and any value that would be truncated by
270 * conversion in assignement is by definition greater than all
271 * values of size_t, including old i->count.
272 */
7dedd3e1 273 if (i->count > count)
0c949334
AV
274 i->count = count;
275}
276
b42b15fd
AV
277/*
278 * reexpand a previously truncated iterator; count must be no more than how much
279 * we had shrunk it.
280 */
281static inline void iov_iter_reexpand(struct iov_iter *i, size_t count)
282{
283 i->count = count;
284}
52cbd23a
WB
285
286struct csum_state {
287 __wsum csum;
288 size_t off;
289};
290
291size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *csstate, struct iov_iter *i);
a604ec7e 292size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum, struct iov_iter *i);
4b6c132b
AV
293
294static __always_inline __must_check
295bool csum_and_copy_from_iter_full(void *addr, size_t bytes,
296 __wsum *csum, struct iov_iter *i)
297{
298 size_t copied = csum_and_copy_from_iter(addr, bytes, csum, i);
299 if (likely(copied == bytes))
300 return true;
301 iov_iter_revert(i, copied);
302 return false;
303}
d05f4435
SG
304size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
305 struct iov_iter *i);
b42b15fd 306
bfdc5970
CH
307struct iovec *iovec_from_user(const struct iovec __user *uvector,
308 unsigned long nr_segs, unsigned long fast_segs,
309 struct iovec *fast_iov, bool compat);
310ssize_t import_iovec(int type, const struct iovec __user *uvec,
311 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
312 struct iov_iter *i);
313ssize_t __import_iovec(int type, const struct iovec __user *uvec,
314 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp,
315 struct iov_iter *i, bool compat);
bc917be8
AV
316int import_single_range(int type, void __user *buf, size_t len,
317 struct iovec *iov, struct iov_iter *i);
318
812ed032 319#endif