aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux/page_ref.h
blob: 68e8d10bfe60e24a188c9584e1ba1be02e2b4565 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#ifndef _LINUX_PAGE_REF_H
#define _LINUX_PAGE_REF_H

#include <linux/atomic.h>
#include <linux/mm_types.h>
#include <linux/page-flags.h>

static inline struct page *compound_head_by_tail(struct page *tail)
{
	struct page *head = tail->first_page;

	/*
	 * page->first_page may be a dangling pointer to an old
	 * compound page, so recheck that it is still a tail
	 * page before returning.
	 */
	smp_rmb();
	if (likely(PageTail(tail)))
		return head;
	return tail;
}

static inline struct page *compound_head(struct page *page)
{
	if (unlikely(PageTail(page)))
		return compound_head_by_tail(page);
	return page;
}

static inline int page_ref_count(struct page *page)
{
	return atomic_read(&page->_count);
}

static inline int page_count(struct page *page)
{
	return atomic_read(&compound_head(page)->_count);
}

static inline void set_page_count(struct page *page, int v)
{
	atomic_set(&page->_count, v);
}

/*
 * Setup the page count before being freed into the page allocator for
 * the first time (boot or memory hotplug)
 */
static inline void init_page_count(struct page *page)
{
	set_page_count(page, 1);
}

static inline void page_ref_add(struct page *page, int nr)
{
	atomic_add(nr, &page->_count);
}

static inline void page_ref_sub(struct page *page, int nr)
{
	atomic_sub(nr, &page->_count);
}

static inline void page_ref_inc(struct page *page)
{
	atomic_inc(&page->_count);
}

static inline void page_ref_dec(struct page *page)
{
	atomic_dec(&page->_count);
}

static inline int page_ref_sub_and_test(struct page *page, int nr)
{
	return atomic_sub_and_test(nr, &page->_count);
}

static inline int page_ref_dec_and_test(struct page *page)
{
	return atomic_dec_and_test(&page->_count);
}

static inline int page_ref_dec_return(struct page *page)
{
	return atomic_dec_return(&page->_count);
}

static inline int page_ref_add_unless(struct page *page, int nr, int u)
{
	return atomic_add_unless(&page->_count, nr, u);
}

static inline int page_ref_freeze(struct page *page, int count)
{
	return likely(atomic_cmpxchg(&page->_count, count, 0) == count);
}

static inline void page_ref_unfreeze(struct page *page, int count)
{
	VM_BUG_ON_PAGE(page_count(page) != 0, page);
	VM_BUG_ON(count == 0);

	atomic_set(&page->_count, count);
}

#endif