MIPS: fix pfn_valid() for FLATMEM
PATCH 1/1 MIPS: fix pfn_valid() for FLATMEM
When CONFIG_FLATMEM enabled, STD/Hiberation will fail on YeeLoong laptop, This patch fixes it:
if pfn is between min_low_pfn and max_mapnr, the old pfn_valid() will return TRUE, but in reality, if the memory is not continuous, it should be false. for example:
$ cat /proc/iomem | grep "System RAM" 00000000-0fffffff : System RAM 90000000-bfffffff : System RAM
as we can see, it is not continuous, so, some of the memory is not valid but regarded as valid by pfn_valid(), and at last make STD/Hibernate fail when shrinking a too large number of invalid memory.
Here, we fix it via checking pfn is in the "System RAM" or not. and Seems pfn_valid() is not called in assembly code, we move it to "!__ASSEMBLY__" to ensure we can simply declare it via "extern int pfn_valid(unsigned long)" without Compiling Error.
(This -v1 version incorporates feedback from Pavel Machek <[email protected]>
and Sergei Shtylyov <[email protected]>)
--- arch/mips/include/asm/page.h | 50 ++++++++++++++++++----------------------- arch/mips/mm/page.c | 17 ++++++++++++++ 2 files changed, 39 insertions(+), 28 deletions(-)
arch/mips/include/asm/page.h +22 -28
@@ -145,36 +145,9 @@ typedef struct { unsigned long pgprot; } pgprot_t;*/ #define ptep_buddy (x) ((pte_t *)((unsigned long)(x) ^ sizeof(pte_t))) -#endif /* !__ASSEMBLY__ */ - -/* -* __pa()/__va() should be used only during mem init. -*/ -#ifdef CONFIG_64BIT -#define __pa (x) \ -({ \ - unsigned long __x = (unsigned long)(x); \ - __x < CKSEG0 ? XPHYSADDR(__x) : CPHYSADDR(__x); \ -}) -#else -#define __pa (x) \ - ((unsigned long)(x) - PAGE_OFFSET + PHYS_OFFSET) -#endif -#define __va (x) ((void *)((unsigned long)(x) + PAGE_OFFSET - PHYS_OFFSET)) -#define __pa_symbol (x) __pa(RELOC_HIDE((unsigned long)(x), 0)) - -#define pfn_to_kaddr (pfn) __va((pfn) << PAGE_SHIFT) -#ifdef CONFIG_FLATMEM -#define pfn_valid(pfn) \ -({ \ - unsigned long __pfn = (pfn); \ - /* avoid <linux/bootmem.h> include hell */ \ - extern unsigned long min_low_pfn; \ - \ - __pfn >= min_low_pfn && __pfn < max_mapnr; \ -}) +extern int pfn_valid (unsigned long );#elif defined(CONFIG_SPARSEMEM )
@@ -193,6 +166,27 @@ typedef struct { unsigned long pgprot; } pgprot_t;#endif + +#endif /* !__ASSEMBLY__ */ + +/* +* __pa()/__va() should be used only during mem init. +*/ +#ifdef CONFIG_64BIT +#define __pa (x) \ +({ \ + unsigned long __x = (unsigned long)(x); \ + __x < CKSEG0 ? XPHYSADDR(__x) : CPHYSADDR(__x); \ +}) +#else +#define __pa (x) \ + ((unsigned long)(x) - PAGE_OFFSET + PHYS_OFFSET) +#endif +#define __va (x) ((void *)((unsigned long)(x) + PAGE_OFFSET - PHYS_OFFSET)) +#define __pa_symbol (x) __pa(RELOC_HIDE((unsigned long)(x), 0)) + +#define pfn_to_kaddr (pfn) __va((pfn) << PAGE_SHIFT) +#define virt_to_page (kaddr) pfn_to_page(PFN_DOWN(virt_to_phys(kaddr)))#define virt_addr_valid (kaddr) pfn_valid(PFN_DOWN(virt_to_phys(kaddr)))
arch/mips/mm/page.c +17 -0
@@ -689,3 +689,20 @@ void copy_page(void *to, void *from) }#endif /* CONFIG_SIBYTE_DMA_PAGEOPS */ + +#ifdef CONFIG_FLATMEM +int pfn_valid (unsigned long pfn) +{ +int i; + +for (i =0 ; i < boot_mem_map.nr_map; i++) { +if (boot_mem_map.map[i].type ==BOOT_MEM_RAM ) { +if ((pfn >=PFN_DOWN (boot_mem_map.map[i].addr)) && + (pfn <PFN_UP (boot_mem_map.map[i].addr + + boot_mem_map.map[i].size))) +return 1 ; + } + } +return 0 ; +} +#endif
Signature · 1 line
1.6.2.1
Quoted 15 lines · Wu Zhangjin
When
CONFIG_FLATMEMenabled, STD/Hiberation will fail on YeeLoong laptop, This patch fixes it:if pfn is between
min_low_pfnandmax_mapnr, the oldpfn_valid()will return TRUE, but in reality, if the memory is not continuous, it should be false. for example:$ cat /proc/iomem | grep "System RAM" 00000000-0fffffff : System RAM 90000000-bfffffff : System RAMas we can see, it is not continuous, so, some of the memory is not valid but regarded as valid by
pfn_valid(), and at last make STD/Hibernate fail when shrinking a too large number of invalid memory.
Quoted 7 lines
Here, we fix it via checking pfn is in the "System RAM" or not. and Seems
pfn_valid()is not called in assembly code, we move it to "!__ASSEMBLY__" to ensure we can simply declare it via "extern intpfn_valid(unsigned long)" without Compiling Error.(This -v1 version incorporates feedback from Pavel Machek <[email protected]>
and Sergei Shtylyov <[email protected]>)
Hmm. What exactly would be wrong with using register_nosave_region() or register_nosave_region_late() like x86 does?
Thanks,
Rafael
Quoted 10 lines · Rafael J. Wysocki
Quoted 7 lines
Here, we fix it via checking pfn is in the "System RAM" or not. and Seems
pfn_valid()is not called in assembly code, we move it to "!__ASSEMBLY__" to ensure we can simply declare it via "extern intpfn_valid(unsigned long)" without Compiling Error.(This -v1 version incorporates feedback from Pavel Machek <[email protected]>
and Sergei Shtylyov <[email protected]>)Hmm. What exactly would be wrong with using
register_nosave_region()orregister_nosave_region_late()like x86 does?
That seems to be more the fix than pfn_valid / PageReserved fiddlery we were discussing in the other thread. Thanks!
Ralf
Quoted 7 lines · Ralf Baechle
Quoted 5 lines · Rafael J. Wysocki
Quoted 5 lines
Here, we fix it via checking pfn is in the "System RAM" or not. and Seems
pfn_valid()is not called in assembly code, we move it to "!__ASSEMBLY__" to ensure we can simply declare it via "extern intpfn_valid(unsigned long)" without Compiling Error.
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
(This -v1 version incorporates feedback from Pavel Machek <[email protected]>
and Sergei Shtylyov <[email protected]>)Hmm. What exactly would be wrong with using
register_nosave_region()orregister_nosave_region_late()like x86 does?
Have tried to use register_nosave_region(), it works, but seems there is something else wrong there.
Quoted 3 lines
That seems to be more the fix than
pfn_valid/ PageReserved fiddlery we were discussing in the other thread. Thanks!
Just checked the arch/mips/loongson/common/mem.c, Seems it did not register any reserved pages, in reality, two sections of memory are reserved.
here should be the patch, tested it, works.
(quoted diff) +0 -0
b/arch/mips/loongson/common/mem.c
index 7c92f79..069d20b 100644
arch/mips/loongson/common/mem.c +3 -1
@@ -15,11 +15,17 @@void __init prom_init_memory (void ) { -add_memory_region (0x0 , (memsize <<20 ),BOOT_MEM_RAM ); +add_memory_region (0x0 , memsize <<20 ,BOOT_MEM_RAM ); + + add_memory_region(memsize << 20, LOONGSON_PCI_MEM_START - (memsize
<<
+ 20), BOOT_MEM_RESERVED);
#ifdef CONFIG_64BIT
if (highmemsize > 0)
add_memory_region(LOONGSON_HIGHMEM_START,
highmemsize << 20, BOOT_MEM_RAM);
+
+ add_memory_region(LOONGSON_PCI_MEM_END + 1, LOONGSON_HIGHMEM_START
-
+ LOONGSON_PCI_MEM_END - 1, BOOT_MEM_RESERVED);
#endif /* CONFIG_64BIT */
}
here is latest result:
$ cat /proc/iomem
00000000-0fffffff : System RAM
00200000-0049ba17 : Kernel code
0049ba18-005415ff : Kernel data
10000000-3fffffff : reserved ---> reserved page
40000000-7fffffff : pci memory space
40000000-40ffffff : 0000:00:08.0
41000000-4101ffff : 0000:00:07.0
41020000-41020fff : 0000:00:09.0
41020000-41020fff : ohci_hcd
41021000-41021fff : 0000:00:0e.4
41021000-41021fff : ohci_hcd
41022000-41022fff : 0000:00:0e.5
41022000-41022fff : ehci_hcd
41023000-410230ff : 0000:00:07.0
41023000-410230ff : 8139too
41023100-410231ff : 0000:00:09.1
41023100-410231ff : ehci_hcd
80000000-8fffffff : reserved --> reserved page
90000000-bfffffff : System RAMand what about pfn_valid(), is there a need to make it "robust"? or the above patch is enough?
if the above patch is okay, I will send it to you later.
Regards,
Wu Zhangjin
Quoted 42 lines · Wu Zhangjin
Quoted 7 lines · Ralf Baechle
Quoted 5 lines · Rafael J. Wysocki
Quoted 5 lines
Here, we fix it via checking pfn is in the "System RAM" or not. and Seems
pfn_valid()is not called in assembly code, we move it to "!__ASSEMBLY__" to ensure we can simply declare it via "extern intpfn_valid(unsigned long)" without Compiling Error.Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
(This -v1 version incorporates feedback from Pavel Machek <[email protected]>
and Sergei Shtylyov <[email protected]>)Hmm. What exactly would be wrong with using
register_nosave_region()orregister_nosave_region_late()like x86 does?Have tried to use
register_nosave_region(), it works, but seems there is something else wrong there.Quoted 3 lines
That seems to be more the fix than
pfn_valid/ PageReserved fiddlery we were discussing in the other thread. Thanks!Just checked the
arch/mips/loongson/common/mem.c, Seems it did not register any reserved pages, in reality, two sections of memory are reserved.here should be the patch, tested it, works.
arch/mips/loongson/common/mem.c +3
-1@@ -15,11 +15,17 @@void __init prom_init_memory (void ) { -add_memory_region (0x0 , (memsize <<20 ),BOOT_MEM_RAM ); +add_memory_region (0x0 , memsize <<20 ,BOOT_MEM_RAM ); + + add_memory_region(memsize << 20, LOONGSON_PCI_MEM_START - (memsize<<
arch/mips/loongson/common/mem.c +1
-0+ 20), BOOT_MEM_RESERVED);
arch/mips/loongson/common/mem.c
Quoted 10 lines
arch/mips/loongson/common/mem.c +3
-1#ifdef CONFIG_64BIT if (highmemsize >0 )add_memory_region (LOONGSON_HIGHMEM_START , highmemsize <<20 ,BOOT_MEM_RAM ); + +add_memory_region (LOONGSON_PCI_MEM_END +1 ,LOONGSON_HIGHMEM_START - + LOONGSON_PCI_MEM_END - 1, BOOT_MEM_RESERVED);#endif /* CONFIG_64BIT */ }
The above patch can not fix the problem when enabled FLATMEM in linux-2.6.32-rc3, the real problem should be that we need register the "pci memory space" as nosave pages, and also, the above "reserved"(not memory) pages should be registered as nosave pages. but the simpler solution should be the pfn_valid() I sent out in this E-mail thread, we just need to check whether they are "valid", if they are "System RAM"(BOOT_MEM_RAM or BOOT_MEM_ROM_DATA), they should be valid.
and what's more? should be register "pci memory space" as nosave pages for all architecture?
Regards,
Wu Zhangjin
Quoted 10 lines · Wu Zhangjin
The above patch can not fix the problem when enabled FLATMEM in linux-2.6.32-rc3, the real problem should be that we need register the "pci memory space" as nosave pages, and also, the above "reserved"(not memory) pages should be registered as nosave pages. but the simpler solution should be the
pfn_valid()I sent out in this E-mail thread, we just need to check whether they are "valid", if they are "System RAM"(BOOT_MEM_RAM orBOOT_MEM_ROM_DATA), they should be valid.and what's more? should be register "pci memory space" as nosave pages for all architecture?
No. You only see this problem because your PCI memory space is between the lowest and the highest memory address. Other systems don't have this issue because they either use the discontig or sparse memory models.
Btw, for systems that actually have memory in the 90000000-bfffffff range and are running a 64-bit kernel with 4k ages the flatmem memory model will waste 28MB of RAM; with 16k pages it's still 7MB.
Time to say gooebye to flatmem?
Ralf
Quoted 17 lines · Ralf Baechle
Quoted 10 lines · Wu Zhangjin
The above patch can not fix the problem when enabled FLATMEM in linux-2.6.32-rc3, the real problem should be that we need register the "pci memory space" as nosave pages, and also, the above "reserved"(not memory) pages should be registered as nosave pages. but the simpler solution should be the
pfn_valid()I sent out in this E-mail thread, we just need to check whether they are "valid", if they are "System RAM"(BOOT_MEM_RAM orBOOT_MEM_ROM_DATA), they should be valid.and what's more? should be register "pci memory space" as nosave pages for all architecture?
No. You only see this problem because your PCI memory space is between the lowest and the highest memory address. Other systems don't have this issue because they either use the discontig or sparse memory models.
Quoted 5 lines
Btw, for systems that actually have memory in the 90000000-bfffffff range and are running a 64-bit kernel with 4k ages the flatmem memory model will waste 28MB of RAM; with 16k pages it's still 7MB.
Time to say gooebye to flatmem?
Okay, I will enable SPARSEMEM by default in the defconfig later.
Regards,
Wu Zhangjin