blob: b74d292b9960e661d64302ffb0784c656a90c5ef [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001/* SPDX-License-Identifier: GPL-2.0-or-later */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Berkeley style UIO structures - Alan Cox 1994.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004 */
5#ifndef __LINUX_UIO_H
6#define __LINUX_UIO_H
7
8#include <linux/kernel.h>
9#include <linux/thread_info.h>
David Brazdil0f672f62019-12-10 10:32:29 +000010#include <crypto/hash.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000011#include <uapi/linux/uio.h>
12
13struct page;
14struct pipe_inode_info;
15
16struct kvec {
17 void *iov_base; /* and that should *never* hold a userland pointer */
18 size_t iov_len;
19};
20
David Brazdil0f672f62019-12-10 10:32:29 +000021enum iter_type {
22 /* iter types */
23 ITER_IOVEC = 4,
24 ITER_KVEC = 8,
25 ITER_BVEC = 16,
26 ITER_PIPE = 32,
27 ITER_DISCARD = 64,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000028};
29
30struct iov_iter {
David Brazdil0f672f62019-12-10 10:32:29 +000031 /*
32 * Bit 0 is the read/write bit, set if we're writing.
33 * Bit 1 is the BVEC_FLAG_NO_REF bit, set if type is a bvec and
34 * the caller isn't expecting to drop a page reference when done.
35 */
36 unsigned int type;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000037 size_t iov_offset;
38 size_t count;
39 union {
40 const struct iovec *iov;
41 const struct kvec *kvec;
42 const struct bio_vec *bvec;
43 struct pipe_inode_info *pipe;
44 };
45 union {
46 unsigned long nr_segs;
47 struct {
48 int idx;
49 int start_idx;
50 };
51 };
52};
53
David Brazdil0f672f62019-12-10 10:32:29 +000054static inline enum iter_type iov_iter_type(const struct iov_iter *i)
55{
56 return i->type & ~(READ | WRITE);
57}
58
59static inline bool iter_is_iovec(const struct iov_iter *i)
60{
61 return iov_iter_type(i) == ITER_IOVEC;
62}
63
64static inline bool iov_iter_is_kvec(const struct iov_iter *i)
65{
66 return iov_iter_type(i) == ITER_KVEC;
67}
68
69static inline bool iov_iter_is_bvec(const struct iov_iter *i)
70{
71 return iov_iter_type(i) == ITER_BVEC;
72}
73
74static inline bool iov_iter_is_pipe(const struct iov_iter *i)
75{
76 return iov_iter_type(i) == ITER_PIPE;
77}
78
79static inline bool iov_iter_is_discard(const struct iov_iter *i)
80{
81 return iov_iter_type(i) == ITER_DISCARD;
82}
83
84static inline unsigned char iov_iter_rw(const struct iov_iter *i)
85{
86 return i->type & (READ | WRITE);
87}
88
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000089/*
90 * Total number of bytes covered by an iovec.
91 *
92 * NOTE that it is not safe to use this function until all the iovec's
93 * segment lengths have been validated. Because the individual lengths can
94 * overflow a size_t when added together.
95 */
96static inline size_t iov_length(const struct iovec *iov, unsigned long nr_segs)
97{
98 unsigned long seg;
99 size_t ret = 0;
100
101 for (seg = 0; seg < nr_segs; seg++)
102 ret += iov[seg].iov_len;
103 return ret;
104}
105
106static inline struct iovec iov_iter_iovec(const struct iov_iter *iter)
107{
108 return (struct iovec) {
109 .iov_base = iter->iov->iov_base + iter->iov_offset,
110 .iov_len = min(iter->count,
111 iter->iov->iov_len - iter->iov_offset),
112 };
113}
114
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000115size_t iov_iter_copy_from_user_atomic(struct page *page,
116 struct iov_iter *i, unsigned long offset, size_t bytes);
117void iov_iter_advance(struct iov_iter *i, size_t bytes);
118void iov_iter_revert(struct iov_iter *i, size_t bytes);
119int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes);
120size_t iov_iter_single_seg_count(const struct iov_iter *i);
121size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
122 struct iov_iter *i);
123size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
124 struct iov_iter *i);
125
126size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i);
127size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i);
128bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i);
129size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i);
130bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i);
131
132static __always_inline __must_check
133size_t copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
134{
135 if (unlikely(!check_copy_size(addr, bytes, true)))
136 return 0;
137 else
138 return _copy_to_iter(addr, bytes, i);
139}
140
141static __always_inline __must_check
142size_t copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
143{
144 if (unlikely(!check_copy_size(addr, bytes, false)))
145 return 0;
146 else
147 return _copy_from_iter(addr, bytes, i);
148}
149
150static __always_inline __must_check
151bool copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i)
152{
153 if (unlikely(!check_copy_size(addr, bytes, false)))
154 return false;
155 else
156 return _copy_from_iter_full(addr, bytes, i);
157}
158
159static __always_inline __must_check
160size_t copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
161{
162 if (unlikely(!check_copy_size(addr, bytes, false)))
163 return 0;
164 else
165 return _copy_from_iter_nocache(addr, bytes, i);
166}
167
168static __always_inline __must_check
169bool copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i)
170{
171 if (unlikely(!check_copy_size(addr, bytes, false)))
172 return false;
173 else
174 return _copy_from_iter_full_nocache(addr, bytes, i);
175}
176
177#ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
178/*
179 * Note, users like pmem that depend on the stricter semantics of
180 * copy_from_iter_flushcache() than copy_from_iter_nocache() must check for
181 * IS_ENABLED(CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE) before assuming that the
182 * destination is flushed from the cache on return.
183 */
184size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i);
185#else
186#define _copy_from_iter_flushcache _copy_from_iter_nocache
187#endif
188
189#ifdef CONFIG_ARCH_HAS_UACCESS_MCSAFE
190size_t _copy_to_iter_mcsafe(const void *addr, size_t bytes, struct iov_iter *i);
191#else
192#define _copy_to_iter_mcsafe _copy_to_iter
193#endif
194
195static __always_inline __must_check
196size_t copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
197{
198 if (unlikely(!check_copy_size(addr, bytes, false)))
199 return 0;
200 else
201 return _copy_from_iter_flushcache(addr, bytes, i);
202}
203
204static __always_inline __must_check
205size_t copy_to_iter_mcsafe(void *addr, size_t bytes, struct iov_iter *i)
206{
207 if (unlikely(!check_copy_size(addr, bytes, true)))
208 return 0;
209 else
210 return _copy_to_iter_mcsafe(addr, bytes, i);
211}
212
213size_t iov_iter_zero(size_t bytes, struct iov_iter *);
214unsigned long iov_iter_alignment(const struct iov_iter *i);
215unsigned long iov_iter_gap_alignment(const struct iov_iter *i);
David Brazdil0f672f62019-12-10 10:32:29 +0000216void iov_iter_init(struct iov_iter *i, unsigned int direction, const struct iovec *iov,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000217 unsigned long nr_segs, size_t count);
David Brazdil0f672f62019-12-10 10:32:29 +0000218void iov_iter_kvec(struct iov_iter *i, unsigned int direction, const struct kvec *kvec,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000219 unsigned long nr_segs, size_t count);
David Brazdil0f672f62019-12-10 10:32:29 +0000220void iov_iter_bvec(struct iov_iter *i, unsigned int direction, const struct bio_vec *bvec,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000221 unsigned long nr_segs, size_t count);
David Brazdil0f672f62019-12-10 10:32:29 +0000222void iov_iter_pipe(struct iov_iter *i, unsigned int direction, struct pipe_inode_info *pipe,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000223 size_t count);
David Brazdil0f672f62019-12-10 10:32:29 +0000224void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000225ssize_t iov_iter_get_pages(struct iov_iter *i, struct page **pages,
226 size_t maxsize, unsigned maxpages, size_t *start);
227ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, struct page ***pages,
228 size_t maxsize, size_t *start);
229int iov_iter_npages(const struct iov_iter *i, int maxpages);
230
231const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags);
232
233static inline size_t iov_iter_count(const struct iov_iter *i)
234{
235 return i->count;
236}
237
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000238/*
239 * Cap the iov_iter by given limit; note that the second argument is
240 * *not* the new size - it's upper limit for such. Passing it a value
241 * greater than the amount of data in iov_iter is fine - it'll just do
242 * nothing in that case.
243 */
244static inline void iov_iter_truncate(struct iov_iter *i, u64 count)
245{
246 /*
247 * count doesn't have to fit in size_t - comparison extends both
248 * operands to u64 here and any value that would be truncated by
249 * conversion in assignement is by definition greater than all
250 * values of size_t, including old i->count.
251 */
252 if (i->count > count)
253 i->count = count;
254}
255
256/*
257 * reexpand a previously truncated iterator; count must be no more than how much
258 * we had shrunk it.
259 */
260static inline void iov_iter_reexpand(struct iov_iter *i, size_t count)
261{
262 i->count = count;
263}
Olivier Deprez0e641232021-09-23 10:07:05 +0200264
265struct csum_state {
266 __wsum csum;
267 size_t off;
268};
269
270size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *csstate, struct iov_iter *i);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000271size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum, struct iov_iter *i);
272bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum, struct iov_iter *i);
David Brazdil0f672f62019-12-10 10:32:29 +0000273size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
274 struct iov_iter *i);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000275
David Brazdil0f672f62019-12-10 10:32:29 +0000276ssize_t import_iovec(int type, const struct iovec __user * uvector,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000277 unsigned nr_segs, unsigned fast_segs,
278 struct iovec **iov, struct iov_iter *i);
279
280#ifdef CONFIG_COMPAT
281struct compat_iovec;
David Brazdil0f672f62019-12-10 10:32:29 +0000282ssize_t compat_import_iovec(int type, const struct compat_iovec __user * uvector,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000283 unsigned nr_segs, unsigned fast_segs,
284 struct iovec **iov, struct iov_iter *i);
285#endif
286
287int import_single_range(int type, void __user *buf, size_t len,
288 struct iovec *iov, struct iov_iter *i);
289
290int iov_iter_for_each_range(struct iov_iter *i, size_t bytes,
291 int (*f)(struct kvec *vec, void *context),
292 void *context);
293
294#endif