Skip to contentSkip to conversation

lightnvm: pblk: adjust the position of the lock

PATCH In review Hua Su 3 participants 6 messages Nov 27, 2018 – Nov 28, 2018 1 message not in the archive Block layer

lore
Share & tools
lore

Hua Su

author
#0 lore raw

PATCH 1/1 lightnvm: pblk: adjust the position of the lock

Add lock protection for list operations.

---
 drivers/lightnvm/pblk-core.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
drivers/lightnvm/pblk-core.c +10 -7
index 6944aac43b01..e490df217dac 100644
@@ -1286,24 +1286,27 @@ int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line)
 	list_del(&line->list);
 
 	ret = pblk_line_prepare(pblk, line);
-	if (ret) {
-		list_add(&line->list, &l_mg->free_list);
-		spin_unlock(&l_mg->free_lock);
-		return ret;
-	}
-	spin_unlock(&l_mg->free_lock);
+	if (ret)
+		goto out;
 
 	ret = pblk_line_alloc_bitmaps(pblk, line);
 	if (ret)
-		return ret;
+		goto out;
 
 	if (!pblk_line_init_bb(pblk, line, 0)) {
 		list_add(&line->list, &l_mg->free_list);
+		spin_unlock(&l_mg->free_lock);
 		return -EINTR;
 	}
+	spin_unlock(&l_mg->free_lock);
 
 	pblk_rl_free_lines_dec(&pblk->rl, line, true);
 	return 0;
+
+out:
+	list_add(&line->list, &l_mg->free_list);
+	spin_unlock(&l_mg->free_lock);
+	return ret;
 }
 
 void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line)
Signature · 1 line
2.19.1
#1 lore raw
Quoted 49 lines · Hua Su

Add lock protection for list operations.

---
drivers/lightnvm/pblk-core.c | 17 ++++++++++-------

1 file changed, 10 insertions(+), 7 deletions(-)
drivers/lightnvm/pblk-core.c +10 -8
index 6944aac43b01..e490df217dac 100644
@@ -1286,24 +1286,27 @@ int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line)
 	list_del(&line->list);
 
 	ret = pblk_line_prepare(pblk, line);
-	if (ret) {
-		list_add(&line->list, &l_mg->free_list);
-		spin_unlock(&l_mg->free_lock);
-		return ret;
-	}
-	spin_unlock(&l_mg->free_lock);
+	if (ret)
+		goto out;
 
 	ret = pblk_line_alloc_bitmaps(pblk, line);
 	if (ret)
-		return ret;
+		goto out;
 
 	if (!pblk_line_init_bb(pblk, line, 0)) {
 		list_add(&line->list, &l_mg->free_list);
+		spin_unlock(&l_mg->free_lock);
 		return -EINTR;
 	}
+	spin_unlock(&l_mg->free_lock);
 
 	pblk_rl_free_lines_dec(&pblk->rl, line, true);
 	return 0;
+
+out:
+	list_add(&line->list, &l_mg->free_list);
+	spin_unlock(&l_mg->free_lock);
+	return ret;
 }
 
 void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line)
--
Quoted 1 line

2.19.1

This path is only touched by the recovery path, which is single threaded, so there is no race condition as is. Also, if recovery fails, pblk will not create the instance at all. This said, it would be good to protect the list_add on the pblk_line_init_bb() error path in case this code is used for some other purpose in the future.

However, the implementation you propose has some issues due to the extended critical zone, which covers pblk_line_alloc_bitmaps(). Here, allocations are intentionally non-atomic (i.e., GFP_KERNEL).

You can instead do the following:

drivers/lightnvm/pblk-core.c +10 -3
index 615817bf97e3..02c4c1708c45 100644
@@ -1301,15 +1301,22 @@ int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line)
 
        ret = pblk_line_alloc_bitmaps(pblk, line);
        if (ret)
