Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
authorLinus Torvalds <torvalds@linux-foundation.org>
Sun, 10 Mar 2019 19:47:57 +0000 (12:47 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Sun, 10 Mar 2019 19:47:57 +0000 (12:47 -0700)
Pull virtio updates from Michael Tsirkin:
 "Several fixes, most notably fix for virtio on swiotlb systems"

* tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost:
  vhost: silence an unused-variable warning
  virtio: hint if callbacks surprisingly might sleep
  virtio-ccw: wire up ->bus_name callback
  s390/virtio: handle find on invalid queue gracefully
  virtio-ccw: diag 500 may return a negative cookie
  virtio_balloon: remove the unnecessary 0-initialization
  virtio-balloon: improve update_balloon_size_func
  virtio-blk: Consider virtio_max_dma_size() for maximum segment size
  virtio: Introduce virtio_max_dma_size()
  dma: Introduce dma_max_mapping_size()
  swiotlb: Add is_swiotlb_active() function
  swiotlb: Introduce swiotlb_max_mapping_size()

1  2 
Documentation/DMA-API.txt
include/linux/dma-mapping.h
include/linux/swiotlb.h
kernel/dma/direct.c
kernel/dma/mapping.c
kernel/dma/swiotlb.c

index 5866436429102115551094fca971245f4b9e6439,acfe3d0f78d1ff4a7f960ee4fa66ae7b0abe8881..0076150fdccbbc1276047f39ff5496edc93b492a
@@@ -195,6 -195,14 +195,14 @@@ Requesting the required mask does not a
  wish to take advantage of it, you should issue a dma_set_mask()
  call to set the mask to the value returned.
  
+ ::
+       size_t
+       dma_direct_max_mapping_size(struct device *dev);
+ Returns the maximum size of a mapping for the device. The size parameter
+ of the mapping functions like dma_map_single(), dma_map_page() and
+ others should not be larger than the returned value.
  
  Part Id - Streaming DMA mappings
  --------------------------------
@@@ -530,8 -538,8 +538,8 @@@ that simply cannot make consistent memo
        dma_free_attrs(struct device *dev, size_t size, void *cpu_addr,
                       dma_addr_t dma_handle, unsigned long attrs)
  
 -Free memory allocated by the dma_alloc_attrs().  All parameters common
 -parameters must identical to those otherwise passed to dma_fre_coherent,
 +Free memory allocated by the dma_alloc_attrs().  All common
 +parameters must be identical to those otherwise passed to dma_free_coherent,
  and the attrs argument must be identical to the attrs passed to
  dma_alloc_attrs().
  
@@@ -566,7 -574,8 +574,7 @@@ boundaries when doing this
  
        int
        dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
 -                                  dma_addr_t device_addr, size_t size, int
 -                                  flags)
 +                                  dma_addr_t device_addr, size_t size);
  
  Declare region of memory to be handed out by dma_alloc_coherent() when
  it's asked for coherent memory for this device.
@@@ -580,6 -589,12 +588,6 @@@ dma_addr_t in dma_alloc_coherent())
  
  size is the size of the area (must be multiples of PAGE_SIZE).
  
 -flags can be ORed together and are:
 -
 -- DMA_MEMORY_EXCLUSIVE - only allocate memory from the declared regions.
 -  Do not allow dma_alloc_coherent() to fall back to system memory when
 -  it's out of memory in the declared region.
 -
  As a simplification for the platforms, only *one* such region of
  memory may be declared per device.
  
@@@ -598,6 -613,23 +606,6 @@@ unconditionally having removed all the 
  driver's job to ensure that no parts of this memory region are
  currently in use.
  
 -::
 -
 -      void *
 -      dma_mark_declared_memory_occupied(struct device *dev,
 -                                        dma_addr_t device_addr, size_t size)
 -
 -This is used to occupy specific regions of the declared space
 -(dma_alloc_coherent() will hand out the first free region it finds).
 -
 -device_addr is the *device* address of the region requested.
 -
 -size is the size (and should be a page-sized multiple).
 -
 -The return value will be either a pointer to the processor virtual
 -address of the memory, or an error (via PTR_ERR()) if any part of the
 -region is occupied.
 -
  Part III - Debug drivers use of the DMA-API
  -------------------------------------------
  
