blob: 41a91df826310ee1e8b8794b10818d72309d74ed [file] [log] [blame]
Yunsheng Lin65941f12024-10-28 19:53:37 +08001/* SPDX-License-Identifier: GPL-2.0 */
2
3#ifndef _LINUX_PAGE_FRAG_CACHE_H
4#define _LINUX_PAGE_FRAG_CACHE_H
5
Yunsheng Lin0c3ce2f2024-10-28 19:53:41 +08006#include <linux/bits.h>
Yunsheng Lin65941f12024-10-28 19:53:37 +08007#include <linux/log2.h>
8#include <linux/mm_types_task.h>
9#include <linux/types.h>
10
Yunsheng Lin0c3ce2f2024-10-28 19:53:41 +080011#if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
12/* Use a full byte here to enable assembler optimization as the shift
13 * operation is usually expecting a byte.
14 */
15#define PAGE_FRAG_CACHE_ORDER_MASK GENMASK(7, 0)
16#else
17/* Compiler should be able to figure out we don't read things as any value
18 * ANDed with 0 is 0.
19 */
20#define PAGE_FRAG_CACHE_ORDER_MASK 0
21#endif
22
23#define PAGE_FRAG_CACHE_PFMEMALLOC_BIT (PAGE_FRAG_CACHE_ORDER_MASK + 1)
24
25static inline bool encoded_page_decode_pfmemalloc(unsigned long encoded_page)
26{
27 return !!(encoded_page & PAGE_FRAG_CACHE_PFMEMALLOC_BIT);
28}
29
Yunsheng Lin3d18dfe2024-10-28 19:53:39 +080030static inline void page_frag_cache_init(struct page_frag_cache *nc)
31{
Yunsheng Lin0c3ce2f2024-10-28 19:53:41 +080032 nc->encoded_page = 0;
Yunsheng Lin3d18dfe2024-10-28 19:53:39 +080033}
34
35static inline bool page_frag_cache_is_pfmemalloc(struct page_frag_cache *nc)
36{
Yunsheng Lin0c3ce2f2024-10-28 19:53:41 +080037 return encoded_page_decode_pfmemalloc(nc->encoded_page);
Yunsheng Lin3d18dfe2024-10-28 19:53:39 +080038}
39
Yunsheng Lin65941f12024-10-28 19:53:37 +080040void page_frag_cache_drain(struct page_frag_cache *nc);
41void __page_frag_cache_drain(struct page *page, unsigned int count);
42void *__page_frag_alloc_align(struct page_frag_cache *nc, unsigned int fragsz,
43 gfp_t gfp_mask, unsigned int align_mask);
44
45static inline void *page_frag_alloc_align(struct page_frag_cache *nc,
46 unsigned int fragsz, gfp_t gfp_mask,
47 unsigned int align)
48{
49 WARN_ON_ONCE(!is_power_of_2(align));
50 return __page_frag_alloc_align(nc, fragsz, gfp_mask, -align);
51}
52
53static inline void *page_frag_alloc(struct page_frag_cache *nc,
54 unsigned int fragsz, gfp_t gfp_mask)
55{
56 return __page_frag_alloc_align(nc, fragsz, gfp_mask, ~0u);
57}
58
59void page_frag_free(void *addr);
60
61#endif