Skip to content

Commit 001002e

Browse files
sumanthkorikkarakpm00
authored andcommitted
mm/memory_hotplug: add missing mem_hotplug_lock
From Documentation/core-api/memory-hotplug.rst: When adding/removing/onlining/offlining memory or adding/removing heterogeneous/device memory, we should always hold the mem_hotplug_lock in write mode to serialise memory hotplug (e.g. access to global/zone variables). mhp_(de)init_memmap_on_memory() functions can change zone stats and struct page content, but they are currently called w/o the mem_hotplug_lock. When memory block is being offlined and when kmemleak goes through each populated zone, the following theoretical race conditions could occur: CPU 0: | CPU 1: memory_offline() | -> offline_pages() | -> mem_hotplug_begin() | ... | -> mem_hotplug_done() | | kmemleak_scan() | -> get_online_mems() | ... -> mhp_deinit_memmap_on_memory() | [not protected by mem_hotplug_begin/done()]| Marks memory section as offline, | Retrieves zone_start_pfn poisons vmemmap struct pages and updates | and struct page members. the zone related data | | ... | -> put_online_mems() Fix this by ensuring mem_hotplug_lock is taken before performing mhp_init_memmap_on_memory(). Also ensure that mhp_deinit_memmap_on_memory() holds the lock. online/offline_pages() are currently only called from memory_block_online/offline(), so it is safe to move the locking there. Link: https://lkml.kernel.org/r/20231120145354.308999-2-sumanthk@linux.ibm.com Fixes: a08a2ae ("mm,memory_hotplug: allocate memmap from the added memory range") Signed-off-by: Sumanth Korikkar <sumanthk@linux.ibm.com> Reviewed-by: Gerald Schaefer <gerald.schaefer@linux.ibm.com> Acked-by: David Hildenbrand <david@redhat.com> Cc: Alexander Gordeev <agordeev@linux.ibm.com> Cc: Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com> Cc: Anshuman Khandual <anshuman.khandual@arm.com> Cc: Heiko Carstens <hca@linux.ibm.com> Cc: Michal Hocko <mhocko@suse.com> Cc: Oscar Salvador <osalvador@suse.de> Cc: Vasily Gorbik <gor@linux.ibm.com> Cc: kernel test robot <lkp@intel.com> Cc: <stable@vger.kernel.org> [5.15+] Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent c540b03 commit 001002e

2 files changed

Lines changed: 21 additions & 10 deletions

File tree

drivers/base/memory.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ static inline unsigned long memblk_nr_poison(struct memory_block *mem)
180180
}
181181
#endif
182182

183+
/*
184+
* Must acquire mem_hotplug_lock in write mode.
185+
*/
183186
static int memory_block_online(struct memory_block *mem)
184187
{
185188
unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
@@ -204,18 +207,19 @@ static int memory_block_online(struct memory_block *mem)
204207
if (mem->altmap)
205208
nr_vmemmap_pages = mem->altmap->free;
206209

210+
mem_hotplug_begin();
207211
if (nr_vmemmap_pages) {
208212
ret = mhp_init_memmap_on_memory(start_pfn, nr_vmemmap_pages, zone);
209213
if (ret)
210-
return ret;
214+
goto out;
211215
}
212216

213217
ret = online_pages(start_pfn + nr_vmemmap_pages,
214218
nr_pages - nr_vmemmap_pages, zone, mem->group);
215219
if (ret) {
216220
if (nr_vmemmap_pages)
217221
mhp_deinit_memmap_on_memory(start_pfn, nr_vmemmap_pages);
218-
return ret;
222+
goto out;
219223
}
220224

221225
/*
@@ -227,9 +231,14 @@ static int memory_block_online(struct memory_block *mem)
227231
nr_vmemmap_pages);
228232

229233
mem->zone = zone;
234+
out:
235+
mem_hotplug_done();
230236
return ret;
231237
}
232238

239+
/*
240+
* Must acquire mem_hotplug_lock in write mode.
241+
*/
233242
static int memory_block_offline(struct memory_block *mem)
234243
{
235244
unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
@@ -247,6 +256,7 @@ static int memory_block_offline(struct memory_block *mem)
247256
if (mem->altmap)
248257
nr_vmemmap_pages = mem->altmap->free;
249258

259+
mem_hotplug_begin();
250260
if (nr_vmemmap_pages)
251261
adjust_present_page_count(pfn_to_page(start_pfn), mem->group,
252262
-nr_vmemmap_pages);
@@ -258,13 +268,15 @@ static int memory_block_offline(struct memory_block *mem)
258268
if (nr_vmemmap_pages)
259269
adjust_present_page_count(pfn_to_page(start_pfn),
260270
mem->group, nr_vmemmap_pages);
261-
return ret;
271+
goto out;
262272
}
263273

264274
if (nr_vmemmap_pages)
265275
mhp_deinit_memmap_on_memory(start_pfn, nr_vmemmap_pages);
266276

267277
mem->zone = NULL;
278+
out:
279+
mem_hotplug_done();
268280
return ret;
269281
}
270282

