KVM
Functions | Variables
page_alloc.c File Reference
#include <asm/kvm_hyp.h>
#include <nvhe/gfp.h>
Include dependency graph for page_alloc.c:

Go to the source code of this file.

Functions

static struct hyp_page__find_buddy_nocheck (struct hyp_pool *pool, struct hyp_page *p, unsigned short order)
 
static struct hyp_page__find_buddy_avail (struct hyp_pool *pool, struct hyp_page *p, unsigned short order)
 
static void page_remove_from_list (struct hyp_page *p)
 
static void page_add_to_list (struct hyp_page *p, struct list_head *head)
 
static struct hyp_pagenode_to_page (struct list_head *node)
 
static void __hyp_attach_page (struct hyp_pool *pool, struct hyp_page *p)
 
static struct hyp_page__hyp_extract_page (struct hyp_pool *pool, struct hyp_page *p, unsigned short order)
 
static void __hyp_put_page (struct hyp_pool *pool, struct hyp_page *p)
 
void hyp_put_page (struct hyp_pool *pool, void *addr)
 
void hyp_get_page (struct hyp_pool *pool, void *addr)
 
void hyp_split_page (struct hyp_page *p)
 
void * hyp_alloc_pages (struct hyp_pool *pool, unsigned short order)
 
int hyp_pool_init (struct hyp_pool *pool, u64 pfn, unsigned int nr_pages, unsigned int reserved_pages)
 

Variables

u64 __hyp_vmemmap
 

Function Documentation

◆ __find_buddy_avail()

static struct hyp_page* __find_buddy_avail ( struct hyp_pool pool,
struct hyp_page p,
unsigned short  order 
)
static

Definition at line 52 of file page_alloc.c.

55 {
56  struct hyp_page *buddy = __find_buddy_nocheck(pool, p, order);
57 
58  if (!buddy || buddy->order != order || buddy->refcount)
59  return NULL;
60 
61  return buddy;
62 
63 }
static struct hyp_page * __find_buddy_nocheck(struct hyp_pool *pool, struct hyp_page *p, unsigned short order)
Definition: page_alloc.c:33
unsigned short refcount
Definition: memory.h:11
unsigned short order
Definition: memory.h:12
Here is the call graph for this function:
Here is the caller graph for this function:

◆ __find_buddy_nocheck()

static struct hyp_page* __find_buddy_nocheck ( struct hyp_pool pool,
struct hyp_page p,
unsigned short  order 
)
static

Definition at line 33 of file page_alloc.c.

36 {
37  phys_addr_t addr = hyp_page_to_phys(p);
38 
39  addr ^= (PAGE_SIZE << order);
40 
41  /*
42  * Don't return a page outside the pool range -- it belongs to
43  * something else and may not be mapped in hyp_vmemmap.
44  */
45  if (addr < pool->range_start || addr >= pool->range_end)
46  return NULL;
47 
48  return hyp_phys_to_page(addr);
49 }
#define hyp_phys_to_page(phys)
Definition: memory.h:32
#define hyp_page_to_phys(page)
Definition: memory.h:37
phys_addr_t range_end
Definition: gfp.h:21
Here is the caller graph for this function:

◆ __hyp_attach_page()

static void __hyp_attach_page ( struct hyp_pool pool,
struct hyp_page p 
)
static

Definition at line 93 of file page_alloc.c.

