Skip to contentSkip to conversation

Introduce per NUMA node memory error statistics

PATCH Applied Jiaqi Yan 3 participants 11 messages Jan 20, 2023 – Feb 4, 2023

Subsystems (5)
lore
Share & tools
lore

Jiaqi Yan

author
#0 lore raw

v2 cover Introduce per NUMA node memory error statistics

Background
==========
In the RFC for Kernel Support of Memory Error Detection [1], one advantage of software-based scanning over hardware patrol scrubber is the ability to make statistics visible to system administrators. The statistics include 2 categories:
* Memory error statistics, for example, how many memory error are encountered, how many of them are recovered by the kernel. Note these memory errors are non-fatal to kernel: during the machine check exception (MCE) handling kernel already classified MCE's severity to be unnecessary to panic (but either action required or optional).
* Scanner statistics, for example how many times the scanner have fully scanned a NUMA node, how many errors are first detected by the scanner.

The memory error statistics are useful to userspace and actually not specific to scanner detected memory errors, and are the focus of this patchset.

Motivation
==========
Memory error stats are important to userspace but insufficient in kernel today. Datacenter administrators can better monitor a machine's memory health with the visible stats. For example, while memory errors are inevitable on servers with 10+ TB memory, starting server maintenance when there are only 1~2 recovered memory errors could be overreacting; in cloud production environment maintenance usually means live migrate all the workload running on the server and this usually causes nontrivial disruption to the customer. Providing insight into the scope of memory errors on a system helps to determine the appropriate follow-up action. In addition, the kernel's existing memory error stats need to be standardized so that userspace can reliably count on their usefulness.

Today kernel provides following memory error info to userspace, but they are not sufficient or have disadvantages:
* HardwareCorrupted in /proc/meminfo: number of bytes poisoned in total, not per NUMA node stats though
* ras:memory_failure_event: only available after explicitly enabled
* /dev/mcelog provides many useful info about the MCEs, but doesn't capture how memory_failure recovered memory MCEs
* kernel logs: userspace needs to process log text

Exposing memory error stats is also a good start for the in-kernel memory error detector. Today the data source of memory error stats are either direct memory error consumption, or hardware patrol scrubber detection (either signaled as UCNA or SRAO). Once in-kernel memory scanner is implemented, it will be the main source as it is usually configured to scan memory DIMMs constantly and faster than hardware patrol scrubber.

How Implemented
===============
As Naoya pointed out [2], exposing memory error statistics to userspace is useful independent of software or hardware scanner. Therefore we implement the memory error statistics independent of the in-kernel memory error detector. It exposes the following per NUMA node memory error counters:

  /sys/devices/system/node/node${X}/memory_failure/total
  /sys/devices/system/node/node${X}/memory_failure/recovered
  /sys/devices/system/node/node${X}/memory_failure/ignored
  /sys/devices/system/node/node${X}/memory_failure/failed
  /sys/devices/system/node/node${X}/memory_failure/delayed

These counters describe how many raw pages are poisoned and after the attempted recoveries by the kernel, their resolutions: how many are recovered, ignored, failed, or delayed respectively. This approach can be easier to extend for future use cases than /proc/meminfo, trace event, and log. The following math holds for the statistics:
* total = recovered + ignored + failed + delayed
These memory error stats are reset during machine boot.

The 1st commit introduces these sysfs entries. The 2nd commit populates memory error stats every time memory_failure attempts memory error recovery. The 3rd commit adds documentations for introduced stats.

[1] https://lore.kernel.org/linux-mm/[email protected]/T/#mc22959244f5388891c523882e61163c6e4d703af
[2] https://lore.kernel.org/linux-mm/[email protected]/T/#m52d8d7a333d8536bd7ce74253298858b1c0c0ac6

Changelog

v2 changes:
- Incorporate feedbacks from Andrew Morton <[email protected]> and Horiguchi Naoya <[email protected]>.
- Correciton in cover letter: both UCNA and SRAO are handled by memory_failure().
- Rename pages_poisoned to total.
- Remove the "pages_" prefix from counter names.
- Correction in cover letter and commit message:
total * PAGE_SIZE * #nodes is not exactly equals to /proc/meminfo/HardwareCorrupted due to cases not accounted.