@@@ -672,9 -704,6 +680,9 @@@ dma-api/disabled           This read-only file c
                                happen when it runs out of memory or if it was
                                disabled at boot time
  
 +dma-api/dump                  This read-only file contains current DMA
 +                              mappings.
 +
  dma-api/error_count           This file is read-only and shows the total
                                numbers of errors found.
  
@@@ -696,7 -725,7 +704,7 @@@ dma-api/num_free_entries   The current nu
  dma-api/nr_total_entries      The total number of dma_debug_entries in the
                                allocator, both free and used.
  
 -dma-api/driver-filter         You can write a name of a driver into this file
 +dma-api/driver_filter         You can write a name of a driver into this file
                                to limit the debug output to requests from that
                                particular driver. Write an empty string to
                                that file to disable the filter and see
index 70ad15758a7027de406e4cc5d4de95c16f579f08,5b21f14802e16b40535990d5245398a59b52391f..75e60be91e5f3f6bae493c8784da79a77db47aab
@@@ -130,6 -130,7 +130,7 @@@ struct dma_map_ops 
                        enum dma_data_direction direction);
        int (*dma_supported)(struct device *dev, u64 mask);
        u64 (*get_required_mask)(struct device *dev);
+       size_t (*max_mapping_size)(struct device *dev);
  };
  
  #define DMA_MAPPING_ERROR             (~(dma_addr_t)0)
@@@ -153,7 -154,7 +154,7 @@@ static inline int is_device_dma_capable
        return dev->dma_mask != NULL && *dev->dma_mask != DMA_MASK_NONE;
  }
  
 -#ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
 +#ifdef CONFIG_DMA_DECLARE_COHERENT
  /*
   * These three functions are only for dma allocator.
   * Don't use them in device drivers.
@@@ -192,7 -193,7 +193,7 @@@ static inline int dma_mmap_from_global_
  {
        return 0;
  }
 -#endif /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
 +#endif /* CONFIG_DMA_DECLARE_COHERENT */
  
  static inline bool dma_is_direct(const struct dma_map_ops *ops)
  {
@@@ -208,8 -209,6 +209,8 @@@ dma_addr_t dma_direct_map_page(struct d
                unsigned long attrs);
  int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents,
                enum dma_data_direction dir, unsigned long attrs);
 +dma_addr_t dma_direct_map_resource(struct device *dev, phys_addr_t paddr,
 +              size_t size, enum dma_data_direction dir, unsigned long attrs);
  
  #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
      defined(CONFIG_SWIOTLB)
@@@ -259,6 -258,8 +260,8 @@@ static inline void dma_direct_sync_sg_f
  }
  #endif
  
+ size_t dma_direct_max_mapping_size(struct device *dev);
  #ifdef CONFIG_HAS_DMA
  #include <asm/dma-mapping.h>
  
@@@ -348,20 -349,19 +351,20 @@@ static inline dma_addr_t dma_map_resour
                                          unsigned long attrs)
  {
        const struct dma_map_ops *ops = get_dma_ops(dev);
 -      dma_addr_t addr;
 +      dma_addr_t addr = DMA_MAPPING_ERROR;
  
        BUG_ON(!valid_dma_direction(dir));
  
        /* Don't allow RAM to be mapped */
 -      BUG_ON(pfn_valid(PHYS_PFN(phys_addr)));
 +      if (WARN_ON_ONCE(pfn_valid(PHYS_PFN(phys_addr))))
 +              return DMA_MAPPING_ERROR;
  
 -      addr = phys_addr;
 -      if (ops && ops->map_resource)
 +      if (dma_is_direct(ops))
 +              addr = dma_direct_map_resource(dev, phys_addr, size, dir, attrs);
 +      else if (ops->map_resource)
                addr = ops->map_resource(dev, phys_addr, size, dir, attrs);
  
        debug_dma_map_resource(dev, phys_addr, size, dir, addr);
 -
        return addr;
  }
  
