Re: [PATCH v2 3/5] generic/pgtable: Introduce set_pte_safe()

2018-12-03 Thread Dan Williams
On Mon, Dec 3, 2018 at 9:53 AM Dave Hansen  wrote:
>
> On 11/30/18 4:35 PM, Dan Williams wrote:
> > +/*
> > + * The _safe versions of set_{pte,pmd,pud,p4d,pgd} validate that the
> > + * entry was not populated previously. I.e. for cases where a flush-tlb
> > + * is elided, double-check that there is no stale mapping to shoot down.
> > + */
>
> Functionally these look great to me.
>
> The only thing I'd suggest is to make the comment more about when to use
> these, instead of what they do:
>
> Use the set_p*_safe() version when confident that *no*
> TLB flush will be required as a result of the "set", such
> as setting non-present entries or when possibly superfluously
> re-setting an entry.

The second sentence was meant to be a "why", but yes, it's entirely too subtle.


Re: [PATCH v2 3/5] generic/pgtable: Introduce set_pte_safe()

2018-12-03 Thread Dave Hansen
On 11/30/18 4:35 PM, Dan Williams wrote:
> +/*
> + * The _safe versions of set_{pte,pmd,pud,p4d,pgd} validate that the
> + * entry was not populated previously. I.e. for cases where a flush-tlb
> + * is elided, double-check that there is no stale mapping to shoot down.
> + */

Functionally these look great to me.

The only thing I'd suggest is to make the comment more about when to use
these, instead of what they do:

Use the set_p*_safe() version when confident that *no*
TLB flush will be required as a result of the "set", such
as setting non-present entries or when possibly superfluously
re-setting an entry.



[PATCH v2 3/5] generic/pgtable: Introduce set_pte_safe()

2018-11-30 Thread Dan Williams
Commit f77084d96355 "x86/mm/pat: Disable preemption around
__flush_tlb_all()" introduced a warning to capture cases
__flush_tlb_all() is called without pre-emption disabled. It triggers a
false positive warning in the memory hotplug path. On investigation it
was found that the __flush_tlb_all() calls are not necessary. However,
they are only "not necessary" in practice provided the ptes are being
initially populated from the !present state. Introduce set_pte_safe() as
a sanity check that the pte is being updated in a way that does not
require a tlb flush.

Forgive the macro, the availability of the various of set_pte() levels
is hit and miss across architectures.

Link: 
https://lore.kernel.org/lkml/279dadae-9148-465c-7ec6-3f37e026c...@intel.com
Cc: Kirill A. Shutemov 
Cc: Sebastian Andrzej Siewior 
Cc: Thomas Gleixner 
Cc: Peter Zijlstra 
Cc: Borislav Petkov 
Suggested-by: Peter Zijlstra 
Suggested-by: Dave Hansen 
Signed-off-by: Dan Williams 
---
 include/asm-generic/pgtable.h |   35 +++
 1 file changed, 35 insertions(+)

diff --git a/include/asm-generic/pgtable.h b/include/asm-generic/pgtable.h
index dae7f98babed..62be0d5e1a9a 100644
--- a/include/asm-generic/pgtable.h
+++ b/include/asm-generic/pgtable.h
@@ -400,6 +400,41 @@ static inline int pgd_same(pgd_t pgd_a, pgd_t pgd_b)
 }
 #endif
 
+/*
+ * The _safe versions of set_{pte,pmd,pud,p4d,pgd} validate that the
+ * entry was not populated previously. I.e. for cases where a flush-tlb
+ * is elided, double-check that there is no stale mapping to shoot down.
+ */
+#define set_pte_safe(ptep, pte) \
+({ \
+   WARN_ON_ONCE(pte_present(*ptep) && !pte_same(*ptep, pte)); \
+   set_pte(ptep, pte); \
+})
+
+#define set_pmd_safe(pmdp, pmd) \
+({ \
+   WARN_ON_ONCE(pmd_present(*pmdp) && !pmd_same(*pmdp, pmd)); \
+   set_pmd(pmdp, pmd); \
+})
+
+#define set_pud_safe(pudp, pud) \
+({ \
+   WARN_ON_ONCE(pud_present(*pudp) && !pud_same(*pudp, pud)); \
+   set_pud(pudp, pud); \
+})
+
+#define set_p4d_safe(p4dp, p4d) \
+({ \
+   WARN_ON_ONCE(p4d_present(*p4dp) && !p4d_same(*p4dp, p4d)); \
+   set_p4d(p4dp, p4d); \
+})
+
+#define set_pgd_safe(pgdp, pgd) \
+({ \
+   WARN_ON_ONCE(pgd_present(*pgdp) && !pgd_same(*pgdp, pgd)); \
+   set_pgd(pgdp, pgd); \
+})
+
 #ifndef __HAVE_ARCH_DO_SWAP_PAGE
 /*
  * Some architectures support metadata associated with a page. When a