drm/modes: Fix drm_mode_vrefres() docs
[drm/drm-misc.git] / net / sctp / stream_sched_prio.c
blob4d4d9da331f4c3b06e45b767376ce8bb63c60cb5
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* SCTP kernel implementation
3 * (C) Copyright Red Hat Inc. 2017
5 * This file is part of the SCTP kernel implementation
7 * These functions manipulate sctp stream queue/scheduling.
9 * Please send any bug reports or fixes you make to the
10 * email addresched(es):
11 * lksctp developers <linux-sctp@vger.kernel.org>
13 * Written or modified by:
14 * Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
17 #include <linux/list.h>
18 #include <net/sctp/sctp.h>
19 #include <net/sctp/sm.h>
20 #include <net/sctp/stream_sched.h>
22 /* Priority handling
23 * RFC DRAFT ndata section 3.4
26 static void sctp_sched_prio_unsched_all(struct sctp_stream *stream);
28 static struct sctp_stream_priorities *sctp_sched_prio_head_get(struct sctp_stream_priorities *p)
30 p->users++;
31 return p;
34 static void sctp_sched_prio_head_put(struct sctp_stream_priorities *p)
36 if (p && --p->users == 0)
37 kfree(p);
40 static struct sctp_stream_priorities *sctp_sched_prio_new_head(
41 struct sctp_stream *stream, int prio, gfp_t gfp)
43 struct sctp_stream_priorities *p;
45 p = kmalloc(sizeof(*p), gfp);
46 if (!p)
47 return NULL;
49 INIT_LIST_HEAD(&p->prio_sched);
50 INIT_LIST_HEAD(&p->active);
51 p->next = NULL;
52 p->prio = prio;
53 p->users = 1;
55 return p;
58 static struct sctp_stream_priorities *sctp_sched_prio_get_head(
59 struct sctp_stream *stream, int prio, gfp_t gfp)
61 struct sctp_stream_priorities *p;
62 int i;
64 /* Look into scheduled priorities first, as they are sorted and
65 * we can find it fast IF it's scheduled.
67 list_for_each_entry(p, &stream->prio_list, prio_sched) {
68 if (p->prio == prio)
69 return sctp_sched_prio_head_get(p);
70 if (p->prio > prio)
71 break;
74 /* No luck. So we search on all streams now. */
75 for (i = 0; i < stream->outcnt; i++) {
76 if (!SCTP_SO(stream, i)->ext)
77 continue;
79 p = SCTP_SO(stream, i)->ext->prio_head;
80 if (!p)
81 /* Means all other streams won't be initialized
82 * as well.
84 break;
85 if (p->prio == prio)
86 return sctp_sched_prio_head_get(p);
89 /* If not even there, allocate a new one. */
90 return sctp_sched_prio_new_head(stream, prio, gfp);
93 static void sctp_sched_prio_next_stream(struct sctp_stream_priorities *p)
95 struct list_head *pos;
97 pos = p->next->prio_list.next;
98 if (pos == &p->active)
99 pos = pos->next;
100 p->next = list_entry(pos, struct sctp_stream_out_ext, prio_list);
103 static bool sctp_sched_prio_unsched(struct sctp_stream_out_ext *soute)
105 bool scheduled = false;
107 if (!list_empty(&soute->prio_list)) {
108 struct sctp_stream_priorities *prio_head = soute->prio_head;
110 /* Scheduled */
111 scheduled = true;
113 if (prio_head->next == soute)
114 /* Try to move to the next stream */
115 sctp_sched_prio_next_stream(prio_head);
117 list_del_init(&soute->prio_list);
119 /* Also unsched the priority if this was the last stream */
120 if (list_empty(&prio_head->active)) {
121 list_del_init(&prio_head->prio_sched);
122 /* If there is no stream left, clear next */
123 prio_head->next = NULL;
127 return scheduled;
130 static void sctp_sched_prio_sched(struct sctp_stream *stream,
131 struct sctp_stream_out_ext *soute)
133 struct sctp_stream_priorities *prio, *prio_head;
135 prio_head = soute->prio_head;
137 /* Nothing to do if already scheduled */
138 if (!list_empty(&soute->prio_list))
139 return;
141 /* Schedule the stream. If there is a next, we schedule the new
142 * one before it, so it's the last in round robin order.
143 * If there isn't, we also have to schedule the priority.
145 if (prio_head->next) {
146 list_add(&soute->prio_list, prio_head->next->prio_list.prev);
147 return;
150 list_add(&soute->prio_list, &prio_head->active);
151 prio_head->next = soute;
153 list_for_each_entry(prio, &stream->prio_list, prio_sched) {
154 if (prio->prio > prio_head->prio) {
155 list_add(&prio_head->prio_sched, prio->prio_sched.prev);
156 return;
160 list_add_tail(&prio_head->prio_sched, &stream->prio_list);
163 static int sctp_sched_prio_set(struct sctp_stream *stream, __u16 sid,
164 __u16 prio, gfp_t gfp)
166 struct sctp_stream_out *sout = SCTP_SO(stream, sid);
167 struct sctp_stream_out_ext *soute = sout->ext;
168 struct sctp_stream_priorities *prio_head, *old;
169 bool reschedule = false;
171 old = soute->prio_head;
172 if (old && old->prio == prio)
173 return 0;
175 prio_head = sctp_sched_prio_get_head(stream, prio, gfp);
176 if (!prio_head)
177 return -ENOMEM;
179 reschedule = sctp_sched_prio_unsched(soute);
180 soute->prio_head = prio_head;
181 if (reschedule)
182 sctp_sched_prio_sched(stream, soute);
184 sctp_sched_prio_head_put(old);
185 return 0;
188 static int sctp_sched_prio_get(struct sctp_stream *stream, __u16 sid,
189 __u16 *value)
191 *value = SCTP_SO(stream, sid)->ext->prio_head->prio;
192 return 0;
195 static int sctp_sched_prio_init(struct sctp_stream *stream)
197 INIT_LIST_HEAD(&stream->prio_list);
199 return 0;
202 static int sctp_sched_prio_init_sid(struct sctp_stream *stream, __u16 sid,
203 gfp_t gfp)
205 INIT_LIST_HEAD(&SCTP_SO(stream, sid)->ext->prio_list);
206 return sctp_sched_prio_set(stream, sid, 0, gfp);
209 static void sctp_sched_prio_free_sid(struct sctp_stream *stream, __u16 sid)
211 sctp_sched_prio_head_put(SCTP_SO(stream, sid)->ext->prio_head);
212 SCTP_SO(stream, sid)->ext->prio_head = NULL;
215 static void sctp_sched_prio_enqueue(struct sctp_outq *q,
216 struct sctp_datamsg *msg)
218 struct sctp_stream *stream;
219 struct sctp_chunk *ch;
220 __u16 sid;
222 ch = list_first_entry(&msg->chunks, struct sctp_chunk, frag_list);
223 sid = sctp_chunk_stream_no(ch);
224 stream = &q->asoc->stream;
225 sctp_sched_prio_sched(stream, SCTP_SO(stream, sid)->ext);
228 static struct sctp_chunk *sctp_sched_prio_dequeue(struct sctp_outq *q)
230 struct sctp_stream *stream = &q->asoc->stream;
231 struct sctp_stream_priorities *prio;
232 struct sctp_stream_out_ext *soute;
233 struct sctp_chunk *ch = NULL;
235 /* Bail out quickly if queue is empty */
236 if (list_empty(&q->out_chunk_list))
237 goto out;
239 /* Find which chunk is next. It's easy, it's either the current
240 * one or the first chunk on the next active stream.
242 if (stream->out_curr) {
243 soute = stream->out_curr->ext;
244 } else {
245 prio = list_entry(stream->prio_list.next,
246 struct sctp_stream_priorities, prio_sched);
247 soute = prio->next;
249 ch = list_entry(soute->outq.next, struct sctp_chunk, stream_list);
250 sctp_sched_dequeue_common(q, ch);
252 out:
253 return ch;
256 static void sctp_sched_prio_dequeue_done(struct sctp_outq *q,
257 struct sctp_chunk *ch)
259 struct sctp_stream_priorities *prio;
260 struct sctp_stream_out_ext *soute;
261 __u16 sid;
263 /* Last chunk on that msg, move to the next stream on
264 * this priority.
266 sid = sctp_chunk_stream_no(ch);
267 soute = SCTP_SO(&q->asoc->stream, sid)->ext;
268 prio = soute->prio_head;
270 sctp_sched_prio_next_stream(prio);
272 if (list_empty(&soute->outq))
273 sctp_sched_prio_unsched(soute);
276 static void sctp_sched_prio_sched_all(struct sctp_stream *stream)
278 struct sctp_association *asoc;
279 struct sctp_stream_out *sout;
280 struct sctp_chunk *ch;
282 asoc = container_of(stream, struct sctp_association, stream);
283 list_for_each_entry(ch, &asoc->outqueue.out_chunk_list, list) {
284 __u16 sid;
286 sid = sctp_chunk_stream_no(ch);
287 sout = SCTP_SO(stream, sid);
288 if (sout->ext)
289 sctp_sched_prio_sched(stream, sout->ext);
293 static void sctp_sched_prio_unsched_all(struct sctp_stream *stream)
295 struct sctp_stream_priorities *p, *tmp;
296 struct sctp_stream_out_ext *soute, *souttmp;
298 list_for_each_entry_safe(p, tmp, &stream->prio_list, prio_sched)
299 list_for_each_entry_safe(soute, souttmp, &p->active, prio_list)
300 sctp_sched_prio_unsched(soute);
303 static struct sctp_sched_ops sctp_sched_prio = {
304 .set = sctp_sched_prio_set,
305 .get = sctp_sched_prio_get,
306 .init = sctp_sched_prio_init,
307 .init_sid = sctp_sched_prio_init_sid,
308 .free_sid = sctp_sched_prio_free_sid,
309 .enqueue = sctp_sched_prio_enqueue,
310 .dequeue = sctp_sched_prio_dequeue,
311 .dequeue_done = sctp_sched_prio_dequeue_done,
312 .sched_all = sctp_sched_prio_sched_all,
313 .unsched_all = sctp_sched_prio_unsched_all,
316 void sctp_sched_ops_prio_init(void)
318 sctp_sched_ops_register(SCTP_SS_PRIO, &sctp_sched_prio);