@@@ -372,7 -372,7 +375,7 @@@ static inline void dma_unmap_resource(s
        const struct dma_map_ops *ops = get_dma_ops(dev);
  
        BUG_ON(!valid_dma_direction(dir));
 -      if (ops && ops->unmap_resource)
 +      if (!dma_is_direct(ops) && ops->unmap_resource)
                ops->unmap_resource(dev, addr, size, dir, attrs);
        debug_dma_unmap_resource(dev, addr, size, dir);
  }
@@@ -463,6 -463,7 +466,7 @@@ int dma_supported(struct device *dev, u
  int dma_set_mask(struct device *dev, u64 mask);
  int dma_set_coherent_mask(struct device *dev, u64 mask);
  u64 dma_get_required_mask(struct device *dev);
+ size_t dma_max_mapping_size(struct device *dev);
  #else /* CONFIG_HAS_DMA */
  static inline dma_addr_t dma_map_page_attrs(struct device *dev,
                struct page *page, size_t offset, size_t size,
@@@ -564,6 -565,10 +568,10 @@@ static inline u64 dma_get_required_mask
  {
        return 0;
  }
+ static inline size_t dma_max_mapping_size(struct device *dev)
+ {
+       return 0;
+ }
  #endif /* CONFIG_HAS_DMA */
  
  static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr,
@@@ -671,23 -676,15 +679,23 @@@ static inline int dma_coerce_mask_and_c
        return dma_set_mask_and_coherent(dev, mask);
  }
  
 -#ifndef arch_setup_dma_ops
 +#ifdef CONFIG_ARCH_HAS_SETUP_DMA_OPS
 +void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
 +              const struct iommu_ops *iommu, bool coherent);
 +#else
  static inline void arch_setup_dma_ops(struct device *dev, u64 dma_base,
 -                                    u64 size, const struct iommu_ops *iommu,
 -                                    bool coherent) { }
 -#endif
 +              u64 size, const struct iommu_ops *iommu, bool coherent)
 +{
 +}
 +#endif /* CONFIG_ARCH_HAS_SETUP_DMA_OPS */
  
 -#ifndef arch_teardown_dma_ops
 -static inline void arch_teardown_dma_ops(struct device *dev) { }
 -#endif
 +#ifdef CONFIG_ARCH_HAS_TEARDOWN_DMA_OPS
 +void arch_teardown_dma_ops(struct device *dev);
 +#else
 +static inline void arch_teardown_dma_ops(struct device *dev)
 +{
 +}
 +#endif /* CONFIG_ARCH_HAS_TEARDOWN_DMA_OPS */
  
  static inline unsigned int dma_get_max_seg_size(struct device *dev)
  {
@@@ -736,14 -733,19 +744,14 @@@ static inline int dma_get_cache_alignme
        return 1;
  }
  
 -/* flags for the coherent memory api */
 -#define DMA_MEMORY_EXCLUSIVE          0x01
 -
 -#ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
 +#ifdef CONFIG_DMA_DECLARE_COHERENT
  int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
 -                              dma_addr_t device_addr, size_t size, int flags);
 +                              dma_addr_t device_addr, size_t size);
  void dma_release_declared_memory(struct device *dev);
 -void *dma_mark_declared_memory_occupied(struct device *dev,
 -                                      dma_addr_t device_addr, size_t size);
  #else
  static inline int
  dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
 -                          dma_addr_t device_addr, size_t size, int flags)
 +                          dma_addr_t device_addr, size_t size)
  {
        return -ENOSYS;
  }
