1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (c) 2024, Google LLC.
7 #ifndef __IOMMU_PAGES_H
8 #define __IOMMU_PAGES_H
10 #include <linux/vmstat.h>
11 #include <linux/gfp.h>
15 * All page allocations that should be reported to as "iommu-pagetables" to
16 * userspace must use one of the functions below. This includes allocations of
17 * page-tables and other per-iommu_domain configuration structures.
19 * This is necessary for the proper accounting as IOMMU state can be rather
20 * large, i.e. multiple gigabytes in size.
24 * __iommu_alloc_account - account for newly allocated page.
25 * @page: head struct page of the page.
26 * @order: order of the page
28 static inline void __iommu_alloc_account(struct page *page, int order)
30 const long pgcnt = 1l << order;
32 mod_node_page_state(page_pgdat(page), NR_IOMMU_PAGES, pgcnt);
33 mod_lruvec_page_state(page, NR_SECONDARY_PAGETABLE, pgcnt);
37 * __iommu_free_account - account a page that is about to be freed.
38 * @page: head struct page of the page.
39 * @order: order of the page
41 static inline void __iommu_free_account(struct page *page, int order)
43 const long pgcnt = 1l << order;
45 mod_node_page_state(page_pgdat(page), NR_IOMMU_PAGES, -pgcnt);
46 mod_lruvec_page_state(page, NR_SECONDARY_PAGETABLE, -pgcnt);
50 * __iommu_alloc_pages - allocate a zeroed page of a given order.
51 * @gfp: buddy allocator flags
54 * returns the head struct page of the allocated page.
56 static inline struct page *__iommu_alloc_pages(gfp_t gfp, int order)
60 page = alloc_pages(gfp | __GFP_ZERO, order);
64 __iommu_alloc_account(page, order);
70 * __iommu_free_pages - free page of a given order
71 * @page: head struct page of the page
74 static inline void __iommu_free_pages(struct page *page, int order)
79 __iommu_free_account(page, order);
80 __free_pages(page, order);
84 * iommu_alloc_pages_node - allocate a zeroed page of a given order from
86 * @nid: memory NUMA node id
87 * @gfp: buddy allocator flags
90 * returns the virtual address of the allocated page
92 static inline void *iommu_alloc_pages_node(int nid, gfp_t gfp, int order)
94 struct page *page = alloc_pages_node(nid, gfp | __GFP_ZERO, order);
99 __iommu_alloc_account(page, order);
101 return page_address(page);
105 * iommu_alloc_pages - allocate a zeroed page of a given order
106 * @gfp: buddy allocator flags
109 * returns the virtual address of the allocated page
111 static inline void *iommu_alloc_pages(gfp_t gfp, int order)
113 struct page *page = __iommu_alloc_pages(gfp, order);
118 return page_address(page);
122 * iommu_alloc_page_node - allocate a zeroed page at specific NUMA node.
123 * @nid: memory NUMA node id
124 * @gfp: buddy allocator flags
126 * returns the virtual address of the allocated page
128 static inline void *iommu_alloc_page_node(int nid, gfp_t gfp)
130 return iommu_alloc_pages_node(nid, gfp, 0);
134 * iommu_alloc_page - allocate a zeroed page
135 * @gfp: buddy allocator flags
137 * returns the virtual address of the allocated page
139 static inline void *iommu_alloc_page(gfp_t gfp)
141 return iommu_alloc_pages(gfp, 0);
145 * iommu_free_pages - free page of a given order
146 * @virt: virtual address of the page to be freed.
149 static inline void iommu_free_pages(void *virt, int order)
154 __iommu_free_pages(virt_to_page(virt), order);
158 * iommu_free_page - free page
159 * @virt: virtual address of the page to be freed.
161 static inline void iommu_free_page(void *virt)
163 iommu_free_pages(virt, 0);
167 * iommu_put_pages_list - free a list of pages.
168 * @page: the head of the lru list to be freed.
170 * There are no locking requirement for these pages, as they are going to be
171 * put on a free list as soon as refcount reaches 0. Pages are put on this LRU
172 * list once they are removed from the IOMMU page tables. However, they can
173 * still be access through debugfs.
175 static inline void iommu_put_pages_list(struct list_head *page)
177 while (!list_empty(page)) {
178 struct page *p = list_entry(page->prev, struct page, lru);
181 __iommu_free_account(p, 0);
186 #endif /* __IOMMU_PAGES_H */