iocontext.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef IOCONTEXT_H
  3. #define IOCONTEXT_H
  4. #include <linux/radix-tree.h>
  5. #include <linux/rcupdate.h>
  6. #include <linux/workqueue.h>
  7. enum {
  8. ICQ_EXITED = 1 << 2,
  9. ICQ_DESTROYED = 1 << 3,
  10. };
  11. /*
  12. * An io_cq (icq) is association between an io_context (ioc) and a
  13. * request_queue (q). This is used by elevators which need to track
  14. * information per ioc - q pair.
  15. *
  16. * Elevator can request use of icq by setting elevator_type->icq_size and
  17. * ->icq_align. Both size and align must be larger than that of struct
  18. * io_cq and elevator can use the tail area for private information. The
  19. * recommended way to do this is defining a struct which contains io_cq as
  20. * the first member followed by private members and using its size and
  21. * align. For example,
  22. *
  23. * struct snail_io_cq {
  24. * struct io_cq icq;
  25. * int poke_snail;
  26. * int feed_snail;
  27. * };
  28. *
  29. * struct elevator_type snail_elv_type {
  30. * .ops = { ... },
  31. * .icq_size = sizeof(struct snail_io_cq),
  32. * .icq_align = __alignof__(struct snail_io_cq),
  33. * ...
  34. * };
  35. *
  36. * If icq_size is set, block core will manage icq's. All requests will
  37. * have its ->elv.icq field set before elevator_ops->elevator_set_req_fn()
  38. * is called and be holding a reference to the associated io_context.
  39. *
  40. * Whenever a new icq is created, elevator_ops->elevator_init_icq_fn() is
  41. * called and, on destruction, ->elevator_exit_icq_fn(). Both functions
  42. * are called with both the associated io_context and queue locks held.
  43. *
  44. * Elevator is allowed to lookup icq using ioc_lookup_icq() while holding
  45. * queue lock but the returned icq is valid only until the queue lock is
  46. * released. Elevators can not and should not try to create or destroy
  47. * icq's.
  48. *
  49. * As icq's are linked from both ioc and q, the locking rules are a bit
  50. * complex.
  51. *
  52. * - ioc lock nests inside q lock.
  53. *
  54. * - ioc->icq_list and icq->ioc_node are protected by ioc lock.
  55. * q->icq_list and icq->q_node by q lock.
  56. *
  57. * - ioc->icq_tree and ioc->icq_hint are protected by ioc lock, while icq
  58. * itself is protected by q lock. However, both the indexes and icq
  59. * itself are also RCU managed and lookup can be performed holding only
  60. * the q lock.
  61. *
  62. * - icq's are not reference counted. They are destroyed when either the
  63. * ioc or q goes away. Each request with icq set holds an extra
  64. * reference to ioc to ensure it stays until the request is completed.
  65. *
  66. * - Linking and unlinking icq's are performed while holding both ioc and q
  67. * locks. Due to the lock ordering, q exit is simple but ioc exit
  68. * requires reverse-order double lock dance.
  69. */
  70. struct io_cq {
  71. struct request_queue *q;
  72. struct io_context *ioc;
  73. /*
  74. * q_node and ioc_node link io_cq through icq_list of q and ioc
  75. * respectively. Both fields are unused once ioc_exit_icq() is
  76. * called and shared with __rcu_icq_cache and __rcu_head which are
  77. * used for RCU free of io_cq.
  78. */
  79. union {
  80. struct list_head q_node;
  81. struct kmem_cache *__rcu_icq_cache;
  82. };
  83. union {
  84. struct hlist_node ioc_node;
  85. struct rcu_head __rcu_head;
  86. };
  87. unsigned int flags;
  88. };
  89. /*
  90. * I/O subsystem state of the associated processes. It is refcounted
  91. * and kmalloc'ed. These could be shared between processes.
  92. */
  93. struct io_context {
  94. atomic_long_t refcount;
  95. atomic_t active_ref;
  96. unsigned short ioprio;
  97. #ifdef CONFIG_BLK_ICQ
  98. /* all the fields below are protected by this lock */
  99. spinlock_t lock;
  100. struct radix_tree_root icq_tree;
  101. struct io_cq __rcu *icq_hint;
  102. struct hlist_head icq_list;
  103. struct work_struct release_work;
  104. #endif /* CONFIG_BLK_ICQ */
  105. };
  106. struct task_struct;
  107. #ifdef CONFIG_BLOCK
  108. void put_io_context(struct io_context *ioc);
  109. void exit_io_context(struct task_struct *task);
  110. int __copy_io(unsigned long clone_flags, struct task_struct *tsk);
  111. static inline int copy_io(unsigned long clone_flags, struct task_struct *tsk)
  112. {
  113. if (!current->io_context)
  114. return 0;
  115. return __copy_io(clone_flags, tsk);
  116. }
  117. #else
  118. struct io_context;
  119. static inline void put_io_context(struct io_context *ioc) { }
  120. static inline void exit_io_context(struct task_struct *task) { }
  121. static inline int copy_io(unsigned long clone_flags, struct task_struct *tsk)
  122. {
  123. return 0;
  124. }
  125. #endif /* CONFIG_BLOCK */
  126. #endif /* IOCONTEXT_H */