x86, pat: Update the page flags for memtype atomically instead of using memtype_lock

While testing an application using the xpmem (out of kernel) driver, we
noticed a significant page fault rate reduction of x86_64 with respect
to ia64. For one test running with 32 cpus, one thread per cpu, it
took 01:08 for each of the threads to vm_insert_pfn 2GB worth of pages.
For the same test running on 256 cpus, one thread per cpu, it took 14:48
to vm_insert_pfn 2 GB worth of pages.

The slowdown was tracked to lookup_memtype which acquires the
spinlock memtype_lock. This heavily contended lock was slowing down
vm_insert_pfn().

With the cmpxchg on page->flags method, both the 32 cpu and 256 cpu
cases take approx 00:01.3 seconds to complete.

Signed-off-by: Robin Holt <holt@sgi.com>
LKML-Reference: <20100423153627.751194346@gulag1.americas.sgi.com>
Cc: Venkatesh Pallipadi <venkatesh.pallipadi@gmail.com>
Cc: Rafael Wysocki <rjw@novell.com>
Reviewed-by: Suresh Siddha <suresh.b.siddha@intel.com>
Signed-off-by: H. Peter Anvin <hpa@zytor.com>

authored by Robin Holt and committed by H. Peter Anvin 1f9cc3cb 4daa2a80

