All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jan Beulich <jbeulich@suse.com>
To: "xen-devel@lists.xenproject.org" <xen-devel@lists.xenproject.org>
Cc: "Andrew Cooper" <andrew.cooper3@citrix.com>,
	"Paul Durrant" <paul@xen.org>,
	"Roger Pau Monné" <roger.pau@citrix.com>
Subject: [PATCH v7 11/14] AMD/IOMMU: replace all-contiguous page tables by superpage mappings
Date: Tue, 5 Jul 2022 14:48:34 +0200	[thread overview]
Message-ID: <718906a5-8c18-530a-89f5-02e2a2efb211@suse.com> (raw)
In-Reply-To: <4c9d4b32-d4db-3a4f-fe92-6022e63c3f82@suse.com>

When a page table ends up with all contiguous entries (including all
identical attributes), it can be replaced by a superpage entry at the
next higher level. The page table itself can then be scheduled for
freeing.

Signed-off-by: Jan Beulich <jbeulich@suse.com>
Reviewed-by: Roger Pau Monné <roger.pau@citrix.com>
---
Unlike the freeing of all-empty page tables, this causes quite a bit of
back and forth for PV domains, due to their mapping/unmapping of pages
when they get converted to/from being page tables. It may therefore be
worth considering to delay re-coalescing a little, to avoid doing so
when the superpage would otherwise get split again pretty soon. But I
think this would better be the subject of a separate change anyway.

Of course this could also be helped by more "aware" kernel side
behavior: They could avoid immediately mapping freed page tables
writable again, in anticipation of re-using that same page for another
page table elsewhere.
---
v4: Re-base over changes earlier in the series.
v3: New.

--- a/xen/drivers/passthrough/amd/iommu_map.c
+++ b/xen/drivers/passthrough/amd/iommu_map.c
@@ -81,7 +81,8 @@ static union amd_iommu_pte set_iommu_pte
                                                  unsigned long dfn,
                                                  unsigned long next_mfn,
                                                  unsigned int level,
-                                                 bool iw, bool ir)
+                                                 bool iw, bool ir,
+                                                 bool *contig)
 {
     union amd_iommu_pte *table, *pde, old;
 
@@ -94,11 +95,15 @@ static union amd_iommu_pte set_iommu_pte
          old.iw != iw || old.ir != ir )
     {
         set_iommu_pde_present(pde, next_mfn, 0, iw, ir);
-        pt_update_contig_markers(&table->raw, pfn_to_pde_idx(dfn, level),
-                                 level, PTE_kind_leaf);
+        *contig = pt_update_contig_markers(&table->raw,
+                                           pfn_to_pde_idx(dfn, level),
+                                           level, PTE_kind_leaf);
     }
     else
+    {
         old.pr = false; /* signal "no change" to the caller */
+        *contig = false;
+    }
 
     unmap_domain_page(table);
 
