linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] ocxl: Allow contexts to be attached with a NULL mm
@ 2019-06-20  4:12 Alastair D'Silva
  2019-06-20  5:25 ` Nicholas Piggin
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Alastair D'Silva @ 2019-06-20  4:12 UTC (permalink / raw)
  To: alastair
  Cc: Andrew Donnellan, Arnd Bergmann, Greg Kroah-Hartman, linux-kernel,
	Nicholas Piggin, Masahiro Yamada, Paul Mackerras,
	Suraj Jitindar Singh, Frederic Barrat, Andrew Morton,
	linuxppc-dev, Thomas Gleixner

From: Alastair D'Silva <alastair@d-silva.org>

If an OpenCAPI context is to be used directly by a kernel driver, there
may not be a suitable mm to use.

The patch makes the mm parameter to ocxl_context_attach optional.

Signed-off-by: Alastair D'Silva <alastair@d-silva.org>
---
 arch/powerpc/mm/book3s64/radix_tlb.c |  5 +++++
 drivers/misc/ocxl/context.c          |  9 ++++++---
 drivers/misc/ocxl/link.c             | 28 ++++++++++++++++++++++++----
 3 files changed, 35 insertions(+), 7 deletions(-)

diff --git a/arch/powerpc/mm/book3s64/radix_tlb.c b/arch/powerpc/mm/book3s64/radix_tlb.c
index bb9835681315..ce8a77fae6a7 100644
--- a/arch/powerpc/mm/book3s64/radix_tlb.c
+++ b/arch/powerpc/mm/book3s64/radix_tlb.c
@@ -666,6 +666,11 @@ EXPORT_SYMBOL(radix__flush_tlb_page);
 #define radix__flush_all_mm radix__local_flush_all_mm
 #endif /* CONFIG_SMP */
 
+/*
+ * If kernel TLBIs ever become local rather than global, then
+ * drivers/misc/ocxl/link.c:ocxl_link_add_pe will need some work, as it
+ * assumes kernel TLBIs are global.
+ */
 void radix__flush_tlb_kernel_range(unsigned long start, unsigned long end)
 {
 	_tlbie_pid(0, RIC_FLUSH_ALL);
diff --git a/drivers/misc/ocxl/context.c b/drivers/misc/ocxl/context.c
index bab9c9364184..994563a078eb 100644
--- a/drivers/misc/ocxl/context.c
+++ b/drivers/misc/ocxl/context.c
@@ -69,6 +69,7 @@ static void xsl_fault_error(void *data, u64 addr, u64 dsisr)
 int ocxl_context_attach(struct ocxl_context *ctx, u64 amr, struct mm_struct *mm)
 {
 	int rc;
+	unsigned long pidr = 0;
 
 	// Locks both status & tidr
 	mutex_lock(&ctx->status_mutex);
@@ -77,9 +78,11 @@ int ocxl_context_attach(struct ocxl_context *ctx, u64 amr, struct mm_struct *mm)
 		goto out;
 	}
 
-	rc = ocxl_link_add_pe(ctx->afu->fn->link, ctx->pasid,
-			mm->context.id, ctx->tidr, amr, mm,
-			xsl_fault_error, ctx);
+	if (mm)
+		pidr = mm->context.id;
+
+	rc = ocxl_link_add_pe(ctx->afu->fn->link, ctx->pasid, pidr, ctx->tidr,
+			      amr, mm, xsl_fault_error, ctx);
 	if (rc)
 		goto out;
 
diff --git a/drivers/misc/ocxl/link.c b/drivers/misc/ocxl/link.c
index cce5b0d64505..58d111afd9f6 100644
--- a/drivers/misc/ocxl/link.c
+++ b/drivers/misc/ocxl/link.c
@@ -224,6 +224,17 @@ static irqreturn_t xsl_fault_handler(int irq, void *data)
 		ack_irq(spa, ADDRESS_ERROR);
 		return IRQ_HANDLED;
 	}
+
+	if (!pe_data->mm) {
+		/*
+		 * translation fault from a kernel context - an OpenCAPI
+		 * device tried to access a bad kernel address
+		 */
+		rcu_read_unlock();
+		pr_warn("Unresolved OpenCAPI xsl fault in kernel context\n");
+		ack_irq(spa, ADDRESS_ERROR);
+		return IRQ_HANDLED;
+	}
 	WARN_ON(pe_data->mm->context.id != pid);
 
 	if (mmget_not_zero(pe_data->mm)) {
@@ -523,7 +534,13 @@ int ocxl_link_add_pe(void *link_handle, int pasid, u32 pidr, u32 tidr,
 	pe->amr = cpu_to_be64(amr);
 	pe->software_state = cpu_to_be32(SPA_PE_VALID);
 
-	mm_context_add_copro(mm);
+	/*
+	 * For user contexts, register a copro so that TLBIs are seen
+	 * by the nest MMU. If we have a kernel context, TLBIs are
+	 * already global.
+	 */
+	if (mm)
+		mm_context_add_copro(mm);
 	/*
 	 * Barrier is to make sure PE is visible in the SPA before it
 	 * is used by the device. It also helps with the global TLBI
@@ -546,7 +563,8 @@ int ocxl_link_add_pe(void *link_handle, int pasid, u32 pidr, u32 tidr,
 	 * have a reference on mm_users. Incrementing mm_count solves
 	 * the problem.
 	 */
-	mmgrab(mm);
+	if (mm)
+		mmgrab(mm);
 	trace_ocxl_context_add(current->pid, spa->spa_mem, pasid, pidr, tidr);
 unlock:
 	mutex_unlock(&spa->spa_lock);
@@ -652,8 +670,10 @@ int ocxl_link_remove_pe(void *link_handle, int pasid)
 	if (!pe_data) {
 		WARN(1, "Couldn't find pe data when removing PE\n");
 	} else {
-		mm_context_remove_copro(pe_data->mm);
-		mmdrop(pe_data->mm);
+		if (pe_data->mm) {
+			mm_context_remove_copro(pe_data->mm);
+			mmdrop(pe_data->mm);
+		}
 		kfree_rcu(pe_data, rcu);
 	}
 unlock:
-- 
2.21.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] ocxl: Allow contexts to be attached with a NULL mm
  2019-06-20  4:12 [PATCH v2] ocxl: Allow contexts to be attached with a NULL mm Alastair D'Silva
@ 2019-06-20  5:25 ` Nicholas Piggin
  2019-06-20  9:20 ` Frederic Barrat
  2019-06-21  0:22 ` Andrew Donnellan
  2 siblings, 0 replies; 4+ messages in thread