Jiaqi Yan (3):
mm: memory-failure: Add memory failure stats to sysfs
mm: memory-failure: Bump memory failure stats to pglist_data
mm: memory-failure: Document memory failure stats

 Documentation/ABI/stable/sysfs-devices-node | 39 +++++++++++
 drivers/base/node.c                         |  3 +
 include/linux/mm.h                          |  5 ++
 include/linux/mmzone.h                      | 28 ++++++++
 mm/memory-failure.c                         | 71 +++++++++++++++++++++
 5 files changed, 146 insertions(+)
Signature · 1 line
2.39.0.246.g2a6d74b583-goog
author
#1 lore raw

PATCH v2 1/3 mm: memory-failure: Add memory failure stats to sysfs

Today kernel provides following memory error info to userspace, but each has its own disadvantage
* HardwareCorrupted in /proc/meminfo: number of bytes poisoned in total, not per NUMA node stats though
* ras:memory_failure_event: only available after explicitly enabled
* /dev/mcelog provides many useful info about the MCEs, but doesn't capture how memory_failure recovered memory MCEs
* kernel logs: userspace needs to process log text

Exposes per NUMA node memory error stats as sysfs entries:

  /sys/devices/system/node/node${X}/memory_failure/total
  /sys/devices/system/node/node${X}/memory_failure/recovered
  /sys/devices/system/node/node${X}/memory_failure/ignored
  /sys/devices/system/node/node${X}/memory_failure/failed
  /sys/devices/system/node/node${X}/memory_failure/delayed

These counters describe how many raw pages are poisoned and after the attempted recoveries by the kernel, their resolutions: how many are recovered, ignored, failed, or delayed respectively. The following math holds for the statistics:
* total = recovered + ignored + failed + delayed

---
 drivers/base/node.c    |  3 +++
 include/linux/mm.h     |  5 +++++
 include/linux/mmzone.h | 28 ++++++++++++++++++++++++++++
 mm/memory-failure.c    | 35 +++++++++++++++++++++++++++++++++++
 4 files changed, 71 insertions(+)
drivers/base/node.c +3 -0
index faf3597a96da..b46db17124f3 100644
@@ -586,6 +586,9 @@ static const struct attribute_group *node_dev_groups[] = {
 	&node_dev_group,
 #ifdef CONFIG_HAVE_ARCH_NODE_DEV_GROUP
 	&arch_node_dev_group,
+#endif
+#ifdef CONFIG_MEMORY_FAILURE
+	&memory_failure_attr_group,
 #endif
 	NULL
 };
include/linux/mm.h +5 -0
index f3f196e4d66d..888576884eb9 100644
@@ -3521,6 +3521,11 @@ enum mf_action_page_type {
 	MF_MSG_UNKNOWN,
 };
 