@@@ -752,7 -754,14 +760,7 @@@ static inline voi
  dma_release_declared_memory(struct device *dev)
  {
  }
 -
 -static inline void *
 -dma_mark_declared_memory_occupied(struct device *dev,
 -                                dma_addr_t device_addr, size_t size)
 -{
 -      return ERR_PTR(-EBUSY);
 -}
 -#endif /* CONFIG_HAVE_GENERIC_DMA_COHERENT */
 +#endif /* CONFIG_DMA_DECLARE_COHERENT */
  
  static inline void *dmam_alloc_coherent(struct device *dev, size_t size,
                dma_addr_t *dma_handle, gfp_t gfp)
diff --combined include/linux/swiotlb.h
index 54254388899e589431f80b29ab7f387f44822fe6,29bc3a203283e108f9183522b723b1590339f3f9..361f62bb4a8e92efd26e3f0d91e65193085dcb3b
@@@ -60,6 -60,9 +60,6 @@@ extern void swiotlb_tbl_sync_single(str
                                    size_t size, enum dma_data_direction dir,
                                    enum dma_sync_target target);
  
 -extern int
 -swiotlb_dma_supported(struct device *hwdev, u64 mask);
 -
  #ifdef CONFIG_SWIOTLB
  extern enum swiotlb_force swiotlb_force;
  extern phys_addr_t io_tlb_start, io_tlb_end;
@@@ -73,6 -76,8 +73,8 @@@ bool swiotlb_map(struct device *dev, ph
                size_t size, enum dma_data_direction dir, unsigned long attrs);
  void __init swiotlb_exit(void);
  unsigned int swiotlb_max_segment(void);
+ size_t swiotlb_max_mapping_size(struct device *dev);
+ bool is_swiotlb_active(void);
  #else
  #define swiotlb_force SWIOTLB_NO_FORCE
  static inline bool is_swiotlb_buffer(phys_addr_t paddr)
@@@ -92,6 -97,15 +94,15 @@@ static inline unsigned int swiotlb_max_
  {
        return 0;
  }
+ static inline size_t swiotlb_max_mapping_size(struct device *dev)
+ {
+       return SIZE_MAX;
+ }
+ static inline bool is_swiotlb_active(void)
+ {
+       return false;
+ }
  #endif /* CONFIG_SWIOTLB */
  
  extern void swiotlb_print_info(void);
diff --combined kernel/dma/direct.c
index 7ff807f2121c5748dcb56b552fcc8551b0e85bdb,6310ad01f915b1807bc87cbee1c44bae99eb5666..fcdb23e8d2fccf1e6d27f64a23a66f7878224c04
@@@ -132,7 -132,8 +132,7 @@@ again
                        goto again;
                }
  
 -              if (IS_ENABLED(CONFIG_ZONE_DMA) &&
 -                  phys_mask < DMA_BIT_MASK(32) && !(gfp & GFP_DMA)) {
 +              if (IS_ENABLED(CONFIG_ZONE_DMA) && !(gfp & GFP_DMA)) {
                        gfp = (gfp & ~GFP_DMA32) | GFP_DMA;
                        goto again;
                }
@@@ -355,20 -356,6 +355,20 @@@ out_unmap
  }
  EXPORT_SYMBOL(dma_direct_map_sg);
  
 +dma_addr_t dma_direct_map_resource(struct device *dev, phys_addr_t paddr,
 +              size_t size, enum dma_data_direction dir, unsigned long attrs)
 +{
 +      dma_addr_t dma_addr = paddr;
 +
 +      if (unlikely(!dma_direct_possible(dev, dma_addr, size))) {
 +              report_addr(dev, dma_addr, size);
 +              return DMA_MAPPING_ERROR;
 +      }
 +
 +      return dma_addr;
 +}
 +EXPORT_SYMBOL(dma_direct_map_resource);
 +
  /*
   * Because 32-bit DMA masks are so common we expect every architecture to be
   * able to satisfy them - either by not supporting more physical memory, or by
@@@ -393,3 -380,14 +393,14 @@@ int dma_direct_supported(struct device 
         */
        return mask >= __phys_to_dma(dev, min_mask);
  }