From: Nicholas Piggin @ 2019-06-20  5:25 UTC (permalink / raw)
  To: Alastair D'Silva, alastair
  Cc: Andrew Donnellan, Arnd Bergmann, linux-kernel, Masahiro Yamada,
	Paul Mackerras, Suraj Jitindar Singh, Greg Kroah-Hartman,
	Frederic Barrat, Andrew Morton, linuxppc-dev, Thomas Gleixner

Alastair D'Silva's on June 20, 2019 2:12 pm:
> From: Alastair D'Silva <alastair@d-silva.org>
> 
> If an OpenCAPI context is to be used directly by a kernel driver, there
> may not be a suitable mm to use.
> 
> The patch makes the mm parameter to ocxl_context_attach optional.
> 
> Signed-off-by: Alastair D'Silva <alastair@d-silva.org>

Yeah I don't think you need to manage a kernel context explicitly
because it will always be flushed with tlbie, comment helps. For
the powerpc/mm bit,

Acked-by: Nicholas Piggin <npiggin@gmail.com>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] ocxl: Allow contexts to be attached with a NULL mm
  2019-06-20  4:12 [PATCH v2] ocxl: Allow contexts to be attached with a NULL mm Alastair D'Silva
  2019-06-20  5:25 ` Nicholas Piggin
@ 2019-06-20  9:20 ` Frederic Barrat
  2019-06-21  0:22 ` Andrew Donnellan
  2 siblings, 0 replies; 4+ messages in thread
From: Frederic Barrat @ 2019-06-20  9:20 UTC (permalink / raw)
  To: Alastair D'Silva, alastair
  Cc: Andrew Donnellan, Arnd Bergmann, linux-kernel, Nicholas Piggin,
	Masahiro Yamada, Paul Mackerras, Suraj Jitindar Singh,
	Greg Kroah-Hartman, Andrew Morton, linuxppc-dev, Thomas Gleixner



Le 20/06/2019 à 06:12, Alastair D'Silva a écrit :
> From: Alastair D'Silva <alastair@d-silva.org>
> 
> If an OpenCAPI context is to be used directly by a kernel driver, there
> may not be a suitable mm to use.
> 
> The patch makes the mm parameter to ocxl_context_attach optional.
> 
> Signed-off-by: Alastair D'Silva <alastair@d-silva.org>
> ---

Thanks for the update.
Acked-by: Frederic Barrat <fbarrat@linux.ibm.com>



>   arch/powerpc/mm/book3s64/radix_tlb.c |  5 +++++
>   drivers/misc/ocxl/context.c          |  9 ++++++---
>   drivers/misc/ocxl/link.c             | 28 ++++++++++++++++++++++++----
>   3 files changed, 35 insertions(+), 7 deletions(-)
> 
> diff --git a/arch/powerpc/mm/book3s64/radix_tlb.c b/arch/powerpc/mm/book3s64/radix_tlb.c
> index bb9835681315..ce8a77fae6a7 100644
> --- a/arch/powerpc/mm/book3s64/radix_tlb.c
> +++ b/arch/powerpc/mm/book3s64/radix_tlb.c
> @@ -666,6 +666,11 @@ EXPORT_SYMBOL(radix__flush_tlb_page);
>   #define radix__flush_all_mm radix__local_flush_all_mm
>   #endif /* CONFIG_SMP */
>   
> +/*
> + * If kernel TLBIs ever become local rather than global, then
> + * drivers/misc/ocxl/link.c:ocxl_link_add_pe will need some work, as it
> + * assumes kernel TLBIs are global.
> + */
>   void radix__flush_tlb_kernel_range(unsigned long start, unsigned long end)
>   {
>   	_tlbie_pid(0, RIC_FLUSH_ALL);
> diff --git a/drivers/misc/ocxl/context.c b/drivers/misc/ocxl/context.c
> index bab9c9364184..994563a078eb 100644
> --- a/drivers/misc/ocxl/context.c
> +++ b/drivers/misc/ocxl/context.c
> @@ -69,6 +69,7 @@ static void xsl_fault_error(void *data, u64 addr, u64 dsisr)
>   int ocxl_context_attach(struct ocxl_context *ctx, u64 amr, struct mm_struct *mm)
>   {
>   	int rc;
> +	unsigned long pidr = 0;
>   
>   	// Locks both status & tidr
>   	mutex_lock(&ctx->status_mutex);
> @@ -77,9 +78,11 @@ int ocxl_context_attach(struct ocxl_context *ctx, u64 amr, struct mm_struct *mm)
>   		goto out;
>   	}
>   
> -	rc = ocxl_link_add_pe(ctx->afu->fn->link, ctx->pasid,
> -			mm->context.id, ctx->tidr, amr, mm,
> -			xsl_fault_error, ctx);
> +	if (mm)
> +		pidr = mm->context.id;
> +
> +	rc = ocxl_link_add_pe(ctx->afu->fn->link, ctx->pasid, pidr, ctx->tidr,
> +			      amr, mm, xsl_fault_error, ctx);
>   	if (rc)
>   		goto out;
>   
> diff --git a/drivers/misc/ocxl/link.c b/drivers/misc/ocxl/link.c
> index cce5b0d64505..58d111afd9f6 100644
> --- a/drivers/misc/ocxl/link.c
> +++ b/drivers/misc/ocxl/link.c
> @@ -224,6 +224,17 @@ static irqreturn_t xsl_fault_handler(int irq, void *data)
>   		ack_irq(spa, ADDRESS_ERROR);
>   		return IRQ_HANDLED;
>   	}
> +
> +	if (!pe_data->mm) {
> +		/*
> +		 * translation fault from a kernel context - an OpenCAPI
> +		 * device tried to access a bad kernel address
> +		 */
> +		rcu_read_unlock();
> +		pr_warn("Unresolved OpenCAPI xsl fault in kernel context\n");
> +		ack_irq(spa, ADDRESS_ERROR);
> +		return IRQ_HANDLED;
> +	}
>   	WARN_ON(pe_data->mm->context.id != pid);
>   
>   	if (mmget_not_zero(pe_data->mm)) {
> @@ -523,7 +534,13 @@ int ocxl_link_add_pe(void *link_handle, int pasid, u32 pidr, u32 tidr,
>   	pe->amr = cpu_to_be64(amr);
>   	pe->software_state = cpu_to_be32(SPA_PE_VALID);
>   
> -	mm_context_add_copro(mm);
> +	/*
> +	 * For user contexts, register a copro so that TLBIs are seen
> +	 * by the nest MMU. If we have a kernel context, TLBIs are
> +	 * already global.
> +	 */
> +	if (mm)
> +		mm_context_add_copro(mm);
>   	/*
>   	 * Barrier is to make sure PE is visible in the SPA before it
>   	 * is used by the device. It also helps with the global TLBI
> @@ -546,7 +563,8 @@ int ocxl_link_add_pe(void *link_handle, int pasid, u32 pidr, u32 tidr,
>   	 * have a reference on mm_users. Incrementing mm_count solves
>   	 * the problem.
>   	 */
> -	mmgrab(mm);
> +	if (mm)
> +		mmgrab(mm);
>   	trace_ocxl_context_add(current->pid, spa->spa_mem, pasid, pidr, tidr);
>   unlock:
>   	mutex_unlock(&spa->spa_lock);
> @@ -652,8 +670,10 @@ int ocxl_link_remove_pe(void *link_handle, int pasid)
>   	if (!pe_data) {
>   		WARN(1, "Couldn't find pe data when removing PE\n");
>   	} else {
> -		mm_context_remove_copro(pe_data->mm);
> -		mmdrop(pe_data->mm);
> +		if (pe_data->mm) {
> +			mm_context_remove_copro(pe_data->mm);
> +			mmdrop(pe_data->mm);
> +		}
>   		kfree_rcu(pe_data, rcu);
>   	}
>   unlock:
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] ocxl: Allow contexts to be attached with a NULL mm
  2019-06-20  4:12 [PATCH v2] ocxl: Allow contexts to be attached with a NULL mm Alastair D'Silva
  2019-06-20  5:25 ` Nicholas Piggin
  2019-06-20  9:20 ` Frederic Barrat
@ 2019-06-21  0:22 ` Andrew Donnellan
  2 siblings, 0 replies; 4+ messages in thread
