04f3d2b0ca7db4636b42b58163de7f16e15b4129
[platform/kernel/linux-starfive.git] / block / blk-ioc.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Functions related to io context handling
4  */
5 #include <linux/kernel.h>
6 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/bio.h>
9 #include <linux/blkdev.h>
10 #include <linux/slab.h>
11 #include <linux/sched/task.h>
12
13 #include "blk.h"
14 #include "blk-mq-sched.h"
15
16 /*
17  * For io context allocations
18  */
19 static struct kmem_cache *iocontext_cachep;
20
21 /**
22  * get_io_context - increment reference count to io_context
23  * @ioc: io_context to get
24  *
25  * Increment reference count to @ioc.
26  */
27 static void get_io_context(struct io_context *ioc)
28 {
29         BUG_ON(atomic_long_read(&ioc->refcount) <= 0);
30         atomic_long_inc(&ioc->refcount);
31 }
32
33 static void icq_free_icq_rcu(struct rcu_head *head)
34 {
35         struct io_cq *icq = container_of(head, struct io_cq, __rcu_head);
36
37         kmem_cache_free(icq->__rcu_icq_cache, icq);
38 }
39
40 /*
41  * Exit an icq. Called with ioc locked for blk-mq, and with both ioc
42  * and queue locked for legacy.
43  */
44 static void ioc_exit_icq(struct io_cq *icq)
45 {
46         struct elevator_type *et = icq->q->elevator->type;
47
48         if (icq->flags & ICQ_EXITED)
49                 return;
50
51         if (et->ops.exit_icq)
52                 et->ops.exit_icq(icq);
53
54         icq->flags |= ICQ_EXITED;
55 }
56
57 static void ioc_exit_icqs(struct io_context *ioc)
58 {
59         struct io_cq *icq;
60
61         spin_lock_irq(&ioc->lock);
62         hlist_for_each_entry(icq, &ioc->icq_list, ioc_node)
63                 ioc_exit_icq(icq);
64         spin_unlock_irq(&ioc->lock);
65 }
66
67 /*
68  * Release an icq. Called with ioc locked for blk-mq, and with both ioc
69  * and queue locked for legacy.
70  */
71 static void ioc_destroy_icq(struct io_cq *icq)
72 {
73         struct io_context *ioc = icq->ioc;
74         struct request_queue *q = icq->q;
75         struct elevator_type *et = q->elevator->type;
76
77         lockdep_assert_held(&ioc->lock);
78
79         radix_tree_delete(&ioc->icq_tree, icq->q->id);
80         hlist_del_init(&icq->ioc_node);
81         list_del_init(&icq->q_node);
82
83         /*
84          * Both setting lookup hint to and clearing it from @icq are done
85          * under queue_lock.  If it's not pointing to @icq now, it never
86          * will.  Hint assignment itself can race safely.
87          */
88         if (rcu_access_pointer(ioc->icq_hint) == icq)
89                 rcu_assign_pointer(ioc->icq_hint, NULL);
90
91         ioc_exit_icq(icq);
92
93         /*
94          * @icq->q might have gone away by the time RCU callback runs
95          * making it impossible to determine icq_cache.  Record it in @icq.
96          */
97         icq->__rcu_icq_cache = et->icq_cache;
98         icq->flags |= ICQ_DESTROYED;
99         call_rcu(&icq->__rcu_head, icq_free_icq_rcu);
100 }
101
102 /*
103  * Slow path for ioc release in put_io_context().  Performs double-lock
104  * dancing to unlink all icq's and then frees ioc.
105  */
106 static void ioc_release_fn(struct work_struct *work)
107 {
108         struct io_context *ioc = container_of(work, struct io_context,
109                                               release_work);
110         spin_lock_irq(&ioc->lock);
111
112         while (!hlist_empty(&ioc->icq_list)) {
113                 struct io_cq *icq = hlist_entry(ioc->icq_list.first,
114                                                 struct io_cq, ioc_node);
115                 struct request_queue *q = icq->q;
116
117                 if (spin_trylock(&q->queue_lock)) {
118                         ioc_destroy_icq(icq);
119                         spin_unlock(&q->queue_lock);
120                 } else {
121                         /* Make sure q and icq cannot be freed. */
122                         rcu_read_lock();
123
124                         /* Re-acquire the locks in the correct order. */
125                         spin_unlock(&ioc->lock);
126                         spin_lock(&q->queue_lock);
127                         spin_lock(&ioc->lock);
128
129                         /*
130                          * The icq may have been destroyed when the ioc lock
131                          * was released.
132                          */
133                         if (!(icq->flags & ICQ_DESTROYED))
134                                 ioc_destroy_icq(icq);
135
136                         spin_unlock(&q->queue_lock);
137                         rcu_read_unlock();
138                 }
139         }
140
141         spin_unlock_irq(&ioc->lock);
142
143         kmem_cache_free(iocontext_cachep, ioc);
144 }
145
146 /**
147  * put_io_context - put a reference of io_context
148  * @ioc: io_context to put
149  *
150  * Decrement reference count of @ioc and release it if the count reaches
151  * zero.
152  */
153 void put_io_context(struct io_context *ioc)
154 {
155         unsigned long flags;
156         bool free_ioc = false;
157
158         BUG_ON(atomic_long_read(&ioc->refcount) <= 0);
159
160         /*
161          * Releasing ioc requires reverse order double locking and we may
162          * already be holding a queue_lock.  Do it asynchronously from wq.
163          */
164         if (atomic_long_dec_and_test(&ioc->refcount)) {
165                 spin_lock_irqsave(&ioc->lock, flags);
166                 if (!hlist_empty(&ioc->icq_list))
167                         queue_work(system_power_efficient_wq,
168                                         &ioc->release_work);
169                 else
170                         free_ioc = true;
171                 spin_unlock_irqrestore(&ioc->lock, flags);
172         }
173
174         if (free_ioc)
175                 kmem_cache_free(iocontext_cachep, ioc);
176 }
177 EXPORT_SYMBOL_GPL(put_io_context);
178
179 /* Called by the exiting task */
180 void exit_io_context(struct task_struct *task)
181 {
182         struct io_context *ioc;
183
184         task_lock(task);
185         ioc = task->io_context;
186         task->io_context = NULL;
187         task_unlock(task);
188
189         if (atomic_dec_and_test(&ioc->active_ref)) {
190                 ioc_exit_icqs(ioc);
191                 put_io_context(ioc);
192         }
193 }
194
195 static void __ioc_clear_queue(struct list_head *icq_list)
196 {
197         unsigned long flags;
198
199         rcu_read_lock();
200         while (!list_empty(icq_list)) {
201                 struct io_cq *icq = list_entry(icq_list->next,
202                                                 struct io_cq, q_node);
203                 struct io_context *ioc = icq->ioc;
204
205                 spin_lock_irqsave(&ioc->lock, flags);
206                 if (icq->flags & ICQ_DESTROYED) {
207                         spin_unlock_irqrestore(&ioc->lock, flags);
208                         continue;
209                 }
210                 ioc_destroy_icq(icq);
211                 spin_unlock_irqrestore(&ioc->lock, flags);
212         }
213         rcu_read_unlock();
214 }
215
216 /**
217  * ioc_clear_queue - break any ioc association with the specified queue
218  * @q: request_queue being cleared
219  *
220  * Walk @q->icq_list and exit all io_cq's.
221  */
222 void ioc_clear_queue(struct request_queue *q)
223 {
224         LIST_HEAD(icq_list);
225
226         spin_lock_irq(&q->queue_lock);
227         list_splice_init(&q->icq_list, &icq_list);
228         spin_unlock_irq(&q->queue_lock);
229
230         __ioc_clear_queue(&icq_list);
231 }
232
233 static struct io_context *alloc_io_context(gfp_t gfp_flags, int node)
234 {
235         struct io_context *ioc;
236
237         ioc = kmem_cache_alloc_node(iocontext_cachep, gfp_flags | __GFP_ZERO,
238                                     node);
239         if (unlikely(!ioc))
240                 return NULL;
241
242         atomic_long_set(&ioc->refcount, 1);
243         atomic_set(&ioc->active_ref, 1);
244         spin_lock_init(&ioc->lock);
245         INIT_RADIX_TREE(&ioc->icq_tree, GFP_ATOMIC);
246         INIT_HLIST_HEAD(&ioc->icq_list);
247         INIT_WORK(&ioc->release_work, ioc_release_fn);
248         return ioc;
249 }
250
251 static struct io_context *create_task_io_context(struct task_struct *task,
252                 gfp_t gfp_flags, int node)
253 {
254         struct io_context *ioc;
255
256         ioc = alloc_io_context(gfp_flags, node);
257         if (!ioc)
258                 return NULL;
259
260         /*
261          * Try to install.  ioc shouldn't be installed if someone else
262          * already did or @task, which isn't %current, is exiting.  Note
263          * that we need to allow ioc creation on exiting %current as exit
264          * path may issue IOs from e.g. exit_files().  The exit path is
265          * responsible for not issuing IO after exit_io_context().
266          */
267         task_lock(task);
268         if (!task->io_context &&
269             (task == current || !(task->flags & PF_EXITING)))
270                 task->io_context = ioc;
271         else
272                 kmem_cache_free(iocontext_cachep, ioc);
273
274         ioc = task->io_context;
275         if (ioc)
276                 get_io_context(ioc);
277         task_unlock(task);
278         return ioc;
279 }
280
281 /**
282  * get_task_io_context - get io_context of a task
283  * @task: task of interest
284  * @gfp_flags: allocation flags, used if allocation is necessary
285  * @node: allocation node, used if allocation is necessary
286  *
287  * Return io_context of @task.  If it doesn't exist, it is created with
288  * @gfp_flags and @node.  The returned io_context has its reference count
289  * incremented.
290  *
291  * This function always goes through task_lock() and it's better to use
292  * %current->io_context + get_io_context() for %current.
293  */
294 struct io_context *get_task_io_context(struct task_struct *task,
295                                        gfp_t gfp_flags, int node)
296 {
297         struct io_context *ioc;
298
299         might_sleep_if(gfpflags_allow_blocking(gfp_flags));
300
301         task_lock(task);
302         ioc = task->io_context;
303         if (unlikely(!ioc)) {
304                 task_unlock(task);
305                 return create_task_io_context(task, gfp_flags, node);
306         }
307         get_io_context(ioc);
308         task_unlock(task);
309         return ioc;
310 }
311
312 int __copy_io(unsigned long clone_flags, struct task_struct *tsk)
313 {
314         struct io_context *ioc = current->io_context;
315
316         /*
317          * Share io context with parent, if CLONE_IO is set
318          */
319         if (clone_flags & CLONE_IO) {
320                 atomic_inc(&ioc->active_ref);
321                 tsk->io_context = ioc;
322         } else if (ioprio_valid(ioc->ioprio)) {
323                 tsk->io_context = alloc_io_context(GFP_KERNEL, NUMA_NO_NODE);
324                 if (!tsk->io_context)
325                         return -ENOMEM;
326                 tsk->io_context->ioprio = ioc->ioprio;
327         }
328
329         return 0;
330 }
331
332 /**
333  * ioc_lookup_icq - lookup io_cq from ioc
334  * @q: the associated request_queue
335  *
336  * Look up io_cq associated with @ioc - @q pair from @ioc.  Must be called
337  * with @q->queue_lock held.
338  */
339 struct io_cq *ioc_lookup_icq(struct request_queue *q)
340 {
341         struct io_context *ioc = current->io_context;
342         struct io_cq *icq;
343
344         lockdep_assert_held(&q->queue_lock);
345
346         /*
347          * icq's are indexed from @ioc using radix tree and hint pointer,
348          * both of which are protected with RCU.  All removals are done
349          * holding both q and ioc locks, and we're holding q lock - if we
350          * find a icq which points to us, it's guaranteed to be valid.
351          */
352         rcu_read_lock();
353         icq = rcu_dereference(ioc->icq_hint);
354         if (icq && icq->q == q)
355                 goto out;
356
357         icq = radix_tree_lookup(&ioc->icq_tree, q->id);
358         if (icq && icq->q == q)
359                 rcu_assign_pointer(ioc->icq_hint, icq); /* allowed to race */
360         else
361                 icq = NULL;
362 out:
363         rcu_read_unlock();
364         return icq;
365 }
366 EXPORT_SYMBOL(ioc_lookup_icq);
367
368 /**
369  * ioc_create_icq - create and link io_cq
370  * @q: request_queue of interest
371  *
372  * Make sure io_cq linking @ioc and @q exists.  If icq doesn't exist, they
373  * will be created using @gfp_mask.
374  *
375  * The caller is responsible for ensuring @ioc won't go away and @q is
376  * alive and will stay alive until this function returns.
377  */
378 static struct io_cq *ioc_create_icq(struct request_queue *q)
379 {
380         struct io_context *ioc = current->io_context;
381         struct elevator_type *et = q->elevator->type;
382         struct io_cq *icq;
383
384         /* allocate stuff */
385         icq = kmem_cache_alloc_node(et->icq_cache, GFP_ATOMIC | __GFP_ZERO,
386                                     q->node);
387         if (!icq)
388                 return NULL;
389
390         if (radix_tree_maybe_preload(GFP_ATOMIC) < 0) {
391                 kmem_cache_free(et->icq_cache, icq);
392                 return NULL;
393         }
394
395         icq->ioc = ioc;
396         icq->q = q;
397         INIT_LIST_HEAD(&icq->q_node);
398         INIT_HLIST_NODE(&icq->ioc_node);
399
400         /* lock both q and ioc and try to link @icq */
401         spin_lock_irq(&q->queue_lock);
402         spin_lock(&ioc->lock);
403
404         if (likely(!radix_tree_insert(&ioc->icq_tree, q->id, icq))) {
405                 hlist_add_head(&icq->ioc_node, &ioc->icq_list);
406                 list_add(&icq->q_node, &q->icq_list);
407                 if (et->ops.init_icq)
408                         et->ops.init_icq(icq);
409         } else {
410                 kmem_cache_free(et->icq_cache, icq);
411                 icq = ioc_lookup_icq(q);
412                 if (!icq)
413                         printk(KERN_ERR "cfq: icq link failed!\n");
414         }
415
416         spin_unlock(&ioc->lock);
417         spin_unlock_irq(&q->queue_lock);
418         radix_tree_preload_end();
419         return icq;
420 }
421
422 struct io_cq *ioc_find_get_icq(struct request_queue *q)
423 {
424         struct io_context *ioc = current->io_context;
425         struct io_cq *icq = NULL;
426
427         if (unlikely(!ioc)) {
428                 ioc = create_task_io_context(current, GFP_ATOMIC, q->node);
429                 if (!ioc)
430                         return NULL;
431         } else {
432                 get_io_context(ioc);
433
434                 spin_lock_irq(&q->queue_lock);
435                 icq = ioc_lookup_icq(q);
436                 spin_unlock_irq(&q->queue_lock);
437         }
438
439         if (!icq) {
440                 icq = ioc_create_icq(q);
441                 if (!icq) {
442                         put_io_context(ioc);
443                         return NULL;
444                 }
445         }
446         return icq;
447 }
448 EXPORT_SYMBOL_GPL(ioc_find_get_icq);
449
450 static int __init blk_ioc_init(void)
451 {
452         iocontext_cachep = kmem_cache_create("blkdev_ioc",
453                         sizeof(struct io_context), 0, SLAB_PANIC, NULL);
454         return 0;
455 }
456 subsys_initcall(blk_ioc_init);