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