Skip to contentSkip to conversation

mm, show_mem: drop pgdat_resize_lock in show_mem()

PATCH Superseded Wei Yang 4 participants 13 messages Nov 28, 2018 – Nov 30, 2018

Subsystems (2)
lore
Share & tools
lore

Wei Yang

author
#0 lore raw

PATCH 1/1 mm, show_mem: drop pgdat_resize_lock in show_mem()

Function show_mem() is used to print system memory status when user requires or fail to allocate memory. Generally, this is a best effort information and not willing to affect core mm subsystem.

The data protected by pgdat_resize_lock is mostly correct except there is:

* page struct defer init
* memory hotplug

---
 lib/show_mem.c | 2 --
 1 file changed, 2 deletions(-)
lib/show_mem.c +0 -2
index 0beaa1d899aa..1d996e5771ab 100644
@@ -21,7 +21,6 @@ void show_mem(unsigned int filter, nodemask_t *nodemask)
 		unsigned long flags;
 		int zoneid;
 
-		pgdat_resize_lock(pgdat, &flags);
 		for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
 			struct zone *zone = &pgdat->node_zones[zoneid];
 			if (!populated_zone(zone))
@@ -33,7 +32,6 @@ void show_mem(unsigned int filter, nodemask_t *nodemask)
 			if (is_highmem_idx(zoneid))
 				highmem += zone->present_pages;
 		}
-		pgdat_resize_unlock(pgdat, &flags);
 	}
 
 	printk("%lu pages RAM\n", total);
Signature · 1 line
2.15.1
maintainer
#1 lore raw
Quoted 8 lines · On Thu, 29 Nov 2018 05:08:15 +0800 Wei Yang <[email protected]> wrote:

Function show_mem() is used to print system memory status when user requires or fail to allocate memory. Generally, this is a best effort information and not willing to affect core mm subsystem.

The data protected by pgdat_resize_lock is mostly correct except there is:

* page struct defer init
* memory hotplug

What is the advantage in doing this? What problem does the taking of that lock cause?

author
#2 lore raw
Quoted 6 lines · Andrew Morton
Quoted 4 lines · On Thu, 29 Nov 2018 05:08:15 +0800 Wei Yang <[email protected]> wrote:

Function show_mem() is used to print system memory status when user requires or fail to allocate memory. Generally, this is a best effort information and not willing to affect core mm subsystem.

Quoted 7 lines
Quoted 4 lines

The data protected by pgdat_resize_lock is mostly correct except there is:

* page struct defer init
* memory hotplug

What is the advantage in doing this? What problem does the taking of that lock cause?

Michal and I had a discussion in https://patchwork.kernel.org/patch/10689759/

The purpose of this is to see whehter it is nessary to make pgdat_resize_lock IRQ context safe. After went through the code, most of the users are not from IRQ context.

If my understanding is correct, Michal's suggestion is to drop the lock here. (The second last reply from Michal.)

Signature · 2 lines
Wei Yang
Help you, Help me
#3 lore raw
In reply to Wei Yang
Quoted 3 lines · Wei Yang

Function show_mem() is used to print system memory status when user requires or fail to allocate memory. Generally, this is a best effort information and not willing to affect core mm subsystem.

I would drop the part after and

Quoted 4 lines

The data protected by pgdat_resize_lock is mostly correct except there is:

* page struct defer init
* memory hotplug

This is more confusing than helpful. I would just drop it.

The changelog doesn't explain what is done and why. The second one is much more important. I would say this

"
Function show_mem() is used to print system memory status when user requires or fail to allocate memory. Generally, this is a best effort information so any races with memory hotplug (or very theoretically an early initialization) should be toleratable and the worst that could happen is to print an imprecise node state.

Drop the resize lock because this is the only place which might hold the lock from the interrupt context and so all other callers might use a simple spinlock. Even though this doesn't solve any real issue it makes the code easier to follow and tiny more effective.
"

lib/show_mem.c void show_mem(unsigned int filter, nodemask_t *nodemask) · line 21

Quoted 12 lines
---
 lib/show_mem.c | 2 --
 1 file changed, 2 deletions(-)
lib/show_mem.c +0 -0
index 0beaa1d899aa..1d996e5771ab 100644
@@ -21,7 +21,6 @@ void show_mem(unsigned int filter, nodemask_t *nodemask)
 		unsigned long flags;

btw. you want to drop flags.

Quoted 17 lines
lib/show_mem.c +0 -2
 		int zoneid;
 
-		pgdat_resize_lock(pgdat, &flags);
 		for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
 			struct zone *zone = &pgdat->node_zones[zoneid];
 			if (!populated_zone(zone))
