@@ -27,10 +27,7 @@ DEFINE_STATIC_KEY_FALSE(frontswap_enabled_key);
2727 * may be registered, but implementations can never deregister. This
2828 * is a simple singly-linked list of all registered implementations.
2929 */
30- static struct frontswap_ops * frontswap_ops __read_mostly ;
31-
32- #define for_each_frontswap_ops (ops ) \
33- for ((ops) = frontswap_ops; (ops); (ops) = (ops)->next)
30+ static const struct frontswap_ops * frontswap_ops __read_mostly ;
3431
3532#ifdef CONFIG_DEBUG_FS
3633/*
@@ -97,18 +94,14 @@ static inline void inc_frontswap_invalidates(void) { }
9794/*
9895 * Register operations for frontswap
9996 */
100- void frontswap_register_ops (struct frontswap_ops * ops )
97+ int frontswap_register_ops (const struct frontswap_ops * ops )
10198{
102- /*
103- * Setting frontswap_ops must happen after the ops->init() calls
104- * above; cmpxchg implies smp_mb() which will ensure the init is
105- * complete at this point.
106- */
107- do {
108- ops -> next = frontswap_ops ;
109- } while (cmpxchg (& frontswap_ops , ops -> next , ops ) != ops -> next );
99+ if (frontswap_ops )
100+ return - EINVAL ;
110101
102+ frontswap_ops = ops ;
111103 static_branch_inc (& frontswap_enabled_key );
104+ return 0 ;
112105}
113106
114107/*
@@ -117,7 +110,6 @@ void frontswap_register_ops(struct frontswap_ops *ops)
117110void frontswap_init (unsigned type , unsigned long * map )
118111{
119112 struct swap_info_struct * sis = swap_info [type ];
120- struct frontswap_ops * ops ;
121113
122114 VM_BUG_ON (sis == NULL );
123115
@@ -133,9 +125,7 @@ void frontswap_init(unsigned type, unsigned long *map)
133125 * p->frontswap set to something valid to work properly.
134126 */
135127 frontswap_map_set (sis , map );
136-
137- for_each_frontswap_ops (ops )
138- ops -> init (type );
128+ frontswap_ops -> init (type );
139129}
140130
141131static bool __frontswap_test (struct swap_info_struct * sis ,
@@ -174,7 +164,6 @@ int __frontswap_store(struct page *page)
174164 int type = swp_type (entry );
175165 struct swap_info_struct * sis = swap_info [type ];
176166 pgoff_t offset = swp_offset (entry );
177- struct frontswap_ops * ops ;
178167
179168 VM_BUG_ON (!frontswap_ops );
180169 VM_BUG_ON (!PageLocked (page ));
@@ -188,16 +177,10 @@ int __frontswap_store(struct page *page)
188177 */
189178 if (__frontswap_test (sis , offset )) {
190179 __frontswap_clear (sis , offset );
191- for_each_frontswap_ops (ops )
192- ops -> invalidate_page (type , offset );
180+ frontswap_ops -> invalidate_page (type , offset );
193181 }
194182
195- /* Try to store in each implementation, until one succeeds. */
196- for_each_frontswap_ops (ops ) {
197- ret = ops -> store (type , offset , page );
198- if (!ret ) /* successful store */
199- break ;
200- }
183+ ret = frontswap_ops -> store (type , offset , page );
201184 if (ret == 0 ) {
202185 __frontswap_set (sis , offset );
203186 inc_frontswap_succ_stores ();
@@ -220,7 +203,6 @@ int __frontswap_load(struct page *page)
220203 int type = swp_type (entry );
221204 struct swap_info_struct * sis = swap_info [type ];
222205 pgoff_t offset = swp_offset (entry );
223- struct frontswap_ops * ops ;
224206
225207 VM_BUG_ON (!frontswap_ops );
226208 VM_BUG_ON (!PageLocked (page ));
@@ -230,11 +212,7 @@ int __frontswap_load(struct page *page)
230212 return -1 ;
231213
232214 /* Try loading from each implementation, until one succeeds. */
233- for_each_frontswap_ops (ops ) {
234- ret = ops -> load (type , offset , page );
235- if (!ret ) /* successful load */
236- break ;
237- }
215+ ret = frontswap_ops -> load (type , offset , page );
238216 if (ret == 0 )
239217 inc_frontswap_loads ();
240218 return ret ;
@@ -247,16 +225,14 @@ int __frontswap_load(struct page *page)
247225void __frontswap_invalidate_page (unsigned type , pgoff_t offset )
248226{
249227 struct swap_info_struct * sis = swap_info [type ];
250- struct frontswap_ops * ops ;
251228
252229 VM_BUG_ON (!frontswap_ops );
253230 VM_BUG_ON (sis == NULL );
254231
255232 if (!__frontswap_test (sis , offset ))
256233 return ;
257234
258- for_each_frontswap_ops (ops )
259- ops -> invalidate_page (type , offset );
235+ frontswap_ops -> invalidate_page (type , offset );
260236 __frontswap_clear (sis , offset );
261237 inc_frontswap_invalidates ();
262238}
@@ -268,16 +244,14 @@ void __frontswap_invalidate_page(unsigned type, pgoff_t offset)
268244void __frontswap_invalidate_area (unsigned type )
269245{
270246 struct swap_info_struct * sis = swap_info [type ];
271- struct frontswap_ops * ops ;
272247
273248 VM_BUG_ON (!frontswap_ops );
274249 VM_BUG_ON (sis == NULL );
275250
276251 if (sis -> frontswap_map == NULL )
277252 return ;
278253
279- for_each_frontswap_ops (ops )
280- ops -> invalidate_area (type );
254+ frontswap_ops -> invalidate_area (type );
281255 atomic_set (& sis -> frontswap_pages , 0 );
282256 bitmap_zero (sis -> frontswap_map , sis -> max );
283257}
0 commit comments