blob: 3d8f1acc142c2a3b4a700b0b84f20af9965588d9 [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>
David Howells607ca462012-10-13 10:46:48 +010014#include <uapi/linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Kent Overstreet92236872013-11-27 16:29:46 -080016struct page;
Al Viro241699c2016-09-22 16:33:12 -040017struct pipe_inode_info;
Jiri Slaby812ed032009-07-29 15:04:19 -070018
19struct kvec {
20 void *iov_base; /* and that should *never* hold a userland pointer */
21 size_t iov_len;
22};
23
David Howells00e23702018-10-22 13:07:28 +010024enum iter_type {
Al Viro62a80672014-04-04 23:12:29 -040025 ITER_IOVEC = 0,
26 ITER_KVEC = 2,
27 ITER_BVEC = 4,
Al Viro241699c2016-09-22 16:33:12 -040028 ITER_PIPE = 8,
Al Viro62a80672014-04-04 23:12:29 -040029};
30
Kent Overstreet92236872013-11-27 16:29:46 -080031struct iov_iter {
David Howellsaa563d72018-10-20 00:57:56 +010032 unsigned int type;
Kent Overstreet92236872013-11-27 16:29:46 -080033 size_t iov_offset;
34 size_t count;
Al Viro62a80672014-04-04 23:12:29 -040035 union {
36 const struct iovec *iov;
Al Viroa2804552014-11-27 14:48:42 -050037 const struct kvec *kvec;
Al Viro62a80672014-04-04 23:12:29 -040038 const struct bio_vec *bvec;
Al Viro241699c2016-09-22 16:33:12 -040039 struct pipe_inode_info *pipe;
Al Viro62a80672014-04-04 23:12:29 -040040 };
Al Viro241699c2016-09-22 16:33:12 -040041 union {
42 unsigned long nr_segs;
Al Viro27c0e372017-02-17 18:42:24 -050043 struct {
44 int idx;
45 int start_idx;
46 };
Al Viro241699c2016-09-22 16:33:12 -040047 };
Kent Overstreet92236872013-11-27 16:29:46 -080048};
49
David Howells00e23702018-10-22 13:07:28 +010050static inline enum iter_type iov_iter_type(const struct iov_iter *i)
51{
52 return i->type & ~(READ | WRITE);
53}
54
55static inline bool iter_is_iovec(const struct iov_iter *i)
56{
57 return iov_iter_type(i) == ITER_IOVEC;
58}
59
60static inline bool iov_iter_is_kvec(const struct iov_iter *i)
61{
62 return iov_iter_type(i) == ITER_KVEC;
63}
64
65static inline bool iov_iter_is_bvec(const struct iov_iter *i)
66{
67 return iov_iter_type(i) == ITER_BVEC;
68}
69
70static inline bool iov_iter_is_pipe(const struct iov_iter *i)
71{
72 return iov_iter_type(i) == ITER_PIPE;
73}
74
75static inline unsigned char iov_iter_rw(const struct iov_iter *i)
76{
77 return i->type & (READ | WRITE);
78}
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/*
81 * Total number of bytes covered by an iovec.
82 *
83 * NOTE that it is not safe to use this function until all the iovec's
84 * segment lengths have been validated. Because the individual lengths can
85 * overflow a size_t when added together.
86 */
87static inline size_t iov_length(const struct iovec *iov, unsigned long nr_segs)
88{
89 unsigned long seg;
90 size_t ret = 0;
91
92 for (seg = 0; seg < nr_segs; seg++)
93 ret += iov[seg].iov_len;
94 return ret;
95}
96
Kent Overstreet92236872013-11-27 16:29:46 -080097static inline struct iovec iov_iter_iovec(const struct iov_iter *iter)
98{
99 return (struct iovec) {
100 .iov_base = iter->iov->iov_base + iter->iov_offset,
101 .iov_len = min(iter->count,
102 iter->iov->iov_len - iter->iov_offset),
103 };
104}
105
106#define iov_for_each(iov, iter, start) \
David Howells00e23702018-10-22 13:07:28 +0100107 if (iov_iter_type(start) == ITER_IOVEC || \
108 iov_iter_type(start) == ITER_KVEC) \
Kent Overstreet92236872013-11-27 16:29:46 -0800109 for (iter = (start); \
110 (iter).count && \
111 ((iov = iov_iter_iovec(&(iter))), 1); \
112 iov_iter_advance(&(iter), (iov).iov_len))
113
Kent Overstreet92236872013-11-27 16:29:46 -0800114size_t iov_iter_copy_from_user_atomic(struct page *page,
115 struct iov_iter *i, unsigned long offset, size_t bytes);
Kent Overstreet92236872013-11-27 16:29:46 -0800116void iov_iter_advance(struct iov_iter *i, size_t bytes);
Al Viro27c0e372017-02-17 18:42:24 -0500117void iov_iter_revert(struct iov_iter *i, size_t bytes);
Kent Overstreet92236872013-11-27 16:29:46 -0800118int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes);
119size_t iov_iter_single_seg_count(const struct iov_iter *i);
Al Viro6e58e792014-02-03 17:07:03 -0500120size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
121 struct iov_iter *i);
Al Virof0d1bec2014-04-03 15:05:18 -0400122size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
123 struct iov_iter *i);
Al Viroaa28de22017-06-29 21:45:10 -0400124
125size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i);
126size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i);
127bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i);
128size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i);
129bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i);
130
131static __always_inline __must_check
132size_t copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
133{
134 if (unlikely(!check_copy_size(addr, bytes, true)))
Al Viroc43aeb12017-07-10 07:40:49 -0400135 return 0;
Al Viroaa28de22017-06-29 21:45:10 -0400136 else
137 return _copy_to_iter(addr, bytes, i);
138}
139
140static __always_inline __must_check
141size_t copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
142{
143 if (unlikely(!check_copy_size(addr, bytes, false)))
Al Viroc43aeb12017-07-10 07:40:49 -0400144 return 0;
Al Viroaa28de22017-06-29 21:45:10 -0400145 else
146 return _copy_from_iter(addr, bytes, i);
147}
148
149static __always_inline __must_check
150bool copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i)
151{
152 if (unlikely(!check_copy_size(addr, bytes, false)))
153 return false;
154 else
155 return _copy_from_iter_full(addr, bytes, i);
156}
157
158static __always_inline __must_check
159size_t copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i)
160{
161 if (unlikely(!check_copy_size(addr, bytes, false)))
Al Viroc43aeb12017-07-10 07:40:49 -0400162 return 0;
Al Viroaa28de22017-06-29 21:45:10 -0400163 else
164 return _copy_from_iter_nocache(addr, bytes, i);
165}
166
167static __always_inline __must_check
168bool copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i)
169{
170 if (unlikely(!check_copy_size(addr, bytes, false)))
171 return false;
172 else
173 return _copy_from_iter_full_nocache(addr, bytes, i);
174}
175
Dan Williams0aed55a2017-05-29 12:22:50 -0700176#ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
177/*
178 * Note, users like pmem that depend on the stricter semantics of
179 * copy_from_iter_flushcache() than copy_from_iter_nocache() must check for
180 * IS_ENABLED(CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE) before assuming that the
181 * destination is flushed from the cache on return.
182 */
Linus Torvalds6a37e942017-07-07 20:39:20 -0700183size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i);
Dan Williams0aed55a2017-05-29 12:22:50 -0700184#else
Linus Torvalds6a37e942017-07-07 20:39:20 -0700185#define _copy_from_iter_flushcache _copy_from_iter_nocache
Dan Williams0aed55a2017-05-29 12:22:50 -0700186#endif
Linus Torvalds6a37e942017-07-07 20:39:20 -0700187
Dan Williams87803562018-05-03 17:06:31 -0700188#ifdef CONFIG_ARCH_HAS_UACCESS_MCSAFE
Dan Williams522239b2018-05-22 23:17:03 -0700189size_t _copy_to_iter_mcsafe(const void *addr, size_t bytes, struct iov_iter *i);
Dan Williams87803562018-05-03 17:06:31 -0700190#else
191#define _copy_to_iter_mcsafe _copy_to_iter
192#endif
193
Linus Torvalds6a37e942017-07-07 20:39:20 -0700194static __always_inline __must_check
195size_t copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i)
196{
197 if (unlikely(!check_copy_size(addr, bytes, false)))
Al Viroc43aeb12017-07-10 07:40:49 -0400198 return 0;
Linus Torvalds6a37e942017-07-07 20:39:20 -0700199 else
200 return _copy_from_iter_flushcache(addr, bytes, i);
201}
202
Dan Williams87803562018-05-03 17:06:31 -0700203static __always_inline __must_check
204size_t copy_to_iter_mcsafe(void *addr, size_t bytes, struct iov_iter *i)
205{
Dave Jiangdfb06cb2018-09-05 13:31:40 -0700206 if (unlikely(!check_copy_size(addr, bytes, true)))
Dan Williams87803562018-05-03 17:06:31 -0700207 return 0;
208 else
209 return _copy_to_iter_mcsafe(addr, bytes, i);
210}
211
Matthew Wilcoxc35e02482014-08-01 09:27:22 -0400212size_t iov_iter_zero(size_t bytes, struct iov_iter *);
Al Viro886a3912014-03-05 13:50:45 -0500213unsigned long iov_iter_alignment(const struct iov_iter *i);
Al Viro357f4352016-04-08 19:05:19 -0400214unsigned long iov_iter_gap_alignment(const struct iov_iter *i);
David Howellsaa563d72018-10-20 00:57:56 +0100215void iov_iter_init(struct iov_iter *i, unsigned int direction, const struct iovec *iov,
Al Viro71d8e532014-03-05 19:28:09 -0500216 unsigned long nr_segs, size_t count);
David Howellsaa563d72018-10-20 00:57:56 +0100217void iov_iter_kvec(struct iov_iter *i, unsigned int direction, const struct kvec *kvec,
Al Viro05afcb72015-01-23 01:08:07 -0500218 unsigned long nr_segs, size_t count);
David Howellsaa563d72018-10-20 00:57:56 +0100219void iov_iter_bvec(struct iov_iter *i, unsigned int direction, const struct bio_vec *bvec,
Al Viroabb78f82014-11-24 14:46:11 -0500220 unsigned long nr_segs, size_t count);
David Howellsaa563d72018-10-20 00:57:56 +0100221void iov_iter_pipe(struct iov_iter *i, unsigned int direction, struct pipe_inode_info *pipe,
Al Viro241699c2016-09-22 16:33:12 -0400222 size_t count);
Al Viro7b2c99d2014-03-15 04:05:57 -0400223ssize_t iov_iter_get_pages(struct iov_iter *i, struct page **pages,
Miklos Szeredi2c809292014-09-24 17:09:11 +0200224 size_t maxsize, unsigned maxpages, size_t *start);
Al Viro91f79c42014-03-21 04:58:33 -0400225ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, struct page ***pages,
226 size_t maxsize, size_t *start);
Al Virof67da302014-03-19 01:16:16 -0400227int iov_iter_npages(const struct iov_iter *i, int maxpages);
Kent Overstreet92236872013-11-27 16:29:46 -0800228
Al Viro4b8164b2015-01-31 20:08:47 -0500229const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags);
230
Al Virob57332b2016-10-10 13:57:37 -0400231static inline size_t iov_iter_count(const struct iov_iter *i)
Kent Overstreet92236872013-11-27 16:29:46 -0800232{
233 return i->count;
234}
235
Omar Sandovalbd8e0ff2015-03-17 14:04:02 -0700236/*
Al Viro0b86dbf2014-06-23 08:44:40 +0100237 * Cap the iov_iter by given limit; note that the second argument is
238 * *not* the new size - it's upper limit for such. Passing it a value
239 * greater than the amount of data in iov_iter is fine - it'll just do
240 * nothing in that case.
241 */
242static inline void iov_iter_truncate(struct iov_iter *i, u64 count)
Al Viro0c949332014-03-22 06:51:37 -0400243{
Al Viro0b86dbf2014-06-23 08:44:40 +0100244 /*
245 * count doesn't have to fit in size_t - comparison extends both
246 * operands to u64 here and any value that would be truncated by
247 * conversion in assignement is by definition greater than all
248 * values of size_t, including old i->count.
249 */
Al Viro0c949332014-03-22 06:51:37 -0400250 if (i->count > count)
251 i->count = count;
252}
253
Al Virob42b15f2014-04-04 12:15:19 -0400254/*
255 * reexpand a previously truncated iterator; count must be no more than how much
256 * we had shrunk it.
257 */
258static inline void iov_iter_reexpand(struct iov_iter *i, size_t count)
259{
260 i->count = count;
261}
Al Viro36f7a8a2015-12-06 16:49:22 -0500262size_t csum_and_copy_to_iter(const void *addr, size_t bytes, __wsum *csum, struct iov_iter *i);
Al Viroa604ec72014-11-24 01:08:00 -0500263size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum, struct iov_iter *i);
Al Virocbbd26b2016-11-01 22:09:04 -0400264bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum, struct iov_iter *i);
Al Virob42b15f2014-04-04 12:15:19 -0400265
Al Virobc917be2015-03-21 17:45:43 -0400266int import_iovec(int type, const struct iovec __user * uvector,
267 unsigned nr_segs, unsigned fast_segs,
268 struct iovec **iov, struct iov_iter *i);
269
270#ifdef CONFIG_COMPAT
271struct compat_iovec;
272int compat_import_iovec(int type, const struct compat_iovec __user * uvector,
273 unsigned nr_segs, unsigned fast_segs,
274 struct iovec **iov, struct iov_iter *i);
275#endif
276
277int import_single_range(int type, void __user *buf, size_t len,
278 struct iovec *iov, struct iov_iter *i);
279
Al Viro09cf6982017-02-18 01:44:03 -0500280int iov_iter_for_each_range(struct iov_iter *i, size_t bytes,
281 int (*f)(struct kvec *vec, void *context),
282 void *context);
283
Jiri Slaby812ed032009-07-29 15:04:19 -0700284#endif