blob: b467be6d367f8b3725c1b7449e9d052ea1c3e768 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _X86_64_PGALLOC_H
2#define _X86_64_PGALLOC_H
3
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include <asm/pda.h>
5#include <linux/threads.h>
6#include <linux/mm.h>
Christoph Lameter34feb2c2007-07-21 17:10:30 +02007#include <linux/quicklist.h>
8
9#define QUICK_PGD 0 /* We preserve special mappings over free */
10#define QUICK_PT 1 /* Other page table pages that are zero on free */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12#define pmd_populate_kernel(mm, pmd, pte) \
13 set_pmd(pmd, __pmd(_PAGE_TABLE | __pa(pte)))
14#define pud_populate(mm, pud, pmd) \
15 set_pud(pud, __pud(_PAGE_TABLE | __pa(pmd)))
16#define pgd_populate(mm, pgd, pud) \
17 set_pgd(pgd, __pgd(_PAGE_TABLE | __pa(pud)))
18
19static inline void pmd_populate(struct mm_struct *mm, pmd_t *pmd, struct page *pte)
20{
21 set_pmd(pmd, __pmd(_PAGE_TABLE | (page_to_pfn(pte) << PAGE_SHIFT)));
22}
23
Adrian Bunk9c0aa0f2005-09-12 18:49:24 +020024static inline void pmd_free(pmd_t *pmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
26 BUG_ON((unsigned long)pmd & (PAGE_SIZE-1));
Christoph Lameter34feb2c2007-07-21 17:10:30 +020027 quicklist_free(QUICK_PT, NULL, pmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028}
29
30static inline pmd_t *pmd_alloc_one (struct mm_struct *mm, unsigned long addr)
31{
Christoph Lameter34feb2c2007-07-21 17:10:30 +020032 return (pmd_t *)quicklist_alloc(QUICK_PT, GFP_KERNEL|__GFP_REPEAT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033}
34
35static inline pud_t *pud_alloc_one(struct mm_struct *mm, unsigned long addr)
36{
Christoph Lameter34feb2c2007-07-21 17:10:30 +020037 return (pud_t *)quicklist_alloc(QUICK_PT, GFP_KERNEL|__GFP_REPEAT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038}
39
40static inline void pud_free (pud_t *pud)
41{
42 BUG_ON((unsigned long)pud & (PAGE_SIZE-1));
Christoph Lameter34feb2c2007-07-21 17:10:30 +020043 quicklist_free(QUICK_PT, NULL, pud);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044}
45
Jan Beulich8c914cb2006-03-25 16:29:40 +010046static inline void pgd_list_add(pgd_t *pgd)
47{
48 struct page *page = virt_to_page(pgd);
49
50 spin_lock(&pgd_lock);
Christoph Lameter2bff7382007-05-02 19:27:10 +020051 list_add(&page->lru, &pgd_list);
Jan Beulich8c914cb2006-03-25 16:29:40 +010052 spin_unlock(&pgd_lock);
53}
54
55static inline void pgd_list_del(pgd_t *pgd)
56{
Christoph Lameter2bff7382007-05-02 19:27:10 +020057 struct page *page = virt_to_page(pgd);
Jan Beulich8c914cb2006-03-25 16:29:40 +010058
59 spin_lock(&pgd_lock);
Christoph Lameter2bff7382007-05-02 19:27:10 +020060 list_del(&page->lru);
Jan Beulich8c914cb2006-03-25 16:29:40 +010061 spin_unlock(&pgd_lock);
62}
63
Christoph Lameter34feb2c2007-07-21 17:10:30 +020064static inline void pgd_ctor(void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 unsigned boundary;
Christoph Lameter34feb2c2007-07-21 17:10:30 +020067 pgd_t *pgd = x;
68 struct page *page = virt_to_page(pgd);
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 /*
71 * Copy kernel pointers in from init.
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 */
73 boundary = pgd_index(__PAGE_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 memcpy(pgd + boundary,
Christoph Lameter34feb2c2007-07-21 17:10:30 +020075 init_level4_pgt + boundary,
76 (PTRS_PER_PGD - boundary) * sizeof(pgd_t));
77
78 spin_lock(&pgd_lock);
79 list_add(&page->lru, &pgd_list);
80 spin_unlock(&pgd_lock);
81}
82
83static inline void pgd_dtor(void *x)
84{
85 pgd_t *pgd = x;
86 struct page *page = virt_to_page(pgd);
87
88 spin_lock(&pgd_lock);
89 list_del(&page->lru);
90 spin_unlock(&pgd_lock);
91}
92
93static inline pgd_t *pgd_alloc(struct mm_struct *mm)
94{
95 pgd_t *pgd = (pgd_t *)quicklist_alloc(QUICK_PGD,
96 GFP_KERNEL|__GFP_REPEAT, pgd_ctor);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return pgd;
98}
99
100static inline void pgd_free(pgd_t *pgd)
101{
102 BUG_ON((unsigned long)pgd & (PAGE_SIZE-1));
Christoph Lameter34feb2c2007-07-21 17:10:30 +0200103 quicklist_free(QUICK_PGD, pgd_dtor, pgd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
106static inline pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address)
107{
Christoph Lameter34feb2c2007-07-21 17:10:30 +0200108 return (pte_t *)quicklist_alloc(QUICK_PT, GFP_KERNEL|__GFP_REPEAT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
111static inline struct page *pte_alloc_one(struct mm_struct *mm, unsigned long address)
112{
Christoph Lameter34feb2c2007-07-21 17:10:30 +0200113 void *p = (void *)quicklist_alloc(QUICK_PT, GFP_KERNEL|__GFP_REPEAT, NULL);
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if (!p)
116 return NULL;
117 return virt_to_page(p);
118}
119
120/* Should really implement gc for free page table pages. This could be
121 done with a reference count in struct page. */
122
Adrian Bunk9c0aa0f2005-09-12 18:49:24 +0200123static inline void pte_free_kernel(pte_t *pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 BUG_ON((unsigned long)pte & (PAGE_SIZE-1));
Christoph Lameter34feb2c2007-07-21 17:10:30 +0200126 quicklist_free(QUICK_PT, NULL, pte);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
Adrian Bunk9c0aa0f2005-09-12 18:49:24 +0200129static inline void pte_free(struct page *pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Christoph Lameter34feb2c2007-07-21 17:10:30 +0200131 quicklist_free_page(QUICK_PT, NULL, pte);
132}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Christoph Lameter34feb2c2007-07-21 17:10:30 +0200134#define __pte_free_tlb(tlb,pte) quicklist_free_page(QUICK_PT, NULL,(pte))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Christoph Lameter34feb2c2007-07-21 17:10:30 +0200136#define __pmd_free_tlb(tlb,x) quicklist_free(QUICK_PT, NULL, (x))
137#define __pud_free_tlb(tlb,x) quicklist_free(QUICK_PT, NULL, (x))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Christoph Lameter34feb2c2007-07-21 17:10:30 +0200139static inline void check_pgt_cache(void)
140{
141 quicklist_trim(QUICK_PGD, pgd_dtor, 25, 16);
142 quicklist_trim(QUICK_PT, NULL, 25, 16);
143}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144#endif /* _X86_64_PGALLOC_H */