@@ -33,7 +32,6 @@ void show_mem(unsigned int filter, nodemask_t *nodemask)
 			if (is_highmem_idx(zoneid))
 				highmem += zone->present_pages;
 		}
-		pgdat_resize_unlock(pgdat, &flags);
 	}
 
 	printk("%lu pages RAM\n", total);
Signature · 1 line
2.15.1
Signature · 2 lines
Michal Hocko
SUSE Labs
author
#4 lore raw
Quoted 24 lines · Michal Hocko
Quoted 3 lines · Wei Yang

Function show_mem() is used to print system memory status when user requires or fail to allocate memory. Generally, this is a best effort information and not willing to affect core mm subsystem.

I would drop the part after and

Quoted 4 lines

The data protected by pgdat_resize_lock is mostly correct except there is:

* page struct defer init
* memory hotplug

This is more confusing than helpful. I would just drop it.

The changelog doesn't explain what is done and why. The second one is much more important. I would say this

"
Function show_mem() is used to print system memory status when user requires or fail to allocate memory. Generally, this is a best effort information so any races with memory hotplug (or very theoretically an early initialization) should be toleratable and the worst that could happen is to print an imprecise node state.

Quoted 5 lines

Drop the resize lock because this is the only place which might hold the lock from the interrupt context and so all other callers might use a simple spinlock. Even though this doesn't solve any real issue it makes the code easier to follow and tiny more effective.
"

Ah, I have to admit this is much clearer and easier for audience to understand the reason.

Thanks a lot.

Quoted 7 lines
Quoted 7 lines
---
 lib/show_mem.c | 2 --
 1 file changed, 2 deletions(-)
Quoted 8 lines

lib/show_mem.c void show_mem(unsigned int filter, nodemask_t *nodemask) · line 21

Quoted 6 lines
lib/show_mem.c +0 -0
index 0beaa1d899aa..1d996e5771ab 100644
@@ -21,7 +21,6 @@ void show_mem(unsigned int filter, nodemask_t *nodemask)
 		unsigned long flags;

btw. you want to drop flags.

Oops, what a shame . :-(

Quoted 21 lines
Quoted 17 lines
lib/show_mem.c +0 -2
 		int zoneid;
 
-		pgdat_resize_lock(pgdat, &flags);
 		for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
 			struct zone *zone = &pgdat->node_zones[zoneid];
 			if (!populated_zone(zone))
@@ -33,7 +32,6 @@ void show_mem(unsigned int filter, nodemask_t *nodemask)
 			if (is_highmem_idx(zoneid))
 				highmem += zone->present_pages;
 		}
-		pgdat_resize_unlock(pgdat, &flags);
 	}
 
 	printk("%lu pages RAM\n", total);
Signature · 1 line
2.15.1
Signature · 2 lines
Michal Hocko
SUSE Labs
Signature · 2 lines
Wei Yang
Help you, Help me
author
#5 lore raw
In reply to Michal Hocko
Quoted 17 lines · Michal Hocko
Quoted 3 lines · Wei Yang

Function show_mem() is used to print system memory status when user requires or fail to allocate memory. Generally, this is a best effort information and not willing to affect core mm subsystem.

I would drop the part after and

Quoted 4 lines

The data protected by pgdat_resize_lock is mostly correct except there is:

* page struct defer init
* memory hotplug

This is more confusing than helpful. I would just drop it.

The changelog doesn't explain what is done and why. The second one is much more important. I would say this

Quoted 8 lines

"
Function show_mem() is used to print system memory status when user requires or fail to allocate memory. Generally, this is a best effort information so any races with memory hotplug (or very theoretically an early initialization) should be toleratable and the worst that could happen is to print an imprecise node state.

Drop the resize lock because this is the only place which might hold the

As I mentioned in https://patchwork.kernel.org/patch/10689759/, there is one place used in __remove_zone(). I don't get your suggestion of this place. And is __remove_zone() could be called in IRQ context?

Quoted 5 lines

lock from the interrupt context and so all other callers might use a simple spinlock. Even though this doesn't solve any real issue it makes the code easier to follow and tiny more effective.
"

Signature · 2 lines
Wei Yang
Help you, Help me
#6 lore raw
Quoted 25 lines · Wei Yang
Quoted 24 lines · Michal Hocko
Quoted 3 lines · Wei Yang

Function show_mem() is used to print system memory status when user requires or fail to allocate memory. Generally, this is a best effort information and not willing to affect core mm subsystem.

I would drop the part after and

Quoted 4 lines

