Skip to contentSkip to conversation

DAX: warn when kmem regions are truncated for memory block alignment.

PATCH Superseded Gregory Price 5 participants 6 messages Apr 2, 2025 – Apr 9, 2025

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 DAX: warn when kmem regions are truncated for memory block alignment.

Device capacity intended for use as system ram should be aligned to the archite-defined memory block size or that capacity will be silently truncated and capacity stranded.

As hotplug dax memory becomes more prevelant, the memory block size alignment becomes more important for platform and device vendors to pay attention to - so this truncation should not be silent.

This issue is particularly relevant for CXL Dynamic Capacity devices, whose capacity may arrive in spec-aligned but block-misaligned chunks.

---
 drivers/dax/kmem.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/dax/kmem.c +9 -1
index e97d47f42ee2..32fe3215e11e 100644
@@ -13,6 +13,7 @@
 #include <linux/mman.h>
 #include <linux/memory-tiers.h>
 #include <linux/memory_hotplug.h>
+#include <linux/string_helpers.h>
 #include "dax-private.h"
 #include "bus.h"
 
@@ -68,7 +69,7 @@ static void kmem_put_memory_types(void)
 static int dev_dax_kmem_probe(struct dev_dax *dev_dax)
 {
 	struct device *dev = &dev_dax->dev;
-	unsigned long total_len = 0;
+	unsigned long total_len = 0, orig_len = 0;
 	struct dax_kmem_data *data;
 	struct memory_dev_type *mtype;
 	int i, rc, mapped = 0;
@@ -97,6 +98,7 @@ static int dev_dax_kmem_probe(struct dev_dax *dev_dax)
 	for (i = 0; i < dev_dax->nr_range; i++) {
 		struct range range;
 
+		orig_len += range_len(&dev_dax->ranges[i].range);
 		rc = dax_kmem_range(dev_dax, i, &range);
 		if (rc) {
 			dev_info(dev, "mapping%d: %#llx-%#llx too small after alignment\n",
@@ -109,6 +111,12 @@ static int dev_dax_kmem_probe(struct dev_dax *dev_dax)
 	if (!total_len) {
 		dev_warn(dev, "rejecting DAX region without any memory after alignment\n");
 		return -EINVAL;
+	} else if (total_len != orig_len) {
+		char buf[16];
+
+		string_get_size((orig_len - total_len), 1, STRING_UNITS_2,
+				buf, sizeof(buf));
+		dev_warn(dev, "DAX region truncated by %s due to alignment\n", buf);
 	}
 
 	init_node_memory_type(numa_node, mtype);
Signature · 1 line
2.47.1
Acked-by on v2 1/1
#1 lore raw
Quoted 48 lines · Gregory Price

Device capacity intended for use as system ram should be aligned to the archite-defined memory block size or that capacity will be silently truncated and capacity stranded.

As hotplug dax memory becomes more prevelant, the memory block size alignment becomes more important for platform and device vendors to pay attention to - so this truncation should not be silent.

This issue is particularly relevant for CXL Dynamic Capacity devices, whose capacity may arrive in spec-aligned but block-misaligned chunks.

---
  drivers/dax/kmem.c | 10 +++++++++-
  1 file changed, 9 insertions(+), 1 deletion(-)
drivers/dax/kmem.c +3 -1
index e97d47f42ee2..32fe3215e11e 100644
@@ -13,6 +13,7 @@
  #include <linux/mman.h>
  #include <linux/memory-tiers.h>
  #include <linux/memory_hotplug.h>
+#include <linux/string_helpers.h>
  #include "dax-private.h"
  #include "bus.h"
 
@@ -68,7 +69,7 @@ static void kmem_put_memory_types(void)
  static int dev_dax_kmem_probe(struct dev_dax *dev_dax)
  {
  	struct device *dev = &dev_dax->dev;
-	unsigned long total_len = 0;
+	unsigned long total_len = 0, orig_len = 0;
  	struct dax_kmem_data *data;
  	struct memory_dev_type *mtype;
  	int i, rc, mapped = 0;
@@ -97,6 +98,7 @@ static int dev_dax_kmem_probe(struct dev_dax *dev_dax)
  	for (i = 0; i < dev_dax->nr_range; i++) {
  		struct range range;
 
+		orig_len += range_len(&dev_dax->ranges[i].range);
  		rc = dax_kmem_range(dev_dax, i, &range);
  		if (rc) {
  			dev_info(dev, "mapping%d: %#llx-%#llx too small after alignment\n",

drivers/dax/kmem.c static int dev_dax_kmem_probe(struct dev_dax *dev_dax) · line 119

Quoted 9 lines
drivers/dax/kmem.c +6 -0
@@ -109,6 +111,12 @@ static int dev_dax_kmem_probe(struct dev_dax *dev_dax)
  	if (!total_len) {
  		dev_warn(dev, "rejecting DAX region without any memory after alignment\n");
  		return -EINVAL;
+	} else if (total_len != orig_len) {
+		char buf[16];
+
+		string_get_size((orig_len - total_len), 1, STRING_UNITS_2,
+				buf, sizeof(buf));
+		dev_warn(dev, "DAX region truncated by %s due to alignment\n", buf);
Signature · 3 lines
Cheers,

David / dhildenb
Reviewed-by on v2 1/1
#2 lore raw
In reply to Gregory Price
Quoted 2 lines · On Tue, 1 Apr 2025 21:59:20 -0400 Gregory Price <[email protected]> wrote:

Device capacity intended for use as system ram should be aligned to the archite-defined memory block size or that capacity will be silently

archite?

Quoted 6 lines

truncated and capacity stranded.

As hotplug dax memory becomes more prevelant, the memory block size alignment becomes more important for platform and device vendors to pay attention to - so this truncation should not be silent.

Quoted 6 lines

This issue is particularly relevant for CXL Dynamic Capacity devices, whose capacity may arrive in spec-aligned but block-misaligned chunks.

One trivial comment inline otherwise seems reasonable to me.

Quoted 34 lines
---
 drivers/dax/kmem.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/dax/kmem.c +3 -1
index e97d47f42ee2..32fe3215e11e 100644
@@ -13,6 +13,7 @@
 #include <linux/mman.h>
 #include <linux/memory-tiers.h>
 #include <linux/memory_hotplug.h>
+#include <linux/string_helpers.h>
 #include "dax-private.h"
 #include "bus.h"
 
@@ -68,7 +69,7 @@ static void kmem_put_memory_types(void)
 static int dev_dax_kmem_probe(struct dev_dax *dev_dax)
 {
 	struct device *dev = &dev_dax->dev;
-	unsigned long total_len = 0;
+	unsigned long total_len = 0, orig_len = 0;
 	struct dax_kmem_data *data;
 	struct memory_dev_type *mtype;
 	int i, rc, mapped = 0;
@@ -97,6 +98,7 @@ static int dev_dax_kmem_probe(struct dev_dax *dev_dax)
 	for (i = 0; i < dev_dax->nr_range; i++) {
 		struct range range;
 
+		orig_len += range_len(&dev_dax->ranges[i].range);
 		rc = dax_kmem_range(dev_dax, i, &range);
 		if (rc) {
 			dev_info(dev, "mapping%d: %#llx-%#llx too small after alignment\n",

drivers/dax/kmem.c static int dev_dax_kmem_probe(struct dev_dax *dev_dax) · line 117

Quoted 7 lines
drivers/dax/kmem.c +4 -0
@@ -109,6 +111,12 @@ static int dev_dax_kmem_probe(struct dev_dax *dev_dax)
 	if (!total_len) {
 		dev_warn(dev, "rejecting DAX region without any memory after alignment\n");
 		return -EINVAL;
+	} else if (total_len != orig_len) {
+		char buf[16];
+
+		string_get_size((orig_len - total_len), 1, STRING_UNITS_2,

Trivial but do those inner brackets really add anything?

Quoted 5 lines
drivers/dax/kmem.c +2 -0
+				buf, sizeof(buf));
+		dev_warn(dev, "DAX region truncated by %s due to alignment\n", buf);
 	}
 
 	init_node_memory_type(numa_node, mtype);
maintainer Tested-by on v2 1/1
#3 lore raw
In reply to Gregory Price
Quoted 8 lines · Gregory Price

Device capacity intended for use as system ram should be aligned to the archite-defined memory block size or that capacity will be silently truncated and capacity stranded.

As hotplug dax memory becomes more prevelant, the memory block size alignment becomes more important for platform and device vendors to pay attention to - so this truncation should not be silent.

Quoted 6 lines

This issue is particularly relevant for CXL Dynamic Capacity devices, whose capacity may arrive in spec-aligned but block-misaligned chunks.

Existing unit test 'daxctl-devices.sh' hits this case:

[   52.547521] kmem dax3.0: DAX region truncated by 62.0 MiB due to alignment

snip

Quoted 1 line
#4 lore raw
Quoted 5 lines · Alison Schofield

Existing unit test 'daxctl-devices.sh' hits this case:

[   52.547521] kmem dax3.0: DAX region truncated by 62.0 MiB due to alignment

Thanks for testing, good to know there's an existing test for this

will respin w/ Jonathan's recommendations and tags.

~Gregory

Reviewed-by on v2 1/1
#5 lore raw
In reply to Gregory Price
Quoted 8 lines · Gregory Price

Device capacity intended for use as system ram should be aligned to the archite-defined memory block size or that capacity will be silently truncated and capacity stranded.

As hotplug dax memory becomes more prevelant, the memory block size alignment becomes more important for platform and device vendors to pay attention to - so this truncation should not be silent.

Quoted 6 lines

This issue is particularly relevant for CXL Dynamic Capacity devices, whose capacity may arrive in spec-aligned but block-misaligned chunks.

The typo in the changelog was already noted, and this addresses all my feedback so feel free to add: