dynamic_queue_limits.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #define POSDIFF(A, B) ((int)((A) - (B)) > 0 ? (A) - (B) : 0)
  14. #define AFTER_EQ(A, B) ((int)((A) - (B)) >= 0)
  15. /* Records completed count and recalculates the queue limit */
  16. void dql_completed(struct dql *dql, unsigned int count)
  17. {
  18. unsigned int inprogress, prev_inprogress, limit;
  19. unsigned int ovlimit, completed, num_queued;
  20. bool all_prev_completed;
  21. num_queued = READ_ONCE(dql->num_queued);
  22. /* Can't complete more than what's in queue */
  23. BUG_ON(count > num_queued - dql->num_completed);
  24. completed = dql->num_completed + count;
  25. limit = dql->limit;
  26. ovlimit = POSDIFF(num_queued - dql->num_completed, limit);
  27. inprogress = num_queued - completed;
  28. prev_inprogress = dql->prev_num_queued - dql->num_completed;
  29. all_prev_completed = AFTER_EQ(completed, dql->prev_num_queued);
  30. if ((ovlimit && !inprogress) ||
  31. (dql->prev_ovlimit && all_prev_completed)) {
  32. /*
  33. * Queue considered starved if:
  34. * - The queue was over-limit in the last interval,
  35. * and there is no more data in the queue.
  36. * OR
  37. * - The queue was over-limit in the previous interval and
  38. * when enqueuing it was possible that all queued data
  39. * had been consumed. This covers the case when queue
  40. * may have becomes starved between completion processing
  41. * running and next time enqueue was scheduled.
  42. *
  43. * When queue is starved increase the limit by the amount
  44. * of bytes both sent and completed in the last interval,
  45. * plus any previous over-limit.
  46. */
  47. limit += POSDIFF(completed, dql->prev_num_queued) +
  48. dql->prev_ovlimit;
  49. dql->slack_start_time = jiffies;
  50. dql->lowest_slack = UINT_MAX;
  51. } else if (inprogress && prev_inprogress && !all_prev_completed) {
  52. /*
  53. * Queue was not starved, check if the limit can be decreased.
  54. * A decrease is only considered if the queue has been busy in
  55. * the whole interval (the check above).
  56. *
  57. * If there is slack, the amount of excess data queued above
  58. * the amount needed to prevent starvation, the queue limit
  59. * can be decreased. To avoid hysteresis we consider the
  60. * minimum amount of slack found over several iterations of the
  61. * completion routine.
  62. */
  63. unsigned int slack, slack_last_objs;
  64. /*
  65. * Slack is the maximum of
  66. * - The queue limit plus previous over-limit minus twice
  67. * the number of objects completed. Note that two times
  68. * number of completed bytes is a basis for an upper bound
  69. * of the limit.
  70. * - Portion of objects in the last queuing operation that
  71. * was not part of non-zero previous over-limit. That is
  72. * "round down" by non-overlimit portion of the last
  73. * queueing operation.
  74. */
  75. slack = POSDIFF(limit + dql->prev_ovlimit,
  76. 2 * (completed - dql->num_completed));
  77. slack_last_objs = dql->prev_ovlimit ?
  78. POSDIFF(dql->prev_last_obj_cnt, dql->prev_ovlimit) : 0;
  79. slack = max(slack, slack_last_objs);
  80. if (slack < dql->lowest_slack)
  81. dql->lowest_slack = slack;
  82. if (time_after(jiffies,
  83. dql->slack_start_time + dql->slack_hold_time)) {
  84. limit = POSDIFF(limit, dql->lowest_slack);
  85. dql->slack_start_time = jiffies;
  86. dql->lowest_slack = UINT_MAX;
  87. }
  88. }
  89. /* Enforce bounds on limit */
  90. limit = clamp(limit, dql->min_limit, dql->max_limit);
  91. if (limit != dql->limit) {
  92. dql->limit = limit;
  93. ovlimit = 0;
  94. }
  95. dql->adj_limit = limit + completed;
  96. dql->prev_ovlimit = ovlimit;
  97. dql->prev_last_obj_cnt = dql->last_obj_cnt;
  98. dql->num_completed = completed;
  99. dql->prev_num_queued = num_queued;
  100. }
  101. EXPORT_SYMBOL(dql_completed);
  102. void dql_reset(struct dql *dql)
  103. {
  104. /* Reset all dynamic values */
  105. dql->limit = 0;
  106. dql->num_queued = 0;
  107. dql->num_completed = 0;
  108. dql->last_obj_cnt = 0;
  109. dql->prev_num_queued = 0;
  110. dql->prev_last_obj_cnt = 0;
  111. dql->prev_ovlimit = 0;
  112. dql->lowest_slack = UINT_MAX;
  113. dql->slack_start_time = jiffies;
  114. }
  115. EXPORT_SYMBOL(dql_reset);
  116. void dql_init(struct dql *dql, unsigned int hold_time)
  117. {
  118. dql->max_limit = DQL_MAX_LIMIT;
  119. dql->min_limit = 0;
  120. dql->slack_hold_time = hold_time;
  121. dql_reset(dql);
  122. }
  123. EXPORT_SYMBOL(dql_init);