The data protected by pgdat_resize_lock is mostly correct except there is:

* page struct defer init
* memory hotplug

This is more confusing than helpful. I would just drop it.

The changelog doesn't explain what is done and why. The second one is much more important. I would say this

"
Function show_mem() is used to print system memory status when user requires or fail to allocate memory. Generally, this is a best effort information so any races with memory hotplug (or very theoretically an early initialization) should be toleratable and the worst that could happen is to print an imprecise node state.

Quoted 5 lines
Quoted 1 line

Drop the resize lock because this is the only place which might hold the

As I mentioned in https://patchwork.kernel.org/patch/10689759/, there is one place used in __remove_zone(). I don't get your suggestion of this place. And is __remove_zone() could be called in IRQ context?

It is only called from __remove_pages and that one calls cond_resched so obviosly not.

Signature · 2 lines
Michal Hocko
SUSE Labs
author
#7 lore raw
Quoted 26 lines · Michal Hocko
Quoted 25 lines · Wei Yang
Quoted 24 lines · Michal Hocko
Quoted 3 lines · Wei Yang

Function show_mem() is used to print system memory status when user requires or fail to allocate memory. Generally, this is a best effort information and not willing to affect core mm subsystem.

I would drop the part after and

Quoted 4 lines

The data protected by pgdat_resize_lock is mostly correct except there is:

* page struct defer init
* memory hotplug

This is more confusing than helpful. I would just drop it.

The changelog doesn't explain what is done and why. The second one is much more important. I would say this

"
Function show_mem() is used to print system memory status when user requires or fail to allocate memory. Generally, this is a best effort information so any races with memory hotplug (or very theoretically an early initialization) should be toleratable and the worst that could happen is to print an imprecise node state.

Quoted 9 lines
Quoted 5 lines
Quoted 1 line

Drop the resize lock because this is the only place which might hold the

As I mentioned in https://patchwork.kernel.org/patch/10689759/, there is one place used in __remove_zone(). I don't get your suggestion of this place. And is __remove_zone() could be called in IRQ context?

It is only called from __remove_pages and that one calls cond_resched so obviosly not.

Forgive my poor background knowledge, I went throught the code, but not found where call cond_resched.

__remove_pages()

    release_mem_region_adjustable()
    clear_zone_contiguous()
    __remove_section()
      unregister_memory_section()
      __remove_zone()
      sparse_remove_one_section()
    set_zone_contiguous()

Would you mind giving me a hint?

Quoted 3 lines
Signature · 2 lines
Michal Hocko
SUSE Labs
Signature · 2 lines
Wei Yang
Help you, Help me
#8 lore raw
Quoted 1 line · Wei Yang

On Thu, Nov 29, 2018 at 04:49:22PM +0100, Michal Hocko wrote:

[...]

Quoted 16 lines
Quoted 3 lines

It is only called from __remove_pages and that one calls cond_resched so obviosly not.

Forgive my poor background knowledge, I went throught the code, but not found where call cond_resched.

__remove_pages()

    release_mem_region_adjustable()
    clear_zone_contiguous()
    __remove_section()
      unregister_memory_section()
      __remove_zone()
      sparse_remove_one_section()
    set_zone_contiguous()
Quoted 1 line

Would you mind giving me a hint?

This is the code as of 4.20-rc2

	for (i = 0; i < sections_to_remove; i++) {
		unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;

		cond_resched();
		ret = __remove_section(zone, __pfn_to_section(pfn), map_offset,
				altmap);
		map_offset = 0;
		if (ret)
			break;
	}

Maybe things have changed in the meantime but in general the code is sleepable (e.g. release_mem_region_adjustable does GFP_KERNEL allocation) and that rules out IRQ context.

Signature · 2 lines
Michal Hocko
SUSE Labs
author
#9 lore raw
Quoted 34 lines · Michal Hocko
Quoted 1 line · Wei Yang

On Thu, Nov 29, 2018 at 04:49:22PM +0100, Michal Hocko wrote:

[...]

Quoted 16 lines
Quoted 3 lines

It is only called from __remove_pages and that one calls cond_resched so obviosly not.

Forgive my poor background knowledge, I went throught the code, but not found where call cond_resched.

__remove_pages()

    release_mem_region_adjustable()
    clear_zone_contiguous()
    __remove_section()
      unregister_memory_section()
      __remove_zone()
      sparse_remove_one_section()
    set_zone_contiguous()
Quoted 1 line

Would you mind giving me a hint?

