18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci#ifndef _LINUX_CLOSURE_H 38c2ecf20Sopenharmony_ci#define _LINUX_CLOSURE_H 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ci#include <linux/llist.h> 68c2ecf20Sopenharmony_ci#include <linux/sched.h> 78c2ecf20Sopenharmony_ci#include <linux/sched/task_stack.h> 88c2ecf20Sopenharmony_ci#include <linux/workqueue.h> 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci/* 118c2ecf20Sopenharmony_ci * Closure is perhaps the most overused and abused term in computer science, but 128c2ecf20Sopenharmony_ci * since I've been unable to come up with anything better you're stuck with it 138c2ecf20Sopenharmony_ci * again. 148c2ecf20Sopenharmony_ci * 158c2ecf20Sopenharmony_ci * What are closures? 168c2ecf20Sopenharmony_ci * 178c2ecf20Sopenharmony_ci * They embed a refcount. The basic idea is they count "things that are in 188c2ecf20Sopenharmony_ci * progress" - in flight bios, some other thread that's doing something else - 198c2ecf20Sopenharmony_ci * anything you might want to wait on. 208c2ecf20Sopenharmony_ci * 218c2ecf20Sopenharmony_ci * The refcount may be manipulated with closure_get() and closure_put(). 228c2ecf20Sopenharmony_ci * closure_put() is where many of the interesting things happen, when it causes 238c2ecf20Sopenharmony_ci * the refcount to go to 0. 248c2ecf20Sopenharmony_ci * 258c2ecf20Sopenharmony_ci * Closures can be used to wait on things both synchronously and asynchronously, 268c2ecf20Sopenharmony_ci * and synchronous and asynchronous use can be mixed without restriction. To 278c2ecf20Sopenharmony_ci * wait synchronously, use closure_sync() - you will sleep until your closure's 288c2ecf20Sopenharmony_ci * refcount hits 1. 298c2ecf20Sopenharmony_ci * 308c2ecf20Sopenharmony_ci * To wait asynchronously, use 318c2ecf20Sopenharmony_ci * continue_at(cl, next_function, workqueue); 328c2ecf20Sopenharmony_ci * 338c2ecf20Sopenharmony_ci * passing it, as you might expect, the function to run when nothing is pending 348c2ecf20Sopenharmony_ci * and the workqueue to run that function out of. 358c2ecf20Sopenharmony_ci * 368c2ecf20Sopenharmony_ci * continue_at() also, critically, requires a 'return' immediately following the 378c2ecf20Sopenharmony_ci * location where this macro is referenced, to return to the calling function. 388c2ecf20Sopenharmony_ci * There's good reason for this. 398c2ecf20Sopenharmony_ci * 408c2ecf20Sopenharmony_ci * To use safely closures asynchronously, they must always have a refcount while 418c2ecf20Sopenharmony_ci * they are running owned by the thread that is running them. Otherwise, suppose 428c2ecf20Sopenharmony_ci * you submit some bios and wish to have a function run when they all complete: 438c2ecf20Sopenharmony_ci * 448c2ecf20Sopenharmony_ci * foo_endio(struct bio *bio) 458c2ecf20Sopenharmony_ci * { 468c2ecf20Sopenharmony_ci * closure_put(cl); 478c2ecf20Sopenharmony_ci * } 488c2ecf20Sopenharmony_ci * 498c2ecf20Sopenharmony_ci * closure_init(cl); 508c2ecf20Sopenharmony_ci * 518c2ecf20Sopenharmony_ci * do_stuff(); 528c2ecf20Sopenharmony_ci * closure_get(cl); 538c2ecf20Sopenharmony_ci * bio1->bi_endio = foo_endio; 548c2ecf20Sopenharmony_ci * bio_submit(bio1); 558c2ecf20Sopenharmony_ci * 568c2ecf20Sopenharmony_ci * do_more_stuff(); 578c2ecf20Sopenharmony_ci * closure_get(cl); 588c2ecf20Sopenharmony_ci * bio2->bi_endio = foo_endio; 598c2ecf20Sopenharmony_ci * bio_submit(bio2); 608c2ecf20Sopenharmony_ci * 618c2ecf20Sopenharmony_ci * continue_at(cl, complete_some_read, system_wq); 628c2ecf20Sopenharmony_ci * 638c2ecf20Sopenharmony_ci * If closure's refcount started at 0, complete_some_read() could run before the 648c2ecf20Sopenharmony_ci * second bio was submitted - which is almost always not what you want! More 658c2ecf20Sopenharmony_ci * importantly, it wouldn't be possible to say whether the original thread or 668c2ecf20Sopenharmony_ci * complete_some_read()'s thread owned the closure - and whatever state it was 678c2ecf20Sopenharmony_ci * associated with! 688c2ecf20Sopenharmony_ci * 698c2ecf20Sopenharmony_ci * So, closure_init() initializes a closure's refcount to 1 - and when a 708c2ecf20Sopenharmony_ci * closure_fn is run, the refcount will be reset to 1 first. 718c2ecf20Sopenharmony_ci * 728c2ecf20Sopenharmony_ci * Then, the rule is - if you got the refcount with closure_get(), release it 738c2ecf20Sopenharmony_ci * with closure_put() (i.e, in a bio->bi_endio function). If you have a refcount 748c2ecf20Sopenharmony_ci * on a closure because you called closure_init() or you were run out of a 758c2ecf20Sopenharmony_ci * closure - _always_ use continue_at(). Doing so consistently will help 768c2ecf20Sopenharmony_ci * eliminate an entire class of particularly pernicious races. 778c2ecf20Sopenharmony_ci * 788c2ecf20Sopenharmony_ci * Lastly, you might have a wait list dedicated to a specific event, and have no 798c2ecf20Sopenharmony_ci * need for specifying the condition - you just want to wait until someone runs 808c2ecf20Sopenharmony_ci * closure_wake_up() on the appropriate wait list. In that case, just use 818c2ecf20Sopenharmony_ci * closure_wait(). It will return either true or false, depending on whether the 828c2ecf20Sopenharmony_ci * closure was already on a wait list or not - a closure can only be on one wait 838c2ecf20Sopenharmony_ci * list at a time. 848c2ecf20Sopenharmony_ci * 858c2ecf20Sopenharmony_ci * Parents: 868c2ecf20Sopenharmony_ci * 878c2ecf20Sopenharmony_ci * closure_init() takes two arguments - it takes the closure to initialize, and 888c2ecf20Sopenharmony_ci * a (possibly null) parent. 898c2ecf20Sopenharmony_ci * 908c2ecf20Sopenharmony_ci * If parent is non null, the new closure will have a refcount for its lifetime; 918c2ecf20Sopenharmony_ci * a closure is considered to be "finished" when its refcount hits 0 and the 928c2ecf20Sopenharmony_ci * function to run is null. Hence 938c2ecf20Sopenharmony_ci * 948c2ecf20Sopenharmony_ci * continue_at(cl, NULL, NULL); 958c2ecf20Sopenharmony_ci * 968c2ecf20Sopenharmony_ci * returns up the (spaghetti) stack of closures, precisely like normal return 978c2ecf20Sopenharmony_ci * returns up the C stack. continue_at() with non null fn is better thought of 988c2ecf20Sopenharmony_ci * as doing a tail call. 998c2ecf20Sopenharmony_ci * 1008c2ecf20Sopenharmony_ci * All this implies that a closure should typically be embedded in a particular 1018c2ecf20Sopenharmony_ci * struct (which its refcount will normally control the lifetime of), and that 1028c2ecf20Sopenharmony_ci * struct can very much be thought of as a stack frame. 1038c2ecf20Sopenharmony_ci */ 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_cistruct closure; 1068c2ecf20Sopenharmony_cistruct closure_syncer; 1078c2ecf20Sopenharmony_citypedef void (closure_fn) (struct closure *); 1088c2ecf20Sopenharmony_ciextern struct dentry *bcache_debug; 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_cistruct closure_waitlist { 1118c2ecf20Sopenharmony_ci struct llist_head list; 1128c2ecf20Sopenharmony_ci}; 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_cienum closure_state { 1158c2ecf20Sopenharmony_ci /* 1168c2ecf20Sopenharmony_ci * CLOSURE_WAITING: Set iff the closure is on a waitlist. Must be set by 1178c2ecf20Sopenharmony_ci * the thread that owns the closure, and cleared by the thread that's 1188c2ecf20Sopenharmony_ci * waking up the closure. 1198c2ecf20Sopenharmony_ci * 1208c2ecf20Sopenharmony_ci * The rest are for debugging and don't affect behaviour: 1218c2ecf20Sopenharmony_ci * 1228c2ecf20Sopenharmony_ci * CLOSURE_RUNNING: Set when a closure is running (i.e. by 1238c2ecf20Sopenharmony_ci * closure_init() and when closure_put() runs then next function), and 1248c2ecf20Sopenharmony_ci * must be cleared before remaining hits 0. Primarily to help guard 1258c2ecf20Sopenharmony_ci * against incorrect usage and accidentally transferring references. 1268c2ecf20Sopenharmony_ci * continue_at() and closure_return() clear it for you, if you're doing 1278c2ecf20Sopenharmony_ci * something unusual you can use closure_set_dead() which also helps 1288c2ecf20Sopenharmony_ci * annotate where references are being transferred. 1298c2ecf20Sopenharmony_ci */ 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci CLOSURE_BITS_START = (1U << 26), 1328c2ecf20Sopenharmony_ci CLOSURE_DESTRUCTOR = (1U << 26), 1338c2ecf20Sopenharmony_ci CLOSURE_WAITING = (1U << 28), 1348c2ecf20Sopenharmony_ci CLOSURE_RUNNING = (1U << 30), 1358c2ecf20Sopenharmony_ci}; 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci#define CLOSURE_GUARD_MASK \ 1388c2ecf20Sopenharmony_ci ((CLOSURE_DESTRUCTOR|CLOSURE_WAITING|CLOSURE_RUNNING) << 1) 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci#define CLOSURE_REMAINING_MASK (CLOSURE_BITS_START - 1) 1418c2ecf20Sopenharmony_ci#define CLOSURE_REMAINING_INITIALIZER (1|CLOSURE_RUNNING) 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_cistruct closure { 1448c2ecf20Sopenharmony_ci union { 1458c2ecf20Sopenharmony_ci struct { 1468c2ecf20Sopenharmony_ci struct workqueue_struct *wq; 1478c2ecf20Sopenharmony_ci struct closure_syncer *s; 1488c2ecf20Sopenharmony_ci struct llist_node list; 1498c2ecf20Sopenharmony_ci closure_fn *fn; 1508c2ecf20Sopenharmony_ci }; 1518c2ecf20Sopenharmony_ci struct work_struct work; 1528c2ecf20Sopenharmony_ci }; 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci struct closure *parent; 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci atomic_t remaining; 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci#ifdef CONFIG_BCACHE_CLOSURES_DEBUG 1598c2ecf20Sopenharmony_ci#define CLOSURE_MAGIC_DEAD 0xc054dead 1608c2ecf20Sopenharmony_ci#define CLOSURE_MAGIC_ALIVE 0xc054a11e 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci unsigned int magic; 1638c2ecf20Sopenharmony_ci struct list_head all; 1648c2ecf20Sopenharmony_ci unsigned long ip; 1658c2ecf20Sopenharmony_ci unsigned long waiting_on; 1668c2ecf20Sopenharmony_ci#endif 1678c2ecf20Sopenharmony_ci}; 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_civoid closure_sub(struct closure *cl, int v); 1708c2ecf20Sopenharmony_civoid closure_put(struct closure *cl); 1718c2ecf20Sopenharmony_civoid __closure_wake_up(struct closure_waitlist *list); 1728c2ecf20Sopenharmony_cibool closure_wait(struct closure_waitlist *list, struct closure *cl); 1738c2ecf20Sopenharmony_civoid __closure_sync(struct closure *cl); 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci/** 1768c2ecf20Sopenharmony_ci * closure_sync - sleep until a closure a closure has nothing left to wait on 1778c2ecf20Sopenharmony_ci * 1788c2ecf20Sopenharmony_ci * Sleeps until the refcount hits 1 - the thread that's running the closure owns 1798c2ecf20Sopenharmony_ci * the last refcount. 1808c2ecf20Sopenharmony_ci */ 1818c2ecf20Sopenharmony_cistatic inline void closure_sync(struct closure *cl) 1828c2ecf20Sopenharmony_ci{ 1838c2ecf20Sopenharmony_ci if ((atomic_read(&cl->remaining) & CLOSURE_REMAINING_MASK) != 1) 1848c2ecf20Sopenharmony_ci __closure_sync(cl); 1858c2ecf20Sopenharmony_ci} 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci#ifdef CONFIG_BCACHE_CLOSURES_DEBUG 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_civoid closure_debug_init(void); 1908c2ecf20Sopenharmony_civoid closure_debug_create(struct closure *cl); 1918c2ecf20Sopenharmony_civoid closure_debug_destroy(struct closure *cl); 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_ci#else 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_cistatic inline void closure_debug_init(void) {} 1968c2ecf20Sopenharmony_cistatic inline void closure_debug_create(struct closure *cl) {} 1978c2ecf20Sopenharmony_cistatic inline void closure_debug_destroy(struct closure *cl) {} 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci#endif 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_cistatic inline void closure_set_ip(struct closure *cl) 2028c2ecf20Sopenharmony_ci{ 2038c2ecf20Sopenharmony_ci#ifdef CONFIG_BCACHE_CLOSURES_DEBUG 2048c2ecf20Sopenharmony_ci cl->ip = _THIS_IP_; 2058c2ecf20Sopenharmony_ci#endif 2068c2ecf20Sopenharmony_ci} 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_cistatic inline void closure_set_ret_ip(struct closure *cl) 2098c2ecf20Sopenharmony_ci{ 2108c2ecf20Sopenharmony_ci#ifdef CONFIG_BCACHE_CLOSURES_DEBUG 2118c2ecf20Sopenharmony_ci cl->ip = _RET_IP_; 2128c2ecf20Sopenharmony_ci#endif 2138c2ecf20Sopenharmony_ci} 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_cistatic inline void closure_set_waiting(struct closure *cl, unsigned long f) 2168c2ecf20Sopenharmony_ci{ 2178c2ecf20Sopenharmony_ci#ifdef CONFIG_BCACHE_CLOSURES_DEBUG 2188c2ecf20Sopenharmony_ci cl->waiting_on = f; 2198c2ecf20Sopenharmony_ci#endif 2208c2ecf20Sopenharmony_ci} 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_cistatic inline void closure_set_stopped(struct closure *cl) 2238c2ecf20Sopenharmony_ci{ 2248c2ecf20Sopenharmony_ci atomic_sub(CLOSURE_RUNNING, &cl->remaining); 2258c2ecf20Sopenharmony_ci} 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_cistatic inline void set_closure_fn(struct closure *cl, closure_fn *fn, 2288c2ecf20Sopenharmony_ci struct workqueue_struct *wq) 2298c2ecf20Sopenharmony_ci{ 2308c2ecf20Sopenharmony_ci closure_set_ip(cl); 2318c2ecf20Sopenharmony_ci cl->fn = fn; 2328c2ecf20Sopenharmony_ci cl->wq = wq; 2338c2ecf20Sopenharmony_ci /* between atomic_dec() in closure_put() */ 2348c2ecf20Sopenharmony_ci smp_mb__before_atomic(); 2358c2ecf20Sopenharmony_ci} 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_cistatic inline void closure_queue(struct closure *cl) 2388c2ecf20Sopenharmony_ci{ 2398c2ecf20Sopenharmony_ci struct workqueue_struct *wq = cl->wq; 2408c2ecf20Sopenharmony_ci /** 2418c2ecf20Sopenharmony_ci * Changes made to closure, work_struct, or a couple of other structs 2428c2ecf20Sopenharmony_ci * may cause work.func not pointing to the right location. 2438c2ecf20Sopenharmony_ci */ 2448c2ecf20Sopenharmony_ci BUILD_BUG_ON(offsetof(struct closure, fn) 2458c2ecf20Sopenharmony_ci != offsetof(struct work_struct, func)); 2468c2ecf20Sopenharmony_ci if (wq) { 2478c2ecf20Sopenharmony_ci INIT_WORK(&cl->work, cl->work.func); 2488c2ecf20Sopenharmony_ci BUG_ON(!queue_work(wq, &cl->work)); 2498c2ecf20Sopenharmony_ci } else 2508c2ecf20Sopenharmony_ci cl->fn(cl); 2518c2ecf20Sopenharmony_ci} 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci/** 2548c2ecf20Sopenharmony_ci * closure_get - increment a closure's refcount 2558c2ecf20Sopenharmony_ci */ 2568c2ecf20Sopenharmony_cistatic inline void closure_get(struct closure *cl) 2578c2ecf20Sopenharmony_ci{ 2588c2ecf20Sopenharmony_ci#ifdef CONFIG_BCACHE_CLOSURES_DEBUG 2598c2ecf20Sopenharmony_ci BUG_ON((atomic_inc_return(&cl->remaining) & 2608c2ecf20Sopenharmony_ci CLOSURE_REMAINING_MASK) <= 1); 2618c2ecf20Sopenharmony_ci#else 2628c2ecf20Sopenharmony_ci atomic_inc(&cl->remaining); 2638c2ecf20Sopenharmony_ci#endif 2648c2ecf20Sopenharmony_ci} 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_ci/** 2678c2ecf20Sopenharmony_ci * closure_init - Initialize a closure, setting the refcount to 1 2688c2ecf20Sopenharmony_ci * @cl: closure to initialize 2698c2ecf20Sopenharmony_ci * @parent: parent of the new closure. cl will take a refcount on it for its 2708c2ecf20Sopenharmony_ci * lifetime; may be NULL. 2718c2ecf20Sopenharmony_ci */ 2728c2ecf20Sopenharmony_cistatic inline void closure_init(struct closure *cl, struct closure *parent) 2738c2ecf20Sopenharmony_ci{ 2748c2ecf20Sopenharmony_ci memset(cl, 0, sizeof(struct closure)); 2758c2ecf20Sopenharmony_ci cl->parent = parent; 2768c2ecf20Sopenharmony_ci if (parent) 2778c2ecf20Sopenharmony_ci closure_get(parent); 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ci atomic_set(&cl->remaining, CLOSURE_REMAINING_INITIALIZER); 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_ci closure_debug_create(cl); 2828c2ecf20Sopenharmony_ci closure_set_ip(cl); 2838c2ecf20Sopenharmony_ci} 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_cistatic inline void closure_init_stack(struct closure *cl) 2868c2ecf20Sopenharmony_ci{ 2878c2ecf20Sopenharmony_ci memset(cl, 0, sizeof(struct closure)); 2888c2ecf20Sopenharmony_ci atomic_set(&cl->remaining, CLOSURE_REMAINING_INITIALIZER); 2898c2ecf20Sopenharmony_ci} 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_ci/** 2928c2ecf20Sopenharmony_ci * closure_wake_up - wake up all closures on a wait list, 2938c2ecf20Sopenharmony_ci * with memory barrier 2948c2ecf20Sopenharmony_ci */ 2958c2ecf20Sopenharmony_cistatic inline void closure_wake_up(struct closure_waitlist *list) 2968c2ecf20Sopenharmony_ci{ 2978c2ecf20Sopenharmony_ci /* Memory barrier for the wait list */ 2988c2ecf20Sopenharmony_ci smp_mb(); 2998c2ecf20Sopenharmony_ci __closure_wake_up(list); 3008c2ecf20Sopenharmony_ci} 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_ci/** 3038c2ecf20Sopenharmony_ci * continue_at - jump to another function with barrier 3048c2ecf20Sopenharmony_ci * 3058c2ecf20Sopenharmony_ci * After @cl is no longer waiting on anything (i.e. all outstanding refs have 3068c2ecf20Sopenharmony_ci * been dropped with closure_put()), it will resume execution at @fn running out 3078c2ecf20Sopenharmony_ci * of @wq (or, if @wq is NULL, @fn will be called by closure_put() directly). 3088c2ecf20Sopenharmony_ci * 3098c2ecf20Sopenharmony_ci * This is because after calling continue_at() you no longer have a ref on @cl, 3108c2ecf20Sopenharmony_ci * and whatever @cl owns may be freed out from under you - a running closure fn 3118c2ecf20Sopenharmony_ci * has a ref on its own closure which continue_at() drops. 3128c2ecf20Sopenharmony_ci * 3138c2ecf20Sopenharmony_ci * Note you are expected to immediately return after using this macro. 3148c2ecf20Sopenharmony_ci */ 3158c2ecf20Sopenharmony_ci#define continue_at(_cl, _fn, _wq) \ 3168c2ecf20Sopenharmony_cido { \ 3178c2ecf20Sopenharmony_ci set_closure_fn(_cl, _fn, _wq); \ 3188c2ecf20Sopenharmony_ci closure_sub(_cl, CLOSURE_RUNNING + 1); \ 3198c2ecf20Sopenharmony_ci} while (0) 3208c2ecf20Sopenharmony_ci 3218c2ecf20Sopenharmony_ci/** 3228c2ecf20Sopenharmony_ci * closure_return - finish execution of a closure 3238c2ecf20Sopenharmony_ci * 3248c2ecf20Sopenharmony_ci * This is used to indicate that @cl is finished: when all outstanding refs on 3258c2ecf20Sopenharmony_ci * @cl have been dropped @cl's ref on its parent closure (as passed to 3268c2ecf20Sopenharmony_ci * closure_init()) will be dropped, if one was specified - thus this can be 3278c2ecf20Sopenharmony_ci * thought of as returning to the parent closure. 3288c2ecf20Sopenharmony_ci */ 3298c2ecf20Sopenharmony_ci#define closure_return(_cl) continue_at((_cl), NULL, NULL) 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_ci/** 3328c2ecf20Sopenharmony_ci * continue_at_nobarrier - jump to another function without barrier 3338c2ecf20Sopenharmony_ci * 3348c2ecf20Sopenharmony_ci * Causes @fn to be executed out of @cl, in @wq context (or called directly if 3358c2ecf20Sopenharmony_ci * @wq is NULL). 3368c2ecf20Sopenharmony_ci * 3378c2ecf20Sopenharmony_ci * The ref the caller of continue_at_nobarrier() had on @cl is now owned by @fn, 3388c2ecf20Sopenharmony_ci * thus it's not safe to touch anything protected by @cl after a 3398c2ecf20Sopenharmony_ci * continue_at_nobarrier(). 3408c2ecf20Sopenharmony_ci */ 3418c2ecf20Sopenharmony_ci#define continue_at_nobarrier(_cl, _fn, _wq) \ 3428c2ecf20Sopenharmony_cido { \ 3438c2ecf20Sopenharmony_ci set_closure_fn(_cl, _fn, _wq); \ 3448c2ecf20Sopenharmony_ci closure_queue(_cl); \ 3458c2ecf20Sopenharmony_ci} while (0) 3468c2ecf20Sopenharmony_ci 3478c2ecf20Sopenharmony_ci/** 3488c2ecf20Sopenharmony_ci * closure_return_with_destructor - finish execution of a closure, 3498c2ecf20Sopenharmony_ci * with destructor 3508c2ecf20Sopenharmony_ci * 3518c2ecf20Sopenharmony_ci * Works like closure_return(), except @destructor will be called when all 3528c2ecf20Sopenharmony_ci * outstanding refs on @cl have been dropped; @destructor may be used to safely 3538c2ecf20Sopenharmony_ci * free the memory occupied by @cl, and it is called with the ref on the parent 3548c2ecf20Sopenharmony_ci * closure still held - so @destructor could safely return an item to a 3558c2ecf20Sopenharmony_ci * freelist protected by @cl's parent. 3568c2ecf20Sopenharmony_ci */ 3578c2ecf20Sopenharmony_ci#define closure_return_with_destructor(_cl, _destructor) \ 3588c2ecf20Sopenharmony_cido { \ 3598c2ecf20Sopenharmony_ci set_closure_fn(_cl, _destructor, NULL); \ 3608c2ecf20Sopenharmony_ci closure_sub(_cl, CLOSURE_RUNNING - CLOSURE_DESTRUCTOR + 1); \ 3618c2ecf20Sopenharmony_ci} while (0) 3628c2ecf20Sopenharmony_ci 3638c2ecf20Sopenharmony_ci/** 3648c2ecf20Sopenharmony_ci * closure_call - execute @fn out of a new, uninitialized closure 3658c2ecf20Sopenharmony_ci * 3668c2ecf20Sopenharmony_ci * Typically used when running out of one closure, and we want to run @fn 3678c2ecf20Sopenharmony_ci * asynchronously out of a new closure - @parent will then wait for @cl to 3688c2ecf20Sopenharmony_ci * finish. 3698c2ecf20Sopenharmony_ci */ 3708c2ecf20Sopenharmony_cistatic inline void closure_call(struct closure *cl, closure_fn fn, 3718c2ecf20Sopenharmony_ci struct workqueue_struct *wq, 3728c2ecf20Sopenharmony_ci struct closure *parent) 3738c2ecf20Sopenharmony_ci{ 3748c2ecf20Sopenharmony_ci closure_init(cl, parent); 3758c2ecf20Sopenharmony_ci continue_at_nobarrier(cl, fn, wq); 3768c2ecf20Sopenharmony_ci} 3778c2ecf20Sopenharmony_ci 3788c2ecf20Sopenharmony_ci#endif /* _LINUX_CLOSURE_H */ 379