FAILED: patch "[PATCH] mm/memory_hotplug: add missing mem_hotplug_lock" failed to apply to 5.15-stable tree
Subsystems (4)
[email protected]
maintainerThe patch below does not apply to the 5.15-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-5.15.y
git checkout FETCH_HEAD
git cherry-pick -x 001002e73712cdf6b8d9a103648cda3040ad7647
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2023120927-immobile-half-41d8@gregkh' --subject-prefix 'PATCH 5.15.y' HEAD^..
Possible dependencies:
001002e73712 ("mm/memory_hotplug: add missing mem_hotplug_lock")1a8c64e11043 ("mm/memory_hotplug: embed vmem_altmap details in memory block") 2d1f649c7c08 ("mm/memory_hotplug: support memmap_on_memory when memmap is not aligned to pageblocks") 85a2b4b08f20 ("mm/memory_hotplug: allow architecture to override memmap on memory support check") e3c2bfdd33a3 ("mm/memory_hotplug: allow memmap on memory hotplug request to fallback") 5033091de814 ("mm/hwpoison: introduce per-memory_block hwpoison counter") a46c9304b4bb ("mm/hwpoison: pass pfn to num_poisoned_pages_*()")d027122d8363 ("mm/hwpoison: move definitions of num_poisoned_pages_* to memory-failure.c") e591ef7d96d6 ("mm,hwpoison,hugetlb,memory_hotplug: hotremove memory section with hwpoisoned hugepage") 0d206b5d2e0d ("mm/swap: add swp_offset_pfn() to fetch PFN from swap entry") eba4d770efc8 ("mm/swap: comment all the ifdef in swapops.h")21c9e90ab9a4 ("mm, hwpoison: use num_poisoned_pages_sub() to decrease num_poisoned_pages") f36a5543a748 ("mm, hwpoison: fix page refcnt leaking in try_memory_failure_hugetlb()") 7453bf621cfa ("mm, hwpoison: make __page_handle_poison returns int")38f6d29397cc ("mm, hwpoison: set PG_hwpoison for busy hugetlb pages")ac5fcde0a96a ("mm, hwpoison: make unpoison aware of raw error info in hwpoisoned hugepage") 161df60e9e89 ("mm, hwpoison, hugetlb: support saving mechanism of raw error pages") 6213834c10de ("mm: hugetlb_vmemmap: improve hugetlb_vmemmap code readability") 998a2997885f ("mm: hugetlb_vmemmap: move vmemmap code related to HugeTLB to hugetlb_vmemmap.c") dff033818a06 ("mm: hugetlb_vmemmap: introduce the name HVO")
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 001002e73712cdf6b8d9a103648cda3040ad7647 Mon Sep 17 00:00:00 2001
From: Sumanth Korikkar <[email protected]>
Date: Mon, 20 Nov 2023 15:53:52 +0100
Subject: [PATCH] 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.
drivers/base/memory.c +15 -3
@@ -180,6 +180,9 @@ static inline unsigned long memblk_nr_poison(struct memory_block *mem) }#endif +/* +* Must acquire mem_hotplug_lock in write mode. +*/ static int memory_block_online (struct memory_block *mem) {unsigned long start_pfn =section_nr_to_pfn (mem->start_section_nr);
@@ -204,10 +207,11 @@ static int memory_block_online(struct memory_block *mem)if (mem->altmap) nr_vmemmap_pages = mem->altmap->free; +mem_hotplug_begin ();if (nr_vmemmap_pages) { ret =mhp_init_memmap_on_memory (start_pfn, nr_vmemmap_pages, zone);if (ret) -return ret; + goto out; } ret = online_pages(start_pfn + nr_vmemmap_pages,
@@ -215,7 +219,7 @@ static int memory_block_online(struct memory_block *mem)if (ret) {if (nr_vmemmap_pages)mhp_deinit_memmap_on_memory (start_pfn, nr_vmemmap_pages); -return ret; + goto out; }/*
@@ -227,9 +231,14 @@ static int memory_block_online(struct memory_block *mem) nr_vmemmap_pages); mem->zone = zone; +out: +mem_hotplug_done ();return ret; } +/* +* Must acquire mem_hotplug_lock in write mode. +*/ static int memory_block_offline (struct memory_block *mem) {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)if (mem->altmap) nr_vmemmap_pages = mem->altmap->free; +mem_hotplug_begin ();if (nr_vmemmap_pages)adjust_present_page_count (pfn_to_page (start_pfn), mem->group, -nr_vmemmap_pages);
@@ -258,13 +268,15 @@ static int memory_block_offline(struct memory_block *mem)if (nr_vmemmap_pages)adjust_present_page_count (pfn_to_page (start_pfn), mem->group, nr_vmemmap_pages); -return ret; + goto out; }if (nr_vmemmap_pages)mhp_deinit_memmap_on_memory (start_pfn, nr_vmemmap_pages); mem->zone =NULL ; +out: +mem_hotplug_done ();return ret; }
mm/memory_hotplug.c +6 -7
@@ -1129,6 +1129,9 @@ void mhp_deinit_memmap_on_memory(unsigned long pfn, unsigned long nr_pages)kasan_remove_zero_shadow (__va (PFN_PHYS (pfn)),PFN_PHYS (nr_pages)); } +/* +* Must be called with mem_hotplug_lock in write mode. +*/ int __ref online_pages (unsigned long pfn,unsigned long nr_pages,struct zone *zone,struct memory_group *group) {
@@ -1149,7 +1152,6 @@ int __ref online_pages(unsigned long pfn, unsigned long nr_pages, !IS_ALIGNED(pfn + nr_pages, PAGES_PER_SECTION)))return -EINVAL ; -mem_hotplug_begin ();/* associate pfn range with the zone */ 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,writeback_set_ratelimit ();memory_notify (MEM_ONLINE , &arg); -mem_hotplug_done ();return 0 ; failed_addition:
@@ -1217,7 +1218,6 @@ int __ref online_pages(unsigned long pfn, unsigned long nr_pages, (((unsigned long long) pfn + nr_pages) << PAGE_SHIFT) - 1);memory_notify (MEM_CANCEL_ONLINE , &arg);remove_pfn_range_from_zone (zone, pfn, nr_pages); -mem_hotplug_done ();return ret; }
@@ -1863,6 +1863,9 @@ static int count_system_ram_pages_cb(unsigned long start_pfn,return 0 ; } +/* +* Must be called with mem_hotplug_lock in write mode. +*/ int __ref offline_pages (unsigned long start_pfn,unsigned long nr_pages,struct zone *zone,struct memory_group *group) {
@@ -1885,8 +1888,6 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages, !IS_ALIGNED(start_pfn + nr_pages, PAGES_PER_SECTION)))return -EINVAL ; -mem_hotplug_begin (); -/* * Don't allow to offline memory blocks that contain holes. * 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,memory_notify (MEM_OFFLINE , &arg);remove_pfn_range_from_zone (zone, start_pfn, nr_pages); -mem_hotplug_done ();return 0 ; failed_removal_isolated:
@@ -2046,7 +2046,6 @@ int __ref offline_pages(unsigned long start_pfn, unsigned long nr_pages, (unsigned long long ) start_pfn <<PAGE_SHIFT , ((unsigned long long ) end_pfn <<PAGE_SHIFT ) -1 , reason); -mem_hotplug_done ();return ret; }