95 {
96  phys_addr_t phys = hyp_page_to_phys(p);
97  unsigned short order = p->order;
98  struct hyp_page *buddy;
99 
100  memset(hyp_page_to_virt(p), 0, PAGE_SIZE << p->order);
101 
102  /* Skip coalescing for 'external' pages being freed into the pool. */
103  if (phys < pool->range_start || phys >= pool->range_end)
104  goto insert;
105 
106  /*
107  * Only the first struct hyp_page of a high-order page (otherwise known
108  * as the 'head') should have p->order set. The non-head pages should
109  * have p->order = HYP_NO_ORDER. Here @p may no longer be the head
110  * after coalescing, so make sure to mark it HYP_NO_ORDER proactively.
111  */
112  p->order = HYP_NO_ORDER;
113  for (; (order + 1) <= pool->max_order; order++) {
114  buddy = __find_buddy_avail(pool, p, order);
115  if (!buddy)
116  break;
117 
118  /* Take the buddy out of its list, and coalesce with @p */
119  page_remove_from_list(buddy);
120  buddy->order = HYP_NO_ORDER;
121  p = min(p, buddy);
122  }
123 
124 insert:
125  /* Mark the new head, and insert it */
126  p->order = order;
127  page_add_to_list(p, &pool->free_area[order]);
128 }
#define HYP_NO_ORDER
Definition: gfp.h:10
#define hyp_page_to_virt(page)
Definition: memory.h:38
static void page_add_to_list(struct hyp_page *p, struct list_head *head)
Definition: page_alloc.c:80
static void page_remove_from_list(struct hyp_page *p)
Definition: page_alloc.c:72
static struct hyp_page * __find_buddy_avail(struct hyp_pool *pool, struct hyp_page *p, unsigned short order)
Definition: page_alloc.c:52
unsigned short max_order
Definition: gfp.h:22
struct list_head free_area[NR_PAGE_ORDERS]
Definition: gfp.h:19
Here is the call graph for this function:
Here is the caller graph for this function:

◆ __hyp_extract_page()

static struct hyp_page* __hyp_extract_page ( struct hyp_pool pool,
struct hyp_page p,
unsigned short  order 
)
static

Definition at line 130 of file page_alloc.c.