From: Andrew Donnellan @ 2019-06-21  0:22 UTC (permalink / raw)
  To: Alastair D'Silva, alastair
  Cc: Arnd Bergmann, linux-kernel, Nicholas Piggin, Masahiro Yamada,
	Paul Mackerras, Suraj Jitindar Singh, Greg Kroah-Hartman,
	Frederic Barrat, Andrew Morton, linuxppc-dev, Thomas Gleixner

On 20/6/19 2:12 pm, Alastair D'Silva wrote:
> From: Alastair D'Silva <alastair@d-silva.org>
> 
> If an OpenCAPI context is to be used directly by a kernel driver, there
> may not be a suitable mm to use.
> 
> The patch makes the mm parameter to ocxl_context_attach optional.
> 
> Signed-off-by: Alastair D'Silva <alastair@d-silva.org>

Acked-by: Andrew Donnellan <ajd@linux.ibm.com>

> ---
>   arch/powerpc/mm/book3s64/radix_tlb.c |  5 +++++
>   drivers/misc/ocxl/context.c          |  9 ++++++---
>   drivers/misc/ocxl/link.c             | 28 ++++++++++++++++++++++++----
>   3 files changed, 35 insertions(+), 7 deletions(-)
> 
> diff --git a/arch/powerpc/mm/book3s64/radix_tlb.c b/arch/powerpc/mm/book3s64/radix_tlb.c
> index bb9835681315..ce8a77fae6a7 100644
> --- a/arch/powerpc/mm/book3s64/radix_tlb.c
> +++ b/arch/powerpc/mm/book3s64/radix_tlb.c
> @@ -666,6 +666,11 @@ EXPORT_SYMBOL(radix__flush_tlb_page);
>   #define radix__flush_all_mm radix__local_flush_all_mm
>   #endif /* CONFIG_SMP */
>   
> +/*
> + * If kernel TLBIs ever become local rather than global, then
> + * drivers/misc/ocxl/link.c:ocxl_link_add_pe will need some work, as it
> + * assumes kernel TLBIs are global.
> + */
>   void radix__flush_tlb_kernel_range(unsigned long start, unsigned long end)
>   {
>   	_tlbie_pid(0, RIC_FLUSH_ALL);
> diff --git a/drivers/misc/ocxl/context.c b/drivers/misc/ocxl/context.c
> index bab9c9364184..994563a078eb 100644
> --- a/drivers/misc/ocxl/context.c
> +++ b/drivers/misc/ocxl/context.c
> @@ -69,6 +69,7 @@ static void xsl_fault_error(void *data, u64 addr, u64 dsisr)
>   int ocxl_context_attach(struct ocxl_context *ctx, u64 amr, struct mm_struct *mm)
>   {
>   	int rc;
> +	unsigned long pidr = 0;
>   
>   	// Locks both status & tidr
>   	mutex_lock(&ctx->status_mutex);
> @@ -77,9 +78,11 @@ int ocxl_context_attach(struct ocxl_context *ctx, u64 amr, struct mm_struct *mm)
>   		goto out;
>   	}
>   
> -	rc = ocxl_link_add_pe(ctx->afu->fn->link, ctx->pasid,
> -			mm->context.id, ctx->tidr, amr, mm,
> -			xsl_fault_error, ctx);
> +	if (mm)
> +		pidr = mm->context.id;
> +
> +	rc = ocxl_link_add_pe(ctx->afu->fn->link, ctx->pasid, pidr, ctx->tidr,
> +			      amr, mm, xsl_fault_error, ctx);
>   	if (rc)
>   		goto out;
>   
> diff --git a/drivers/misc/ocxl/link.c b/drivers/misc/ocxl/link.c
> index cce5b0d64505..58d111afd9f6 100644
> --- a/drivers/misc/ocxl/link.c
> +++ b/drivers/misc/ocxl/link.c
> @@ -224,6 +224,17 @@ static irqreturn_t xsl_fault_handler(int irq, void *data)
>   		ack_irq(spa, ADDRESS_ERROR);
>   		return IRQ_HANDLED;
>   	}
> +
> +	if (!pe_data->mm) {
> +		/*
> +		 * translation fault from a kernel context - an OpenCAPI
> +		 * device tried to access a bad kernel address
> +		 */
> +		rcu_read_unlock();
> +		pr_warn("Unresolved OpenCAPI xsl fault in kernel context\n");
> +		ack_irq(spa, ADDRESS_ERROR);
> +		return IRQ_HANDLED;
> +	}
>   	WARN_ON(pe_data->mm->context.id != pid);
>   
>   	if (mmget_not_zero(pe_data->mm)) {
> @@ -523,7 +534,13 @@ int ocxl_link_add_pe(void *link_handle, int pasid, u32 pidr, u32 tidr,
>   	pe->amr = cpu_to_be64(amr);
>   	pe->software_state = cpu_to_be32(SPA_PE_VALID);
>   
> -	mm_context_add_copro(mm);
> +	/*
> +	 * For user contexts, register a copro so that TLBIs are seen
> +	 * by the nest MMU. If we have a kernel context, TLBIs are
> +	 * already global.
> +	 */
> +	if (mm)
> +		mm_context_add_copro(mm);
>   	/*
>   	 * Barrier is to make sure PE is visible in the SPA before it
>   	 * is used by the device. It also helps with the global TLBI
> @@ -546,7 +563,8 @@ int ocxl_link_add_pe(void *link_handle, int pasid, u32 pidr, u32 tidr,
>   	 * have a reference on mm_users. Incrementing mm_count solves
>   	 * the problem.
>   	 */
> -	mmgrab(mm);
> +	if (mm)
> +		mmgrab(mm);
>   	trace_ocxl_context_add(current->pid, spa->spa_mem, pasid, pidr, tidr);
>   unlock:
>   	mutex_unlock(&spa->spa_lock);
> @@ -652,8 +670,10 @@ int ocxl_link_remove_pe(void *link_handle, int pasid)
>   	if (!pe_data) {
>   		WARN(1, "Couldn't find pe data when removing PE\n");
>   	} else {
> -		mm_context_remove_copro(pe_data->mm);
> -		mmdrop(pe_data->mm);
> +		if (pe_data->mm) {
> +			mm_context_remove_copro(pe_data->mm);
> +			mmdrop(pe_data->mm);
> +		}
>   		kfree_rcu(pe_data, rcu);
>   	}
>   unlock:
> 

-- 
Andrew Donnellan              OzLabs, ADL Canberra
ajd@linux.ibm.com             IBM Australia Limited


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-06-21  0:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-06-20  4:12 [PATCH v2] ocxl: Allow contexts to be attached with a NULL mm Alastair D'Silva
2019-06-20  5:25 ` Nicholas Piggin
2019-06-20  9:20 ` Frederic Barrat
2019-06-21  0:22 ` Andrew Donnellan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).