2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* Berkeley style UIO structures - Alan Cox 1994.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*/
|
2012-10-13 13:46:48 +04:00
|
|
|
#ifndef __LINUX_UIO_H
|
|
|
|
#define __LINUX_UIO_H
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2013-11-28 04:29:46 +04:00
|
|
|
#include <linux/kernel.h>
|
2012-10-13 13:46:48 +04:00
|
|
|
#include <uapi/linux/uio.h>
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2013-11-28 04:29:46 +04:00
|
|
|
struct page;
|
2009-07-30 02:04:19 +04:00
|
|
|
|
|
|
|
struct kvec {
|
|
|
|
void *iov_base; /* and that should *never* hold a userland pointer */
|
|
|
|
size_t iov_len;
|
|
|
|
};
|
|
|
|
|
2014-04-05 07:12:29 +04:00
|
|
|
enum {
|
|
|
|
ITER_IOVEC = 0,
|
|
|
|
ITER_KVEC = 2,
|
|
|
|
ITER_BVEC = 4,
|
|
|
|
};
|
|
|
|
|
2013-11-28 04:29:46 +04:00
|
|
|
struct iov_iter {
|
2014-03-06 04:28:09 +04:00
|
|
|
int type;
|
2013-11-28 04:29:46 +04:00
|
|
|
size_t iov_offset;
|
|
|
|
size_t count;
|
2014-04-05 07:12:29 +04:00
|
|
|
union {
|
|
|
|
const struct iovec *iov;
|
2014-11-27 22:48:42 +03:00
|
|
|
const struct kvec *kvec;
|
2014-04-05 07:12:29 +04:00
|
|
|
const struct bio_vec *bvec;
|
|
|
|
};
|
|
|
|
unsigned long nr_segs;
|
2013-11-28 04:29:46 +04:00
|
|
|
};
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
/*
|
|
|
|
* Total number of bytes covered by an iovec.
|
|
|
|
*
|
|
|
|
* NOTE that it is not safe to use this function until all the iovec's
|
|
|
|
* segment lengths have been validated. Because the individual lengths can
|
|
|
|
* overflow a size_t when added together.
|
|
|
|
*/
|
|
|
|
static inline size_t iov_length(const struct iovec *iov, unsigned long nr_segs)
|
|
|
|
{
|
|
|
|
unsigned long seg;
|
|
|
|
size_t ret = 0;
|
|
|
|
|
|
|
|
for (seg = 0; seg < nr_segs; seg++)
|
|
|
|
ret += iov[seg].iov_len;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-11-28 04:29:46 +04:00
|
|
|
static inline struct iovec iov_iter_iovec(const struct iov_iter *iter)
|
|
|
|
{
|
|
|
|
return (struct iovec) {
|
|
|
|
.iov_base = iter->iov->iov_base + iter->iov_offset,
|
|
|
|
.iov_len = min(iter->count,
|
|
|
|
iter->iov->iov_len - iter->iov_offset),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#define iov_for_each(iov, iter, start) \
|
2014-04-05 07:12:29 +04:00
|
|
|
if (!((start).type & ITER_BVEC)) \
|
2013-11-28 04:29:46 +04:00
|
|
|
for (iter = (start); \
|
|
|
|
(iter).count && \
|
|
|
|
((iov = iov_iter_iovec(&(iter))), 1); \
|
|
|
|
iov_iter_advance(&(iter), (iov).iov_len))
|
|
|
|
|
2005-04-17 02:20:36 +04:00
|
|
|
unsigned long iov_shorten(struct iovec *iov, unsigned long nr_segs, size_t to);
|
2013-05-17 03:35:21 +04:00
|
|
|
|
2013-11-28 04:29:46 +04:00
|
|
|
size_t iov_iter_copy_from_user_atomic(struct page *page,
|
|
|
|
struct iov_iter *i, unsigned long offset, size_t bytes);
|
|
|
|
void iov_iter_advance(struct iov_iter *i, size_t bytes);
|
|
|
|
int iov_iter_fault_in_readable(struct iov_iter *i, size_t bytes);
|
2015-03-11 17:43:31 +03:00
|
|
|
int iov_iter_fault_in_multipages_readable(struct iov_iter *i, size_t bytes);
|
2013-11-28 04:29:46 +04:00
|
|
|
size_t iov_iter_single_seg_count(const struct iov_iter *i);
|
2014-02-04 02:07:03 +04:00
|
|
|
size_t copy_page_to_iter(struct page *page, size_t offset, size_t bytes,
|
|
|
|
struct iov_iter *i);
|
2014-04-03 23:05:18 +04:00
|
|
|
size_t copy_page_from_iter(struct page *page, size_t offset, size_t bytes,
|
|
|
|
struct iov_iter *i);
|
2015-12-07 00:49:22 +03:00
|
|
|
size_t copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i);
|
2014-08-01 17:27:22 +04:00
|
|
|
size_t copy_from_iter(void *addr, size_t bytes, struct iov_iter *i);
|
2014-11-28 04:27:08 +03:00
|
|
|
size_t copy_from_iter_nocache(void *addr, size_t bytes, struct iov_iter *i);
|
2014-08-01 17:27:22 +04:00
|
|
|
size_t iov_iter_zero(size_t bytes, struct iov_iter *);
|
2014-03-05 22:50:45 +04:00
|
|
|
unsigned long iov_iter_alignment(const struct iov_iter *i);
|
2014-03-06 04:28:09 +04:00
|
|
|
void iov_iter_init(struct iov_iter *i, int direction, const struct iovec *iov,
|
|
|
|
unsigned long nr_segs, size_t count);
|
2015-01-23 09:08:07 +03:00
|
|
|
void iov_iter_kvec(struct iov_iter *i, int direction, const struct kvec *kvec,
|
|
|
|
unsigned long nr_segs, size_t count);
|
|
|
|
void iov_iter_bvec(struct iov_iter *i, int direction, const struct bio_vec *bvec,
|
2014-11-24 22:46:11 +03:00
|
|
|
unsigned long nr_segs, size_t count);
|
2014-03-15 12:05:57 +04:00
|
|
|
ssize_t iov_iter_get_pages(struct iov_iter *i, struct page **pages,
|
2014-09-24 19:09:11 +04:00
|
|
|
size_t maxsize, unsigned maxpages, size_t *start);
|
2014-03-21 12:58:33 +04:00
|
|
|
ssize_t iov_iter_get_pages_alloc(struct iov_iter *i, struct page ***pages,
|
|
|
|
size_t maxsize, size_t *start);
|
2014-03-19 09:16:16 +04:00
|
|
|
int iov_iter_npages(const struct iov_iter *i, int maxpages);
|
2013-11-28 04:29:46 +04:00
|
|
|
|
2015-02-01 04:08:47 +03:00
|
|
|
const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags);
|
|
|
|
|
2013-11-28 04:29:46 +04:00
|
|
|
static inline size_t iov_iter_count(struct iov_iter *i)
|
|
|
|
{
|
|
|
|
return i->count;
|
|
|
|
}
|
|
|
|
|
2014-12-17 12:46:46 +03:00
|
|
|
static inline bool iter_is_iovec(struct iov_iter *i)
|
|
|
|
{
|
|
|
|
return !(i->type & (ITER_BVEC | ITER_KVEC));
|
|
|
|
}
|
|
|
|
|
2015-03-18 00:04:02 +03:00
|
|
|
/*
|
|
|
|
* Get one of READ or WRITE out of iter->type without any other flags OR'd in
|
|
|
|
* with it.
|
|
|
|
*
|
|
|
|
* The ?: is just for type safety.
|
|
|
|
*/
|
|
|
|
#define iov_iter_rw(i) ((0 ? (struct iov_iter *)0 : (i))->type & RW_MASK)
|
|
|
|
|
2014-06-23 11:44:40 +04:00
|
|
|
/*
|
|
|
|
* Cap the iov_iter by given limit; note that the second argument is
|
|
|
|
* *not* the new size - it's upper limit for such. Passing it a value
|
|
|
|
* greater than the amount of data in iov_iter is fine - it'll just do
|
|
|
|
* nothing in that case.
|
|
|
|
*/
|
|
|
|
static inline void iov_iter_truncate(struct iov_iter *i, u64 count)
|
2014-03-22 14:51:37 +04:00
|
|
|
{
|
2014-06-23 11:44:40 +04:00
|
|
|
/*
|
|
|
|
* count doesn't have to fit in size_t - comparison extends both
|
|
|
|
* operands to u64 here and any value that would be truncated by
|
|
|
|
* conversion in assignement is by definition greater than all
|
|
|
|
* values of size_t, including old i->count.
|
|
|
|
*/
|
2014-03-22 14:51:37 +04:00
|
|
|
if (i->count > count)
|
|
|
|
i->count = count;
|
|
|
|
}
|
|
|
|
|
2014-04-04 20:15:19 +04:00
|
|
|
/*
|
|
|
|
* reexpand a previously truncated iterator; count must be no more than how much
|
|
|
|
* we had shrunk it.
|
|
|
|
*/
|
|
|
|
static inline void iov_iter_reexpand(struct iov_iter *i, size_t count)
|
|
|
|
{
|
|
|
|
i->count = count;
|
|
|
|
}
|
2015-12-07 00:49:22 +03:00
|
|
|
size_t csum_and_copy_to_iter(const void *addr, size_t bytes, __wsum *csum, struct iov_iter *i);
|
2014-11-24 09:08:00 +03:00
|
|
|
size_t csum_and_copy_from_iter(void *addr, size_t bytes, __wsum *csum, struct iov_iter *i);
|
2014-04-04 20:15:19 +04:00
|
|
|
|
saner iov_iter initialization primitives
iovec-backed iov_iter instances are assumed to satisfy several properties:
* no more than UIO_MAXIOV elements in iovec array
* total size of all ranges is no more than MAX_RW_COUNT
* all ranges pass access_ok().
The problem is, invariants of data structures should be established in the
primitives creating those data structures, not in the code using those
primitives. And iov_iter_init() violates that principle. For a while we
managed to get away with that, but once the use of iov_iter started to
spread, it didn't take long for shit to hit the fan - missed check in
sys_sendto() had introduced a roothole.
We _do_ have primitives for importing and validating iovecs (both native and
compat ones) and those primitives are almost always followed by shoving the
resulting iovec into iov_iter. Life would be considerably simpler (and safer)
if we combined those primitives with initializing iov_iter.
That gives us two new primitives - import_iovec() and compat_import_iovec().
Calling conventions:
iovec = iov_array;
err = import_iovec(direction, uvec, nr_segs,
ARRAY_SIZE(iov_array), &iovec,
&iter);
imports user vector into kernel space (into iov_array if it fits, allocated
if it doesn't fit or if iovec was NULL), validates it and sets iter up to
refer to it. On success 0 is returned and allocated kernel copy (or NULL
if the array had fit into caller-supplied one) is returned via iovec.
On failure all allocations are undone and -E... is returned. If the total
size of ranges exceeds MAX_RW_COUNT, the excess is silently truncated.
compat_import_iovec() expects uvec to be a pointer to user array of compat_iovec;
otherwise it's identical to import_iovec().
Finally, import_single_range() sets iov_iter backed by single-element iovec
covering a user-supplied range -
err = import_single_range(direction, address, size, iovec, &iter);
does validation and sets iter up. Again, size in excess of MAX_RW_COUNT gets
silently truncated.
Next commits will be switching the things up to use of those and reducing
the amount of iov_iter_init() instances.
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
2015-03-22 00:45:43 +03:00
|
|
|
int import_iovec(int type, const struct iovec __user * uvector,
|
|
|
|
unsigned nr_segs, unsigned fast_segs,
|
|
|
|
struct iovec **iov, struct iov_iter *i);
|
|
|
|
|
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
struct compat_iovec;
|
|
|
|
int compat_import_iovec(int type, const struct compat_iovec __user * uvector,
|
|
|
|
unsigned nr_segs, unsigned fast_segs,
|
|
|
|
struct iovec **iov, struct iov_iter *i);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int import_single_range(int type, void __user *buf, size_t len,
|
|
|
|
struct iovec *iov, struct iov_iter *i);
|
|
|
|
|
2009-07-30 02:04:19 +04:00
|
|
|
#endif
|