Skip to content

Commit 5ec8e8e

Browse files
Charan Teja Kallaakpm00
authored andcommitted
mm/sparsemem: fix race in accessing memory_section->usage
The below race is observed on a PFN which falls into the device memory region with the system memory configuration where PFN's are such that [ZONE_NORMAL ZONE_DEVICE ZONE_NORMAL]. Since normal zone start and end pfn contains the device memory PFN's as well, the compaction triggered will try on the device memory PFN's too though they end up in NOP(because pfn_to_online_page() returns NULL for ZONE_DEVICE memory sections). When from other core, the section mappings are being removed for the ZONE_DEVICE region, that the PFN in question belongs to, on which compaction is currently being operated is resulting into the kernel crash with CONFIG_SPASEMEM_VMEMAP enabled. The crash logs can be seen at [1]. compact_zone() memunmap_pages ------------- --------------- __pageblock_pfn_to_page ...... (a)pfn_valid(): valid_section()//return true (b)__remove_pages()-> sparse_remove_section()-> section_deactivate(): [Free the array ms->usage and set ms->usage = NULL] pfn_section_valid() [Access ms->usage which is NULL] NOTE: From the above it can be said that the race is reduced to between the pfn_valid()/pfn_section_valid() and the section deactivate with SPASEMEM_VMEMAP enabled. The commit b943f04("mm/sparse: fix kernel crash with pfn_section_valid check") tried to address the same problem by clearing the SECTION_HAS_MEM_MAP with the expectation of valid_section() returns false thus ms->usage is not accessed. Fix this issue by the below steps: a) Clear SECTION_HAS_MEM_MAP before freeing the ->usage. b) RCU protected read side critical section will either return NULL when SECTION_HAS_MEM_MAP is cleared or can successfully access ->usage. c) Free the ->usage with kfree_rcu() and set ms->usage = NULL. No attempt will be made to access ->usage after this as the SECTION_HAS_MEM_MAP is cleared thus valid_section() return false. Thanks to David/Pavan for their inputs on this patch. [1] https://lore.kernel.org/linux-mm/[email protected]/ On Snapdragon SoC, with the mentioned memory configuration of PFN's as [ZONE_NORMAL ZONE_DEVICE ZONE_NORMAL], we are able to see bunch of issues daily while testing on a device farm. For this particular issue below is the log. Though the below log is not directly pointing to the pfn_section_valid(){ ms->usage;}, when we loaded this dump on T32 lauterbach tool, it is pointing. [ 540.578056] Unable to handle kernel NULL pointer dereference at virtual address 0000000000000000 [ 540.578068] Mem abort info: [ 540.578070] ESR = 0x0000000096000005 [ 540.578073] EC = 0x25: DABT (current EL), IL = 32 bits [ 540.578077] SET = 0, FnV = 0 [ 540.578080] EA = 0, S1PTW = 0 [ 540.578082] FSC = 0x05: level 1 translation fault [ 540.578085] Data abort info: [ 540.578086] ISV = 0, ISS = 0x00000005 [ 540.578088] CM = 0, WnR = 0 [ 540.579431] pstate: 82400005 (Nzcv daif +PAN -UAO +TCO -DIT -SSBSBTYPE=--) [ 540.579436] pc : __pageblock_pfn_to_page+0x6c/0x14c [ 540.579454] lr : compact_zone+0x994/0x1058 [ 540.579460] sp : ffffffc03579b510 [ 540.579463] x29: ffffffc03579b510 x28: 0000000000235800 x27:000000000000000c [ 540.579470] x26: 0000000000235c00 x25: 0000000000000068 x24:ffffffc03579b640 [ 540.579477] x23: 0000000000000001 x22: ffffffc03579b660 x21:0000000000000000 [ 540.579483] x20: 0000000000235bff x19: ffffffdebf7e3940 x18:ffffffdebf66d140 [ 540.579489] x17: 00000000739ba063 x16: 00000000739ba063 x15:00000000009f4bff [ 540.579495] x14: 0000008000000000 x13: 0000000000000000 x12:0000000000000001 [ 540.579501] x11: 0000000000000000 x10: 0000000000000000 x9 :ffffff897d2cd440 [ 540.579507] x8 : 0000000000000000 x7 : 0000000000000000 x6 :ffffffc03579b5b4 [ 540.579512] x5 : 0000000000027f25 x4 : ffffffc03579b5b8 x3 :0000000000000001 [ 540.579518] x2 : ffffffdebf7e3940 x1 : 0000000000235c00 x0 :0000000000235800 [ 540.579524] Call trace: [ 540.579527] __pageblock_pfn_to_page+0x6c/0x14c [ 540.579533] compact_zone+0x994/0x1058 [ 540.579536] try_to_compact_pages+0x128/0x378 [ 540.579540] __alloc_pages_direct_compact+0x80/0x2b0 [ 540.579544] __alloc_pages_slowpath+0x5c0/0xe10 [ 540.579547] __alloc_pages+0x250/0x2d0 [ 540.579550] __iommu_dma_alloc_noncontiguous+0x13c/0x3fc [ 540.579561] iommu_dma_alloc+0xa0/0x320 [ 540.579565] dma_alloc_attrs+0xd4/0x108 [[email protected]: use kfree_rcu() in place of synchronize_rcu(), per David] Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Fixes: f46edbd ("mm/sparsemem: add helpers track active portions of a section at boot") Signed-off-by: Charan Teja Kalla <[email protected]> Cc: Aneesh Kumar K.V <[email protected]> Cc: Dan Williams <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: Mel Gorman <[email protected]> Cc: Oscar Salvador <[email protected]> Cc: Vlastimil Babka <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 7fbb5e1 commit 5ec8e8e

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

