--- v2
+++ v12
@@ -1,121 +1,155 @@
-There moves locked pages accounting to helpers.
-Later they will be reused for Dynamic DMA windows (DDW).
+This is a pretty mechanical patch to make next patches simpler.
-While we are here, update the comment explaining why RLIMIT_MEMLOCK
-might be required to be bigger than the guest RAM.
+New tce_iommu_unuse_page() helper does put_page() now but it might skip
+that after the memory registering patch applied.
+
+As we are here, this removes unnecessary checks for a value returned
+by pfn_to_page() as it cannot possibly return NULL.
+
+This moves tce_iommu_disable() later to let tce_iommu_clear() know if
+the container has been enabled because if it has not been, then
+put_page() must not be called on TCEs from the TCE table. This situation
+is not yet possible but it will after KVM acceleration patchset is
+applied.
+
+This changes code to work with physical addresses rather than linear
+mapping addresses for better code readability. Following patches will
+add an xchg() callback for an IOMMU table which will accept/return
+physical addresses (unlike current tce_build()) which will eliminate
+redundant conversions.
Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
+[aw: for the vfio related changes]
+Acked-by: Alex Williamson <alex.williamson@redhat.com>
+Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
+Reviewed-by: Gavin Shan <gwshan@linux.vnet.ibm.com>
---
- drivers/vfio/vfio_iommu_spapr_tce.c | 71 +++++++++++++++++++++++++++----------
- 1 file changed, 53 insertions(+), 18 deletions(-)
+Changes:
+v9:
+* changed helpers to work with physical addresses rather than linear
+(for simplicity - later ::xchg() will receive physical and avoid
+additional convertions)
+
+v6:
+* tce_get_hva() returns hva via a pointer
+---
+ drivers/vfio/vfio_iommu_spapr_tce.c | 61 +++++++++++++++++++++++++------------
+ 1 file changed, 41 insertions(+), 20 deletions(-)
diff --git a/drivers/vfio/vfio_iommu_spapr_tce.c b/drivers/vfio/vfio_iommu_spapr_tce.c
-index 1c1a9c4..c9fac97 100644
+index 5bbdf37..cf5d4a1 100644
--- a/drivers/vfio/vfio_iommu_spapr_tce.c
+++ b/drivers/vfio/vfio_iommu_spapr_tce.c
-@@ -29,6 +29,46 @@
- static void tce_iommu_detach_group(void *iommu_data,
- struct iommu_group *iommu_group);
+@@ -191,69 +191,90 @@ static void tce_iommu_release(void *iommu_data)
+ struct tce_container *container = iommu_data;
-+static long try_increment_locked_vm(struct iommu_table *tbl)
+ WARN_ON(container->tbl && !container->tbl->it_group);
+- tce_iommu_disable(container);
+
+ if (container->tbl && container->tbl->it_group)
+ tce_iommu_detach_group(iommu_data, container->tbl->it_group);
+
++ tce_iommu_disable(container);
+ mutex_destroy(&container->lock);
+
+ kfree(container);
+ }
+
++static void tce_iommu_unuse_page(struct tce_container *container,
++ unsigned long oldtce)
+{
-+ long ret = 0, locked, lock_limit, npages;
++ struct page *page;
+
-+ if (!current || !current->mm)
-+ return -ESRCH; /* process exited */
++ if (!(oldtce & (TCE_PCI_READ | TCE_PCI_WRITE)))
++ return;
+
-+ npages = (tbl->it_size << IOMMU_PAGE_SHIFT_4K) >> PAGE_SHIFT;
++ page = pfn_to_page(oldtce >> PAGE_SHIFT);
+
-+ down_write(¤t->mm->mmap_sem);
-+ locked = current->mm->locked_vm + npages;
-+ lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
-+ if (locked > lock_limit && !capable(CAP_IPC_LOCK)) {
-+ pr_warn("RLIMIT_MEMLOCK (%ld) exceeded\n",
-+ rlimit(RLIMIT_MEMLOCK));
-+ ret = -ENOMEM;
-+ } else {
-+ current->mm->locked_vm += npages;
-+ }
-+ up_write(¤t->mm->mmap_sem);
++ if (oldtce & TCE_PCI_WRITE)
++ SetPageDirty(page);
+
-+ return ret;
++ put_page(page);
+}
+
-+static void decrement_locked_vm(struct iommu_table *tbl)
+ static int tce_iommu_clear(struct tce_container *container,
+ struct iommu_table *tbl,
+ unsigned long entry, unsigned long pages)
+ {
+ unsigned long oldtce;
+- struct page *page;
+
+ for ( ; pages; --pages, ++entry) {
+ oldtce = iommu_clear_tce(tbl, entry);
+ if (!oldtce)
+ continue;
+
+- page = pfn_to_page(oldtce >> PAGE_SHIFT);
+- WARN_ON(!page);
+- if (page) {
+- if (oldtce & TCE_PCI_WRITE)
+- SetPageDirty(page);
+- put_page(page);
+- }
++ tce_iommu_unuse_page(container, oldtce);
+ }
+
+ return 0;
+ }
+
++static int tce_iommu_use_page(unsigned long tce, unsigned long *hpa)
+{
-+ long npages;
++ struct page *page = NULL;
++ enum dma_data_direction direction = iommu_tce_direction(tce);
+
-+ if (!current || !current->mm)
-+ return; /* process exited */
++ if (get_user_pages_fast(tce & PAGE_MASK, 1,
++ direction != DMA_TO_DEVICE, &page) != 1)
++ return -EFAULT;
+
-+ npages = (tbl->it_size << IOMMU_PAGE_SHIFT_4K) >> PAGE_SHIFT;
++ *hpa = __pa((unsigned long) page_address(page));
+
-+ down_write(¤t->mm->mmap_sem);
-+ if (npages > current->mm->locked_vm)
-+ npages = current->mm->locked_vm;
-+ current->mm->locked_vm -= npages;
-+ up_write(¤t->mm->mmap_sem);
++ return 0;
+}
+
- /*
- * VFIO IOMMU fd for SPAPR_TCE IOMMU implementation
- *
-@@ -86,7 +126,6 @@ static void tce_iommu_take_ownership_notify(struct spapr_tce_iommu_group *data,
- static int tce_iommu_enable(struct tce_container *container)
+ static long tce_iommu_build(struct tce_container *container,
+ struct iommu_table *tbl,
+ unsigned long entry, unsigned long tce, unsigned long pages)
{
- int ret = 0;
-- unsigned long locked, lock_limit, npages;
- struct iommu_table *tbl;
- struct spapr_tce_iommu_group *data;
+ long i, ret = 0;
+- struct page *page = NULL;
+- unsigned long hva;
++ struct page *page;
++ unsigned long hpa;
+ enum dma_data_direction direction = iommu_tce_direction(tce);
-@@ -120,24 +159,23 @@ static int tce_iommu_enable(struct tce_container *container)
- * Also we don't have a nice way to fail on H_PUT_TCE due to ulimits,
- * that would effectively kill the guest at random points, much better
- * enforcing the limit based on the max that the guest can map.
-+ *
-+ * Unfortunately at the moment it counts whole tables, no matter how
-+ * much memory the guest has. I.e. for 4GB guest and 4 IOMMU groups
-+ * each with 2GB DMA window, 8GB will be counted here. The reason for
-+ * this is that we cannot tell here the amount of RAM used by the guest
-+ * as this information is only available from KVM and VFIO is
-+ * KVM agnostic.
- */
- tbl = data->ops->get_table(data, 0);
- if (!tbl)
- return -ENXIO;
+ for (i = 0; i < pages; ++i) {
+ unsigned long offset = tce & IOMMU_PAGE_MASK(tbl) & ~PAGE_MASK;
-- down_write(¤t->mm->mmap_sem);
-- npages = (tbl->it_size << IOMMU_PAGE_SHIFT_4K) >> PAGE_SHIFT;
-- locked = current->mm->locked_vm + npages;
-- lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
-- if (locked > lock_limit && !capable(CAP_IPC_LOCK)) {
-- pr_warn("RLIMIT_MEMLOCK (%ld) exceeded\n",
-- rlimit(RLIMIT_MEMLOCK));
-- ret = -ENOMEM;
-- } else {
-- current->mm->locked_vm += npages;
-- container->enabled = true;
-- }
-- up_write(¤t->mm->mmap_sem);
-+ ret = try_increment_locked_vm(tbl);
-+ if (ret)
-+ return ret;
-+
-+ container->enabled = true;
+- ret = get_user_pages_fast(tce & PAGE_MASK, 1,
+- direction != DMA_TO_DEVICE, &page);
+- if (unlikely(ret != 1)) {
+- ret = -EFAULT;
++ ret = tce_iommu_use_page(tce, &hpa);
++ if (ret)
+ break;
+- }
- return ret;
- }
-@@ -163,10 +201,7 @@ static void tce_iommu_disable(struct tce_container *container)
- if (!tbl)
- return;
++ page = pfn_to_page(hpa >> PAGE_SHIFT);
+ if (!tce_page_is_contained(page, tbl->it_page_shift)) {
+ ret = -EPERM;
+ break;
+ }
-- down_write(¤t->mm->mmap_sem);
-- current->mm->locked_vm -= (tbl->it_size <<
-- IOMMU_PAGE_SHIFT_4K) >> PAGE_SHIFT;
-- up_write(¤t->mm->mmap_sem);
-+ decrement_locked_vm(tbl);
- }
-
- static void *tce_iommu_open(unsigned long arg)
+- hva = (unsigned long) page_address(page) + offset;
+-
+- ret = iommu_tce_build(tbl, entry + i, hva, direction);
++ hpa |= offset;
++ ret = iommu_tce_build(tbl, entry + i, (unsigned long) __va(hpa),
++ direction);
+ if (ret) {
+- put_page(page);
++ tce_iommu_unuse_page(container, hpa);
+ pr_err("iommu_tce: %s failed ioba=%lx, tce=%lx, ret=%ld\n",
+ __func__, entry << tbl->it_page_shift,
+ tce, ret);
--
-2.0.0
+2.4.0.rc3.8.gfb3e7d5