Skip to contentSkip to conversation

RAS/CEC: fix __find_elem

PATCH In review WANG Chao 3 participants 11 messages Apr 18, 2019 – Jun 8, 2019

Subsystems (2)
lore
Share & tools
lore

WANG Chao

author
#0 lore raw

PATCH 1/3 RAS/CEC: fix __find_elem

A left over pfn (because we don't clear) at ca->array[n] can be a match in __find_elem. Later it'd cause a memmove size overflow in del_elem.

---
 drivers/ras/cec.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/ras/cec.c +1 -1
index 2d9ec378a8bc..2e0bf1269c31 100644
@@ -206,7 +206,7 @@ static int __find_elem(struct ce_array *ca, u64 pfn, unsigned int *to)
 
 	this_pfn = PFN(ca->array[min]);
 
-	if (this_pfn == pfn)
+	if (this_pfn == pfn && ca->n > min)
 		return min;
 
 	return -ENOKEY;
author
#1 lore raw

PATCH 2/3 RAS/CEC: make ces_entered smp safe

ces_entered should be put in a critical section to avoid race condition.

---
 drivers/ras/cec.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/ras/cec.c +2 -2
index 2e0bf1269c31..702e4c02c713 100644
@@ -286,10 +286,10 @@ int cec_add_elem(u64 pfn)
 	if (!ce_arr.array || ce_arr.disabled)
 		return -ENODEV;
 
-	ca->ces_entered++;
-
 	mutex_lock(&ce_mutex);
 
+	ca->ces_entered++;
+
 	if (ca->n == MAX_ELEMS)
 		WARN_ON(!del_lru_elem_unlocked(ca));
 
#2 lore raw

[tip:ras/core] RAS/CEC: Increment cec_entered under the mutex lock

RAS/CEC: Increment cec_entered under the mutex lock
Commit-ID:  06e0fe2d8e9178bda874a75083bc13647fbf983f
Gitweb:     https://git.kernel.org/tip/06e0fe2d8e9178bda874a75083bc13647fbf983f
Author:     WANG Chao <chao.wang () ucloud ! cn>
AuthorDate: Thu, 18 Apr 2019 03:41:14 +0000
Committer:  Borislav Petkov <[email protected]>
CommitDate: Sat, 20 Apr 2019 12:13:13 +0200

RAS/CEC: Increment cec_entered under the mutex lock

Modify ->cec_entered in the critical section of the mutex.

---
 drivers/ras/cec.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/ras/cec.c +2 -2
index 2d9ec378a8bc..88e4f3ff0cb8 100644
@@ -286,10 +286,10 @@ int cec_add_elem(u64 pfn)
 	if (!ce_arr.array || ce_arr.disabled)
 		return -ENODEV;
 
-	ca->ces_entered++;
-
 	mutex_lock(&ce_mutex);
 
+	ca->ces_entered++;
+
 	if (ca->n == MAX_ELEMS)
 		WARN_ON(!del_lru_elem_unlocked(ca));
 
#3 lore raw

[tip:ras/core] RAS/CEC: Increment cec_entered under the mutex lock

In reply to WANG Chao
RAS/CEC: Increment cec_entered under the mutex lock
Commit-ID:  09cbd2197e9291d6a3d3f42873f06ca1f388c1a4
Gitweb:     https://git.kernel.org/tip/09cbd2197e9291d6a3d3f42873f06ca1f388c1a4
Author:     WANG Chao <[email protected]>
AuthorDate: Thu, 18 Apr 2019 03:41:14 +0000
Committer:  Borislav Petkov <[email protected]>
CommitDate: Sat, 20 Apr 2019 12:16:52 +0200

RAS/CEC: Increment cec_entered under the mutex lock

Modify ->cec_entered in the critical section of the mutex.

---
 drivers/ras/cec.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/ras/cec.c +2 -2
index 2d9ec378a8bc..88e4f3ff0cb8 100644
@@ -286,10 +286,10 @@ int cec_add_elem(u64 pfn)
 	if (!ce_arr.array || ce_arr.disabled)
 		return -ENODEV;
 
-	ca->ces_entered++;
-
 	mutex_lock(&ce_mutex);
 
+	ca->ces_entered++;
+
 	if (ca->n == MAX_ELEMS)
 		WARN_ON(!del_lru_elem_unlocked(ca));
 
author
#4 lore raw

PATCH 3/3 RAS/CEC: immediate soft-offline page when count_threshold == 1

In reply to WANG Chao

count_threshol == 1 isn't working as expected. CEC only does soft offline the second time the same pfn is hit by a correctable error.

---
 drivers/ras/cec.c | 36 +++++++++++++++++++++---------------
 1 file changed, 21 insertions(+), 15 deletions(-)
drivers/ras/cec.c +21 -15
index 702e4c02c713..ac879c45377c 100644
@@ -272,7 +272,22 @@ static u64 __maybe_unused del_lru_elem(void)
 	return pfn;
 }
 
