blob: 41907d25ed384e2ee42e46d1df461bbd2886732f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* pgalloc.c: page directory & page table allocation
2 *
3 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/mm.h>
15#include <linux/highmem.h>
Christoph Lameter8defab32007-05-09 02:32:48 -070016#include <linux/quicklist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/pgalloc.h>
18#include <asm/page.h>
19#include <asm/cacheflush.h>
20
21pgd_t swapper_pg_dir[PTRS_PER_PGD] __attribute__((aligned(PAGE_SIZE)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
24{
25 pte_t *pte = (pte_t *)__get_free_page(GFP_KERNEL|__GFP_REPEAT);
26 if (pte)
27 clear_page(pte);
28 return pte;
29}
30
Martin Schwidefsky2f569af2008-02-08 04:22:04 -080031pgtable_t pte_alloc_one(struct mm_struct *mm, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
33 struct page *page;
34
35#ifdef CONFIG_HIGHPTE
36 page = alloc_pages(GFP_KERNEL|__GFP_HIGHMEM|__GFP_REPEAT, 0);
37#else
38 page = alloc_pages(GFP_KERNEL|__GFP_REPEAT, 0);
39#endif
Kirill A. Shutemov3b9cf772013-11-14 14:31:30 -080040 if (!page)
41 return NULL;
42
43 clear_highpage(page);
44 if (!pgtable_page_ctor(page)) {
45 __free_page(page);
46 return NULL;
Martin Schwidefsky2f569af2008-02-08 04:22:04 -080047 }
Kirill A. Shutemov3b9cf772013-11-14 14:31:30 -080048 flush_dcache_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 return page;
50}
51
52void __set_pmd(pmd_t *pmdptr, unsigned long pmd)
53{
54 unsigned long *__ste_p = pmdptr->ste;
55 int loop;
56
57 if (!pmd) {
58 memset(__ste_p, 0, PME_SIZE);
59 }
60 else {
61 BUG_ON(pmd & (0x3f00 | xAMPRx_SS | 0xe));
62
63 for (loop = PME_SIZE; loop > 0; loop -= 4) {
64 *__ste_p++ = pmd;
65 pmd += __frv_PT_SIZE;
66 }
67 }
68
69 frv_dcache_writeback((unsigned long) pmdptr, (unsigned long) (pmdptr + 1));
70}
71
72/*
73 * List of all pgd's needed for non-PAE so it can invalidate entries
74 * in both cached and uncached pgd's; not needed for PAE since the
75 * kernel pmd is shared. If PAE were not to share the pmd a similar
76 * tactic would be needed. This is essentially codepath-based locking
77 * against pageattr.c; it is the unique case in which a valid change
78 * of kernel pagetables can't be lazily synchronized by vmalloc faults.
79 * vmalloc faults work because attached pagetables are never freed.
80 * If the locking proves to be non-performant, a ticketing scheme with
81 * checks at dup_mmap(), exec(), and other mmlist addition points
82 * could be used. The locking scheme was chosen on the basis of
83 * manfred's recommendations and having no core impact whatsoever.
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010084 * -- nyc
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 */
86DEFINE_SPINLOCK(pgd_lock);
87struct page *pgd_list;
88
89static inline void pgd_list_add(pgd_t *pgd)
90{
91 struct page *page = virt_to_page(pgd);
92 page->index = (unsigned long) pgd_list;
93 if (pgd_list)
David Howells8080f232005-11-28 13:43:51 -080094 set_page_private(pgd_list, (unsigned long) &page->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 pgd_list = page;
Hugh Dickins4c21e2f2005-10-29 18:16:40 -070096 set_page_private(page, (unsigned long)&pgd_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
99static inline void pgd_list_del(pgd_t *pgd)
100{
101 struct page *next, **pprev, *page = virt_to_page(pgd);
102 next = (struct page *) page->index;
David Howells8080f232005-11-28 13:43:51 -0800103 pprev = (struct page **) page_private(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 *pprev = next;
105 if (next)
David Howells8080f232005-11-28 13:43:51 -0800106 set_page_private(next, (unsigned long) pprev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Christoph Lameter8defab32007-05-09 02:32:48 -0700109void pgd_ctor(void *pgd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 unsigned long flags;
112
113 if (PTRS_PER_PMD == 1)
114 spin_lock_irqsave(&pgd_lock, flags);
115
116 memcpy((pgd_t *) pgd + USER_PGDS_IN_LAST_PML4,
117 swapper_pg_dir + USER_PGDS_IN_LAST_PML4,
118 (PTRS_PER_PGD - USER_PGDS_IN_LAST_PML4) * sizeof(pgd_t));
119
120 if (PTRS_PER_PMD > 1)
121 return;
122
123 pgd_list_add(pgd);
124 spin_unlock_irqrestore(&pgd_lock, flags);
125 memset(pgd, 0, USER_PGDS_IN_LAST_PML4 * sizeof(pgd_t));
126}
127
128/* never called when PTRS_PER_PMD > 1 */
Christoph Lameter8defab32007-05-09 02:32:48 -0700129void pgd_dtor(void *pgd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 unsigned long flags; /* can be called from interrupt context */
132
133 spin_lock_irqsave(&pgd_lock, flags);
134 pgd_list_del(pgd);
135 spin_unlock_irqrestore(&pgd_lock, flags);
136}
137
138pgd_t *pgd_alloc(struct mm_struct *mm)
139{
Greg Dietschedbfe8982011-07-29 16:46:16 +0100140 return quicklist_alloc(0, GFP_KERNEL, pgd_ctor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -0800143void pgd_free(struct mm_struct *mm, pgd_t *pgd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 /* in the non-PAE case, clear_page_tables() clears user pgd entries */
Christoph Lameter8defab32007-05-09 02:32:48 -0700146 quicklist_free(0, pgd_dtor, pgd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149void __init pgtable_cache_init(void)
150{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
Christoph Lameter8defab32007-05-09 02:32:48 -0700152
153void check_pgt_cache(void)
154{
155 quicklist_trim(0, pgd_dtor, 25, 16);
156}
157