-               return ret;
+               goto fail;
 
        if (!pblk_line_init_bb(pblk, line, 0)) {
-               list_add(&line->list, &l_mg->free_list);
-               return -EINTR;
+               ret = -EINTR;
+               goto fail;
        }
 
        pblk_rl_free_lines_dec(&pblk->rl, line, true);
        return 0;
+
+fail:
+       spin_lock(&l_mg->free_lock);
+       list_add(&line->list, &l_mg->free_list);
+       spin_unlock(&l_mg->free_lock);
+
+       return ret;
 }
 
 void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line)

Javier

#2 lore raw
Quoted 47 lines · Javier Gonzalez
Quoted 47 lines · Hua Su

Add lock protection for list operations.

---
drivers/lightnvm/pblk-core.c | 17 ++++++++++-------

1 file changed, 10 insertions(+), 7 deletions(-)
drivers/lightnvm/pblk-core.c +10 -7
index 6944aac43b01..e490df217dac 100644
@@ -1286,24 +1286,27 @@ int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line)
 	list_del(&line->list);
 
 	ret = pblk_line_prepare(pblk, line);
-	if (ret) {
-		list_add(&line->list, &l_mg->free_list);
-		spin_unlock(&l_mg->free_lock);
-		return ret;
-	}
-	spin_unlock(&l_mg->free_lock);
+	if (ret)
+		goto out;
 
 	ret = pblk_line_alloc_bitmaps(pblk, line);
 	if (ret)
-		return ret;
+		goto out;
 
 	if (!pblk_line_init_bb(pblk, line, 0)) {
 		list_add(&line->list, &l_mg->free_list);
+		spin_unlock(&l_mg->free_lock);
 		return -EINTR;
 	}
+	spin_unlock(&l_mg->free_lock);
 
 	pblk_rl_free_lines_dec(&pblk->rl, line, true);
 	return 0;
+
+out:
+	list_add(&line->list, &l_mg->free_list);
+	spin_unlock(&l_mg->free_lock);
+	return ret;
 }
 
Quoted 9 lines
Quoted 3 lines
drivers/lightnvm/pblk-core.c +0 -1
 void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line)
--

2.19.1

This path is only touched by the recovery path, which is single threaded, so there is no race condition as is. Also, if recovery fails, pblk will not create the instance at all. This said, it would be good to protect the list_add on the pblk_line_init_bb() error path in case this code is used for some other purpose in the future.

I like your explanation here. Another option is that we could add a comment to notify the developer that it safe in this context?

Quoted 41 lines

However, the implementation you propose has some issues due to the extended critical zone, which covers pblk_line_alloc_bitmaps(). Here, allocations are intentionally non-atomic (i.e., GFP_KERNEL).

You can instead do the following:

drivers/lightnvm/pblk-core.c +10 -3
index 615817bf97e3..02c4c1708c45 100644
@@ -1301,15 +1301,22 @@ int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line)
 
         ret = pblk_line_alloc_bitmaps(pblk, line);
         if (ret)
-               return ret;
+               goto fail;
 
         if (!pblk_line_init_bb(pblk, line, 0)) {
-               list_add(&line->list, &l_mg->free_list);
-               return -EINTR;
+               ret = -EINTR;
+               goto fail;
         }
 
         pblk_rl_free_lines_dec(&pblk->rl, line, true);
         return 0;
+
+fail:
+       spin_lock(&l_mg->free_lock);
+       list_add(&line->list, &l_mg->free_list);
+       spin_unlock(&l_mg->free_lock);
+
+       return ret;
  }
 
  void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line)

Javier

#3 lore raw
Quoted 59 lines · Matias Bjørling
Quoted 46 lines · Javier Gonzalez
Quoted 46 lines · Hua Su

Add lock protection for list operations.

---
drivers/lightnvm/pblk-core.c | 17 ++++++++++-------

1 file changed, 10 insertions(+), 7 deletions(-)
drivers/lightnvm/pblk-core.c +10 -7
index 6944aac43b01..e490df217dac 100644
@@ -1286,24 +1286,27 @@ int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line)
   list_del(&line->list);
 
   ret = pblk_line_prepare(pblk, line);
