iocontext.h 4.4 KB

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