Skip to contentSkip to conversation

mm, kmemleak: Little optimization while scanning

PATCH In review Oscar Salvador 3 participants 6 messages Dec 6, 2018 – Dec 7, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm, kmemleak: Little optimization while scanning

kmemleak_scan() goes through all online nodes and tries
to scan all used pages.
We can do better and use pfn_to_online_page(), so in case we have CONFIG_MEMORY_HOTPLUG, offlined pages will be skiped automatically. For boxes where CONFIG_MEMORY_HOTPLUG is not present, pfn_to_online_page() will fallback to pfn_valid().

Another little optimization is to check if the page belongs to the node we are currently checking, so in case we have nodes interleaved we will not check the same pfn multiple times.

I ran some tests:

Add some memory to node1 and node2 making it interleaved:

(qemu) object_add memory-backend-ram,id=ram0,size=1G (qemu) device_add pc-dimm,id=dimm0,memdev=ram0,node=1 (qemu) object_add memory-backend-ram,id=ram1,size=1G (qemu) device_add pc-dimm,id=dimm1,memdev=ram1,node=2 (qemu) object_add memory-backend-ram,id=ram2,size=1G (qemu) device_add pc-dimm,id=dimm2,memdev=ram2,node=1

Then, we offline that memory:

 # for i in {32..39} ; do echo "offline" > /sys/devices/system/node/node1/memory$i/state;done
 # for i in {48..55} ; do echo "offline" > /sys/devices/system/node/node1/memory$i/state;don
 # for i in {40..47} ; do echo "offline" > /sys/devices/system/node/node2/memory$i/state;done

And we run kmemleak_scan:

 # echo "scan" > /sys/kernel/debug/kmemleak

before the patch:

kmemleak: time spend: 41596 us

after the patch:

kmemleak: time spend: 34899 us

---
 mm/kmemleak.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
mm/kmemleak.c +7 -3
index 877de4fa0720..5ce1e6a46d77 100644
@@ -113,6 +113,7 @@
 #include <linux/kmemleak.h>
 #include <linux/memory_hotplug.h>
 
+
 /*
  * Kmemleak configuration and common defines.
  */
