]> git.baikalelectronics.ru Git - kernel.git/commitdiff
iov: introduce iov_iter_aligned
authorKeith Busch <kbusch@kernel.org>
Fri, 10 Jun 2022 19:58:27 +0000 (12:58 -0700)
committerJens Axboe <axboe@kernel.dk>
Mon, 27 Jun 2022 12:29:11 +0000 (06:29 -0600)
The existing iov_iter_alignment() function returns the logical OR of
address and length. For cases where address and length need to be
considered separately, introduce a helper function that a caller can
specificy length and address masks that indicate if the iov is
unaligned.

Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Link: https://lore.kernel.org/r/20220610195830.3574005-9-kbusch@fb.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
include/linux/uio.h
lib/iov_iter.c

index 739285fe5a2f211ea0e668d31f58c5700b5475ab..34ba4a731179cdf4dc4f333ac9d41b34c0b75515 100644 (file)
@@ -219,6 +219,8 @@ size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i);
 #endif
 
 size_t iov_iter_zero(size_t bytes, struct iov_iter *);
+bool iov_iter_is_aligned(const struct iov_iter *i, unsigned addr_mask,
+                       unsigned len_mask);
 unsigned long iov_iter_alignment(const struct iov_iter *i);
 unsigned long iov_iter_gap_alignment(const struct iov_iter *i);
 void iov_iter_init(struct iov_iter *i, unsigned int direction, const struct iovec *iov,
index 0b64695ab632f6f94d38627ce2bb4177107d8756..507e732ef7cf18734173c5e31d22115842b8a390 100644 (file)
@@ -1268,6 +1268,98 @@ void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
 }
 EXPORT_SYMBOL(iov_iter_discard);
 
+static bool iov_iter_aligned_iovec(const struct iov_iter *i, unsigned addr_mask,
+                                  unsigned len_mask)
+{
+       size_t size = i->count;
+       size_t skip = i->iov_offset;
+       unsigned k;
+
+       for (k = 0; k < i->nr_segs; k++, skip = 0) {
+               size_t len = i->iov[k].iov_len - skip;
+
+               if (len > size)
+                       len = size;
+               if (len & len_mask)
+                       return false;
+               if ((unsigned long)(i->iov[k].iov_base + skip) & addr_mask)
+                       return false;
+
+               size -= len;
+               if (!size)
+                       break;
+       }
+       return true;
+}
+
+static bool iov_iter_aligned_bvec(const struct iov_iter *i, unsigned addr_mask,
+                                 unsigned len_mask)
+{
+       size_t size = i->count;
+       unsigned skip = i->iov_offset;
+       unsigned k;
+
+       for (k = 0; k < i->nr_segs; k++, skip = 0) {
+               size_t len = i->bvec[k].bv_len - skip;
+
+               if (len > size)
+                       len = size;
+               if (len & len_mask)
+                       return false;
+               if ((unsigned long)(i->bvec[k].bv_offset + skip) & addr_mask)
+                       return false;
+
+               size -= len;
+               if (!size)
+                       break;
+       }
+       return true;
+}
+
+/**
+ * iov_iter_is_aligned() - Check if the addresses and lengths of each segments
+ *     are aligned to the parameters.
+ *
+ * @i: &struct iov_iter to restore
+ * @addr_mask: bit mask to check against the iov element's addresses
+ * @len_mask: bit mask to check against the iov element's lengths
+ *
+ * Return: false if any addresses or lengths intersect with the provided masks
+ */
+bool iov_iter_is_aligned(const struct iov_iter *i, unsigned addr_mask,
+                        unsigned len_mask)
+{
+       if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i)))
+               return iov_iter_aligned_iovec(i, addr_mask, len_mask);
+
+       if (iov_iter_is_bvec(i))
+               return iov_iter_aligned_bvec(i, addr_mask, len_mask);
+
+       if (iov_iter_is_pipe(i)) {
+               unsigned int p_mask = i->pipe->ring_size - 1;
+               size_t size = i->count;
+
+               if (size & len_mask)
+                       return false;
+               if (size && allocated(&i->pipe->bufs[i->head & p_mask])) {
+                       if (i->iov_offset & addr_mask)
+                               return false;
+               }
+
+               return true;
+       }
+
+       if (iov_iter_is_xarray(i)) {
+               if (i->count & len_mask)
+                       return false;
+               if ((i->xarray_start + i->iov_offset) & addr_mask)
+                       return false;
+       }
+
+       return true;
+}
+EXPORT_SYMBOL_GPL(iov_iter_is_aligned);
+
 static unsigned long iov_iter_alignment_iovec(const struct iov_iter *i)
 {
        unsigned long res = 0;