-    if (ret) {
-        list_add(&line->list, &l_mg->free_list);
-        spin_unlock(&l_mg->free_lock);
-        return ret;
-    }
-    spin_unlock(&l_mg->free_lock);
+    if (ret)
+        goto out;
 
   ret = pblk_line_alloc_bitmaps(pblk, line);
   if (ret)
-        return ret;
+        goto out;
 
   if (!pblk_line_init_bb(pblk, line, 0)) {
       list_add(&line->list, &l_mg->free_list);
+        spin_unlock(&l_mg->free_lock);
       return -EINTR;
   }
+    spin_unlock(&l_mg->free_lock);
 
   pblk_rl_free_lines_dec(&pblk->rl, line, true);
   return 0;
+
+out:
+    list_add(&line->list, &l_mg->free_list);
+    spin_unlock(&l_mg->free_lock);
+    return ret;
 }
 
Quoted 8 lines
Quoted 3 lines
drivers/lightnvm/pblk-core.c +0 -1
 void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line)
--

2.19.1

This path is only touched by the recovery path, which is single threaded, so there is no race condition as is. Also, if recovery fails, pblk will not create the instance at all. This said, it would be good to protect the list_add on the pblk_line_init_bb() error path in case this code is used for some other purpose in the future.

Quoted 1 line

I like your explanation here. Another option is that we could add a comment to notify the developer that it safe in this context?

Sure. Do you want to add it? Or should I send it?

Javier

#4 lore raw
Quoted 59 lines · Javier Gonzalez
Quoted 59 lines · Matias Bjørling
Quoted 54 lines · Javier Gonzalez
Quoted 48 lines · Hua Su

Add lock protection for list operations.

---
drivers/lightnvm/pblk-core.c | 17 ++++++++++-------

1 file changed, 10 insertions(+), 7 deletions(-)
drivers/lightnvm/pblk-core.c +10 -8
index 6944aac43b01..e490df217dac 100644
@@ -1286,24 +1286,27 @@ int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line)
    list_del(&line->list);
 
    ret = pblk_line_prepare(pblk, line);
-    if (ret) {
-        list_add(&line->list, &l_mg->free_list);
-        spin_unlock(&l_mg->free_lock);
-        return ret;
-    }
-    spin_unlock(&l_mg->free_lock);
+    if (ret)
+        goto out;
 
    ret = pblk_line_alloc_bitmaps(pblk, line);
    if (ret)
-        return ret;
+        goto out;
 
    if (!pblk_line_init_bb(pblk, line, 0)) {
        list_add(&line->list, &l_mg->free_list);
+        spin_unlock(&l_mg->free_lock);
        return -EINTR;
    }
+    spin_unlock(&l_mg->free_lock);
 
    pblk_rl_free_lines_dec(&pblk->rl, line, true);
    return 0;
+
+out:
+    list_add(&line->list, &l_mg->free_list);
+    spin_unlock(&l_mg->free_lock);
+    return ret;
 }
 
 void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line)
--
Quoted 1 line

2.19.1

This path is only touched by the recovery path, which is single threaded, so there is no race condition as is. Also, if recovery fails, pblk will not create the instance at all. This said, it would be good to protect the list_add on the pblk_line_init_bb() error path in case this code is used for some other purpose in the future.

Quoted 6 lines
Quoted 1 line

I like your explanation here. Another option is that we could add a comment to notify the developer that it safe in this context?

Sure. Do you want to add it? Or should I send it?

Javier

Please send it. Thanks!

A message that is not in the archive (sent off-list or not yet received): [email protected]

#6 lore raw
Quoted 4 lines · HUA SU

Thank you for your kind correction, I ignored some of the content, it will be perfected by Javier.

You are welcome to re-send the patch with the code I proposed. I'll move a couple of things around to better comment the single-threaded recovery path afterwards.

Javier