@@ -409,6 +414,7 @@ int cf_check amd_iommu_map_page(
 {
     struct domain_iommu *hd = dom_iommu(d);
     unsigned int level = (IOMMUF_order(flags) / PTE_PER_TABLE_SHIFT) + 1;
+    bool contig;
     int rc;
     unsigned long pt_mfn = 0;
     union amd_iommu_pte old;
@@ -452,8 +458,26 @@ int cf_check amd_iommu_map_page(
 
     /* Install mapping */
     old = set_iommu_pte_present(pt_mfn, dfn_x(dfn), mfn_x(mfn), level,
-                                (flags & IOMMUF_writable),
-                                (flags & IOMMUF_readable));
+                                flags & IOMMUF_writable,
+                                flags & IOMMUF_readable, &contig);
+
+    while ( unlikely(contig) && ++level < hd->arch.amd.paging_mode )
+    {
+        struct page_info *pg = mfn_to_page(_mfn(pt_mfn));
+        unsigned long next_mfn;
+
+        if ( iommu_pde_from_dfn(d, dfn_x(dfn), level, &pt_mfn, flush_flags,
+                                false) )
+            BUG();
+        BUG_ON(!pt_mfn);
+
+        next_mfn = mfn_x(mfn) & (~0UL << (PTE_PER_TABLE_SHIFT * (level - 1)));
+        set_iommu_pte_present(pt_mfn, dfn_x(dfn), next_mfn, level,
+                              flags & IOMMUF_writable,
+                              flags & IOMMUF_readable, &contig);
+        *flush_flags |= IOMMU_FLUSHF_modified | IOMMU_FLUSHF_all;
+        iommu_queue_free_pgtable(hd, pg);
+    }
 
     spin_unlock(&hd->arch.mapping_lock);
 



  parent reply	other threads:[~2022-07-05 13:01 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-05 12:41 [PATCH v7 00/14] IOMMU: superpage support when not sharing pagetables Jan Beulich
2022-07-05 12:43 ` [PATCH v7 01/14] iommu: add preemption support to iommu_{un,}map() Jan Beulich
2022-07-22 16:31   ` Durrant, Paul
2022-07-05 12:44 ` [PATCH v7 02/14] IOMMU/x86: perform PV Dom0 mappings in batches Jan Beulich
2022-07-22 16:36   ` Durrant, Paul
2022-07-05 12:44 ` [PATCH v7 03/14] IOMMU/x86: support freeing of pagetables Jan Beulich
2022-07-22 16:44   ` Durrant, Paul
2022-07-05 12:45 ` [PATCH v7 04/14] IOMMU/x86: new command line option to suppress use of superpage mappings Jan Beulich
2022-07-07  2:26   ` Tian, Kevin
2022-07-22 16:46   ` Durrant, Paul
2022-07-05 12:45 ` [PATCH v7 05/14] AMD/IOMMU: allow " Jan Beulich
2022-07-22 16:48   ` Durrant, Paul
2022-07-05 12:46 ` [PATCH v7 06/14] VT-d: " Jan Beulich
2022-07-22 16:51   ` Durrant, Paul
2022-07-05 12:47 ` [PATCH v7 07/14] x86: introduce helper for recording degree of contiguity in page tables Jan Beulich
2022-07-22 16:54   ` Durrant, Paul
2022-07-05 12:47 ` [PATCH v7 08/14] IOMMU/x86: prefill newly allocate " Jan Beulich
2022-07-22 16:56   ` Durrant, Paul
2022-07-05 12:47 ` [PATCH v7 09/14] AMD/IOMMU: free all-empty " Jan Beulich
2022-07-22 16:58   ` Durrant, Paul
2022-07-05 12:48 ` [PATCH v7 10/14] VT-d: " Jan Beulich
2022-07-22 17:00   ` Durrant, Paul
2022-07-05 12:48 ` Jan Beulich [this message]
2022-07-22 17:02   ` [PATCH v7 11/14] AMD/IOMMU: replace all-contiguous page tables by superpage mappings Durrant, Paul
2022-07-05 12:48 ` [PATCH v7 12/14] VT-d: " Jan Beulich
2022-07-22 17:09   ` Durrant, Paul
2022-07-05 12:49 ` [PATCH v7 13/14] IOMMU/x86: add perf counters for page table splitting / coalescing Jan Beulich
2022-07-22 17:09   ` Durrant, Paul
2022-07-05 12:49 ` [PATCH v7 14/14] VT-d: fold dma_pte_clear_one() into its only caller Jan Beulich
2022-07-22 17:11   ` Durrant, Paul
2022-07-05 12:51 ` [PATCH v7 00/14] IOMMU: superpage support when not sharing pagetables Jan Beulich
2022-07-07  6:20 ` Jan Beulich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=718906a5-8c18-530a-89f5-02e2a2efb211@suse.com \
    --to=jbeulich@suse.com \
    --cc=andrew.cooper3@citrix.com \
    --cc=paul@xen.org \
    --cc=roger.pau@citrix.com \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.