linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] memremap: Convert devmap static branch to {inc,dec}
@ 2020-08-10 23:53 ira.weiny
  2020-08-11 11:59 ` William Kucharski
  0 siblings, 1 reply; 2+ messages in thread
From: ira.weiny @ 2020-08-10 23:53 UTC (permalink / raw)
  To: Andrew Morton, Vishal Verma
  Cc: Ira Weiny, Dan Williams, Aneesh Kumar K.V, linux-mm, linux-kernel

From: Ira Weiny <ira.weiny@intel.com>

While reviewing Protection Key Supervisor support it was pointed out
that using a counter to track static branch enable was an anti-pattern
which was better solved using the provided static_branch_{inc,dec}
functions.[1]

Fix up devmap_managed_key to work the same way.  Also this should be
safer because there is a very small (very unlikely) race when multiple
callers try to enable at the same time.

[1] https://lore.kernel.org/lkml/20200714194031.GI5523@worktop.programming.kicks-ass.net/

Cc: Dan Williams <dan.j.williams@intel.com>
Cc: Vishal Verma <vishal.l.verma@intel.com>
Signed-off-by: Ira Weiny <ira.weiny@intel.com>
---
 mm/memremap.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/mm/memremap.c b/mm/memremap.c
index 03e38b7a38f1..9fb9ad500e78 100644
--- a/mm/memremap.c
+++ b/mm/memremap.c
@@ -40,12 +40,10 @@ EXPORT_SYMBOL_GPL(memremap_compat_align);
 #ifdef CONFIG_DEV_PAGEMAP_OPS
 DEFINE_STATIC_KEY_FALSE(devmap_managed_key);
 EXPORT_SYMBOL(devmap_managed_key);
-static atomic_t devmap_managed_enable;
 
 static void devmap_managed_enable_put(void)
 {
-	if (atomic_dec_and_test(&devmap_managed_enable))
-		static_branch_disable(&devmap_managed_key);
+	static_branch_dec(&devmap_managed_key);
 }
 
 static int devmap_managed_enable_get(struct dev_pagemap *pgmap)
@@ -56,8 +54,7 @@ static int devmap_managed_enable_get(struct dev_pagemap *pgmap)
 		return -EINVAL;
 	}
 
-	if (atomic_inc_return(&devmap_managed_enable) == 1)
-		static_branch_enable(&devmap_managed_key);
+	static_branch_inc(&devmap_managed_key);
 	return 0;
 }
 #else
-- 
2.28.0.rc0.12.gb6a658bd00c9


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

* Re: [PATCH] memremap: Convert devmap static branch to {inc,dec}
  2020-08-10 23:53 [PATCH] memremap: Convert devmap static branch to {inc,dec} ira.weiny
@ 2020-08-11 11:59 ` William Kucharski
  0 siblings, 0 replies; 2+ messages in thread
From: William Kucharski @ 2020-08-11 11:59 UTC (permalink / raw)
  To: Ira Weiny
  Cc: Andrew Morton, Vishal Verma, Dan Williams, Aneesh Kumar K.V,
	linux-mm, linux-kernel

Looks good to me.

Reviewed-by: William Kucharski <william.kucharski@oracle.com>

> On Aug 10, 2020, at 5:53 PM, ira.weiny@intel.com wrote:
> 
> From: Ira Weiny <ira.weiny@intel.com>
> 
> While reviewing Protection Key Supervisor support it was pointed out
> that using a counter to track static branch enable was an anti-pattern
> which was better solved using the provided static_branch_{inc,dec}
> functions.[1]
> 
> Fix up devmap_managed_key to work the same way.  Also this should be
> safer because there is a very small (very unlikely) race when multiple
> callers try to enable at the same time.
> 
> [1] https://lore.kernel.org/lkml/20200714194031.GI5523@worktop.programming.kicks-ass.net/
> 
> Cc: Dan Williams <dan.j.williams@intel.com>
> Cc: Vishal Verma <vishal.l.verma@intel.com>
> Signed-off-by: Ira Weiny <ira.weiny@intel.com>
> ---
> mm/memremap.c | 7 ++-----
> 1 file changed, 2 insertions(+), 5 deletions(-)
> 
> diff --git a/mm/memremap.c b/mm/memremap.c
> index 03e38b7a38f1..9fb9ad500e78 100644
> --- a/mm/memremap.c
> +++ b/mm/memremap.c
> @@ -40,12 +40,10 @@ EXPORT_SYMBOL_GPL(memremap_compat_align);
> #ifdef CONFIG_DEV_PAGEMAP_OPS
> DEFINE_STATIC_KEY_FALSE(devmap_managed_key);
> EXPORT_SYMBOL(devmap_managed_key);
> -static atomic_t devmap_managed_enable;
> 
> static void devmap_managed_enable_put(void)
> {
> -	if (atomic_dec_and_test(&devmap_managed_enable))
> -		static_branch_disable(&devmap_managed_key);
> +	static_branch_dec(&devmap_managed_key);
> }
> 
> static int devmap_managed_enable_get(struct dev_pagemap *pgmap)
> @@ -56,8 +54,7 @@ static int devmap_managed_enable_get(struct dev_pagemap *pgmap)
> 		return -EINVAL;
> 	}
> 
> -	if (atomic_inc_return(&devmap_managed_enable) == 1)
> -		static_branch_enable(&devmap_managed_key);
> +	static_branch_inc(&devmap_managed_key);
> 	return 0;
> }
> #else
> -- 
> 2.28.0.rc0.12.gb6a658bd00c9
> 
> 


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

end of thread, other threads:[~2020-08-11 11:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-10 23:53 [PATCH] memremap: Convert devmap static branch to {inc,dec} ira.weiny
2020-08-11 11:59 ` William Kucharski

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).