Add virtqueue_add_inbuf_cache_clean() for passing DMA_ATTR_CPU_CACHE_CLEAN
to virtqueue operations. This suppresses DMA debug cacheline overlap
warnings for buffers where proper cache management is ensured by the
caller.

Signed-off-by: Michael S. Tsirkin <[email protected]>
---
 drivers/virtio/virtio_ring.c | 72 ++++++++++++++++++++++++++----------
 include/linux/virtio.h       |  5 +++
 2 files changed, 58 insertions(+), 19 deletions(-)

diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
index 1832ea7982a6..19a4a8cd22f9 100644
--- a/drivers/virtio/virtio_ring.c
+++ b/drivers/virtio/virtio_ring.c
@@ -382,7 +382,7 @@ static int vring_mapping_error(const struct vring_virtqueue 
*vq,
 /* Map one sg entry. */
 static int vring_map_one_sg(const struct vring_virtqueue *vq, struct 
scatterlist *sg,
                            enum dma_data_direction direction, dma_addr_t *addr,
-                           u32 *len, bool premapped)
+                           u32 *len, bool premapped, unsigned long attr)
 {
        if (premapped) {
                *addr = sg_dma_address(sg);
@@ -410,7 +410,7 @@ static int vring_map_one_sg(const struct vring_virtqueue 
*vq, struct scatterlist
         */
        *addr = virtqueue_map_page_attrs(&vq->vq, sg_page(sg),
                                         sg->offset, sg->length,
-                                        direction, 0);
+                                        direction, attr);
 
        if (vring_mapping_error(vq, *addr))
                return -ENOMEM;
@@ -539,7 +539,8 @@ static inline int virtqueue_add_split(struct 
vring_virtqueue *vq,
                                      void *data,
                                      void *ctx,
                                      bool premapped,
-                                     gfp_t gfp)
+                                     gfp_t gfp,
+                                     unsigned long attr)
 {
        struct vring_desc_extra *extra;
        struct scatterlist *sg;
@@ -605,7 +606,8 @@ static inline int virtqueue_add_split(struct 
vring_virtqueue *vq,
                        dma_addr_t addr;
                        u32 len;
 
-                       if (vring_map_one_sg(vq, sg, DMA_TO_DEVICE, &addr, 
&len, premapped))
+                       if (vring_map_one_sg(vq, sg, DMA_TO_DEVICE, &addr, &len,
+                                            premapped, attr))
                                goto unmap_release;
 
                        prev = i;
@@ -622,7 +624,8 @@ static inline int virtqueue_add_split(struct 
vring_virtqueue *vq,
                        dma_addr_t addr;
                        u32 len;
 
-                       if (vring_map_one_sg(vq, sg, DMA_FROM_DEVICE, &addr, 
&len, premapped))
+                       if (vring_map_one_sg(vq, sg, DMA_FROM_DEVICE, &addr, 
&len,
+                                            premapped, attr))
                                goto unmap_release;
 
                        prev = i;
@@ -1315,7 +1318,8 @@ static int virtqueue_add_indirect_packed(struct 
vring_virtqueue *vq,
                                         unsigned int in_sgs,
                                         void *data,
                                         bool premapped,
-                                        gfp_t gfp)
+                                        gfp_t gfp,
+                                        unsigned long attr)
 {
        struct vring_desc_extra *extra;
        struct vring_packed_desc *desc;
@@ -1346,7 +1350,7 @@ static int virtqueue_add_indirect_packed(struct 
vring_virtqueue *vq,
                for (sg = sgs[n]; sg; sg = sg_next(sg)) {
                        if (vring_map_one_sg(vq, sg, n < out_sgs ?
                                             DMA_TO_DEVICE : DMA_FROM_DEVICE,
-                                            &addr, &len, premapped))
+                                            &addr, &len, premapped, attr))
                                goto unmap_release;
 
                        desc[i].flags = cpu_to_le16(n < out_sgs ?
@@ -1441,7 +1445,8 @@ static inline int virtqueue_add_packed(struct 
vring_virtqueue *vq,
                                       void *data,
                                       void *ctx,
                                       bool premapped,
-                                      gfp_t gfp)
+                                      gfp_t gfp,
+                                      unsigned long attr)
 {
        struct vring_packed_desc *desc;
        struct scatterlist *sg;
@@ -1466,7 +1471,7 @@ static inline int virtqueue_add_packed(struct 
vring_virtqueue *vq,
 
        if (virtqueue_use_indirect(vq, total_sg)) {
                err = virtqueue_add_indirect_packed(vq, sgs, total_sg, out_sgs,
-                                                   in_sgs, data, premapped, 
gfp);
+                                                   in_sgs, data, premapped, 
gfp, attr);
                if (err != -ENOMEM) {
                        END_USE(vq);
                        return err;
@@ -1502,7 +1507,7 @@ static inline int virtqueue_add_packed(struct 
vring_virtqueue *vq,
 
                        if (vring_map_one_sg(vq, sg, n < out_sgs ?
                                             DMA_TO_DEVICE : DMA_FROM_DEVICE,
-                                            &addr, &len, premapped))
+                                            &addr, &len, premapped, attr))
                                goto unmap_release;
 
                        flags = cpu_to_le16(vq->packed.avail_used_flags |
@@ -2244,14 +2249,17 @@ static inline int virtqueue_add(struct virtqueue *_vq,
                                void *data,
                                void *ctx,
                                bool premapped,
-                               gfp_t gfp)
+                               gfp_t gfp,
+                               unsigned long attr)
 {
        struct vring_virtqueue *vq = to_vvq(_vq);
 
        return vq->packed_ring ? virtqueue_add_packed(vq, sgs, total_sg,
-                                       out_sgs, in_sgs, data, ctx, premapped, 
gfp) :
+                                       out_sgs, in_sgs, data, ctx, premapped, 
gfp,
+                                       attr) :
                                 virtqueue_add_split(vq, sgs, total_sg,
-                                       out_sgs, in_sgs, data, ctx, premapped, 
gfp);
+                                       out_sgs, in_sgs, data, ctx, premapped, 
gfp,
+                                       attr);
 }
 
 /**
@@ -2289,7 +2297,7 @@ int virtqueue_add_sgs(struct virtqueue *_vq,
                        total_sg++;
        }
        return virtqueue_add(_vq, sgs, total_sg, out_sgs, in_sgs,
-                            data, NULL, false, gfp);
+                            data, NULL, false, gfp, 0);
 }
 EXPORT_SYMBOL_GPL(virtqueue_add_sgs);
 
@@ -2311,7 +2319,7 @@ int virtqueue_add_outbuf(struct virtqueue *vq,
                         void *data,
                         gfp_t gfp)
 {
-       return virtqueue_add(vq, &sg, num, 1, 0, data, NULL, false, gfp);
+       return virtqueue_add(vq, &sg, num, 1, 0, data, NULL, false, gfp, 0);
 }
 EXPORT_SYMBOL_GPL(virtqueue_add_outbuf);
 
@@ -2334,7 +2342,7 @@ int virtqueue_add_outbuf_premapped(struct virtqueue *vq,
                                   void *data,
                                   gfp_t gfp)
 {
-       return virtqueue_add(vq, &sg, num, 1, 0, data, NULL, true, gfp);
+       return virtqueue_add(vq, &sg, num, 1, 0, data, NULL, true, gfp, 0);
 }
 EXPORT_SYMBOL_GPL(virtqueue_add_outbuf_premapped);
 
@@ -2356,10 +2364,36 @@ int virtqueue_add_inbuf(struct virtqueue *vq,
                        void *data,
                        gfp_t gfp)
 {
-       return virtqueue_add(vq, &sg, num, 0, 1, data, NULL, false, gfp);
+       return virtqueue_add(vq, &sg, num, 0, 1, data, NULL, false, gfp, 0);
 }
 EXPORT_SYMBOL_GPL(virtqueue_add_inbuf);
 
+/**
+ * virtqueue_add_inbuf_cache_clean - expose input buffers with cache clean hint
+ * @vq: the struct virtqueue we're talking about.
+ * @sg: scatterlist (must be well-formed and terminated!)
+ * @num: the number of entries in @sg writable by other side
+ * @data: the token identifying the buffer.
+ * @gfp: how to do memory allocations (if necessary).
+ *
+ * Adds DMA_ATTR_CPU_CACHE_CLEAN attribute to suppress overlapping cacheline
+ * warnings in DMA debug builds. Has no effect in production builds.
+ *
+ * Caller must ensure we don't call this with other virtqueue operations
+ * at the same time (except where noted).
+ *
+ * Returns zero or a negative error (ie. ENOSPC, ENOMEM, EIO).
+ */
+int virtqueue_add_inbuf_cache_clean(struct virtqueue *vq,
+                                   struct scatterlist *sg, unsigned int num,
+                                   void *data,
+                                   gfp_t gfp)
+{
+       return virtqueue_add(vq, &sg, num, 0, 1, data, NULL, false, gfp,
+                            DMA_ATTR_CPU_CACHE_CLEAN);
+}
+EXPORT_SYMBOL_GPL(virtqueue_add_inbuf_cache_clean);
+
 /**
  * virtqueue_add_inbuf_ctx - expose input buffers to other end
  * @vq: the struct virtqueue we're talking about.
@@ -2380,7 +2414,7 @@ int virtqueue_add_inbuf_ctx(struct virtqueue *vq,
                        void *ctx,
                        gfp_t gfp)
 {
-       return virtqueue_add(vq, &sg, num, 0, 1, data, ctx, false, gfp);
+       return virtqueue_add(vq, &sg, num, 0, 1, data, ctx, false, gfp, 0);
 }
 EXPORT_SYMBOL_GPL(virtqueue_add_inbuf_ctx);
 
@@ -2405,7 +2439,7 @@ int virtqueue_add_inbuf_premapped(struct virtqueue *vq,
                                  void *ctx,
                                  gfp_t gfp)
 {
-       return virtqueue_add(vq, &sg, num, 0, 1, data, ctx, true, gfp);
+       return virtqueue_add(vq, &sg, num, 0, 1, data, ctx, true, gfp, 0);
 }
 EXPORT_SYMBOL_GPL(virtqueue_add_inbuf_premapped);
 
diff --git a/include/linux/virtio.h b/include/linux/virtio.h
index 3626eb694728..63bb05ece8c5 100644
--- a/include/linux/virtio.h
+++ b/include/linux/virtio.h
@@ -62,6 +62,11 @@ int virtqueue_add_inbuf(struct virtqueue *vq,
                        void *data,
                        gfp_t gfp);
 
+int virtqueue_add_inbuf_cache_clean(struct virtqueue *vq,
+                                   struct scatterlist sg[], unsigned int num,
+                                   void *data,
+                                   gfp_t gfp);
+
 int virtqueue_add_inbuf_ctx(struct virtqueue *vq,
                            struct scatterlist sg[], unsigned int num,
                            void *data,
-- 
MST


Reply via email to