dynamic_queue_limits.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Dynamic byte queue limits. See include/linux/dynamic_queue_limits.h
  4. *
  5. * Copyright (c) 2011, Tom Herbert <therbert@google.com>
  6. */
  7. #include <linux/types.h>
  8. #include <linux/kernel.h>
  9. #include <linux/jiffies.h>
  10. #include <linux/dynamic_queue_limits.h>
  11. #include <linux/compiler.h>
  12. #include <linux/export.h>
  13. #include <trace/events/napi.h>
  14. #define POSDIFF(A, B) ((int)((A) - (B)) > 0 ? (A) - (B) : 0)
  15. #define AFTER_EQ(A, B) ((int)((A) - (B)) >= 0)
  16. static void dql_check_stall(struct dql *dql, unsigned short stall_thrs)
  17. {
  18. unsigned long now;
  19. if (!stall_thrs)
  20. return;
  21. now = jiffies;
  22. /* Check for a potential stall */
  23. if (time_after_eq(now, dql->last_reap + stall_thrs)) {
  24. unsigned long hist_head, t, start, end;
  25. /* We are trying to detect a period of at least @stall_thrs
  26. * jiffies without any Tx completions, but during first half
  27. * of which some Tx was posted.
  28. */
  29. dqs_again:
  30. hist_head = READ_ONCE(dql->history_head);
  31. /* pairs with smp_wmb() in dql_queued() */
  32. smp_rmb();
  33. /* Get the previous entry in the ring buffer, which is the
  34. * oldest sample.
  35. */
  36. start = (hist_head - DQL_HIST_LEN + 1) * BITS_PER_LONG;
  37. /* Advance start to continue from the last reap time */
  38. if (time_before(start, dql->last_reap + 1))
  39. start = dql->last_reap + 1;
  40. /* Newest sample we should have already seen a completion for */
  41. end = hist_head * BITS_PER_LONG + (BITS_PER_LONG - 1);
  42. /* Shrink the search space to [start, (now - start_thrs/2)] if
  43. * `end` is beyond the stall zone
  44. */
  45. if (time_before(now, end + stall_thrs / 2))
  46. end = now - stall_thrs / 2;
  47. /* Search for the queued time in [t, end] */
  48. for (t = start; time_before_eq(t, end); t++)
  49. if (test_bit(t % (DQL_HIST_LEN * BITS_PER_LONG),
  50. dql->history))
  51. break;
  52. /* Variable t contains the time of the queue */
  53. if (!time_before_eq(t, end))
  54. goto no_stall;
  55. /* The ring buffer was modified in the meantime, retry */
  56. if (hist_head != READ_ONCE(dql->history_head))
  57. goto dqs_again;
  58. dql->stall_cnt++;
  59. dql->stall_max = max_t(unsigned short, dql->stall_max, now - t);
  60. trace_dql_stall_detected(dql->stall_thrs, now - t,
  61. dql->last_reap, dql->history_head,
  62. now, dql->history);
  63. }
  64. no_stall:
  65. dql->last_reap = now;
  66. }
  67. /* Records completed count and recalculates the queue limit */
  68. void dql_completed(struct dql *dql, unsigned int count)
  69. {
  70. unsigned int inprogress, prev_inprogress, limit;
  71. unsigned int ovlimit, completed, num_queued;
  72. unsigned short stall_thrs;
  73. bool all_prev_completed;
  74. num_queued = READ_ONCE(dql->num_queued);
  75. /* Read stall_thrs in advance since it belongs to the same (first)
  76. * cache line as ->num_queued. This way, dql_check_stall() does not
  77. * need to touch the first cache line again later, reducing the window
  78. * of possible false sharing.
  79. */
  80. stall_thrs = READ_ONCE(dql->stall_thrs);
  81. /* Can't complete more than what's in queue */
  82. BUG_ON(count > num_queued - dql->num_completed);
  83. completed = dql->num_completed + count;
  84. limit = dql->limit;
  85. ovlimit = POSDIFF(num_queued - dql->num_completed, limit);
  86. inprogress = num_queued - completed;
  87. prev_inprogress = dql->prev_num_queued - dql->num_completed;
  88. all_prev_completed = AFTER_EQ(completed, dql->prev_num_queued);
  89. if ((ovlimit && !inprogress) ||
  90. (dql->prev_ovlimit && all_prev_completed)) {
  91. /*
  92. * Queue considered starved if:
  93. * - The queue was over-limit in the last interval,
  94. * and there is no more data in the queue.
  95. * OR
  96. * - The queue was over-limit in the previous interval and
  97. * when enqueuing it was possible that all queued data
  98. * had been consumed. This covers the case when queue
  99. * may have becomes starved between completion processing
  100. * running and next time enqueue was scheduled.
  101. *
  102. * When queue is starved increase the limit by the amount
  103. * of bytes both sent and completed in the last interval,
  104. * plus any previous over-limit.
  105. */
  106. limit += POSDIFF(completed, dql->prev_num_queued) +
  107. dql->prev_ovlimit;
  108. dql->slack_start_time = jiffies;
  109. dql->lowest_slack = UINT_MAX;
  110. } else if (inprogress && prev_inprogress && !all_prev_completed) {
  111. /*
  112. * Queue was not starved, check if the limit can be decreased.
  113. * A decrease is only considered if the queue has been busy in
  114. * the whole interval (the check above).
  115. *
  116. * If there is slack, the amount of excess data queued above
  117. * the amount needed to prevent starvation, the queue limit
  118. * can be decreased. To avoid hysteresis we consider the
  119. * minimum amount of slack found over several iterations of the
  120. * completion routine.
  121. */
  122. unsigned int slack, slack_last_objs;
  123. /*
  124. * Slack is the maximum of
  125. * - The queue limit plus previous over-limit minus twice
  126. * the number of objects completed. Note that two times
  127. * number of completed bytes is a basis for an upper bound
  128. * of the limit.
  129. * - Portion of objects in the last queuing operation that
  130. * was not part of non-zero previous over-limit. That is
  131. * "round down" by non-overlimit portion of the last
  132. * queueing operation.
  133. */
  134. slack = POSDIFF(limit + dql->prev_ovlimit,
  135. 2 * (completed - dql->num_completed));
  136. slack_last_objs = dql->prev_ovlimit ?
  137. POSDIFF(dql->prev_last_obj_cnt, dql->prev_ovlimit) : 0;
  138. slack = max(slack, slack_last_objs);
  139. if (slack < dql->lowest_slack)
  140. dql->lowest_slack = slack;
  141. if (time_after(jiffies,
  142. dql->slack_start_time + dql->slack_hold_time)) {
  143. limit = POSDIFF(limit, dql->lowest_slack);
  144. dql->slack_start_time = jiffies;
  145. dql->lowest_slack = UINT_MAX;
  146. }
  147. }
  148. /* Enforce bounds on limit */
  149. limit = clamp(limit, dql->min_limit, dql->max_limit);
  150. if (limit != dql->limit) {
  151. dql->limit = limit;
  152. ovlimit = 0;
  153. }
  154. dql->adj_limit = limit + completed;
  155. dql->prev_ovlimit = ovlimit;
  156. dql->prev_last_obj_cnt = READ_ONCE(dql->last_obj_cnt);
  157. dql->num_completed = completed;
  158. dql->prev_num_queued = num_queued;
  159. dql_check_stall(dql, stall_thrs);
  160. }
  161. EXPORT_SYMBOL(dql_completed);
  162. void dql_reset(struct dql *dql)
  163. {
  164. /* Reset all dynamic values */
  165. dql->limit = dql->min_limit;
  166. dql->num_queued = 0;
  167. dql->num_completed = 0;
  168. dql->last_obj_cnt = 0;
  169. dql->prev_num_queued = 0;
  170. dql->prev_last_obj_cnt = 0;
  171. dql->prev_ovlimit = 0;
  172. dql->lowest_slack = UINT_MAX;
  173. dql->slack_start_time = jiffies;
  174. dql->last_reap = jiffies;
  175. dql->history_head = jiffies / BITS_PER_LONG;
  176. memset(dql->history, 0, sizeof(dql->history));
  177. }
  178. EXPORT_SYMBOL(dql_reset);
  179. void dql_init(struct dql *dql, unsigned int hold_time)
  180. {
  181. dql->max_limit = DQL_MAX_LIMIT;
  182. dql->min_limit = 0;
  183. dql->slack_hold_time = hold_time;
  184. dql->stall_thrs = 0;
  185. dql_reset(dql);
  186. }
  187. EXPORT_SYMBOL(dql_init);