Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef IOCONTEXT_H
0003 #define IOCONTEXT_H
0004 
0005 #include <linux/radix-tree.h>
0006 #include <linux/rcupdate.h>
0007 #include <linux/workqueue.h>
0008 
0009 enum {
0010     ICQ_EXITED      = 1 << 2,
0011     ICQ_DESTROYED       = 1 << 3,
0012 };
0013 
0014 /*
0015  * An io_cq (icq) is association between an io_context (ioc) and a
0016  * request_queue (q).  This is used by elevators which need to track
0017  * information per ioc - q pair.
0018  *
0019  * Elevator can request use of icq by setting elevator_type->icq_size and
0020  * ->icq_align.  Both size and align must be larger than that of struct
0021  * io_cq and elevator can use the tail area for private information.  The
0022  * recommended way to do this is defining a struct which contains io_cq as
0023  * the first member followed by private members and using its size and
0024  * align.  For example,
0025  *
0026  *  struct snail_io_cq {
0027  *      struct io_cq    icq;
0028  *      int     poke_snail;
0029  *      int     feed_snail;
0030  *  };
0031  *
0032  *  struct elevator_type snail_elv_type {
0033  *      .ops =      { ... },
0034  *      .icq_size = sizeof(struct snail_io_cq),
0035  *      .icq_align =    __alignof__(struct snail_io_cq),
0036  *      ...
0037  *  };
0038  *
0039  * If icq_size is set, block core will manage icq's.  All requests will
0040  * have its ->elv.icq field set before elevator_ops->elevator_set_req_fn()
0041  * is called and be holding a reference to the associated io_context.
0042  *
0043  * Whenever a new icq is created, elevator_ops->elevator_init_icq_fn() is
0044  * called and, on destruction, ->elevator_exit_icq_fn().  Both functions
0045  * are called with both the associated io_context and queue locks held.
0046  *
0047  * Elevator is allowed to lookup icq using ioc_lookup_icq() while holding
0048  * queue lock but the returned icq is valid only until the queue lock is
0049  * released.  Elevators can not and should not try to create or destroy
0050  * icq's.
0051  *
0052  * As icq's are linked from both ioc and q, the locking rules are a bit
0053  * complex.
0054  *
0055  * - ioc lock nests inside q lock.
0056  *
0057  * - ioc->icq_list and icq->ioc_node are protected by ioc lock.
0058  *   q->icq_list and icq->q_node by q lock.
0059  *
0060  * - ioc->icq_tree and ioc->icq_hint are protected by ioc lock, while icq
0061  *   itself is protected by q lock.  However, both the indexes and icq
0062  *   itself are also RCU managed and lookup can be performed holding only
0063  *   the q lock.
0064  *
0065  * - icq's are not reference counted.  They are destroyed when either the
0066  *   ioc or q goes away.  Each request with icq set holds an extra
0067  *   reference to ioc to ensure it stays until the request is completed.
0068  *
0069  * - Linking and unlinking icq's are performed while holding both ioc and q
0070  *   locks.  Due to the lock ordering, q exit is simple but ioc exit
0071  *   requires reverse-order double lock dance.
0072  */
0073 struct io_cq {
0074     struct request_queue    *q;
0075     struct io_context   *ioc;
0076 
0077     /*
0078      * q_node and ioc_node link io_cq through icq_list of q and ioc
0079      * respectively.  Both fields are unused once ioc_exit_icq() is
0080      * called and shared with __rcu_icq_cache and __rcu_head which are
0081      * used for RCU free of io_cq.
0082      */
0083     union {
0084         struct list_head    q_node;
0085         struct kmem_cache   *__rcu_icq_cache;
0086     };
0087     union {
0088         struct hlist_node   ioc_node;
0089         struct rcu_head     __rcu_head;
0090     };
0091 
0092     unsigned int        flags;
0093 };
0094 
0095 /*
0096  * I/O subsystem state of the associated processes.  It is refcounted
0097  * and kmalloc'ed. These could be shared between processes.
0098  */
0099 struct io_context {
0100     atomic_long_t refcount;
0101     atomic_t active_ref;
0102 
0103     unsigned short ioprio;
0104 
0105 #ifdef CONFIG_BLK_ICQ
0106     /* all the fields below are protected by this lock */
0107     spinlock_t lock;
0108 
0109     struct radix_tree_root  icq_tree;
0110     struct io_cq __rcu  *icq_hint;
0111     struct hlist_head   icq_list;
0112 
0113     struct work_struct release_work;
0114 #endif /* CONFIG_BLK_ICQ */
0115 };
0116 
0117 struct task_struct;
0118 #ifdef CONFIG_BLOCK
0119 void put_io_context(struct io_context *ioc);
0120 void exit_io_context(struct task_struct *task);
0121 int __copy_io(unsigned long clone_flags, struct task_struct *tsk);
0122 static inline int copy_io(unsigned long clone_flags, struct task_struct *tsk)
0123 {
0124     if (!current->io_context)
0125         return 0;
0126     return __copy_io(clone_flags, tsk);
0127 }
0128 #else
0129 struct io_context;
0130 static inline void put_io_context(struct io_context *ioc) { }
0131 static inline void exit_io_context(struct task_struct *task) { }
0132 static inline int copy_io(unsigned long clone_flags, struct task_struct *tsk)
0133 {
0134     return 0;
0135 }
0136 #endif /* CONFIG_BLOCK */
0137 
0138 #endif /* IOCONTEXT_H */