powerpc/mm: Mark memory contexts requiring global TLBIs
Subsystems (3)
v2 cover powerpc/mm: Mark memory contexts requiring global TLBIs
capi2 and opencapi require the TLB invalidations being sent for addresses used on the cxl adapter or opencapi device to be global, as there's a translation cache in the PSL (for capi2) or NPU (for opencapi). The CAPP (for PSL) and NPU snoop the power bus.
This is not new: for the hash memory model, as soon as the cxl driver is active, all local TLBIs become global. We need a similar mechanism for the radix memory model. This patch tries to improve things a bit by flagging the contexts requiring global TLBIs, therefore limiting the "upgrade" and not affecting contexts not used by the card.
A longer-term goal is to modify the current implementation for hash to follow the same direction, i.e. identify contexts needing global TLBIs, but that will be for later. It would be required to support hash for opencapi.
Changelog:
Quoted 1 line
From previous comments:
- rename MM_CONTEXT_GLOBAL_TLBI -> MM_GLOBAL_TLBIE
- add memory barriers to make sure the device doesn't miss any TLBI
- also add barrier for the hash implemention to fix the same issue
Frederic Barrat (3):
powerpc/mm: Add marker for contexts requiring global TLB invalidations
cxl: Mark context requiring global TLBIs
cxl: Add memory barrier to guarantee TLBI scope
arch/powerpc/include/asm/book3s/64/mmu.h | 18 ++++++++++++++++++ arch/powerpc/include/asm/tlb.h | 23 +++++++++++++++++++++-- arch/powerpc/mm/mmu_context_book3s64.c | 1 + drivers/misc/cxl/api.c | 12 ++++++++++-- drivers/misc/cxl/file.c | 12 ++++++++++-- include/misc/cxl-base.h | 18 +++++++++++++++++- 6 files changed, 77 insertions(+), 7 deletions(-)
Signature · 1 line
2.11.0
RFC v2 1/3 powerpc/mm: Add marker for contexts requiring global TLB invalidations
Introduce a new 'flags' attribute per context and define its first bit to be a marker requiring all TLBIs for that context to be broadcasted globally. Once that marker is set on a context, it cannot be removed.
Such a marker is useful for memory contexts used by devices behind the NPU and CAPP/PSL. The NPU and the PSL keep their own translation cache so they need to see all the TLBIs for those contexts.
--- arch/powerpc/include/asm/book3s/64/mmu.h | 18 ++++++++++++++++++ arch/powerpc/include/asm/tlb.h | 23 +++++++++++++++++++++-- arch/powerpc/mm/mmu_context_book3s64.c | 1 + 3 files changed, 40 insertions(+), 2 deletions(-)
arch/powerpc/include/asm/book3s/64/mmu.h +18 -0
@@ -78,8 +78,12 @@ struct spinlock;/* Maximum possible number of NPUs in a system. */ #define NV_MAX_NPUS 8 +/* Bits definition for the context flags */ +#define MM_GLOBAL_TLBIE 0/* TLBI must be global */ + typedef struct { mm_context_id_t id; + unsigned long flags; u16 user_psize; /* page size index */ /* NPU NMMU context */
@@ -164,5 +168,19 @@ extern void radix_init_pseries(void);static inline void radix_init_pseries (void ) { };#endif +/* +* Mark the memory context as requiring global TLBIs, when used by +* GPUs or CAPI accelerators managing their own TLB or ERAT. +*/ +static inline void mm_context_set_global_tlbi (mm_context_t *ctx) +{ +set_bit (MM_GLOBAL_TLBIE , &ctx->flags); +} + +static inline bool mm_context_get_global_tlbi (mm_context_t *ctx) +{ +return test_bit (MM_GLOBAL_TLBIE , &ctx->flags); +} +#endif /* __ASSEMBLY__ */ #endif /* _ASM_POWERPC_BOOK3S_64_MMU_H_ */
arch/powerpc/include/asm/tlb.h +21 -2
@@ -71,8 +71,27 @@ static inline int mm_is_core_local(struct mm_struct *mm)static inline int mm_is_thread_local (struct mm_struct *mm) { -return cpumask_equal (mm_cpumask (mm), -cpumask_of (smp_processor_id ())); +int rc; + + rc =cpumask_equal (mm_cpumask (mm), +cpumask_of (smp_processor_id ())); +#ifdef CONFIG_PPC_BOOK3S_64 +if (rc) { +/* +* Check if context requires global TLBI. +* +* We need to make sure the PTE update is happening +* before reading the context global flag. Otherwise, +* reading the flag may be re-ordered and happen +* first, and we could end up in a situation where the +* old PTE was seen by a device, but the TLBI is not +* global. +*/ +smp_mb (); + rc = !mm_context_get_global_tlbi (&mm->context); + } +#endif +return rc; }#else
arch/powerpc/mm/mmu_context_book3s64.c +1 -0
@@ -156,6 +156,7 @@ int init_new_context(struct task_struct *tsk, struct mm_struct *mm)return index; mm->context.id = index; + mm->context.flags =0 ;#ifdef CONFIG_PPC_ICSWX mm->context.cop_lockp =kmalloc (sizeof (spinlock_t),GFP_KERNEL );if (!mm->context.cop_lockp) {
Signature · 1 line
2.11.0
RFC v2 2/3 cxl: Mark context requiring global TLBIs
In reply to Frederic BarratThe PSL needs to see all TLBIs pertinent to the memory contexts used on the cxl adapter. For the hash memory model, it was done by making all TLBIs global as soon as the cxl driver is in use. For radix, we need something similar, but we can refine and only convert to global the invalidations for contexts actually used by the device.
So mark the contexts being attached to the cxl adapter as requiring global TLBIs.
--- drivers/misc/cxl/api.c | 12 ++++++++++-- drivers/misc/cxl/file.c | 12 ++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-)
drivers/misc/cxl/api.c +10 -2
@@ -332,8 +332,17 @@ int cxl_start_context(struct cxl_context *ctx, u64 wed,cxl_context_mm_count_get (ctx);/* decrement the use count */ -if (ctx->mm) +if (ctx->mm) {mmput (ctx->mm); +#ifdef CONFIG_PPC_BOOK3S_64 +mm_context_set_global_tlbi (&ctx->mm->context); +/* +* Barrier guarantees that the device will +* receive all TLBIs from that point on +*/ +smp_wmb (); +#endif + } }cxl_ctx_get ();
@@ -347,7 +356,6 @@ int cxl_start_context(struct cxl_context *ctx, u64 wed,cxl_context_mm_count_put (ctx); goto out; } - ctx->status =STARTED ; out:mutex_unlock (&ctx->status_mutex);
drivers/misc/cxl/file.c +10 -2
@@ -222,8 +222,17 @@ static long afu_ioctl_start_work(struct cxl_context *ctx,cxl_context_mm_count_get (ctx);/* decrement the use count */ -if (ctx->mm) +if (ctx->mm) {mmput (ctx->mm); +#ifdef CONFIG_PPC_BOOK3S_64 +mm_context_set_global_tlbi (&ctx->mm->context); +/* +* Barrier guarantees that the device will receive all +* TLBIs from that point on +*/ +smp_wmb (); +#endif + }trace_cxl_attach (ctx, work.work_element_descriptor, work.num_interrupts, amr);
@@ -236,7 +245,6 @@ static long afu_ioctl_start_work(struct cxl_context *ctx,cxl_context_mm_count_put (ctx); goto out; } - ctx->status =STARTED ; rc =0 ; out:
Signature · 1 line
2.11.0
RFC v2 3/3 cxl: Add memory barrier to guarantee TLBI scope
In reply to Frederic BarratWith the hash memory model, all TLBIs become global when the cxl driver is active, i.e. as soon as one context is open.
It is theoretically possible to send a TLBI with the wrong scope as there's currently no memory barrier between when the driver is marked as in use, and attaching a context to the device, therefore we are exposed to re-ordering. It is highly unlikely as the use count for the driver is incremented on open() and the attachment to the device happens on a different system call (ioctl)
--- include/misc/cxl-base.h | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-)
include/misc/cxl-base.h +17 -1
@@ -25,12 +25,28 @@ extern atomic_t cxl_use_count;static inline bool cxl_ctx_in_use (void ) { -return (atomic_read (&cxl_use_count) !=0 ); +/* +* This is called when sending an TLBI, to know whether it +* should be global or local. +* +* We need to make sure the PTE update is happening before +* reading the context global flag. Otherwise, reading the +* flag may be re-ordered and happen first, and we could end +* up in a situation where the old PTE is seen by the device, +* but the TLBI is not global. +*/ +smp_mb (); +return (atomic_read (&cxl_use_count) !=0 ); }static inline void cxl_ctx_get (void ) {atomic_inc (&cxl_use_count); +/* +* Barrier guarantees that the device will receive all TLBIs +* from that point on +*/ +smp_wmb (); }static inline void cxl_ctx_put (void )
Signature · 1 line
2.11.0