1 /* 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 */ 9 #ifndef __LINUX_UIO_H 10 #define __LINUX_UIO_H 11 12 #include <linux/kernel.h> 13 #include <linux/thread_info.h> 14 #include <crypto/hash.h> 15 #include <uapi/linux/uio.h> 16 17 struct page; 18 struct pipe_inode_info; 19 20 struct kvec { 21 void *iov_base; /* and that should *never* hold a userland pointer */ 22 size_t iov_len; 23 }; 24 25 enum iter_type { 26 /* 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, 35 }; 36 37 struct iov_iter { 38 /* 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 */ 43 unsigned int type; 44 size_t iov_offset; 45 size_t count; 46 union { 47 const struct iovec *iov; 48 const struct kvec *kvec; 49 const struct bio_vec *bvec; 50 struct pipe_inode_info *pipe; 51 }; 52 union { 53 unsigned long nr_segs; 54 struct { 55 int idx; 56 int start_idx; 57 }; 58 }; 59 }; 60 61 static inline enum iter_type iov_iter_type(const struct iov_iter *i) 62 { 63 return i->type & ~(READ | WRITE | ITER_BVEC_FLAG_NO_REF); 64 } 65 66 static inline bool iter_is_iovec(const struct iov_iter *i) 67 { 68 return iov_iter_type(i) == ITER_IOVEC; 69 } 70 71 static inline bool iov_iter_is_kvec(const struct iov_iter *i) 72 { 73 return iov_iter_type(i) == ITER_KVEC; 74 } 75 76 static inline bool iov_iter_is_bvec(const struct iov_iter *i) 77 { 78 return iov_iter_type(i) == ITER_BVEC; 79 } 80 81 static inline bool iov_iter_is_pipe(const struct iov_iter *i) 82 { 83 return iov_iter_type(i) == ITER_PIPE; 84 } 85 86 static inline bool iov_iter_is_discard(const struct iov_iter *i) 87 { 88 return iov_iter_type(i) == ITER_DISCARD; 89 } 90 91 static inline unsigned char iov_iter_rw(const struct iov_iter *i) 92 { 93 return i->type & (READ | WRITE); 94 } 95 96 static 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 101 /* 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 */ 108 static 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 118 static 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 127 size_t iov_iter_copy_from_user_atomic(struct page *page, 128 struct iov_iter *i, unsigned long offset, size_t bytes); 129 void iov_iter_advance(struct iov_iter *i, size_t bytes); 130 void iov_iter_revert(struct iov_iter *i, size_t bytes); 131 int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes); 132 size_t iov_iter_single_seg_count(const struct iov_iter *i); 133 size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes, 134 struct iov_iter *i); 135 size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes, 136 struct iov_iter *i); 137 138 size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i); 139 size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i); 140 bool _copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i); 141 size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i); 142 bool _copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i); 143 144 static __always_inline __must_check 145 size_t copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i) 146 { 147 if (unlikely(!check_copy_size(addr, bytes, true))) 148 return 0; 149 else 150 return _copy_to_iter(addr, bytes, i); 151 } 152 153 static __always_inline __must_check 154 size_t copy_from_iter(void *addr, size_t bytes, struct iov_iter *i) 155 { 156 if (unlikely(!check_copy_size(addr, bytes, false))) 157 return 0; 158 else 159 return _copy_from_iter(addr, bytes, i); 160 } 161 162 static __always_inline __must_check 163 bool 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 171 static __always_inline __must_check 172 size_t copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i) 173 { 174 if (unlikely(!check_copy_size(addr, bytes, false))) 175 return 0; 176 else 177 return _copy_from_iter_nocache(addr, bytes, i); 178 } 179 180 static __always_inline __must_check 181 bool 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 189 #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 */ 196 size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i); 197 #else 198 #define _copy_from_iter_flushcache _copy_from_iter_nocache 199 #endif 200 201 #ifdef CONFIG_ARCH_HAS_UACCESS_MCSAFE 202 size_t _copy_to_iter_mcsafe(const void *addr, size_t bytes, struct iov_iter *i); 203 #else 204 #define _copy_to_iter_mcsafe _copy_to_iter 205 #endif 206 207 static __always_inline __must_check 208 size_t copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i) 209 { 210 if (unlikely(!check_copy_size(addr, bytes, false))) 211 return 0; 212 else 213 return _copy_from_iter_flushcache(addr, bytes, i); 214 } 215 216 static __always_inline __must_check 217 size_t copy_to_iter_mcsafe(void *addr, size_t bytes, struct iov_iter *i) 218 { 219 if (unlikely(!check_copy_size(addr, bytes, true))) 220 return 0; 221 else 222 return _copy_to_iter_mcsafe(addr, bytes, i); 223 } 224 225 size_t iov_iter_zero(size_t bytes, struct iov_iter *); 226 unsigned long iov_iter_alignment(const struct iov_iter *i); 227 unsigned long iov_iter_gap_alignment(const struct iov_iter *i); 228 void iov_iter_init(struct iov_iter *i, unsigned int direction, const struct iovec *iov, 229 unsigned long nr_segs, size_t count); 230 void iov_iter_kvec(struct iov_iter *i, unsigned int direction, const struct kvec *kvec, 231 unsigned long nr_segs, size_t count); 232 void iov_iter_bvec(struct iov_iter *i, unsigned int direction, const struct bio_vec *bvec, 233 unsigned long nr_segs, size_t count); 234 void iov_iter_pipe(struct iov_iter *i, unsigned int direction, struct pipe_inode_info *pipe, 235 size_t count); 236 void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count); 237 ssize_t iov_iter_get_pages(struct iov_iter *i, struct page **pages, 238 size_t maxsize, unsigned maxpages, size_t *start); 239 ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, struct page ***pages, 240 size_t maxsize, size_t *start); 241 int iov_iter_npages(const struct iov_iter *i, int maxpages); 242 243 const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags); 244 245 static inline size_t iov_iter_count(const struct iov_iter *i) 246 { 247 return i->count; 248 } 249 250 /* 251 * 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 */ 256 static inline void iov_iter_truncate(struct iov_iter *i, u64 count) 257 { 258 /* 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 */ 264 if (i->count > count) 265 i->count = count; 266 } 267 268 /* 269 * reexpand a previously truncated iterator; count must be no more than how much 270 * we had shrunk it. 271 */ 272 static inline void iov_iter_reexpand(struct iov_iter *i, size_t count) 273 { 274 i->count = count; 275 } 276 size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *csump, struct iov_iter *i); 277 size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum, struct iov_iter *i); 278 bool csum_and_copy_from_iter_full(void *addr, size_t bytes, __wsum *csum, struct iov_iter *i); 279 size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp, 280 struct iov_iter *i); 281 282 int import_iovec(int type, const struct iovec __user * uvector, 283 unsigned nr_segs, unsigned fast_segs, 284 struct iovec **iov, struct iov_iter *i); 285 286 #ifdef CONFIG_COMPAT 287 struct compat_iovec; 288 int compat_import_iovec(int type, const struct compat_iovec __user * uvector, 289 unsigned nr_segs, unsigned fast_segs, 290 struct iovec **iov, struct iov_iter *i); 291 #endif 292 293 int import_single_range(int type, void __user *buf, size_t len, 294 struct iovec *iov, struct iov_iter *i); 295 296 int iov_iter_for_each_range(struct iov_iter *i, size_t bytes, 297 int (*f)(struct kvec *vec, void *context), 298 void *context); 299 300 #endif 301