This is the code as of 4.20-rc2

	for (i = 0; i < sections_to_remove; i++) {
		unsigned long pfn = phys_start_pfn + i*PAGES_PER_SECTION;

		cond_resched();
		ret = __remove_section(zone, __pfn_to_section(pfn), map_offset,
				altmap);
		map_offset = 0;
		if (ret)
			break;
	}
Quoted 3 lines

Maybe things have changed in the meantime but in general the code is sleepable (e.g. release_mem_region_adjustable does GFP_KERNEL allocation) and that rules out IRQ context.

Thanks, my code is not up to date.

Quoted 3 lines
Signature · 2 lines
Michal Hocko
SUSE Labs
Signature · 2 lines
Wei Yang
Help you, Help me
author
#10 lore raw

PATCH v2 1/1 mm, show_mem: drop pgdat_resize_lock in show_mem()

In reply to Wei Yang

Function show_mem() is used to print system memory status when user requires or fail to allocate memory. Generally, this is a best effort information so any races with memory hotplug (or very theoretically an early initialization) should be tolerable and the worst that could happen is to print an imprecise node state.

Drop the resize lock because this is the only place which might hold the lock from the interrupt context and so all other callers might use a simple spinlock. Even though this doesn't solve any real issue it makes the code easier to follow and tiny more effective.

---
v2:
* adjust the changelog to show the reason of this change
* remove unused variable flags

---
 lib/show_mem.c | 3 ---
 1 file changed, 3 deletions(-)
lib/show_mem.c +0 -3
index 0beaa1d899aa..f4e029e1ddec 100644
@@ -18,10 +18,8 @@ void show_mem(unsigned int filter, nodemask_t *nodemask)
 	show_free_areas(filter, nodemask);
 
 	for_each_online_pgdat(pgdat) {
-		unsigned long flags;
 		int zoneid;
 
-		pgdat_resize_lock(pgdat, &flags);
 		for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
 			struct zone *zone = &pgdat->node_zones[zoneid];
 			if (!populated_zone(zone))
@@ -33,7 +31,6 @@ void show_mem(unsigned int filter, nodemask_t *nodemask)
 			if (is_highmem_idx(zoneid))
 				highmem += zone->present_pages;
 		}
-		pgdat_resize_unlock(pgdat, &flags);
 	}
 
 	printk("%lu pages RAM\n", total);
Signature · 1 line
2.15.1
Acked-by on v2 1/1
#11 lore raw
Quoted 6 lines · Wei Yang

Function show_mem() is used to print system memory status when user requires or fail to allocate memory. Generally, this is a best effort information so any races with memory hotplug (or very theoretically an early initialization) should be tolerable and the worst that could happen is to print an imprecise node state.

Quoted 6 lines

Drop the resize lock because this is the only place which might hold the lock from the interrupt context and so all other callers might use a simple spinlock. Even though this doesn't solve any real issue it makes the code easier to follow and tiny more effective.

Quoted 35 lines

---
v2:
* adjust the changelog to show the reason of this change
* remove unused variable flags

---
 lib/show_mem.c | 3 ---
 1 file changed, 3 deletions(-)
lib/show_mem.c +0 -3
index 0beaa1d899aa..f4e029e1ddec 100644
@@ -18,10 +18,8 @@ void show_mem(unsigned int filter, nodemask_t *nodemask)
 	show_free_areas(filter, nodemask);
 
 	for_each_online_pgdat(pgdat) {
-		unsigned long flags;
 		int zoneid;
 
-		pgdat_resize_lock(pgdat, &flags);
 		for (zoneid = 0; zoneid < MAX_NR_ZONES; zoneid++) {
 			struct zone *zone = &pgdat->node_zones[zoneid];
 			if (!populated_zone(zone))
@@ -33,7 +31,6 @@ void show_mem(unsigned int filter, nodemask_t *nodemask)
 			if (is_highmem_idx(zoneid))
 				highmem += zone->present_pages;
 		}
-		pgdat_resize_unlock(pgdat, &flags);
 	}
 
 	printk("%lu pages RAM\n", total);
Signature · 1 line
2.15.1
Signature · 2 lines
Michal Hocko
SUSE Labs
Reviewed-by on v2 1/1
#12 lore raw
In reply to Wei Yang
Quoted 11 lines · Wei Yang

Function show_mem() is used to print system memory status when user requires or fail to allocate memory. Generally, this is a best effort information so any races with memory hotplug (or very theoretically an early initialization) should be tolerable and the worst that could happen is to print an imprecise node state.

Drop the resize lock because this is the only place which might hold the lock from the interrupt context and so all other callers might use a simple spinlock. Even though this doesn't solve any real issue it makes the code easier to follow and tiny more effective.