@@ -1547,11 +1548,14 @@ static void kmemleak_scan(void)
 		unsigned long pfn;
 
 		for (pfn = start_pfn; pfn < end_pfn; pfn++) {
-			struct page *page;
+			struct page *page = pfn_to_online_page(pfn);
+
+			if (!page)
+				continue;
 
-			if (!pfn_valid(pfn))
+			/* only scan pages belonging to this node */
+			if (page_to_nid(page) != i)
 				continue;
-			page = pfn_to_page(pfn);
 			/* only scan if page is in use */
 			if (page_count(page) == 0)
 				continue;
Signature · 1 line
2.13.7
#1 lore raw
Quoted 49 lines · Oscar Salvador

kmemleak_scan() goes through all online nodes and tries
to scan all used pages.
We can do better and use pfn_to_online_page(), so in case we have CONFIG_MEMORY_HOTPLUG, offlined pages will be skiped automatically. For boxes where CONFIG_MEMORY_HOTPLUG is not present, pfn_to_online_page() will fallback to pfn_valid().

Another little optimization is to check if the page belongs to the node we are currently checking, so in case we have nodes interleaved we will not check the same pfn multiple times.

I ran some tests:

Add some memory to node1 and node2 making it interleaved:

(qemu) object_add memory-backend-ram,id=ram0,size=1G (qemu) device_add pc-dimm,id=dimm0,memdev=ram0,node=1 (qemu) object_add memory-backend-ram,id=ram1,size=1G (qemu) device_add pc-dimm,id=dimm1,memdev=ram1,node=2 (qemu) object_add memory-backend-ram,id=ram2,size=1G (qemu) device_add pc-dimm,id=dimm2,memdev=ram2,node=1

Then, we offline that memory:

# for i in {32..39} ; do echo "offline" > /sys/devices/system/node/node1/memory$i/state;done
# for i in {48..55} ; do echo "offline" > /sys/devices/system/node/node1/memory$i/state;don
# for i in {40..47} ; do echo "offline" > /sys/devices/system/node/node2/memory$i/state;done

And we run kmemleak_scan:

# echo "scan" > /sys/kernel/debug/kmemleak

before the patch:

kmemleak: time spend: 41596 us

after the patch:

kmemleak: time spend: 34899 us

---
mm/kmemleak.c | 10 +++++++---

1 file changed, 7 insertions(+), 3 deletions(-)
mm/kmemleak.c +0 -0
index 877de4fa0720..5ce1e6a46d77 100644

mm/kmemleak.c line 116

Quoted 4 lines
mm/kmemleak.c +1 -0
@@ -113,6 +113,7 @@
 #include <linux/kmemleak.h>
 #include <linux/memory_hotplug.h>
 
+

This one maybe not necessary.

Quoted 7 lines
mm/kmemleak.c +0 -0
 /*
 * Kmemleak configuration and common defines.
 */
@@ -1547,11 +1548,14 @@ static void kmemleak_scan(void)
 		unsigned long pfn;
 
 		for (pfn = start_pfn; pfn < end_pfn; pfn++) {

mm/kmemleak.c static void kmemleak_scan(void) · line 1558

Quoted 10 lines
mm/kmemleak.c +6 -2
-			struct page *page;
+			struct page *page = pfn_to_online_page(pfn);
+
+			if (!page)
+				continue;
 
-			if (!pfn_valid(pfn))
+			/* only scan pages belonging to this node */
+			if (page_to_nid(page) != i)
 				continue;

Not farmiliar with this situation. Is this often?

Quoted 6 lines
mm/kmemleak.c +0 -1
-			page = pfn_to_page(pfn);
 			/* only scan if page is in use */
 			if (page_count(page) == 0)
 				continue;
Signature · 1 line
2.13.7
Signature · 2 lines
Wei Yang
Help you, Help me
#4 lore raw
In reply to Oscar Salvador

I just realized I forgot to add that this was suggested by Michal. Sorry, I was a but rushy.

Quoted 39 lines · Oscar Salvador

kmemleak_scan() goes through all online nodes and tries to scan all used pages.
We can do better and use pfn_to_online_page(), so in case we have CONFIG_MEMORY_HOTPLUG, offlined pages will be skiped automatically. For boxes where CONFIG_MEMORY_HOTPLUG is not present, pfn_to_online_page()
will fallback to pfn_valid().

Another little optimization is to check if the page belongs to the node
we are currently checking, so in case we have nodes interleaved we will
not check the same pfn multiple times.

I ran some tests:

Add some memory to node1 and node2 making it interleaved:

(qemu) object_add memory-backend-ram,id=ram0,size=1G (qemu) device_add pc-dimm,id=dimm0,memdev=ram0,node=1 (qemu) object_add memory-backend-ram,id=ram1,size=1G (qemu) device_add pc-dimm,id=dimm1,memdev=ram1,node=2 (qemu) object_add memory-backend-ram,id=ram2,size=1G (qemu) device_add pc-dimm,id=dimm2,memdev=ram2,node=1

Then, we offline that memory:

 # for i in {32..39} ; do echo "offline" >
/sys/devices/system/node/node1/memory$i/state;done
 # for i in {48..55} ; do echo "offline" >
/sys/devices/system/node/node1/memory$i/state;don
 # for i in {40..47} ; do echo "offline" >
/sys/devices/system/node/node2/memory$i/state;done

And we run kmemleak_scan:

 # echo "scan" > /sys/kernel/debug/kmemleak

before the patch:

Quoted 7 lines

kmemleak: time spend: 41596 us

after the patch:

kmemleak: time spend: 34899 us

Quoted 34 lines
---
 mm/kmemleak.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
mm/kmemleak.c +7 -3
index 877de4fa0720..5ce1e6a46d77 100644
@@ -113,6 +113,7 @@
 #include <linux/kmemleak.h>
 #include <linux/memory_hotplug.h>
 
+
 /*
  * Kmemleak configuration and common defines.
  */
@@ -1547,11 +1548,14 @@ static void kmemleak_scan(void)
 		unsigned long pfn;
 
 		for (pfn = start_pfn; pfn < end_pfn; pfn++) {
-			struct page *page;
+			struct page *page = pfn_to_online_page(pfn);
+
+			if (!page)
+				continue;
 
-			if (!pfn_valid(pfn))
+			/* only scan pages belonging to this node */
+			if (page_to_nid(page) != i)
 				continue;
-			page = pfn_to_page(pfn);
 			/* only scan if page is in use */
 			if (page_count(page) == 0)
 				continue;
Signature · 2 lines
Oscar Salvador
SUSE L3
#2 lore raw
In reply to Wei Yang
Quoted 4 lines
Quoted 2 lines

+

This one maybe not necessary.

Yeah, that is a remind of an include file I used for time measurement. I hope Andrew can drop that if this is taken.

Quoted 14 lines
Quoted 14 lines

/*
* Kmemleak configuration and common defines.
*/

mm/kmemleak.c +4 -1
@@ -1547,11 +1548,14 @@ static void kmemleak_scan(void)
 		unsigned long pfn;
 
 		for (pfn = start_pfn; pfn < end_pfn; pfn++) {
-			struct page *page;
+			struct page *page =
 pfn_to_online_page(pfn);
+
+			if (!page)
+				continue;
 
Quoted 7 lines

mm/kmemleak.c static void kmemleak_scan(void) · line 1558

Quoted 5 lines
mm/kmemleak.c +2 -1
-			if (!pfn_valid(pfn))
+			/* only scan pages belonging to this node
 */
+			if (page_to_nid(page) != i)
 				continue;

Not farmiliar with this situation. Is this often?

Well, hard to tell how often that happens because that mostly depends on the Hardware in case of baremetal.
Virtual systems can also have it though.

Quoted 9 lines
Quoted 7 lines
mm/kmemleak.c +0 -1
-			page = pfn_to_page(pfn);
 			/* only scan if page is in use */
			if (page_count(page) == 0)
				continue;
Signature · 1 line
2.13.7
Signature · 2 lines
Oscar Salvador
SUSE L3
Reviewed-by
#3 lore raw
Quoted 29 lines · Oscar Salvador
Quoted 4 lines
Quoted 2 lines

+

This one maybe not necessary.

Yeah, that is a remind of an include file I used for time measurement. I hope Andrew can drop that if this is taken.

Quoted 14 lines
Quoted 14 lines

/*
* Kmemleak configuration and common defines.
*/

mm/kmemleak.c +4 -1
@@ -1547,11 +1548,14 @@ static void kmemleak_scan(void)
 		unsigned long pfn;
 
 		for (pfn = start_pfn; pfn < end_pfn; pfn++) {
-			struct page *page;
+			struct page *page =
 pfn_to_online_page(pfn);
+
+			if (!page)
+				continue;
 
Quoted 7 lines

mm/kmemleak.c static void kmemleak_scan(void) · line 1558

Quoted 5 lines
mm/kmemleak.c +2 -1
-			if (!pfn_valid(pfn))
+			/* only scan pages belonging to this node
 */
+			if (page_to_nid(page) != i)
 				continue;

Not farmiliar with this situation. Is this often?

Quoted 4 lines

Well, hard to tell how often that happens because that mostly depends on the Hardware in case of baremetal.
Virtual systems can also have it though.

Ok, generally looks good to me.

Quoted 12 lines
Quoted 9 lines
Quoted 7 lines
mm/kmemleak.c +0 -1
-			page = pfn_to_page(pfn);
 			/* only scan if page is in use */
			if (page_count(page) == 0)
				continue;
Signature · 1 line
2.13.7
Signature · 2 lines
Oscar Salvador
SUSE L3
Signature · 2 lines
Wei Yang
Help you, Help me
maintainer Acked-by on v1 1/1
#5 lore raw
In reply to Oscar Salvador
Quoted 33 lines · Oscar Salvador

kmemleak_scan() goes through all online nodes and tries
to scan all used pages.
We can do better and use pfn_to_online_page(), so in case we have CONFIG_MEMORY_HOTPLUG, offlined pages will be skiped automatically. For boxes where CONFIG_MEMORY_HOTPLUG is not present, pfn_to_online_page() will fallback to pfn_valid().

Another little optimization is to check if the page belongs to the node we are currently checking, so in case we have nodes interleaved we will not check the same pfn multiple times.

I ran some tests:

Add some memory to node1 and node2 making it interleaved:

(qemu) object_add memory-backend-ram,id=ram0,size=1G (qemu) device_add pc-dimm,id=dimm0,memdev=ram0,node=1 (qemu) object_add memory-backend-ram,id=ram1,size=1G (qemu) device_add pc-dimm,id=dimm1,memdev=ram1,node=2 (qemu) object_add memory-backend-ram,id=ram2,size=1G (qemu) device_add pc-dimm,id=dimm2,memdev=ram2,node=1

Then, we offline that memory:

 # for i in {32..39} ; do echo "offline" > /sys/devices/system/node/node1/memory$i/state;done
 # for i in {48..55} ; do echo "offline" > /sys/devices/system/node/node1/memory$i/state;don
 # for i in {40..47} ; do echo "offline" > /sys/devices/system/node/node2/memory$i/state;done

And we run kmemleak_scan:

 # echo "scan" > /sys/kernel/debug/kmemleak

before the patch:

Quoted 7 lines

kmemleak: time spend: 41596 us

after the patch:

kmemleak: time spend: 34899 us