+static void cec_valid_soft_offline(u64 pfn)
+{
+	if (!pfn_valid(pfn)) {
+		pr_warn("CEC: Invalid pfn: 0x%llx\n", pfn);
+	} else {
+		/* We have reached max count for this page, soft-offline it. */
+		pr_err("Soft-offlining pfn: 0x%llx\n", pfn);
+		memory_failure_queue(pfn, MF_SOFT_OFFLINE, &cec_chain);
+		ce_arr.pfns_poisoned++;
+	}
+}
 
+/*
+ * Return a >0 value to denote that we've reached the offlining
+ * threshold.
+ */
 int cec_add_elem(u64 pfn)
 {
 	struct ce_array *ca = &ce_arr;
@@ -295,6 +310,11 @@ int cec_add_elem(u64 pfn)
 
 	ret = find_elem(ca, pfn, &to);
 	if (ret < 0) {
+		if (count_threshold == 1) {
+			cec_valid_soft_offline(pfn);
+			ret = 1;
+			goto unlock;
+		}
 		/*
 		 * Shift range [to-end] to make room for one more element.
 		 */
@@ -320,23 +340,9 @@ int cec_add_elem(u64 pfn)
 
 		ret = 0;
 	} else {
-		u64 pfn = ca->array[to] >> PAGE_SHIFT;
-
-		if (!pfn_valid(pfn)) {
-			pr_warn("CEC: Invalid pfn: 0x%llx\n", pfn);
-		} else {
-			/* We have reached max count for this page, soft-offline it. */
-			pr_err("Soft-offlining pfn: 0x%llx\n", pfn);
-			memory_failure_queue(pfn, MF_SOFT_OFFLINE);
-			ca->pfns_poisoned++;
-		}
-
+		cec_valid_soft_offline(pfn);
 		del_elem(ca, to);
 
-		/*
-		 * Return a >0 value to denote that we've reached the offlining
-		 * threshold.
-		 */
 		ret = 1;
 
 		goto unlock;
maintainer
#5 lore raw

Re: [PATCH 3/3] RAS/CEC: immediate soft-offline page when count_threshold == 1

Quoted 2 lines · WANG Chao

count_threshol == 1 isn't working as expected. CEC only does soft offline the second time the same pfn is hit by a correctable error.

So this?

drivers/ras/cec.c +2 -7
index b3c377ddf340..750a427e1a73 100644
@@ -333,6 +333,7 @@ int cec_add_elem(u64 pfn)
 
 	mutex_lock(&ce_mutex);
 
+	/* Array full, free the LRU slot. */
 	if (ca->n == MAX_ELEMS)
 		WARN_ON(!del_lru_elem_unlocked(ca));
 
@@ -346,14 +347,9 @@ int cec_add_elem(u64 pfn)
 			(void *)&ca->array[to],
 			(ca->n - to) * sizeof(u64));
 
-		ca->array[to] = (pfn << PAGE_SHIFT) |
-				(DECAY_MASK << COUNT_BITS) | 1;
+		ca->array[to] = (pfn << PAGE_SHIFT) | 1;
 
 		ca->n++;
-
-		ret = 0;
-
-		goto decay;
 	}
 
 	count = COUNT(ca->array[to]);
@@ -386,7 +382,6 @@ int cec_add_elem(u64 pfn)
 		goto unlock;
 	}
 
-decay:
 	ca->decay_count++;
 
 	if (ca->decay_count >= CLEAN_ELEMS)
author
#6 lore raw

Re: [PATCH 3/3] RAS/CEC: immediate soft-offline page when count_threshold == 1

Quoted 36 lines · Borislav Petkov
Quoted 2 lines · WANG Chao

count_threshol == 1 isn't working as expected. CEC only does soft offline the second time the same pfn is hit by a correctable error.

So this?

---

drivers/ras/cec.c +2 -6
index b3c377ddf340..750a427e1a73 100644
@@ -333,6 +333,7 @@ int cec_add_elem(u64 pfn)
 
 	mutex_lock(&ce_mutex);
 
+	/* Array full, free the LRU slot. */
 	if (ca->n == MAX_ELEMS)
 		WARN_ON(!del_lru_elem_unlocked(ca));
 
@@ -346,14 +347,9 @@ int cec_add_elem(u64 pfn)
 			(void *)&ca->array[to],
 			(ca->n - to) * sizeof(u64));
 
-		ca->array[to] = (pfn << PAGE_SHIFT) |
-				(DECAY_MASK << COUNT_BITS) | 1;
+		ca->array[to] = (pfn << PAGE_SHIFT) | 1;
 
 		ca->n++;
-
-		ret = 0;
-
-		goto decay;
 	}
 
 	count = COUNT(ca->array[to]);

drivers/ras/cec.c int cec_add_elem(u64 pfn) · line 387

Quoted 7 lines
drivers/ras/cec.c +0 -1
@@ -386,7 +382,6 @@ int cec_add_elem(u64 pfn)
 		goto unlock;
 	}
 
-decay:
 	ca->decay_count++;
 
 	if (ca->decay_count >= CLEAN_ELEMS)

