1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Dynamic queue limits (dql) - Definitions
4  *
5  * Copyright (c) 2011, Tom Herbert <therbert@google.com>
6  *
7  * This header file contains the definitions for dynamic queue limits (dql).
8  * dql would be used in conjunction with a producer/consumer type queue
9  * (possibly a HW queue).  Such a queue would have these general properties:
10  *
11  *   1) Objects are queued up to some limit specified as number of objects.
12  *   2) Periodically a completion process executes which retires consumed
13  *      objects.
14  *   3) Starvation occurs when limit has been reached, all queued data has
15  *      actually been consumed, but completion processing has not yet run
16  *      so queuing new data is blocked.
17  *   4) Minimizing the amount of queued data is desirable.
18  *
19  * The goal of dql is to calculate the limit as the minimum number of objects
20  * needed to prevent starvation.
21  *
22  * The primary functions of dql are:
23  *    dql_queued - called when objects are enqueued to record number of objects
24  *    dql_avail - returns how many objects are available to be queued based
25  *      on the object limit and how many objects are already enqueued
26  *    dql_completed - called at completion time to indicate how many objects
27  *      were retired from the queue
28  *
29  * The dql implementation does not implement any locking for the dql data
30  * structures, the higher layer should provide this.  dql_queued should
31  * be serialized to prevent concurrent execution of the function; this
32  * is also true for  dql_completed.  However, dql_queued and dlq_completed  can
33  * be executed concurrently (i.e. they can be protected by different locks).
34  */
35 
36 #ifndef _LINUX_DQL_H
37 #define _LINUX_DQL_H
38 
39 #ifdef __KERNEL__
40 
41 #include <linux/bitops.h>
42 #include <asm/bug.h>
43 
44 #define DQL_HIST_LEN		4
45 #define DQL_HIST_ENT(dql, idx)	((dql)->history[(idx) % DQL_HIST_LEN])
46 
47 struct dql {
48 	/* Fields accessed in enqueue path (dql_queued) */
49 	unsigned int	num_queued;		/* Total ever queued */
50 	unsigned int	adj_limit;		/* limit + num_completed */
51 	unsigned int	last_obj_cnt;		/* Count at last queuing */
52 
53 	/* Stall threshold (in jiffies), defined by user */
54 	unsigned short	stall_thrs;
55 
56 	unsigned long	history_head;		/* top 58 bits of jiffies */
57 	/* stall entries, a bit per entry */
58 	unsigned long	history[DQL_HIST_LEN];
59 
60 	/* Fields accessed only by completion path (dql_completed) */
61 
62 	unsigned int	limit ____cacheline_aligned_in_smp; /* Current limit */
63 	unsigned int	num_completed;		/* Total ever completed */
64 
65 	unsigned int	prev_ovlimit;		/* Previous over limit */
66 	unsigned int	prev_num_queued;	/* Previous queue total */
67 	unsigned int	prev_last_obj_cnt;	/* Previous queuing cnt */
68 
69 	unsigned int	lowest_slack;		/* Lowest slack found */
70 	unsigned long	slack_start_time;	/* Time slacks seen */
71 
72 	/* Configuration */
73 	unsigned int	max_limit;		/* Max limit */
74 	unsigned int	min_limit;		/* Minimum limit */
75 	unsigned int	slack_hold_time;	/* Time to measure slack */
76 
77 	/* Longest stall detected, reported to user */
78 	unsigned short	stall_max;
79 	unsigned long	last_reap;		/* Last reap (in jiffies) */
80 	unsigned long	stall_cnt;		/* Number of stalls */
81 };
82 
83 /* Set some static maximums */
84 #define DQL_MAX_OBJECT (UINT_MAX / 16)
85 #define DQL_MAX_LIMIT ((UINT_MAX / 2) - DQL_MAX_OBJECT)
86 
87 /* Populate the bitmap to be processed later in dql_check_stall() */
dql_queue_stall(struct dql * dql)88 static inline void dql_queue_stall(struct dql *dql)
89 {
90 	unsigned long map, now, now_hi, i;
91 
92 	now = jiffies;
93 	now_hi = now / BITS_PER_LONG;
94 
95 	/* The following code set a bit in the ring buffer, where each
96 	 * bit trackes time the packet was queued. The dql->history buffer
97 	 * tracks DQL_HIST_LEN * BITS_PER_LONG time (jiffies) slot
98 	 */
99 	if (unlikely(now_hi != dql->history_head)) {
100 		/* About to reuse slots, clear them */
101 		for (i = 0; i < DQL_HIST_LEN; i++) {
102 			/* Multiplication masks high bits */
103 			if (now_hi * BITS_PER_LONG ==
104 			    (dql->history_head + i) * BITS_PER_LONG)
105 				break;
106 			DQL_HIST_ENT(dql, dql->history_head + i + 1) = 0;
107 		}
108 		/* pairs with smp_rmb() in dql_check_stall() */
109 		smp_wmb();
110 		WRITE_ONCE(dql->history_head, now_hi);
111 	}
112 
113 	/* __set_bit() does not guarantee WRITE_ONCE() semantics */
114 	map = DQL_HIST_ENT(dql, now_hi);
115 
116 	/* Populate the history with an entry (bit) per queued */
117 	if (!(map & BIT_MASK(now)))
118 		WRITE_ONCE(DQL_HIST_ENT(dql, now_hi), map | BIT_MASK(now));
119 }
120 
121 /*
122  * Record number of objects queued. Assumes that caller has already checked
123  * availability in the queue with dql_avail.
124  */
dql_queued(struct dql * dql,unsigned int count)125 static inline void dql_queued(struct dql *dql, unsigned int count)
126 {
127 	if (WARN_ON_ONCE(count > DQL_MAX_OBJECT))
128 		return;
129 
130 	dql->last_obj_cnt = count;
131 
132 	/* We want to force a write first, so that cpu do not attempt
133 	 * to get cache line containing last_obj_cnt, num_queued, adj_limit
134 	 * in Shared state, but directly does a Request For Ownership
135 	 * It is only a hint, we use barrier() only.
136 	 */
137 	barrier();
138 
139 	dql->num_queued += count;
140 
141 	/* Only populate stall information if the threshold is set */
142 	if (READ_ONCE(dql->stall_thrs))
143 		dql_queue_stall(dql);
144 }
145 
146 /* Returns how many objects can be queued, < 0 indicates over limit. */
dql_avail(const struct dql * dql)147 static inline int dql_avail(const struct dql *dql)
148 {
149 	return READ_ONCE(dql->adj_limit) - READ_ONCE(dql->num_queued);
150 }
151 
152 /* Record number of completed objects and recalculate the limit. */
153 void dql_completed(struct dql *dql, unsigned int count);
154 
155 /* Reset dql state */
156 void dql_reset(struct dql *dql);
157 
158 /* Initialize dql state */
159 void dql_init(struct dql *dql, unsigned int hold_time);
160 
161 #endif /* _KERNEL_ */
162 
163 #endif /* _LINUX_DQL_H */
164