include/linux/mmzone.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,6 +1799,7 @@ static inline unsigned long section_nr_to_pfn(unsigned long sec)
17991799
#define SUBSECTION_ALIGN_DOWN(pfn) ((pfn) & PAGE_SUBSECTION_MASK)
18001800

18011801
struct mem_section_usage {
1802+
struct rcu_head rcu;
18021803
#ifdef CONFIG_SPARSEMEM_VMEMMAP
18031804
DECLARE_BITMAP(subsection_map, SUBSECTIONS_PER_SECTION);
18041805
#endif
@@ -1992,7 +1993,7 @@ static inline int pfn_section_valid(struct mem_section *ms, unsigned long pfn)
19921993
{
19931994
int idx = subsection_map_index(pfn);
19941995

1995-
return test_bit(idx, ms->usage->subsection_map);
1996+
return test_bit(idx, READ_ONCE(ms->usage)->subsection_map);
19961997
}
19971998
#else
19981999
static inline int pfn_section_valid(struct mem_section *ms, unsigned long pfn)
@@ -2016,6 +2017,7 @@ static inline int pfn_section_valid(struct mem_section *ms, unsigned long pfn)
20162017
static inline int pfn_valid(unsigned long pfn)
20172018
{
20182019
struct mem_section *ms;
2020+
int ret;
20192021

20202022
/*
20212023
* Ensure the upper PAGE_SHIFT bits are clear in the
@@ -2029,13 +2031,19 @@ static inline int pfn_valid(unsigned long pfn)
20292031
if (pfn_to_section_nr(pfn) >= NR_MEM_SECTIONS)
20302032
return 0;
20312033
ms = __pfn_to_section(pfn);
2032-
if (!valid_section(ms))
2034+
rcu_read_lock();
2035+
if (!valid_section(ms)) {
2036+
rcu_read_unlock();
20332037
return 0;
2038+
}
20342039
/*
20352040
* Traditionally early sections always returned pfn_valid() for
20362041
* the entire section-sized span.
20372042
*/
2038-
return early_section(ms) || pfn_section_valid(ms, pfn);
2043+
ret = early_section(ms) || pfn_section_valid(ms, pfn);
2044+
rcu_read_unlock();
2045+
2046+
return ret;
20392047
}
20402048
#endif
20412049

mm/sparse.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,13 @@ static void section_deactivate(unsigned long pfn, unsigned long nr_pages,
791791
if (empty) {
792792
unsigned long section_nr = pfn_to_section_nr(pfn);
793793

794+
/*
795+
* Mark the section invalid so that valid_section()
796+
* return false. This prevents code from dereferencing
797+
* ms->usage array.
798+
*/
799+
ms->section_mem_map &= ~SECTION_HAS_MEM_MAP;
800+
794801
/*
795802
* When removing an early section, the usage map is kept (as the
796803
* usage maps of other sections fall into the same page). It
@@ -799,16 +806,10 @@ static void section_deactivate(unsigned long pfn, unsigned long nr_pages,
799806
* was allocated during boot.
800807
*/
801808
if (!PageReserved(virt_to_page(ms->usage))) {
802-
kfree(ms->usage);
803-
ms->usage = NULL;
809+
kfree_rcu(ms->usage, rcu);
810+
WRITE_ONCE(ms->usage, NULL);
804811
}
805812
memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr);
806-
/*
807-
* Mark the section invalid so that valid_section()
808-
* return false. This prevents code from dereferencing
809-
* ms->usage array.
810-
*/
811-
ms->section_mem_map &= ~SECTION_HAS_MEM_MAP;
812813
}
813814

814815
/*

0 commit comments

Comments
 (0)