It looks good to me. Thanks for a better fix.

#8 lore raw

[tip:ras/core] RAS/CEC: Check count_threshold unconditionally

In reply to WANG Chao
RAS/CEC: Check count_threshold unconditionally
Commit-ID:  de0e0624d86ff9fc512dedb297f8978698abf21a
Gitweb:     https://git.kernel.org/tip/de0e0624d86ff9fc512dedb297f8978698abf21a
Author:     Borislav Petkov <[email protected]>
AuthorDate: Sat, 20 Apr 2019 14:06:37 +0200
Committer:  Borislav Petkov <[email protected]>
CommitDate: Sat, 8 Jun 2019 17:33:10 +0200

RAS/CEC: Check count_threshold unconditionally

The count_threshold should be checked unconditionally, after insertion too, so that a count_threshold value of 1 can cause an immediate offlining. I.e., offline the page on the *first* error encountered.

Add comments to make it clear what cec_add_elem() does, while at it.

---
 drivers/ras/cec.c | 27 ++++++++++-----------------
 1 file changed, 10 insertions(+), 17 deletions(-)
drivers/ras/cec.c +10 -17
index f5795adc5a6e..73a975c26f9f 100644
@@ -294,6 +294,7 @@ int cec_add_elem(u64 pfn)
 
 	ca->ces_entered++;
 
+	/* Array full, free the LRU slot. */
 	if (ca->n == MAX_ELEMS)
 		WARN_ON(!del_lru_elem_unlocked(ca));
 
@@ -306,24 +307,17 @@ int cec_add_elem(u64 pfn)
 			(void *)&ca->array[to],
 			(ca->n - to) * sizeof(u64));
 
-		ca->array[to] = (pfn << PAGE_SHIFT) |
-				(DECAY_MASK << COUNT_BITS) | 1;
-
+		ca->array[to] = pfn << PAGE_SHIFT;
 		ca->n++;
-
-		ret = 0;
-
-		goto decay;
 	}
 
-	count = COUNT(ca->array[to]);
-
-	if (count < count_threshold) {
-		ca->array[to] |= (DECAY_MASK << COUNT_BITS);
-		ca->array[to]++;
+	/* Add/refresh element generation and increment count */
+	ca->array[to] |= DECAY_MASK << COUNT_BITS;
+	ca->array[to]++;
 
-		ret = 0;
-	} else {
+	/* Check action threshold and soft-offline, if reached. */
+	count = COUNT(ca->array[to]);
+	if (count >= count_threshold) {
 		u64 pfn = ca->array[to] >> PAGE_SHIFT;
 
 		if (!pfn_valid(pfn)) {
@@ -338,15 +332,14 @@ int cec_add_elem(u64 pfn)
 		del_elem(ca, to);
 
 		/*
-		 * Return a >0 value to denote that we've reached the offlining
-		 * threshold.
+		 * Return a >0 value to callers, to denote that we've reached
+		 * the offlining threshold.
 		 */
 		ret = 1;
 
 		goto unlock;
 	}
 
-decay:
 	ca->decay_count++;
 
 	if (ca->decay_count >= CLEAN_ELEMS)
author
#9 lore raw
In reply to WANG Chao
Quoted 13 lines · WANG Chao

A left over pfn (because we don't clear) at ca->array[n] can be a match in __find_elem. Later it'd cause a memmove size overflow in del_elem.

---
 drivers/ras/cec.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/ras/cec.c +0 -0
index 2d9ec378a8bc..2e0bf1269c31 100644

drivers/ras/cec.c static int __find_elem(struct ce_array *ca, u64 pfn, unsigned int *to) · line 212

Quoted 8 lines
drivers/ras/cec.c +1 -1
@@ -206,7 +206,7 @@ static int __find_elem(struct ce_array *ca, u64 pfn, unsigned int *to)
 
 	this_pfn = PFN(ca->array[min]);
 
-	if (this_pfn == pfn)
+	if (this_pfn == pfn && ca->n > min)
 		return min;
 
 	return -ENOKEY;

Any thought on this one?

author
#10 lore raw
Quoted 17 lines · WANG Chao
Quoted 16 lines · WANG Chao

A left over pfn (because we don't clear) at ca->array[n] can be a match in __find_elem. Later it'd cause a memmove size overflow in del_elem.

---
 drivers/ras/cec.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/ras/cec.c +0 -0
index 2d9ec378a8bc..2e0bf1269c31 100644
@@ -206,7 +206,7 @@ static int __find_elem(struct ce_array *ca, u64 pfn, unsigned int *to)
 
 	this_pfn = PFN(ca->array[min]);
 
Quoted 7 lines

drivers/ras/cec.c static int __find_elem(struct ce_array *ca, u64 pfn, unsigned int *to) · line 212

Quoted 5 lines
drivers/ras/cec.c +1 -1
-	if (this_pfn == pfn)
+	if (this_pfn == pfn && ca->n > min)
 		return min;
 
 	return -ENOKEY;

Any thought on this one?

Aha, I see there's another fix queued. Thanks.