mm/memory_hotplug.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,9 @@ void mhp_deinit_memmap_on_memory(unsigned long pfn, unsigned long nr_pages)
11291129
kasan_remove_zero_shadow(__va(PFN_PHYS(pfn)), PFN_PHYS(nr_pages));
11301130
}
11311131

1132+
/*
1133+
* Must be called with mem_hotplug_lock in write mode.
1134+
*/
11321135
int __ref online_pages(unsigned long pfn, unsigned long nr_pages,
11331136
struct zone *zone, struct memory_group *group)
11341137
{
@@ -1149,7 +1152,6 @@ int __ref online_pages(unsigned long pfn, unsigned long nr_pages,
11491152
!IS_ALIGNED(pfn + nr_pages, PAGES_PER_SECTION)))
11501153
return -EINVAL;
11511154

1152-
mem_hotplug_begin();
11531155

11541156
/* associate pfn range with the zone */
11551157
move_pfn_range_to_zone(zone, pfn, nr_pages, NULL, MIGRATE_ISOLATE);
@@ -1208,7 +1210,6 @@ int __ref online_pages(unsigned long pfn, unsigned long nr_pages,
12081210
writeback_set_ratelimit();
12091211

12101212
memory_notify(MEM_ONLINE, &arg);
1211-
mem_hotplug_done();
12121213
return 0;
12131214

12141215
failed_addition:
@@ -1217,7 +1218,6 @@ int __ref online_pages(unsigned long pfn, unsigned long nr_pages,
12171218
(((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1);
12181219
memory_notify(MEM_CANCEL_ONLINE, &arg);
12191220
remove_pfn_range_from_zone(zone, pfn, nr_pages);
1220-
mem_hotplug_done();
12211221
return ret;
12221222
}
12231223

@@ -1863,6 +1863,9 @@ static int count_system_ram_pages_cb(unsigned long start_pfn,
18631863
return 0;
18641864
}
18651865

1866+
/*
1867+
* Must be called with mem_hotplug_lock in write mode.
1868+
*/
18661869
int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages,
18671870
struct zone *zone, struct memory_group *group)
18681871
{
@@ -1885,8 +1888,6 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages,
18851888
!IS_ALIGNED(start_pfn + nr_pages, PAGES_PER_SECTION)))
18861889
return -EINVAL;
18871890

1888-
mem_hotplug_begin();
1889-
18901891
/*
18911892
* Don't allow to offline memory blocks that contain holes.
18921893
* Consequently, memory blocks with holes can never get onlined
@@ -2031,7 +2032,6 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages,
20312032

20322033
memory_notify(MEM_OFFLINE, &arg);
20332034
remove_pfn_range_from_zone(zone, start_pfn, nr_pages);
2034-
mem_hotplug_done();
20352035
return 0;
20362036

20372037
failed_removal_isolated:
@@ -2046,7 +2046,6 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages,
20462046
(unsigned long long) start_pfn << PAGE_SHIFT,
20472047
((unsigned long long) end_pfn << PAGE_SHIFT) - 1,
20482048
reason);
2049-
mem_hotplug_done();
20502049
return ret;
20512050
}
20522051

0 commit comments

Comments
 (0)