1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/staging/android/ion/ion_mem_pool.c
5 * Copyright (C) 2011 Google, Inc.
8 #include <linux/list.h>
9 #include <linux/slab.h>
10 #include <linux/swap.h>
14 static inline struct page
*ion_page_pool_alloc_pages(struct ion_page_pool
*pool
)
16 return alloc_pages(pool
->gfp_mask
, pool
->order
);
19 static void ion_page_pool_free_pages(struct ion_page_pool
*pool
,
22 __free_pages(page
, pool
->order
);
25 static void ion_page_pool_add(struct ion_page_pool
*pool
, struct page
*page
)
27 mutex_lock(&pool
->mutex
);
28 if (PageHighMem(page
)) {
29 list_add_tail(&page
->lru
, &pool
->high_items
);
32 list_add_tail(&page
->lru
, &pool
->low_items
);
36 mod_node_page_state(page_pgdat(page
), NR_INDIRECTLY_RECLAIMABLE_BYTES
,
37 (1 << (PAGE_SHIFT
+ pool
->order
)));
38 mutex_unlock(&pool
->mutex
);
41 static struct page
*ion_page_pool_remove(struct ion_page_pool
*pool
, bool high
)
46 BUG_ON(!pool
->high_count
);
47 page
= list_first_entry(&pool
->high_items
, struct page
, lru
);
50 BUG_ON(!pool
->low_count
);
51 page
= list_first_entry(&pool
->low_items
, struct page
, lru
);
56 mod_node_page_state(page_pgdat(page
), NR_INDIRECTLY_RECLAIMABLE_BYTES
,
57 -(1 << (PAGE_SHIFT
+ pool
->order
)));
61 struct page
*ion_page_pool_alloc(struct ion_page_pool
*pool
)
63 struct page
*page
= NULL
;
67 mutex_lock(&pool
->mutex
);
69 page
= ion_page_pool_remove(pool
, true);
70 else if (pool
->low_count
)
71 page
= ion_page_pool_remove(pool
, false);
72 mutex_unlock(&pool
->mutex
);
75 page
= ion_page_pool_alloc_pages(pool
);
80 void ion_page_pool_free(struct ion_page_pool
*pool
, struct page
*page
)
82 BUG_ON(pool
->order
!= compound_order(page
));
84 ion_page_pool_add(pool
, page
);
87 static int ion_page_pool_total(struct ion_page_pool
*pool
, bool high
)
89 int count
= pool
->low_count
;
92 count
+= pool
->high_count
;
94 return count
<< pool
->order
;
97 int ion_page_pool_shrink(struct ion_page_pool
*pool
, gfp_t gfp_mask
,
103 if (current_is_kswapd())
106 high
= !!(gfp_mask
& __GFP_HIGHMEM
);
109 return ion_page_pool_total(pool
, high
);
111 while (freed
< nr_to_scan
) {
114 mutex_lock(&pool
->mutex
);
115 if (pool
->low_count
) {
116 page
= ion_page_pool_remove(pool
, false);
117 } else if (high
&& pool
->high_count
) {
118 page
= ion_page_pool_remove(pool
, true);
120 mutex_unlock(&pool
->mutex
);
123 mutex_unlock(&pool
->mutex
);
124 ion_page_pool_free_pages(pool
, page
);
125 freed
+= (1 << pool
->order
);
131 struct ion_page_pool
*ion_page_pool_create(gfp_t gfp_mask
, unsigned int order
)
133 struct ion_page_pool
*pool
= kmalloc(sizeof(*pool
), GFP_KERNEL
);
137 pool
->high_count
= 0;
139 INIT_LIST_HEAD(&pool
->low_items
);
140 INIT_LIST_HEAD(&pool
->high_items
);
141 pool
->gfp_mask
= gfp_mask
| __GFP_COMP
;
143 mutex_init(&pool
->mutex
);
144 plist_node_init(&pool
->list
, order
);
149 void ion_page_pool_destroy(struct ion_page_pool
*pool
)