+ size_t dma_direct_max_mapping_size(struct device *dev)
+ {
+       size_t size = SIZE_MAX;
+       /* If SWIOTLB is active, use its maximum mapping size */
+       if (is_swiotlb_active())
+               size = swiotlb_max_mapping_size(dev);
+       return size;
+ }
diff --combined kernel/dma/mapping.c
index ef2aba503467efcf91fcc60c124893c308d03e75,5753008ab2860fba0c5b7dd911c047e1fc770e61..c000906348c936162e1dc4cd7faa1147a57c6d8e
@@@ -207,6 -207,7 +207,6 @@@ int dma_mmap_attrs(struct device *dev, 
  }
  EXPORT_SYMBOL(dma_mmap_attrs);
  
 -#ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
  static u64 dma_default_get_required_mask(struct device *dev)
  {
        u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
@@@ -237,6 -238,7 +237,6 @@@ u64 dma_get_required_mask(struct devic
        return dma_default_get_required_mask(dev);
  }
  EXPORT_SYMBOL_GPL(dma_get_required_mask);
 -#endif
  
  #ifndef arch_dma_alloc_attrs
  #define arch_dma_alloc_attrs(dev)     (true)
@@@ -316,23 -318,18 +316,23 @@@ int dma_supported(struct device *dev, u
  }
  EXPORT_SYMBOL(dma_supported);
  
 -#ifndef HAVE_ARCH_DMA_SET_MASK
 +#ifdef CONFIG_ARCH_HAS_DMA_SET_MASK
 +void arch_dma_set_mask(struct device *dev, u64 mask);
 +#else
 +#define arch_dma_set_mask(dev, mask)  do { } while (0)
 +#endif
 +
  int dma_set_mask(struct device *dev, u64 mask)
  {
        if (!dev->dma_mask || !dma_supported(dev, mask))
                return -EIO;
  
 +      arch_dma_set_mask(dev, mask);
        dma_check_mask(dev, mask);
        *dev->dma_mask = mask;
        return 0;
  }
  EXPORT_SYMBOL(dma_set_mask);
 -#endif
  
  #ifndef CONFIG_ARCH_HAS_DMA_SET_COHERENT_MASK
  int dma_set_coherent_mask(struct device *dev, u64 mask)
@@@ -360,3 -357,17 +360,17 @@@ void dma_cache_sync(struct device *dev
                ops->cache_sync(dev, vaddr, size, dir);
  }
  EXPORT_SYMBOL(dma_cache_sync);
+ size_t dma_max_mapping_size(struct device *dev)
+ {
+       const struct dma_map_ops *ops = get_dma_ops(dev);
+       size_t size = SIZE_MAX;
+       if (dma_is_direct(ops))
+               size = dma_direct_max_mapping_size(dev);
+       else if (ops && ops->max_mapping_size)
+               size = ops->max_mapping_size(dev);
+       return size;
+ }
+ EXPORT_SYMBOL_GPL(dma_max_mapping_size);
diff --combined kernel/dma/swiotlb.c
index a9ad02d4d84e2ffa431fa6018f26a2c3f039cd6e,c873f9cc21460536f52edd26966a7b6cd668948d..9f5851064aea6854b426fa34ffe3ab10588666fe
@@@ -34,9 -34,6 +34,9 @@@
  #include <linux/scatterlist.h>
  #include <linux/mem_encrypt.h>
  #include <linux/set_memory.h>
 +#ifdef CONFIG_DEBUG_FS
 +#include <linux/debugfs.h>
 +#endif
  
  #include <asm/io.h>
  #include <asm/dma.h>
@@@ -75,11 -72,6 +75,11 @@@ phys_addr_t io_tlb_start, io_tlb_end
   */
  static unsigned long io_tlb_nslabs;
  
 +/*
 + * The number of used IO TLB block
 + */
 +static unsigned long io_tlb_used;
 +
  /*
   * This is a free list describing the number of free entries available from
   * each index
@@@ -393,7 -385,7 +393,7 @@@ void __init swiotlb_exit(void
  }
  
  /*
 - * Bounce: copy the swiotlb buffer back to the original dma location
 + * Bounce: copy the swiotlb buffer from or back to the original dma location
   */
  static void swiotlb_bounce(phys_addr_t orig_addr, phys_addr_t tlb_addr,
                           size_t size, enum dma_data_direction dir)
@@@ -483,10 -475,6 +483,10 @@@ phys_addr_t swiotlb_tbl_map_single(stru
         * request and allocate a buffer from that IO TLB pool.
         */
        spin_lock_irqsave(&io_tlb_lock, flags);
 +
 +      if (unlikely(nslots > io_tlb_nslabs - io_tlb_used))
 +              goto not_found;
 +
        index = ALIGN(io_tlb_index, stride);
        if (index >= io_tlb_nslabs)
                index = 0;
@@@ -536,7 -524,6 +536,7 @@@ not_found
                dev_warn(hwdev, "swiotlb buffer is full (sz: %zd bytes)\n", size);
        return DMA_MAPPING_ERROR;
  found:
 +      io_tlb_used += nslots;
        spin_unlock_irqrestore(&io_tlb_lock, flags);
  
        /*
@@@ -597,8 -584,6 +597,8 @@@ void swiotlb_tbl_unmap_single(struct de
                 */
                for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE -1) && io_tlb_list[i]; i--)
                        io_tlb_list[i] = ++count;
 +
 +              io_tlb_used -= nslots;
        }
        spin_unlock_irqrestore(&io_tlb_lock, flags);
  }