+/*
+ * Sysfs entries for memory failure handling statistics.
+ */
+extern const struct attribute_group memory_failure_attr_group;
+
 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
 extern void clear_huge_page(struct page *page,
 			    unsigned long addr_hint,
include/linux/mmzone.h +28 -0
index cd28a100d9e4..2c537b31fa7b 100644
@@ -1110,6 +1110,31 @@ struct deferred_split {
 };
 #endif
 
+#ifdef CONFIG_MEMORY_FAILURE
+/*
+ * Per NUMA node memory failure handling statistics.
+ */
+struct memory_failure_stats {
+	/*
+	 * Number of raw pages poisoned.
+	 * Cases not accounted: memory outside kernel control, offline page,
+	 * arch-specific memory_failure (SGX), hwpoison_filter() filtered
+	 * error events, and unpoison actions from hwpoison_unpoison.
+	 */
+	unsigned long total;
+	/*
+	 * Recovery results of poisoned raw pages handled by memory_failure,
+	 * in sync with mf_result.
+	 * total = ignored + failed + delayed + recovered.
+	 * total * PAGE_SIZE * #nodes = /proc/meminfo/HardwareCorrupted.
+	 */
+	unsigned long ignored;
+	unsigned long failed;
+	unsigned long delayed;
+	unsigned long recovered;
+};
+#endif
+
 /*
  * On NUMA machines, each NUMA node would have a pg_data_t to describe
  * it's memory layout. On UMA machines there is a single pglist_data which
@@ -1253,6 +1278,9 @@ typedef struct pglist_data {
 #ifdef CONFIG_NUMA
 	struct memory_tier __rcu *memtier;
 #endif
+#ifdef CONFIG_MEMORY_FAILURE
+	struct memory_failure_stats mf_stats;
+#endif
 } pg_data_t;
 
 #define node_present_pages(nid)	(NODE_DATA(nid)->node_present_pages)
mm/memory-failure.c +35 -0
index c77a9e37e27e..c628f1db3a4d 100644
@@ -87,6 +87,41 @@ inline void num_poisoned_pages_sub(unsigned long pfn, long i)
 		memblk_nr_poison_sub(pfn, i);
 }
 
+/**
+ * MF_ATTR_RO - Create sysfs entry for each memory failure statistics.
+ * @_name: name of the file in the per NUMA sysfs directory.
+ */
+#define MF_ATTR_RO(_name)					\
+static ssize_t _name##_show(struct device *dev,			\
+			    struct device_attribute *attr,	\
+			    char *buf)				\
+{								\
+	struct memory_failure_stats *mf_stats =			\
+		&NODE_DATA(dev->id)->mf_stats;			\
+	return sprintf(buf, "%lu\n", mf_stats->_name);		\
+}								\
+static DEVICE_ATTR_RO(_name)
+
+MF_ATTR_RO(total);
+MF_ATTR_RO(ignored);
+MF_ATTR_RO(failed);
+MF_ATTR_RO(delayed);
+MF_ATTR_RO(recovered);
+
+static struct attribute *memory_failure_attr[] = {
+	&dev_attr_total.attr,
+	&dev_attr_ignored.attr,
+	&dev_attr_failed.attr,
+	&dev_attr_delayed.attr,
+	&dev_attr_recovered.attr,
+	NULL,
+};
+
+const struct attribute_group memory_failure_attr_group = {
+	.name = "memory_failure",
+	.attrs = memory_failure_attr,
+};
+
 /*
  * Return values:
  *   1:   the page is dissolved (if needed) and taken off from buddy,
Signature · 1 line
2.39.0.246.g2a6d74b583-goog
#2 lore raw

Re: [PATCH v2 1/3] mm: memory-failure: Add memory failure stats to sysfs

Quoted 17 lines · Jiaqi Yan

Today kernel provides following memory error info to userspace, but each has its own disadvantage
* HardwareCorrupted in /proc/meminfo: number of bytes poisoned in total, not per NUMA node stats though
* ras:memory_failure_event: only available after explicitly enabled
* /dev/mcelog provides many useful info about the MCEs, but doesn't capture how memory_failure recovered memory MCEs
* kernel logs: userspace needs to process log text

Exposes per NUMA node memory error stats as sysfs entries:

  /sys/devices/system/node/node${X}/memory_failure/total
  /sys/devices/system/node/node${X}/memory_failure/recovered
  /sys/devices/system/node/node${X}/memory_failure/ignored
  /sys/devices/system/node/node${X}/memory_failure/failed
  /sys/devices/system/node/node${X}/memory_failure/delayed
Quoted 8 lines

These counters describe how many raw pages are poisoned and after the attempted recoveries by the kernel, their resolutions: how many are recovered, ignored, failed, or delayed respectively. The following math holds for the statistics:
* total = recovered + ignored + failed + delayed

Looks good to me, thank you.

#3 lore raw

Re: [PATCH v2 1/3] mm: memory-failure: Add memory failure stats to sysfs

In reply to Jiaqi Yan
Quoted 51 lines · Jiaqi Yan

Today kernel provides following memory error info to userspace, but each has its own disadvantage
* HardwareCorrupted in /proc/meminfo: number of bytes poisoned in total, not per NUMA node stats though
* ras:memory_failure_event: only available after explicitly enabled
* /dev/mcelog provides many useful info about the MCEs, but doesn't capture how memory_failure recovered memory MCEs
* kernel logs: userspace needs to process log text

Exposes per NUMA node memory error stats as sysfs entries:

   /sys/devices/system/node/node${X}/memory_failure/total
   /sys/devices/system/node/node${X}/memory_failure/recovered
   /sys/devices/system/node/node${X}/memory_failure/ignored
   /sys/devices/system/node/node${X}/memory_failure/failed
   /sys/devices/system/node/node${X}/memory_failure/delayed

These counters describe how many raw pages are poisoned and after the attempted recoveries by the kernel, their resolutions: how many are recovered, ignored, failed, or delayed respectively. The following math holds for the statistics:
* total = recovered + ignored + failed + delayed

---
  drivers/base/node.c    |  3 +++
  include/linux/mm.h     |  5 +++++
  include/linux/mmzone.h | 28 ++++++++++++++++++++++++++++
  mm/memory-failure.c    | 35 +++++++++++++++++++++++++++++++++++
  4 files changed, 71 insertions(+)
drivers/base/node.c +3 -0
index faf3597a96da..b46db17124f3 100644
@@ -586,6 +586,9 @@ static const struct attribute_group *node_dev_groups[] = {
  	&node_dev_group,
  #ifdef CONFIG_HAVE_ARCH_NODE_DEV_GROUP
  	&arch_node_dev_group,
+#endif
+#ifdef CONFIG_MEMORY_FAILURE
+	&memory_failure_attr_group,
  #endif
  	NULL
  };
include/linux/mm.h +0 -0
index f3f196e4d66d..888576884eb9 100644

include/linux/mm.h enum mf_action_page_type { · line 3528

Quoted 8 lines
include/linux/mm.h +5 -0
@@ -3521,6 +3521,11 @@ enum mf_action_page_type {
  	MF_MSG_UNKNOWN,
  };
 
+/*
+ * Sysfs entries for memory failure handling statistics.
+ */
+extern const struct attribute_group memory_failure_attr_group;
+