+25 -27
+25 -19
arch/x86/include/asm/cacheflush.h
··· 44 memcpy(dst, src, len); 45 } 46 47 - #define PG_WC PG_arch_1 48 - PAGEFLAG(WC, WC) 49 - 50 #ifdef CONFIG_X86_PAT 51 /* 52 * X86 PAT uses page flags WC and Uncached together to keep track of ··· 52 * _PAGE_CACHE_UC_MINUS and fourth state where page's memory type has not 53 * been changed from its default (value of -1 used to denote this). 54 * Note we do not support _PAGE_CACHE_UC here. 55 - * 56 - * Caller must hold memtype_lock for atomicity. 57 */ 58 static inline unsigned long get_page_memtype(struct page *pg) 59 { 60 - if (!PageUncached(pg) && !PageWC(pg)) 61 return -1; 62 - else if (!PageUncached(pg) && PageWC(pg)) 63 return _PAGE_CACHE_WC; 64 - else if (PageUncached(pg) && !PageWC(pg)) 65 return _PAGE_CACHE_UC_MINUS; 66 else 67 return _PAGE_CACHE_WB; ··· 77 78 static inline void set_page_memtype(struct page *pg, unsigned long memtype) 79 { 80 switch (memtype) { 81 case _PAGE_CACHE_WC: 82 - ClearPageUncached(pg); 83 - SetPageWC(pg); 84 break; 85 case _PAGE_CACHE_UC_MINUS: 86 - SetPageUncached(pg); 87 - ClearPageWC(pg); 88 break; 89 case _PAGE_CACHE_WB: 90 - SetPageUncached(pg); 91 - SetPageWC(pg); 92 - break; 93 - default: 94 - case -1: 95 - ClearPageUncached(pg); 96 - ClearPageWC(pg); 97 break; 98 } 99 } 100 #else 101 static inline unsigned long get_page_memtype(struct page *pg) { return -1; }
··· 44 memcpy(dst, src, len); 45 } 46 47 #ifdef CONFIG_X86_PAT 48 /* 49 * X86 PAT uses page flags WC and Uncached together to keep track of ··· 55 * _PAGE_CACHE_UC_MINUS and fourth state where page's memory type has not 56 * been changed from its default (value of -1 used to denote this). 57 * Note we do not support _PAGE_CACHE_UC here. 58 */ 59 + 60 + #define _PGMT_DEFAULT 0 61 + #define _PGMT_WC (1UL << PG_arch_1) 62 + #define _PGMT_UC_MINUS (1UL << PG_uncached) 63 + #define _PGMT_WB (1UL << PG_uncached | 1UL << PG_arch_1) 64 + #define _PGMT_MASK (1UL << PG_uncached | 1UL << PG_arch_1) 65 + #define _PGMT_CLEAR_MASK (~_PGMT_MASK) 66 + 67 static inline unsigned long get_page_memtype(struct page *pg) 68 { 69 + unsigned long pg_flags = pg->flags & _PGMT_MASK; 70 + 71 + if (pg_flags == _PGMT_DEFAULT) 72 return -1; 73 + else if (pg_flags == _PGMT_WC) 74 return _PAGE_CACHE_WC; 75 + else if (pg_flags == _PGMT_UC_MINUS) 76 return _PAGE_CACHE_UC_MINUS; 77 else 78 return _PAGE_CACHE_WB; ··· 72 73 static inline void set_page_memtype(struct page *pg, unsigned long memtype) 74 { 75 + unsigned long memtype_flags = _PGMT_DEFAULT; 76 + unsigned long old_flags; 77 + unsigned long new_flags; 78 + 79 switch (memtype) { 80 case _PAGE_CACHE_WC: 81 + memtype_flags = _PGMT_WC; 82 break; 83 case _PAGE_CACHE_UC_MINUS: 84 + memtype_flags = _PGMT_UC_MINUS; 85 break; 86 case _PAGE_CACHE_WB: 87 + memtype_flags = _PGMT_WB; 88 break; 89 } 90 + 91 + do { 92 + old_flags = pg->flags; 93 + new_flags = (old_flags & _PGMT_CLEAR_MASK) | memtype_flags; 94 + } while (cmpxchg(&pg->flags, old_flags, new_flags) != old_flags); 95 } 96 #else 97 static inline unsigned long get_page_memtype(struct page *pg) { return -1; }
-8
arch/x86/mm/pat.c
··· 190 * Here we do two pass: 191 * - Find the memtype of all the pages in the range, look for any conflicts 192 * - In case of no conflicts, set the new memtype for pages in the range 193 - * 194 - * Caller must hold memtype_lock for atomicity. 195 */ 196 static int reserve_ram_pages_type(u64 start, u64 end, unsigned long req_type, 197 unsigned long *new_type) ··· 295 is_range_ram = pat_pagerange_is_ram(start, end); 296 if (is_range_ram == 1) { 297 298 - spin_lock(&memtype_lock); 299 err = reserve_ram_pages_type(start, end, req_type, new_type); 300 - spin_unlock(&memtype_lock); 301 302 return err; 303 } else if (is_range_ram < 0) { ··· 347 is_range_ram = pat_pagerange_is_ram(start, end); 348 if (is_range_ram == 1) { 349 350 - spin_lock(&memtype_lock); 351 err = free_ram_pages_type(start, end); 352 - spin_unlock(&memtype_lock); 353 354 return err; 355 } else if (is_range_ram < 0) { ··· 388 389 if (pat_pagerange_is_ram(paddr, paddr + PAGE_SIZE)) { 390 struct page *page; 391 - spin_lock(&memtype_lock); 392 page = pfn_to_page(paddr >> PAGE_SHIFT); 393 rettype = get_page_memtype(page); 394 - spin_unlock(&memtype_lock); 395 /* 396 * -1 from get_page_memtype() implies RAM page is in its 397 * default state and not reserved, and hence of type WB
··· 190 * Here we do two pass: 191 * - Find the memtype of all the pages in the range, look for any conflicts 192 * - In case of no conflicts, set the new memtype for pages in the range 193 */ 194 static int reserve_ram_pages_type(u64 start, u64 end, unsigned long req_type, 195 unsigned long *new_type) ··· 297 is_range_ram = pat_pagerange_is_ram(start, end); 298 if (is_range_ram == 1) { 299 300 err = reserve_ram_pages_type(start, end, req_type, new_type); 301 302 return err; 303 } else if (is_range_ram < 0) { ··· 351 is_range_ram = pat_pagerange_is_ram(start, end); 352 if (is_range_ram == 1) { 353 354 err = free_ram_pages_type(start, end); 355 356 return err; 357 } else if (is_range_ram < 0) { ··· 394 395 if (pat_pagerange_is_ram(paddr, paddr + PAGE_SIZE)) { 396 struct page *page; 397 page = pfn_to_page(paddr >> PAGE_SHIFT); 398 rettype = get_page_memtype(page); 399 /* 400 * -1 from get_page_memtype() implies RAM page is in its 401 * default state and not reserved, and hence of type WB