133 {
134  struct hyp_page *buddy;
135 
137  while (p->order > order) {
138  /*
139  * The buddy of order n - 1 currently has HYP_NO_ORDER as it
140  * is covered by a higher-level page (whose head is @p). Use
141  * __find_buddy_nocheck() to find it and inject it in the
142  * free_list[n - 1], effectively splitting @p in half.
143  */
144  p->order--;
145  buddy = __find_buddy_nocheck(pool, p, p->order);
146  buddy->order = p->order;
147  page_add_to_list(buddy, &pool->free_area[buddy->order]);
148  }
149 
150  return p;
151 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ __hyp_put_page()

static void __hyp_put_page ( struct hyp_pool pool,
struct hyp_page p 
)
static

Definition at line 153 of file page_alloc.c.

154 {
156  __hyp_attach_page(pool, p);
157 }
static int hyp_page_ref_dec_and_test(struct hyp_page *p)
Definition: memory.h:64
static void __hyp_attach_page(struct hyp_pool *pool, struct hyp_page *p)
Definition: page_alloc.c:93
Here is the call graph for this function:
Here is the caller graph for this function:

◆ hyp_alloc_pages()

void* hyp_alloc_pages ( struct hyp_pool pool,
unsigned short  order 
)

Definition at line 198 of file page_alloc.c.

199 {
200  unsigned short i = order;
201  struct hyp_page *p;
202 
203  hyp_spin_lock(&pool->lock);
204 
205  /* Look for a high-enough-order page */
206  while (i <= pool->max_order && list_empty(&pool->free_area[i]))
207  i++;
208  if (i > pool->max_order) {
209  hyp_spin_unlock(&pool->lock);
210  return NULL;
211  }
212 
213  /* Extract it from the tree at the right order */
214  p = node_to_page(pool->free_area[i].next);
215  p = __hyp_extract_page(pool, p, order);
216 
218  hyp_spin_unlock(&pool->lock);
219 
220  return hyp_page_to_virt(p);
221 }
static void hyp_set_page_refcounted(struct hyp_page *p)
Definition: memory.h:70
static struct hyp_page * __hyp_extract_page(struct hyp_pool *pool, struct hyp_page *p, unsigned short order)
Definition: page_alloc.c:130
static struct hyp_page * node_to_page(struct list_head *node)
Definition: page_alloc.c:88
static void hyp_spin_unlock(hyp_spinlock_t *lock)
Definition: spinlock.h:82
static void hyp_spin_lock(hyp_spinlock_t *lock)
Definition: spinlock.h:44
hyp_spinlock_t lock
Definition: gfp.h:18
Here is the call graph for this function:
Here is the caller graph for this function:

◆ hyp_get_page()

void hyp_get_page ( struct hyp_pool pool,
void *  addr 
)

Definition at line 175 of file page_alloc.c.

176 {
177  struct hyp_page *p = hyp_virt_to_page(addr);
178 
179  hyp_spin_lock(&pool->lock);
180  hyp_page_ref_inc(p);
181  hyp_spin_unlock(&pool->lock);
182 }
#define hyp_virt_to_page(virt)
Definition: memory.h:33
static void hyp_page_ref_inc(struct hyp_page *p)
Definition: memory.h:52
Here is the call graph for this function:
Here is the caller graph for this function:

◆ hyp_pool_init()

int hyp_pool_init ( struct hyp_pool pool,
u64  pfn,
unsigned int  nr_pages,
unsigned int  reserved_pages 
)

Definition at line 223 of file page_alloc.c.

225 {
226  phys_addr_t phys = hyp_pfn_to_phys(pfn);
227  struct hyp_page *p;
228  int i;
229 
230  hyp_spin_lock_init(&pool->lock);
231  pool->max_order = min(MAX_PAGE_ORDER,
232  get_order(nr_pages << PAGE_SHIFT));
233  for (i = 0; i <= pool->max_order; i++)
234  INIT_LIST_HEAD(&pool->free_area[i]);
235  pool->range_start = phys;
236  pool->range_end = phys + (nr_pages << PAGE_SHIFT);
237 
238  /* Init the vmemmap portion */
239  p = hyp_phys_to_page(phys);
240  for (i = 0; i < nr_pages; i++)
242 
243  /* Attach the unused pages to the buddy tree */
244  for (i = reserved_pages; i < nr_pages; i++)
245  __hyp_put_page(pool, &p[i]);
246 
247  return 0;
248 }
#define hyp_pfn_to_phys(pfn)
Definition: memory.h:31
static void __hyp_put_page(struct hyp_pool *pool, struct hyp_page *p)
Definition: page_alloc.c:153
#define hyp_spin_lock_init(l)
Definition: spinlock.h:39
phys_addr_t range_start
Definition: gfp.h:20
Here is the call graph for this function:
Here is the caller graph for this function:

◆ hyp_put_page()

void hyp_put_page ( struct hyp_pool pool,
void *  addr 
)

Definition at line 166 of file page_alloc.c.

167 {
168  struct hyp_page *p = hyp_virt_to_page(addr);
169 
170  hyp_spin_lock(&pool->lock);
171  __hyp_put_page(pool, p);
172  hyp_spin_unlock(&pool->lock);
173 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ hyp_split_page()

void hyp_split_page ( struct hyp_page p)

Definition at line 184 of file page_alloc.c.

185 {
186  unsigned short order = p->order;
187  unsigned int i;
188 
189  p->order = 0;
190  for (i = 1; i < (1 << order); i++) {
191  struct hyp_page *tail = p + i;
192 
193  tail->order = 0;
195  }
196 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ node_to_page()

static struct hyp_page* node_to_page ( struct list_head *  node)
inlinestatic

Definition at line 88 of file page_alloc.c.

89 {
90  return hyp_virt_to_page(node);
91 }
Here is the caller graph for this function:

◆ page_add_to_list()

static void page_add_to_list ( struct hyp_page p,
struct list_head *  head 
)
inlinestatic

Definition at line 80 of file page_alloc.c.

81 {
82  struct list_head *node = hyp_page_to_virt(p);
83 
84  INIT_LIST_HEAD(node);
85  list_add_tail(node, head);
86 }
Here is the caller graph for this function:

◆ page_remove_from_list()

static void page_remove_from_list ( struct hyp_page p)
inlinestatic

Definition at line 72 of file page_alloc.c.

73 {
74  struct list_head *node = hyp_page_to_virt(p);
75 
76  __list_del_entry(node);
77  memset(node, 0, sizeof(*node));
78 }
Here is the caller graph for this function:

Variable Documentation

◆ __hyp_vmemmap

u64 __hyp_vmemmap

Definition at line 10 of file page_alloc.c.