This should move under CONFIG_MEMORY_FAILURE

Quoted 95 lines
include/linux/mm.h +0 -0
  #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
  extern void clear_huge_page(struct page *page,
  			    unsigned long addr_hint,
include/linux/mmzone.h +28 -0
index cd28a100d9e4..2c537b31fa7b 100644
@@ -1110,6 +1110,31 @@ struct deferred_split {
  };
  #endif
 
+#ifdef CONFIG_MEMORY_FAILURE
+/*
+ * Per NUMA node memory failure handling statistics.
+ */
+struct memory_failure_stats {
+	/*
+	 * Number of raw pages poisoned.
+	 * Cases not accounted: memory outside kernel control, offline page,
+	 * arch-specific memory_failure (SGX), hwpoison_filter() filtered
+	 * error events, and unpoison actions from hwpoison_unpoison.
+	 */
+	unsigned long total;
+	/*
+	 * Recovery results of poisoned raw pages handled by memory_failure,
+	 * in sync with mf_result.
+	 * total = ignored + failed + delayed + recovered.
+	 * total * PAGE_SIZE * #nodes = /proc/meminfo/HardwareCorrupted.
+	 */
+	unsigned long ignored;
+	unsigned long failed;
+	unsigned long delayed;
+	unsigned long recovered;
+};
+#endif
+
  /*
   * On NUMA machines, each NUMA node would have a pg_data_t to describe
   * it's memory layout. On UMA machines there is a single pglist_data which
@@ -1253,6 +1278,9 @@ typedef struct pglist_data {
  #ifdef CONFIG_NUMA
  	struct memory_tier __rcu *memtier;
  #endif
+#ifdef CONFIG_MEMORY_FAILURE
+	struct memory_failure_stats mf_stats;
+#endif
  } pg_data_t;
 
  #define node_present_pages(nid)	(NODE_DATA(nid)->node_present_pages)
mm/memory-failure.c +35 -0
index c77a9e37e27e..c628f1db3a4d 100644
@@ -87,6 +87,41 @@ inline void num_poisoned_pages_sub(unsigned long pfn, long i)
  		memblk_nr_poison_sub(pfn, i);
  }
 
+/**
+ * MF_ATTR_RO - Create sysfs entry for each memory failure statistics.
+ * @_name: name of the file in the per NUMA sysfs directory.
+ */
+#define MF_ATTR_RO(_name)					\
+static ssize_t _name##_show(struct device *dev,			\
+			    struct device_attribute *attr,	\
+			    char *buf)				\
+{								\
+	struct memory_failure_stats *mf_stats =			\
+		&NODE_DATA(dev->id)->mf_stats;			\
+	return sprintf(buf, "%lu\n", mf_stats->_name);		\
+}								\
+static DEVICE_ATTR_RO(_name)
+
+MF_ATTR_RO(total);
+MF_ATTR_RO(ignored);
+MF_ATTR_RO(failed);
+MF_ATTR_RO(delayed);
+MF_ATTR_RO(recovered);
+
+static struct attribute *memory_failure_attr[] = {
+	&dev_attr_total.attr,
+	&dev_attr_ignored.attr,
+	&dev_attr_failed.attr,
+	&dev_attr_delayed.attr,
+	&dev_attr_recovered.attr,
+	NULL,
+};
+
+const struct attribute_group memory_failure_attr_group = {
+	.name = "memory_failure",
+	.attrs = memory_failure_attr,
+};
+
  /*
   * Return values:
   *   1:   the page is dissolved (if needed) and taken off from buddy,
author
#4 lore raw

Re: [PATCH v2 1/3] mm: memory-failure: Add memory failure stats to sysfs

Quoted 58 lines · Kefeng Wang
Quoted 54 lines · Jiaqi Yan

Today kernel provides following memory error info to userspace, but each has its own disadvantage
* HardwareCorrupted in /proc/meminfo: number of bytes poisoned in total, not per NUMA node stats though
* ras:memory_failure_event: only available after explicitly enabled
* /dev/mcelog provides many useful info about the MCEs, but doesn't capture how memory_failure recovered memory MCEs
* kernel logs: userspace needs to process log text

Exposes per NUMA node memory error stats as sysfs entries:

   /sys/devices/system/node/node${X}/memory_failure/total
   /sys/devices/system/node/node${X}/memory_failure/recovered
   /sys/devices/system/node/node${X}/memory_failure/ignored
   /sys/devices/system/node/node${X}/memory_failure/failed
   /sys/devices/system/node/node${X}/memory_failure/delayed

These counters describe how many raw pages are poisoned and after the attempted recoveries by the kernel, their resolutions: how many are recovered, ignored, failed, or delayed respectively. The following math holds for the statistics:
* total = recovered + ignored + failed + delayed

---
  drivers/base/node.c    |  3 +++
  include/linux/mm.h     |  5 +++++
  include/linux/mmzone.h | 28 ++++++++++++++++++++++++++++
  mm/memory-failure.c    | 35 +++++++++++++++++++++++++++++++++++
  4 files changed, 71 insertions(+)
drivers/base/node.c +3 -0
index faf3597a96da..b46db17124f3 100644
@@ -586,6 +586,9 @@ static const struct attribute_group *node_dev_groups[] = {
      &node_dev_group,
  #ifdef CONFIG_HAVE_ARCH_NODE_DEV_GROUP
      &arch_node_dev_group,
+#endif
+#ifdef CONFIG_MEMORY_FAILURE
+     &memory_failure_attr_group,
  #endif
      NULL
  };
include/linux/mm.h +0 -0
index f3f196e4d66d..888576884eb9 100644
@@ -3521,6 +3521,11 @@ enum mf_action_page_type {
      MF_MSG_UNKNOWN,
  };
 
Quoted 7 lines

include/linux/mm.h enum mf_action_page_type { · line 3528

Quoted 5 lines
include/linux/mm.h +5 -0
+/*
+ * Sysfs entries for memory failure handling statistics.
+ */
+extern const struct attribute_group memory_failure_attr_group;
+

