1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * Berkeley style UIO structures - Alan Cox 1994. 4 */ 5 #ifndef __LINUX_UIO_H 6 #define __LINUX_UIO_H 7 8 #include <linux/kernel.h> 9 #include <linux/thread_info.h> 10 #include <linux/mm_types.h> 11 #include <uapi/linux/uio.h> 12 13 struct page; 14 struct pipe_inode_info; 15 16 typedef unsigned int __bitwise iov_iter_extraction_t; 17 18 struct kvec { 19 void *iov_base; /* and that should *never* hold a userland pointer */ 20 size_t iov_len; 21 }; 22 23 enum iter_type { 24 /* iter types */ 25 ITER_IOVEC, 26 ITER_KVEC, 27 ITER_BVEC, 28 ITER_PIPE, 29 ITER_XARRAY, 30 ITER_DISCARD, 31 ITER_UBUF, 32 }; 33 34 #define ITER_SOURCE 1 // == WRITE 35 #define ITER_DEST 0 // == READ 36 37 struct iov_iter_state { 38 size_t iov_offset; 39 size_t count; 40 unsigned long nr_segs; 41 }; 42 43 struct iov_iter { 44 u8 iter_type; 45 bool nofault; 46 bool data_source; 47 bool user_backed; 48 union { 49 size_t iov_offset; 50 int last_offset; 51 }; 52 size_t count; 53 union { 54 /* use iter_iov() to get the current vec */ 55 const struct iovec *__iov; 56 const struct kvec *kvec; 57 const struct bio_vec *bvec; 58 struct xarray *xarray; 59 struct pipe_inode_info *pipe; 60 void __user *ubuf; 61 }; 62 union { 63 unsigned long nr_segs; 64 struct { 65 unsigned int head; 66 unsigned int start_head; 67 }; 68 loff_t xarray_start; 69 }; 70 }; 71 72 #define iter_iov(iter) (iter)->__iov 73 74 static inline enum iter_type iov_iter_type(const struct iov_iter *i) 75 { 76 return i->iter_type; 77 } 78 79 static inline void iov_iter_save_state(struct iov_iter *iter, 80 struct iov_iter_state *state) 81 { 82 state->iov_offset = iter->iov_offset; 83 state->count = iter->count; 84 state->nr_segs = iter->nr_segs; 85 } 86 87 static inline bool iter_is_ubuf(const struct iov_iter *i) 88 { 89 return iov_iter_type(i) == ITER_UBUF; 90 } 91 92 static inline bool iter_is_iovec(const struct iov_iter *i) 93 { 94 return iov_iter_type(i) == ITER_IOVEC; 95 } 96 97 static inline bool iov_iter_is_kvec(const struct iov_iter *i) 98 { 99 return iov_iter_type(i) == ITER_KVEC; 100 } 101 102 static inline bool iov_iter_is_bvec(const struct iov_iter *i) 103 { 104 return iov_iter_type(i) == ITER_BVEC; 105 } 106 107 static inline bool iov_iter_is_pipe(const struct iov_iter *i) 108 { 109 return iov_iter_type(i) == ITER_PIPE; 110 } 111 112 static inline bool iov_iter_is_discard(const struct iov_iter *i) 113 { 114 return iov_iter_type(i) == ITER_DISCARD; 115 } 116 117 static inline bool iov_iter_is_xarray(const struct iov_iter *i) 118 { 119 return iov_iter_type(i) == ITER_XARRAY; 120 } 121 122 static inline unsigned char iov_iter_rw(const struct iov_iter *i) 123 { 124 return i->data_source ? WRITE : READ; 125 } 126 127 static inline bool user_backed_iter(const struct iov_iter *i) 128 { 129 return i->user_backed; 130 } 131 132 /* 133 * Total number of bytes covered by an iovec. 134 * 135 * NOTE that it is not safe to use this function until all the iovec's 136 * segment lengths have been validated. Because the individual lengths can 137 * overflow a size_t when added together. 138 */ 139 static inline size_t iov_length(const struct iovec *iov, unsigned long nr_segs) 140 { 141 unsigned long seg; 142 size_t ret = 0; 143 144 for (seg = 0; seg < nr_segs; seg++) 145 ret += iov[seg].iov_len; 146 return ret; 147 } 148 149 static inline struct iovec iov_iter_iovec(const struct iov_iter *iter) 150 { 151 return (struct iovec) { 152 .iov_base = iter_iov(iter)->iov_base + iter->iov_offset, 153 .iov_len = min(iter->count, 154 iter_iov(iter)->iov_len - iter->iov_offset), 155 }; 156 } 157 158 size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, 159 size_t bytes, struct iov_iter *i); 160 void iov_iter_advance(struct iov_iter *i, size_t bytes); 161 void iov_iter_revert(struct iov_iter *i, size_t bytes); 162 size_t fault_in_iov_iter_readable(const struct iov_iter *i, size_t bytes); 163 size_t fault_in_iov_iter_writeable(const struct iov_iter *i, size_t bytes); 164 size_t iov_iter_single_seg_count(const struct iov_iter *i); 165 size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes, 166 struct iov_iter *i); 167 size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes, 168 struct iov_iter *i); 169 170 size_t _copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i); 171 size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i); 172 size_t _copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i); 173 174 static inline size_t copy_folio_to_iter(struct folio *folio, size_t offset, 175 size_t bytes, struct iov_iter *i) 176 { 177 return copy_page_to_iter(&folio->page, offset, bytes, i); 178 } 179 180 static __always_inline __must_check 181 size_t copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i) 182 { 183 if (check_copy_size(addr, bytes, true)) 184 return _copy_to_iter(addr, bytes, i); 185 return 0; 186 } 187 188 static __always_inline __must_check 189 size_t copy_from_iter(void *addr, size_t bytes, struct iov_iter *i) 190 { 191 if (check_copy_size(addr, bytes, false)) 192 return _copy_from_iter(addr, bytes, i); 193 return 0; 194 } 195 196 static __always_inline __must_check 197 bool copy_from_iter_full(void *addr, size_t bytes, struct iov_iter *i) 198 { 199 size_t copied = copy_from_iter(addr, bytes, i); 200 if (likely(copied == bytes)) 201 return true; 202 iov_iter_revert(i, copied); 203 return false; 204 } 205 206 static __always_inline __must_check 207 size_t copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i) 208 { 209 if (check_copy_size(addr, bytes, false)) 210 return _copy_from_iter_nocache(addr, bytes, i); 211 return 0; 212 } 213 214 static __always_inline __must_check 215 bool copy_from_iter_full_nocache(void *addr, size_t bytes, struct iov_iter *i) 216 { 217 size_t copied = copy_from_iter_nocache(addr, bytes, i); 218 if (likely(copied == bytes)) 219 return true; 220 iov_iter_revert(i, copied); 221 return false; 222 } 223 224 #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE 225 /* 226 * Note, users like pmem that depend on the stricter semantics of 227 * _copy_from_iter_flushcache() than _copy_from_iter_nocache() must check for 228 * IS_ENABLED(CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE) before assuming that the 229 * destination is flushed from the cache on return. 230 */ 231 size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i); 232 #else 233 #define _copy_from_iter_flushcache _copy_from_iter_nocache 234 #endif 235 236 #ifdef CONFIG_ARCH_HAS_COPY_MC 237 size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i); 238 #else 239 #define _copy_mc_to_iter _copy_to_iter 240 #endif 241 242 size_t iov_iter_zero(size_t bytes, struct iov_iter *); 243 bool iov_iter_is_aligned(const struct iov_iter *i, unsigned addr_mask, 244 unsigned len_mask); 245 unsigned long iov_iter_alignment(const struct iov_iter *i); 246 unsigned long iov_iter_gap_alignment(const struct iov_iter *i); 247 void iov_iter_init(struct iov_iter *i, unsigned int direction, const struct iovec *iov, 248 unsigned long nr_segs, size_t count); 249 void iov_iter_kvec(struct iov_iter *i, unsigned int direction, const struct kvec *kvec, 250 unsigned long nr_segs, size_t count); 251 void iov_iter_bvec(struct iov_iter *i, unsigned int direction, const struct bio_vec *bvec, 252 unsigned long nr_segs, size_t count); 253 void iov_iter_pipe(struct iov_iter *i, unsigned int direction, struct pipe_inode_info *pipe, 254 size_t count); 255 void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count); 256 void iov_iter_xarray(struct iov_iter *i, unsigned int direction, struct xarray *xarray, 257 loff_t start, size_t count); 258 ssize_t iov_iter_get_pages(struct iov_iter *i, struct page **pages, 259 size_t maxsize, unsigned maxpages, size_t *start, 260 iov_iter_extraction_t extraction_flags); 261 ssize_t iov_iter_get_pages2(struct iov_iter *i, struct page **pages, 262 size_t maxsize, unsigned maxpages, size_t *start); 263 ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, 264 struct page ***pages, size_t maxsize, size_t *start, 265 iov_iter_extraction_t extraction_flags); 266 ssize_t iov_iter_get_pages_alloc2(struct iov_iter *i, struct page ***pages, 267 size_t maxsize, size_t *start); 268 int iov_iter_npages(const struct iov_iter *i, int maxpages); 269 void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state); 270 271 const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags); 272 273 static inline size_t iov_iter_count(const struct iov_iter *i) 274 { 275 return i->count; 276 } 277 278 /* 279 * Cap the iov_iter by given limit; note that the second argument is 280 * *not* the new size - it's upper limit for such. Passing it a value 281 * greater than the amount of data in iov_iter is fine - it'll just do 282 * nothing in that case. 283 */ 284 static inline void iov_iter_truncate(struct iov_iter *i, u64 count) 285 { 286 /* 287 * count doesn't have to fit in size_t - comparison extends both 288 * operands to u64 here and any value that would be truncated by 289 * conversion in assignement is by definition greater than all 290 * values of size_t, including old i->count. 291 */ 292 if (i->count > count) 293 i->count = count; 294 } 295 296 /* 297 * reexpand a previously truncated iterator; count must be no more than how much 298 * we had shrunk it. 299 */ 300 static inline void iov_iter_reexpand(struct iov_iter *i, size_t count) 301 { 302 i->count = count; 303 } 304 305 static inline int 306 iov_iter_npages_cap(struct iov_iter *i, int maxpages, size_t max_bytes) 307 { 308 size_t shorted = 0; 309 int npages; 310 311 if (iov_iter_count(i) > max_bytes) { 312 shorted = iov_iter_count(i) - max_bytes; 313 iov_iter_truncate(i, max_bytes); 314 } 315 npages = iov_iter_npages(i, maxpages); 316 if (shorted) 317 iov_iter_reexpand(i, iov_iter_count(i) + shorted); 318 319 return npages; 320 } 321 322 struct csum_state { 323 __wsum csum; 324 size_t off; 325 }; 326 327 size_t csum_and_copy_to_iter(const void *addr, size_t bytes, void *csstate, struct iov_iter *i); 328 size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum, struct iov_iter *i); 329 330 static __always_inline __must_check 331 bool csum_and_copy_from_iter_full(void *addr, size_t bytes, 332 __wsum *csum, struct iov_iter *i) 333 { 334 size_t copied = csum_and_copy_from_iter(addr, bytes, csum, i); 335 if (likely(copied == bytes)) 336 return true; 337 iov_iter_revert(i, copied); 338 return false; 339 } 340 size_t hash_and_copy_to_iter(const void *addr, size_t bytes, void *hashp, 341 struct iov_iter *i); 342 343 struct iovec *iovec_from_user(const struct iovec __user *uvector, 344 unsigned long nr_segs, unsigned long fast_segs, 345 struct iovec *fast_iov, bool compat); 346 ssize_t import_iovec(int type, const struct iovec __user *uvec, 347 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp, 348 struct iov_iter *i); 349 ssize_t __import_iovec(int type, const struct iovec __user *uvec, 350 unsigned nr_segs, unsigned fast_segs, struct iovec **iovp, 351 struct iov_iter *i, bool compat); 352 int import_single_range(int type, void __user *buf, size_t len, 353 struct iovec *iov, struct iov_iter *i); 354 int import_ubuf(int type, void __user *buf, size_t len, struct iov_iter *i); 355 356 static inline void iov_iter_ubuf(struct iov_iter *i, unsigned int direction, 357 void __user *buf, size_t count) 358 { 359 WARN_ON(direction & ~(READ | WRITE)); 360 *i = (struct iov_iter) { 361 .iter_type = ITER_UBUF, 362 .user_backed = true, 363 .data_source = direction, 364 .ubuf = buf, 365 .count = count 366 }; 367 } 368 /* Flags for iov_iter_get/extract_pages*() */ 369 /* Allow P2PDMA on the extracted pages */ 370 #define ITER_ALLOW_P2PDMA ((__force iov_iter_extraction_t)0x01) 371 372 ssize_t iov_iter_extract_pages(struct iov_iter *i, struct page ***pages, 373 size_t maxsize, unsigned int maxpages, 374 iov_iter_extraction_t extraction_flags, 375 size_t *offset0); 376 377 /** 378 * iov_iter_extract_will_pin - Indicate how pages from the iterator will be retained 379 * @iter: The iterator 380 * 381 * Examine the iterator and indicate by returning true or false as to how, if 382 * at all, pages extracted from the iterator will be retained by the extraction 383 * function. 384 * 385 * %true indicates that the pages will have a pin placed in them that the 386 * caller must unpin. This is must be done for DMA/async DIO to force fork() 387 * to forcibly copy a page for the child (the parent must retain the original 388 * page). 389 * 390 * %false indicates that no measures are taken and that it's up to the caller 391 * to retain the pages. 392 */ 393 static inline bool iov_iter_extract_will_pin(const struct iov_iter *iter) 394 { 395 return user_backed_iter(iter); 396 } 397 398 #endif 399