@@@ -666,35 -651,28 +666,49 @@@ bool swiotlb_map(struct device *dev, ph
        return true;
  }
  
 -/*
 - * Return whether the given device DMA address mask can be supported
 - * properly.  For example, if your device can only drive the low 24-bits
 - * during bus mastering, then you would pass 0x00ffffff as the mask to
 - * this function.
 - */
 -int
 -swiotlb_dma_supported(struct device *hwdev, u64 mask)
 -{
 -      return __phys_to_dma(hwdev, io_tlb_end - 1) <= mask;
 -}
 -
+ size_t swiotlb_max_mapping_size(struct device *dev)
+ {
+       return ((size_t)1 << IO_TLB_SHIFT) * IO_TLB_SEGSIZE;
+ }
+ bool is_swiotlb_active(void)
+ {
+       /*
+        * When SWIOTLB is initialized, even if io_tlb_start points to physical
+        * address zero, io_tlb_end surely doesn't.
+        */
+       return io_tlb_end != 0;
+ }
++
 +#ifdef CONFIG_DEBUG_FS
 +
 +static int __init swiotlb_create_debugfs(void)
 +{
 +      struct dentry *d_swiotlb_usage;
 +      struct dentry *ent;
 +
 +      d_swiotlb_usage = debugfs_create_dir("swiotlb", NULL);
 +
 +      if (!d_swiotlb_usage)
 +              return -ENOMEM;
 +
 +      ent = debugfs_create_ulong("io_tlb_nslabs", 0400,
 +                                 d_swiotlb_usage, &io_tlb_nslabs);
 +      if (!ent)
 +              goto fail;
 +
 +      ent = debugfs_create_ulong("io_tlb_used", 0400,
 +                                 d_swiotlb_usage, &io_tlb_used);
 +      if (!ent)
 +              goto fail;
 +
 +      return 0;
 +
 +fail:
 +      debugfs_remove_recursive(d_swiotlb_usage);
 +      return -ENOMEM;
 +}
 +
 +late_initcall(swiotlb_create_debugfs);
 +
 +#endif