This should move under CONFIG_MEMORY_FAILURE

Thanks! I will move it around in the new version.

Quoted 96 lines
Quoted 96 lines
include/linux/mm.h +0 -0
  #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
  extern void clear_huge_page(struct page *page,
                          unsigned long addr_hint,
include/linux/mmzone.h +28 -0
index cd28a100d9e4..2c537b31fa7b 100644
@@ -1110,6 +1110,31 @@ struct deferred_split {
  };
  #endif
 
+#ifdef CONFIG_MEMORY_FAILURE
+/*
+ * Per NUMA node memory failure handling statistics.
+ */
+struct memory_failure_stats {
+     /*
+      * Number of raw pages poisoned.
+      * Cases not accounted: memory outside kernel control, offline page,
+      * arch-specific memory_failure (SGX), hwpoison_filter() filtered
+      * error events, and unpoison actions from hwpoison_unpoison.
+      */
+     unsigned long total;
+     /*
+      * Recovery results of poisoned raw pages handled by memory_failure,
+      * in sync with mf_result.
+      * total = ignored + failed + delayed + recovered.
+      * total * PAGE_SIZE * #nodes = /proc/meminfo/HardwareCorrupted.
+      */
+     unsigned long ignored;
+     unsigned long failed;
+     unsigned long delayed;
+     unsigned long recovered;
+};
+#endif
+
  /*
   * On NUMA machines, each NUMA node would have a pg_data_t to describe
   * it's memory layout. On UMA machines there is a single pglist_data which
@@ -1253,6 +1278,9 @@ typedef struct pglist_data {
  #ifdef CONFIG_NUMA
      struct memory_tier __rcu *memtier;
  #endif
+#ifdef CONFIG_MEMORY_FAILURE
+     struct memory_failure_stats mf_stats;
+#endif
  } pg_data_t;
 
  #define node_present_pages(nid)     (NODE_DATA(nid)->node_present_pages)
mm/memory-failure.c +35 -0
index c77a9e37e27e..c628f1db3a4d 100644
@@ -87,6 +87,41 @@ inline void num_poisoned_pages_sub(unsigned long pfn, long i)
              memblk_nr_poison_sub(pfn, i);
  }
 
+/**
+ * MF_ATTR_RO - Create sysfs entry for each memory failure statistics.
+ * @_name: name of the file in the per NUMA sysfs directory.
+ */
+#define MF_ATTR_RO(_name)                                    \
+static ssize_t _name##_show(struct device *dev,                      \
+                         struct device_attribute *attr,      \
+                         char *buf)                          \
+{                                                            \
+     struct memory_failure_stats *mf_stats =                 \
+             &NODE_DATA(dev->id)->mf_stats;                  \
+     return sprintf(buf, "%lu\n", mf_stats->_name);          \
+}                                                            \
+static DEVICE_ATTR_RO(_name)
+
+MF_ATTR_RO(total);
+MF_ATTR_RO(ignored);
+MF_ATTR_RO(failed);
+MF_ATTR_RO(delayed);
+MF_ATTR_RO(recovered);
+
+static struct attribute *memory_failure_attr[] = {
+     &dev_attr_total.attr,
+     &dev_attr_ignored.attr,
+     &dev_attr_failed.attr,
+     &dev_attr_delayed.attr,
+     &dev_attr_recovered.attr,
+     NULL,
+};
+
+const struct attribute_group memory_failure_attr_group = {
+     .name = "memory_failure",
+     .attrs = memory_failure_attr,
+};
+
  /*
   * Return values:
   *   1:   the page is dissolved (if needed) and taken off from buddy,
author
#5 lore raw

PATCH v2 2/3 mm: memory-failure: Bump memory failure stats to pglist_data

In reply to Jiaqi Yan

Right before memory_failure finishes its handling, accumulate poisoned page's resolution counters to pglist_data's memory_failure_stats, so as to update the corresponding sysfs entries.

Tested:
1) Start an application to allocate memory buffer chunks
2) Convert random memory buffer addresses to physical addresses
3) Inject memory errors using EINJ at chosen physical addresses
4) Access poisoned memory buffer and recover from SIGBUS
5) Check counter values under /sys/devices/system/node/node*/memory_failure/*

---
 mm/memory-failure.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
mm/memory-failure.c +36 -0
index c628f1db3a4d..f4990839ea66 100644
@@ -1227,6 +1227,39 @@ static struct page_state error_states[] = {
 #undef slab
 #undef reserved
 
+static void update_per_node_mf_stats(unsigned long pfn,
+				     enum mf_result result)
+{
+	int nid = MAX_NUMNODES;
+	struct memory_failure_stats *mf_stats = NULL;
+
+	nid = pfn_to_nid(pfn);
+	if (unlikely(nid < 0 || nid >= MAX_NUMNODES)) {
+		WARN_ONCE(1, "Memory failure: pfn=%#lx, invalid nid=%d", pfn, nid);
+		return;
+	}
+
+	mf_stats = &NODE_DATA(nid)->mf_stats;
+	switch (result) {
+	case MF_IGNORED:
+		++mf_stats->ignored;
+		break;
+	case MF_FAILED:
+		++mf_stats->failed;
+		break;
+	case MF_DELAYED:
+		++mf_stats->delayed;
+		break;
+	case MF_RECOVERED:
+		++mf_stats->recovered;
+		break;
+	default:
+		WARN_ONCE(1, "Memory failure: mf_result=%d is not properly handled", result);
+		break;
+	}
+	++mf_stats->total;
+}
+
 /*
  * "Dirty/Clean" indication is not 100% accurate due to the possibility of
  * setting PG_dirty outside page lock. See also comment above set_page_dirty().
@@ -1237,6 +1270,9 @@ static int action_result(unsigned long pfn, enum mf_action_page_type type,
 	trace_memory_failure_event(pfn, type, result);
 
 	num_poisoned_pages_inc(pfn);
+
+	update_per_node_mf_stats(pfn, result);
+
 	pr_err("%#lx: recovery action for %s: %s\n",
 		pfn, action_page_types[type], action_name[result]);
 
Signature · 1 line
2.39.0.246.g2a6d74b583-goog
#6 lore raw

Re: [PATCH v2 2/3] mm: memory-failure: Bump memory failure stats to pglist_data

Quoted 12 lines · Jiaqi Yan

Right before memory_failure finishes its handling, accumulate poisoned page's resolution counters to pglist_data's memory_failure_stats, so as to update the corresponding sysfs entries.

Tested:
1) Start an application to allocate memory buffer chunks
2) Convert random memory buffer addresses to physical addresses
3) Inject memory errors using EINJ at chosen physical addresses
4) Access poisoned memory buffer and recover from SIGBUS
5) Check counter values under /sys/devices/system/node/node*/memory_failure/*

Quoted 2 lines
#7 lore raw

Re: [PATCH v2 2/3] mm: memory-failure: Bump memory failure stats to pglist_data

In reply to Jiaqi Yan
Quoted 38 lines · Jiaqi Yan

Right before memory_failure finishes its handling, accumulate poisoned page's resolution counters to pglist_data's memory_failure_stats, so as to update the corresponding sysfs entries.

Tested:
1) Start an application to allocate memory buffer chunks
2) Convert random memory buffer addresses to physical addresses
3) Inject memory errors using EINJ at chosen physical addresses
4) Access poisoned memory buffer and recover from SIGBUS
5) Check counter values under /sys/devices/system/node/node*/memory_failure/*

---
  mm/memory-failure.c | 36 ++++++++++++++++++++++++++++++++++++
  1 file changed, 36 insertions(+)
mm/memory-failure.c +12 -0
index c628f1db3a4d..f4990839ea66 100644
@@ -1227,6 +1227,39 @@ static struct page_state error_states[] = {
  #undef slab
  #undef reserved
 
+static void update_per_node_mf_stats(unsigned long pfn,
+				     enum mf_result result)
+{
+	int nid = MAX_NUMNODES;
+	struct memory_failure_stats *mf_stats = NULL;
+
+	nid = pfn_to_nid(pfn);
+	if (unlikely(nid < 0 || nid >= MAX_NUMNODES)) {
+		WARN_ONCE(1, "Memory failure: pfn=%#lx, invalid nid=%d", pfn, nid);
+		return;
+	}
+

...

mm/memory-failure.c static struct page_state error_states[] = { · line 1259

Quoted 4 lines
mm/memory-failure.c +4 -0
+	default:
+		WARN_ONCE(1, "Memory failure: mf_result=%d is not properly handled", result);
+		break;
+	}

We already define pr_fmt, the "Memory failure:" prefix should be dropped.

author
#8 lore raw

Re: [PATCH v2 2/3] mm: memory-failure: Bump memory failure stats to pglist_data

Quoted 43 lines · Kefeng Wang
Quoted 38 lines · Jiaqi Yan

Right before memory_failure finishes its handling, accumulate poisoned page's resolution counters to pglist_data's memory_failure_stats, so as to update the corresponding sysfs entries.

Tested:
1) Start an application to allocate memory buffer chunks
2) Convert random memory buffer addresses to physical addresses
3) Inject memory errors using EINJ at chosen physical addresses
4) Access poisoned memory buffer and recover from SIGBUS
5) Check counter values under /sys/devices/system/node/node*/memory_failure/*

---
  mm/memory-failure.c | 36 ++++++++++++++++++++++++++++++++++++
  1 file changed, 36 insertions(+)
mm/memory-failure.c +12 -0
index c628f1db3a4d..f4990839ea66 100644
@@ -1227,6 +1227,39 @@ static struct page_state error_states[] = {
  #undef slab
  #undef reserved
 
+static void update_per_node_mf_stats(unsigned long pfn,
+                                  enum mf_result result)
+{
+     int nid = MAX_NUMNODES;
+     struct memory_failure_stats *mf_stats = NULL;
+
+     nid = pfn_to_nid(pfn);
+     if (unlikely(nid < 0 || nid >= MAX_NUMNODES)) {
+             WARN_ONCE(1, "Memory failure: pfn=%#lx, invalid nid=%d", pfn, nid);
+             return;
+     }
+

...

Quoted 6 lines

mm/memory-failure.c static struct page_state error_states[] = { · line 1259

Quoted 4 lines
mm/memory-failure.c +4 -0
+     default:
+             WARN_ONCE(1, "Memory failure: mf_result=%d is not properly handled", result);
+             break;
+     }

We already define pr_fmt, the "Memory failure:" prefix should be dropped.

"Should be dropped" because it will print duplicated prefixes? Does WARN_ONCE also automatically include pr_fmt? I don't think that's the case when I read __warn_printk.

This is what I saw from dmesg when I add a `WARN_ONCE(1, "Memory
failure: pfn=%#lx\n", pfn);`
at the beginning of update_per_node_mf_stats (above nid=pfn_to_nid(pfn)):

[  523.942688] ------------[ cut here ]------------
[  523.972026] Memory failure: pfn=0x309f8f3
[  523.972038] WARNING: CPU: 4 PID: 21119 at mm/memory-failure.c:1236
action_result+0xec/0x150
[  523.972044] Modules linked in: einj vfat fat i2c_mux_pca954x
i2c_mux spidev cdc_acm xhci_pci xhci_hcd sha3_generic gq(O)
[  523.972054] CPU: 4 PID: 21119 Comm: usemem Tainted: G S M       O
    6.2.0-smp-DEV #1
[  523.972059] RIP: 0010:action_result+0xec/0x150

No duplicated "Memory failure:".

But I realize I should probably add "\n" within WARN_ONCE.

author
#9 lore raw

PATCH v2 3/3 mm: memory-failure: Document memory failure stats

In reply to Jiaqi Yan

Add documentation for memory_failure's per NUMA node sysfs entries

---
 Documentation/ABI/stable/sysfs-devices-node | 39 +++++++++++++++++++++
 1 file changed, 39 insertions(+)
Documentation/ABI/stable/sysfs-devices-node +39 -0
index 8db67aa472f1..402af4b2b905 100644
@@ -182,3 +182,42 @@ Date:		November 2021
 Contact:	Jarkko Sakkinen <[email protected]>
 Description:
 		The total amount of SGX physical memory in bytes.
+
+What:		/sys/devices/system/node/nodeX/memory_failure/total
+Date:		January 2023
+Contact:	Jiaqi Yan <[email protected]>
+Description:
+		The total number of raw poisoned pages (pages containing
+		corrupted data due to memory errors) on a NUMA node.
+
+What:		/sys/devices/system/node/nodeX/memory_failure/ignored
+Date:		January 2023
+Contact:	Jiaqi Yan <[email protected]>
+Description:
+		Of the raw poisoned pages on a NUMA node, how many pages are
+		ignored by memory error recovery attempt, usually because
+		support for this type of pages is unavailable, and kernel
+		gives up the recovery.
+
+What:		/sys/devices/system/node/nodeX/memory_failure/failed
+Date:		January 2023
+Contact:	Jiaqi Yan <[email protected]>
+Description:
+		Of the raw poisoned pages on a NUMA node, how many pages are
+		failed by memory error recovery attempt. This usually means
+		a key recovery operation failed.
+
+What:		/sys/devices/system/node/nodeX/memory_failure/delayed
+Date:		January 2023
+Contact:	Jiaqi Yan <[email protected]>
+Description:
+		Of the raw poisoned pages on a NUMA node, how many pages are
+		delayed by memory error recovery attempt. Delayed poisoned
+		pages usually will be retried by kernel.
+
+What:		/sys/devices/system/node/nodeX/memory_failure/recovered
+Date:		January 2023
+Contact:	Jiaqi Yan <[email protected]>
+Description:
+		Of the raw poisoned pages on a NUMA node, how many pages are
+		recovered by memory error recovery attempt.
Signature · 1 line
2.39.0.246.g2a6d74b583-goog