1 /* SPDX-License-Identifier: GPL-2.0
2 *
3 * linux/drivers/staging/erofs/unzip_pagevec.h
4 *
5 * Copyright (C) 2018 HUAWEI, Inc.
6 * http://www.huawei.com/
7 * Created by Gao Xiang <gaoxiang25@huawei.com>
8 *
9 * This file is subject to the terms and conditions of the GNU General Public
10 * License. See the file COPYING in the main directory of the Linux
11 * distribution for more details.
12 */
13 #ifndef __EROFS_UNZIP_PAGEVEC_H
14 #define __EROFS_UNZIP_PAGEVEC_H
15
16 #include <linux/tagptr.h>
17
18 /* page type in pagevec for unzip subsystem */
19 enum z_erofs_page_type {
20 /* including Z_EROFS_VLE_PAGE_TAIL_EXCLUSIVE */
21 Z_EROFS_PAGE_TYPE_EXCLUSIVE,
22
23 Z_EROFS_VLE_PAGE_TYPE_TAIL_SHARED,
24
25 Z_EROFS_VLE_PAGE_TYPE_HEAD,
26 Z_EROFS_VLE_PAGE_TYPE_MAX
27 };
28
29 extern void __compiletime_error("Z_EROFS_PAGE_TYPE_EXCLUSIVE != 0")
30 __bad_page_type_exclusive(void);
31
32 /* pagevec tagged pointer */
33 typedef tagptr2_t erofs_vtptr_t;
34
35 /* pagevec collector */
36 struct z_erofs_pagevec_ctor {
37 struct page *curr, *next;
38 erofs_vtptr_t *pages;
39
40 unsigned int nr, index;
41 };
42
z_erofs_pagevec_ctor_exit(struct z_erofs_pagevec_ctor * ctor,bool atomic)43 static inline void z_erofs_pagevec_ctor_exit(struct z_erofs_pagevec_ctor *ctor,
44 bool atomic)
45 {
46 if (ctor->curr == NULL)
47 return;
48
49 if (atomic)
50 kunmap_atomic(ctor->pages);
51 else
52 kunmap(ctor->curr);
53 }
54
55 static inline struct page *
z_erofs_pagevec_ctor_next_page(struct z_erofs_pagevec_ctor * ctor,unsigned nr)56 z_erofs_pagevec_ctor_next_page(struct z_erofs_pagevec_ctor *ctor,
57 unsigned nr)
58 {
59 unsigned index;
60
61 /* keep away from occupied pages */
62 if (ctor->next != NULL)
63 return ctor->next;
64
65 for (index = 0; index < nr; ++index) {
66 const erofs_vtptr_t t = ctor->pages[index];
67 const unsigned tags = tagptr_unfold_tags(t);
68
69 if (tags == Z_EROFS_PAGE_TYPE_EXCLUSIVE)
70 return tagptr_unfold_ptr(t);
71 }
72
73 if (unlikely(nr >= ctor->nr))
74 BUG();
75
76 return NULL;
77 }
78
79 static inline void
z_erofs_pagevec_ctor_pagedown(struct z_erofs_pagevec_ctor * ctor,bool atomic)80 z_erofs_pagevec_ctor_pagedown(struct z_erofs_pagevec_ctor *ctor,
81 bool atomic)
82 {
83 struct page *next = z_erofs_pagevec_ctor_next_page(ctor, ctor->nr);
84
85 z_erofs_pagevec_ctor_exit(ctor, atomic);
86
87 ctor->curr = next;
88 ctor->next = NULL;
89 ctor->pages = atomic ?
90 kmap_atomic(ctor->curr) : kmap(ctor->curr);
91
92 ctor->nr = PAGE_SIZE / sizeof(struct page *);
93 ctor->index = 0;
94 }
95
z_erofs_pagevec_ctor_init(struct z_erofs_pagevec_ctor * ctor,unsigned nr,erofs_vtptr_t * pages,unsigned i)96 static inline void z_erofs_pagevec_ctor_init(struct z_erofs_pagevec_ctor *ctor,
97 unsigned nr,
98 erofs_vtptr_t *pages, unsigned i)
99 {
100 ctor->nr = nr;
101 ctor->curr = ctor->next = NULL;
102 ctor->pages = pages;
103
104 if (i >= nr) {
105 i -= nr;
106 z_erofs_pagevec_ctor_pagedown(ctor, false);
107 while (i > ctor->nr) {
108 i -= ctor->nr;
109 z_erofs_pagevec_ctor_pagedown(ctor, false);
110 }
111 }
112
113 ctor->next = z_erofs_pagevec_ctor_next_page(ctor, i);
114 ctor->index = i;
115 }
116
117 static inline bool
z_erofs_pagevec_ctor_enqueue(struct z_erofs_pagevec_ctor * ctor,struct page * page,enum z_erofs_page_type type,bool * occupied)118 z_erofs_pagevec_ctor_enqueue(struct z_erofs_pagevec_ctor *ctor,
119 struct page *page,
120 enum z_erofs_page_type type,
121 bool *occupied)
122 {
123 *occupied = false;
124 if (unlikely(ctor->next == NULL && type))
125 if (ctor->index + 1 == ctor->nr)
126 return false;
127
128 if (unlikely(ctor->index >= ctor->nr))
129 z_erofs_pagevec_ctor_pagedown(ctor, false);
130
131 /* exclusive page type must be 0 */
132 if (Z_EROFS_PAGE_TYPE_EXCLUSIVE != (uintptr_t)NULL)
133 __bad_page_type_exclusive();
134
135 /* should remind that collector->next never equal to 1, 2 */
136 if (type == (uintptr_t)ctor->next) {
137 ctor->next = page;
138 *occupied = true;
139 }
140
141 ctor->pages[ctor->index++] =
142 tagptr_fold(erofs_vtptr_t, page, type);
143 return true;
144 }
145
146 static inline struct page *
z_erofs_pagevec_ctor_dequeue(struct z_erofs_pagevec_ctor * ctor,enum z_erofs_page_type * type)147 z_erofs_pagevec_ctor_dequeue(struct z_erofs_pagevec_ctor *ctor,
148 enum z_erofs_page_type *type)
149 {
150 erofs_vtptr_t t;
151
152 if (unlikely(ctor->index >= ctor->nr)) {
153 BUG_ON(ctor->next == NULL);
154 z_erofs_pagevec_ctor_pagedown(ctor, true);
155 }
156
157 t = ctor->pages[ctor->index];
158
159 *type = tagptr_unfold_tags(t);
160
161 /* should remind that collector->next never equal to 1, 2 */
162 if (*type == (uintptr_t)ctor->next)
163 ctor->next = tagptr_unfold_ptr(t);
164
165 ctor->pages[ctor->index++] =
166 tagptr_fold(erofs_vtptr_t, NULL, 0);
167
168 return tagptr_unfold_ptr(t);
169 }
170
171 #endif
172
173