blob: 2c90a0842ee86d68bb64e6a930bc56c245a487b9 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Berkeley style UIO structures - Alan Cox 1994.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
David Howells607ca462012-10-13 10:46:48 +01005#ifndef __LINUX_UIO_H
6#define __LINUX_UIO_H
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
Kent Overstreet92236872013-11-27 16:29:46 -08008#include <linux/kernel.h>
Al Viroaa28de22017-06-29 21:45:10 -04009#include <linux/thread_info.h>
Sagi Grimbergd05f4432018-12-03 17:52:09 -080010#include <crypto/hash.h>
David Howells607ca462012-10-13 10:46:48 +010011#include <uapi/linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
Kent Overstreet92236872013-11-27 16:29:46 -080013struct page;
Al Viro241699c2016-09-22 16:33:12 -040014struct pipe_inode_info;
Jiri Slaby812ed032009-07-29 15:04:19 -070015
16struct kvec {
17 void *iov_base; /* and that should *never* hold a userland pointer */
18 size_t iov_len;
19};
20
David Howells00e23702018-10-22 13:07:28 +010021enum iter_type {
Jens Axboe875f1d02019-02-27 13:05:25 -070022 /* set if ITER_BVEC doesn't hold a bv_page ref */
23 ITER_BVEC_FLAG_NO_REF = 2,
24
25 /* iter types */
26 ITER_IOVEC = 4,
27 ITER_KVEC = 8,
28 ITER_BVEC = 16,
29 ITER_PIPE = 32,
30 ITER_DISCARD = 64,
Al Viro62a80672014-04-04 23:12:29 -040031};
32
Kent Overstreet92236872013-11-27 16:29:46 -080033struct iov_iter {
Jens Axboe875f1d02019-02-27 13:05:25 -070034 /*
35 * Bit 0 is the read/write bit, set if we're writing.
36 * Bit 1 is the BVEC_FLAG_NO_REF bit, set if type is a bvec and
37 * the caller isn't expecting to drop a page reference when done.
38 */
David Howellsaa563d72018-10-20 00:57:56 +010039 unsigned int type;
Kent Overstreet92236872013-11-27 16:29:46 -080040 size_t iov_offset;
41 size_t count;
Al Viro62a80672014-04-04 23:12:29 -040042 union {
43 const struct iovec *iov;
Al Viroa2804552014-11-27 14:48:42 -050044 const struct kvec *kvec;
Al Viro62a80672014-04-04 23:12:29 -040045 const struct bio_vec *bvec;
Al Viro241699c2016-09-22 16:33:12 -040046 struct pipe_inode_info *pipe;
Al Viro62a80672014-04-04 23:12:29 -040047 };
Al Viro241699c2016-09-22 16:33:12 -040048 union {
49 unsigned long nr_segs;
Al Viro27c0e372017-02-17 18:42:24 -050050 struct {
51 int idx;
52 int start_idx;
53 };
Al Viro241699c2016-09-22 16:33:12 -040054 };
Kent Overstreet92236872013-11-27 16:29:46 -080055};
56
David Howells00e23702018-10-22 13:07:28 +010057static inline enum iter_type iov_iter_type(const struct iov_iter *i)
58{
Ming Leif5eb4d32019-04-26 18:45:21 +080059 return i->type & ~(READ | WRITE | ITER_BVEC_FLAG_NO_REF);
David Howells00e23702018-10-22 13:07:28 +010060}
61
62static inline bool iter_is_iovec(const struct iov_iter *i)
63{
64 return iov_iter_type(i) == ITER_IOVEC;
65}
66
67static inline bool iov_iter_is_kvec(const struct iov_iter *i)
68{
69 return iov_iter_type(i) == ITER_KVEC;
70}
71
72static inline bool iov_iter_is_bvec(const struct iov_iter *i)
73{
74 return iov_iter_type(i) == ITER_BVEC;
75}
76
77static inline bool iov_iter_is_pipe(const struct iov_iter *i)
78{
79 return iov_iter_type(i) == ITER_PIPE;
80}
81
David Howells9ea9ce02018-10-20 00:57:56 +010082static inline bool iov_iter_is_discard(const struct iov_iter *i)
83{
84 return iov_iter_type(i) == ITER_DISCARD;
85}
86
David Howells00e23702018-10-22 13:07:28 +010087static inline unsigned char iov_iter_rw(const struct iov_iter *i)
88{
89 return i->type & (READ | WRITE);
90}
91
Jens Axboe875f1d02019-02-27 13:05:25 -070092static inline bool iov_iter_bvec_no_ref(const struct iov_iter *i)
93{
94 return (i->type & ITER_BVEC_FLAG_NO_REF) != 0;
95}
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/*
98 * Total number of bytes covered by an iovec.
99 *
100 * NOTE that it is not safe to use this function until all the iovec's
101 * segment lengths have been validated. Because the individual lengths can
102 * overflow a size_t when added together.
103 */
104static inline size_t iov_length(const struct iovec *iov, unsigned long nr_segs)
105{
106 unsigned long seg;
107 size_t ret = 0;
108
109 for (seg = 0; seg < nr_segs; seg++)
110 ret += iov[seg].iov_len;
111 return ret;
112}
113
Kent Overstreet92236872013-11-27 16:29:46 -0800114static inline struct iovec iov_iter_iovec(const struct iov_iter *iter)
115{
116 return (struct iovec) {
117 .iov_base = iter->iov->iov_base + iter->iov_offset,
118 .iov_len = min(iter->count,
119 iter->iov->iov_len - iter->iov_offset),
120 };
121}
122
Kent Overstreet92236872013-11-27 16:29:46 -0800123size_t iov_iter_copy_from_user_atomic(struct page *page,
124 struct iov_iter *i, unsigned long offset, size_t bytes);
Kent Overstreet92236872013-11-27 16:29:46 -0800125void iov_iter_advance(struct iov_iter *i, size_t bytes);
Al Viro27c0e372017-02-17 18:42:24 -0500126void iov_iter_revert(struct iov_iter *i, size_t bytes);
Kent Overstreet92236872013-11-27 16:29:46 -0800127int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes);
128size_t iov_iter_single_seg_count(const struct iov_iter *i);
Al Viro6e58e792014-02-03 17:07:03 -0500129size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
130 struct iov_iter *i);
Al Virof0d1bec2014-04-03 15:05:18 -0400131size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
132 struct iov_iter *i);
Al Viroaa28de22017-06-29 21:45:10 -0400133
134size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i);
135size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i);
136bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i);
137size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i);
138bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i);
139
140static __always_inline __must_check
141size_t copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
142{
143 if (unlikely(!check_copy_size(addr, bytes, true)))
Al Viroc43aeb12017-07-10 07:40:49 -0400144 return 0;
Al Viroaa28de22017-06-29 21:45:10 -0400145 else
146 return _copy_to_iter(addr, bytes, i);
147}
148
149static __always_inline __must_check
150size_t copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
151{
152 if (unlikely(!check_copy_size(addr, bytes, false)))
Al Viroc43aeb12017-07-10 07:40:49 -0400153 return 0;
Al Viroaa28de22017-06-29 21:45:10 -0400154 else
155 return _copy_from_iter(addr, bytes, i);
156}
157
158static __always_inline __must_check
159bool copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i)
160{
161 if (unlikely(!check_copy_size(addr, bytes, false)))
162 return false;
163 else
164 return _copy_from_iter_full(addr, bytes, i);
165}
166
167static __always_inline __must_check
168size_t copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
169{
170 if (unlikely(!check_copy_size(addr, bytes, false)))
Al Viroc43aeb12017-07-10 07:40:49 -0400171 return 0;
Al Viroaa28de22017-06-29 21:45:10 -0400172 else
173 return _copy_from_iter_nocache(addr, bytes, i);
174}
175
176static __always_inline __must_check
177bool copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i)
178{
179 if (unlikely(!check_copy_size(addr, bytes, false)))
180 return false;
181 else
182 return _copy_from_iter_full_nocache(addr, bytes, i);
183}
184
Dan Williams0aed55a2017-05-29 12:22:50 -0700185#ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
186/*
187 * Note, users like pmem that depend on the stricter semantics of
188 * copy_from_iter_flushcache() than copy_from_iter_nocache() must check for
189 * IS_ENABLED(CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE) before assuming that the
190 * destination is flushed from the cache on return.
191 */
Linus Torvalds6a37e942017-07-07 20:39:20 -0700192size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i);
Dan Williams0aed55a2017-05-29 12:22:50 -0700193#else
Linus Torvalds6a37e942017-07-07 20:39:20 -0700194#define _copy_from_iter_flushcache _copy_from_iter_nocache
Dan Williams0aed55a2017-05-29 12:22:50 -0700195#endif
Linus Torvalds6a37e942017-07-07 20:39:20 -0700196
Dan Williams87803562018-05-03 17:06:31 -0700197#ifdef CONFIG_ARCH_HAS_UACCESS_MCSAFE
Dan Williams522239b2018-05-22 23:17:03 -0700198size_t _copy_to_iter_mcsafe(const void *addr, size_t bytes, struct iov_iter *i);
Dan Williams87803562018-05-03 17:06:31 -0700199#else
200#define _copy_to_iter_mcsafe _copy_to_iter
201#endif
202
Linus Torvalds6a37e942017-07-07 20:39:20 -0700203static __always_inline __must_check
204size_t copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
205{
206 if (unlikely(!check_copy_size(addr, bytes, false)))
Al Viroc43aeb12017-07-10 07:40:49 -0400207 return 0;
Linus Torvalds6a37e942017-07-07 20:39:20 -0700208 else
209 return _copy_from_iter_flushcache(addr, bytes, i);
210}
211
Dan Williams87803562018-05-03 17:06:31 -0700212static __always_inline __must_check
213size_t copy_to_iter_mcsafe(void *addr, size_t bytes, struct iov_iter *i)
214{
Dave Jiangdfb06cb2018-09-05 13:31:40 -0700215 if (unlikely(!check_copy_size(addr, bytes, true)))
Dan Williams87803562018-05-03 17:06:31 -0700216 return 0;
217 else
218 return _copy_to_iter_mcsafe(addr, bytes, i);
219}
220
Matthew Wilcoxc35e02482014-08-01 09:27:22 -0400221size_t iov_iter_zero(size_t bytes, struct iov_iter *);
Al Viro886a3912014-03-05 13:50:45 -0500222unsigned long iov_iter_alignment(const struct iov_iter *i);
Al Viro357f4352016-04-08 19:05:19 -0400223unsigned long iov_iter_gap_alignment(const struct iov_iter *i);
David Howellsaa563d72018-10-20 00:57:56 +0100224void iov_iter_init(struct iov_iter *i, unsigned int direction, const struct iovec *iov,
Al Viro71d8e532014-03-05 19:28:09 -0500225 unsigned long nr_segs, size_t count);
David Howellsaa563d72018-10-20 00:57:56 +0100226void iov_iter_kvec(struct iov_iter *i, unsigned int direction, const struct kvec *kvec,
Al Viro05afcb72015-01-23 01:08:07 -0500227 unsigned long nr_segs, size_t count);
David Howellsaa563d72018-10-20 00:57:56 +0100228void iov_iter_bvec(struct iov_iter *i, unsigned int direction, const struct bio_vec *bvec,
Al Viroabb78f82014-11-24 14:46:11 -0500229 unsigned long nr_segs, size_t count);
David Howellsaa563d72018-10-20 00:57:56 +0100230void iov_iter_pipe(struct iov_iter *i, unsigned int direction, struct pipe_inode_info *pipe,
Al Viro241699c2016-09-22 16:33:12 -0400231 size_t count);
David Howells9ea9ce02018-10-20 00:57:56 +0100232void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count);
Al Viro7b2c99d2014-03-15 04:05:57 -0400233ssize_t iov_iter_get_pages(struct iov_iter *i, struct page **pages,
Miklos Szeredi2c809292014-09-24 17:09:11 +0200234 size_t maxsize, unsigned maxpages, size_t *start);
Al Viro91f79c42014-03-21 04:58:33 -0400235ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, struct page ***pages,
236 size_t maxsize, size_t *start);
Al Virof67da302014-03-19 01:16:16 -0400237int iov_iter_npages(const struct iov_iter *i, int maxpages);
Kent Overstreet92236872013-11-27 16:29:46 -0800238
Al Viro4b8164b2015-01-31 20:08:47 -0500239const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags);
240
Al Virob57332b2016-10-10 13:57:37 -0400241static inline size_t iov_iter_count(const struct iov_iter *i)
Kent Overstreet92236872013-11-27 16:29:46 -0800242{
243 return i->count;
244}
245
Omar Sandovalbd8e0ff2015-03-17 14:04:02 -0700246/*
Al Viro0b86dbf2014-06-23 08:44:40 +0100247 * Cap the iov_iter by given limit; note that the second argument is
248 * *not* the new size - it's upper limit for such. Passing it a value
249 * greater than the amount of data in iov_iter is fine - it'll just do
250 * nothing in that case.
251 */
252static inline void iov_iter_truncate(struct iov_iter *i, u64 count)
Al Viro0c949332014-03-22 06:51:37 -0400253{
Al Viro0b86dbf2014-06-23 08:44:40 +0100254 /*
255 * count doesn't have to fit in size_t - comparison extends both
256 * operands to u64 here and any value that would be truncated by
257 * conversion in assignement is by definition greater than all
258 * values of size_t, including old i->count.
259 */
Al Viro0c949332014-03-22 06:51:37 -0400260 if (i->count > count)
261 i->count = count;
262}
263
Al Virob42b15f2014-04-04 12:15:19 -0400264/*
265 * reexpand a previously truncated iterator; count must be no more than how much
266 * we had shrunk it.
267 */
268static inline void iov_iter_reexpand(struct iov_iter *i, size_t count)
269{
270 i->count = count;
271}
Sagi Grimbergcb002d02018-12-03 17:52:07 -0800272size_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 -0500273size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum, struct iov_iter *i);
Al Virocbbd26b2016-11-01 22:09:04 -0400274bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum, struct iov_iter *i);
Sagi Grimbergd05f4432018-12-03 17:52:09 -0800275size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp,
276 struct iov_iter *i);
Al Virob42b15f2014-04-04 12:15:19 -0400277
Al Virobc917be2015-03-21 17:45:43 -0400278int import_iovec(int type, const struct iovec __user * uvector,
279 unsigned nr_segs, unsigned fast_segs,
280 struct iovec **iov, struct iov_iter *i);
281
282#ifdef CONFIG_COMPAT
283struct compat_iovec;
284int compat_import_iovec(int type, const struct compat_iovec __user * uvector,
285 unsigned nr_segs, unsigned fast_segs,
286 struct iovec **iov, struct iov_iter *i);
287#endif
288
289int import_single_range(int type, void __user *buf, size_t len,
290 struct iovec *iov, struct iov_iter *i);
291
Al Viro09cf6982017-02-18 01:44:03 -0500292int iov_iter_for_each_range(struct iov_iter *i, size_t bytes,
293 int (*f)(struct kvec *vec, void *context),
294 void *context);
295
Jiri Slaby812ed032009-07-29 15:04:19 -0700296#endif