Skip to contentSkip to conversation

staging: erofs: atomic_cond_read_relaxed on ref-locked workgroup

RFC PATCH New Gao Xiang 1 participant 2 messages Oct 19, 2018 Staging

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

RFC PATCH 1/2 staging: erofs: atomic_cond_read_relaxed on ref-locked workgroup

It's better to use atomic_cond_read_relaxed, which is implemented in hardware instructions to monitor a variable changes currently for ARM64, instead of open-coded busy waiting.

Signed-off-by Gao Xiang <gaoxiang25 at huawei.com>
---
 drivers/staging/erofs/internal.h | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
drivers/staging/erofs/internal.h +4 -8
index 57575c7f5635..a2604ae33e74 100644
@@ -227,17 +227,13 @@ static inline bool erofs_workgroup_get(struct erofs_workgroup *grp, int *ocnt)
 	int o;
 
 repeat:
+#if defined(CONFIG_SMP)
+	o = atomic_cond_read_relaxed(&grp->refcount, VAL != locked);
+#else
 	o = atomic_read(&grp->refcount);
 
-	/* spin if it is temporarily locked at the reclaim path */
-	if (unlikely(o == locked)) {
-#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
-		do
-			cpu_relax();
-		while (atomic_read(&grp->refcount) == locked);
+	BUG_ON(o == locked);
 #endif
-		goto repeat;
-	}
 
 	if (unlikely(o <= 0))
 		return -1;
Signature · 1 line
2.14.4
author
#1 lore raw

RFC PATCH 2/2 staging: erofs: fix `erofs_workgroup_{try_to_freeze, unfreeze}'

There are two minor issues in the current freeze interface:

1) Freeze interfaces have not related with CONFIG_DEBUG_SPINLOCK,

      therefore fix the incorrect conditions;

2) For SMP platforms, it should also disable preemption before doing atomic_cmpxchg in case that some high priority tasks preempt between atomic_cmpxchg and disable_preempt, then spin on the locked refcount later.

Signed-off-by Gao Xiang <gaoxiang25 at huawei.com>
---
 drivers/staging/erofs/internal.h | 28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)
drivers/staging/erofs/internal.h +15 -13
index a2604ae33e74..9d7ed3d130e3 100644
@@ -194,29 +194,31 @@ struct erofs_workgroup {
 
 #define EROFS_LOCKED_MAGIC     (INT_MIN | 0xE0F510CCL)
 
-static inline bool erofs_workgroup_try_to_freeze(
-	struct erofs_workgroup *grp, int v)
+static inline bool
+erofs_workgroup_try_to_freeze(struct erofs_workgroup *grp,
+			      int val)
 {
-#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
-	if (v != atomic_cmpxchg(&grp->refcount,
-		v, EROFS_LOCKED_MAGIC))
-		return false;
+
 	preempt_disable();
+#if defined(CONFIG_SMP)
+	if (val != atomic_cmpxchg(&grp->refcount, val,
+				  EROFS_LOCKED_MAGIC)) {
 #else
-	preempt_disable();
-	if (atomic_read(&grp->refcount) != v) {
+	/* no need to spin on UP platforms, just disable preemption. */
+	if (val != atomic_read(&grp->refcount)) {
+#endif
 		preempt_enable();
 		return false;
 	}
-#endif
 	return true;
 }
 
-static inline void erofs_workgroup_unfreeze(
-	struct erofs_workgroup *grp, int v)
+static inline void
+erofs_workgroup_unfreeze(struct erofs_workgroup *grp,
+			 int orig_val)
 {
-#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
-	atomic_set(&grp->refcount, v);
+#if defined(CONFIG_SMP)
+	atomic_set(&grp->refcount, orig_val);
 #endif
 	preempt_enable();
 }
Signature · 1 line
2.14.4