Skip to contentSkip to conversation

memremap: Convert devmap static branch to {inc,dec}

PATCH Applied [email protected] 2 participants 2 messages Aug 10, 2020 – Aug 11, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 memremap: Convert devmap static branch to {inc,dec}

From: Ira Weiny <[email protected]>

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/[email protected]/

Cc Dan Williams <[email protected]>
Cc Vishal Verma <[email protected]>
---
 mm/memremap.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
mm/memremap.c +2 -5
index 03e38b7a38f1..9fb9ad500e78 100644
@@ -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
Signature · 1 line
2.28.0.rc0.12.gb6a658bd00c9
Reviewed-by on v1 1/1
#1 lore raw

Looks good to me.

Quoted 54 lines · On Aug 10, 2020, at 5:53 PM, [email protected] wrote:

From: Ira Weiny <[email protected]>

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/[email protected]/

Cc Dan Williams <[email protected]>
Cc Vishal Verma <[email protected]>

---
mm/memremap.c | 7 ++-----

1 file changed, 2 insertions(+), 5 deletions(-)
mm/memremap.c +2 -5
index 03e38b7a38f1..9fb9ad500e78 100644
@@ -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
Signature · 1 line
2.28.0.rc0.12.gb6a658bd00c9