xref: /linux/drivers/gpu/drm/scheduler/sched_entity.c (revision 16e7698bc04d3dd19d95a688e4b0297a0e28a93b)
1 /*
2  * Copyright 2015 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 
24 #include <linux/export.h>
25 #include <linux/slab.h>
26 #include <linux/completion.h>
27 
28 #include <drm/drm_print.h>
29 #include <drm/gpu_scheduler.h>
30 
31 #include "sched_internal.h"
32 
33 #include "gpu_scheduler_trace.h"
34 
35 /**
36  * drm_sched_entity_stats_release - Entity stats kref release function
37  * @kref: Entity stats embedded kref pointer
38  */
39 void drm_sched_entity_stats_release(struct kref *kref)
40 {
41 	struct drm_sched_entity_stats *stats =
42 		container_of(kref, typeof(*stats), kref);
43 
44 	kfree(stats);
45 }
46 
47 /**
48  * drm_sched_entity_stats_new - Allocate a new struct drm_sched_entity_stats object
49  *
50  * Return: Pointer to newly allocated struct drm_sched_entity_stats object.
51  */
52 static struct drm_sched_entity_stats *drm_sched_entity_stats_new(void)
53 {
54 	struct drm_sched_entity_stats *stats;
55 
56 	stats = kzalloc_obj(*stats);
57 	if (!stats)
58 		return NULL;
59 
60 	kref_init(&stats->kref);
61 	spin_lock_init(&stats->lock);
62 	ewma_drm_sched_avgtime_init(&stats->avg_job_us);
63 
64 	return stats;
65 }
66 
67 /**
68  * drm_sched_entity_stats_job_add_gpu_time - Account job execution time to entity
69  * @job: Scheduler job to account.
70  *
71  * Accounts the execution time of @job to its respective entity stats object.
72  *
73  * Return: Job's real duration in micro seconds.
74  */
75 ktime_t drm_sched_entity_stats_job_add_gpu_time(struct drm_sched_job *job)
76 {
77 	struct drm_sched_entity_stats *stats = job->entity_stats;
78 	struct drm_sched_fence *s_fence = job->s_fence;
79 	ktime_t start, end, duration;
80 
81 	start = dma_fence_timestamp(&s_fence->scheduled);
82 	end = dma_fence_timestamp(&s_fence->finished);
83 	duration = ktime_sub(end, start);
84 
85 	spin_lock(&stats->lock);
86 	stats->runtime = ktime_add(stats->runtime, duration);
87 	ewma_drm_sched_avgtime_add(&stats->avg_job_us, ktime_to_us(duration));
88 	spin_unlock(&stats->lock);
89 
90 	return duration;
91 }
92 
93 /**
94  * drm_sched_entity_init - Init a context entity used by scheduler when
95  * submit to HW ring.
96  *
97  * @entity: scheduler entity to init
98  * @priority: priority of the entity
99  * @sched_list: the list of drm scheds on which jobs from this
100  *           entity can be submitted
101  * @num_sched_list: number of drm sched in sched_list
102  * @guilty: atomic_t set to 1 when a job on this queue
103  *          is found to be guilty causing a timeout
104  *
105  * Note that the &sched_list must have at least one element to schedule the entity.
106  *
107  * For changing @priority later on at runtime see
108  * drm_sched_entity_set_priority(). For changing the set of schedulers
109  * @sched_list at runtime see drm_sched_entity_modify_sched().
110  *
111  * An entity is cleaned up by calling drm_sched_entity_fini(). See also
112  * drm_sched_entity_destroy().
113  *
114  * Returns 0 on success or a negative error code on failure.
115  */
116 int drm_sched_entity_init(struct drm_sched_entity *entity,
117 			  enum drm_sched_priority priority,
118 			  struct drm_gpu_scheduler **sched_list,
119 			  unsigned int num_sched_list,
120 			  atomic_t *guilty)
121 {
122 	if (!entity || !sched_list || !num_sched_list || !sched_list[0])
123 		return -EINVAL;
124 
125 	memset(entity, 0, sizeof(struct drm_sched_entity));
126 
127 	entity->stats = drm_sched_entity_stats_new();
128 	if (!entity->stats)
129 		return -ENOMEM;
130 
131 	INIT_LIST_HEAD(&entity->list);
132 	entity->rq = NULL;
133 	entity->guilty = guilty;
134 	entity->priority = priority;
135 	entity->last_user = current->group_leader;
136 	entity->num_sched_list = num_sched_list;
137 	entity->sched_list = num_sched_list > 1 ? sched_list : NULL;
138 	entity->rq = &sched_list[0]->rq;
139 	RCU_INIT_POINTER(entity->last_scheduled, NULL);
140 	RB_CLEAR_NODE(&entity->rb_tree_node);
141 	init_completion(&entity->entity_idle);
142 
143 	/* We start in an idle state. */
144 	complete_all(&entity->entity_idle);
145 
146 	spin_lock_init(&entity->lock);
147 	spsc_queue_init(&entity->job_queue);
148 
149 	atomic_set(&entity->fence_seq, 0);
150 	entity->fence_context = dma_fence_context_alloc(2);
151 
152 	return 0;
153 }
154 EXPORT_SYMBOL(drm_sched_entity_init);
155 
156 /**
157  * drm_sched_entity_modify_sched - Modify sched of an entity
158  * @entity: scheduler entity to init
159  * @sched_list: the list of new drm scheds which will replace
160  *		 existing entity->sched_list
161  * @num_sched_list: number of drm sched in sched_list
162  *
163  * Note that this must be called under the same common lock for @entity as
164  * drm_sched_job_arm() and drm_sched_entity_push_job(), or the driver needs to
165  * guarantee through some other means that this is never called while new jobs
166  * can be pushed to @entity.
167  */
168 void drm_sched_entity_modify_sched(struct drm_sched_entity *entity,
169 				    struct drm_gpu_scheduler **sched_list,
170 				    unsigned int num_sched_list)
171 {
172 	WARN_ON(!num_sched_list || !sched_list);
173 
174 	spin_lock(&entity->lock);
175 	entity->sched_list = sched_list;
176 	entity->num_sched_list = num_sched_list;
177 	spin_unlock(&entity->lock);
178 }
179 EXPORT_SYMBOL(drm_sched_entity_modify_sched);
180 
181 static bool drm_sched_entity_is_idle(struct drm_sched_entity *entity)
182 {
183 	rmb(); /* for list_empty to work without lock */
184 
185 	if (list_empty(&entity->list) ||
186 	    spsc_queue_count(&entity->job_queue) == 0 ||
187 	    entity->stopped)
188 		return true;
189 
190 	return false;
191 }
192 
193 /**
194  * drm_sched_entity_error - return error of last scheduled job
195  * @entity: scheduler entity to check
196  *
197  * Opportunistically return the error of the last scheduled job. Result can
198  * change any time when new jobs are pushed to the hw.
199  */
200 int drm_sched_entity_error(struct drm_sched_entity *entity)
201 {
202 	struct dma_fence *fence;
203 	int r;
204 
205 	rcu_read_lock();
206 	fence = rcu_dereference(entity->last_scheduled);
207 	r = fence ? fence->error : 0;
208 	rcu_read_unlock();
209 
210 	return r;
211 }
212 EXPORT_SYMBOL(drm_sched_entity_error);
213 
214 static void drm_sched_entity_kill_jobs_cb(struct dma_fence *f,
215 					  struct dma_fence_cb *cb);
216 
217 static void drm_sched_entity_kill_jobs_work(struct work_struct *wrk)
218 {
219 	struct drm_sched_job *job = container_of(wrk, typeof(*job), work);
220 	struct dma_fence *f;
221 	unsigned long index;
222 
223 	/* Wait for all dependencies to avoid data corruptions */
224 	xa_for_each(&job->dependencies, index, f) {
225 		struct drm_sched_fence *s_fence = to_drm_sched_fence(f);
226 
227 		if (s_fence && f == &s_fence->scheduled) {
228 			/* The dependencies array had a reference on the scheduled
229 			 * fence, and the finished fence refcount might have
230 			 * dropped to zero. Use dma_fence_get_rcu() so we get
231 			 * a NULL fence in that case.
232 			 */
233 			f = dma_fence_get_rcu(&s_fence->finished);
234 
235 			/* Now that we have a reference on the finished fence,
236 			 * we can release the reference the dependencies array
237 			 * had on the scheduled fence.
238 			 */
239 			dma_fence_put(&s_fence->scheduled);
240 		}
241 
242 		xa_erase(&job->dependencies, index);
243 		if (f && !dma_fence_add_callback(f, &job->finish_cb,
244 						 drm_sched_entity_kill_jobs_cb))
245 			return;
246 
247 		dma_fence_put(f);
248 	}
249 
250 	drm_sched_fence_scheduled(job->s_fence, NULL);
251 	drm_sched_fence_finished(job->s_fence, -ESRCH);
252 	WARN_ON(job->s_fence->parent);
253 	job->sched->ops->free_job(job);
254 }
255 
256 /* Signal the scheduler finished fence when the entity in question is killed. */
257 static void drm_sched_entity_kill_jobs_cb(struct dma_fence *f,
258 					  struct dma_fence_cb *cb)
259 {
260 	struct drm_sched_job *job = container_of(cb, struct drm_sched_job,
261 						 finish_cb);
262 
263 	dma_fence_put(f);
264 
265 	INIT_WORK(&job->work, drm_sched_entity_kill_jobs_work);
266 	schedule_work(&job->work);
267 }
268 
269 /**
270  * drm_sched_entity_kill - kill an entity's pending jobs and remove it
271  * @entity: the entity to remove
272  *
273  * Removes the entity from the scheduler's run queue and kills all pending jobs.
274  *
275  * This function should be used over drm_sched_entity_flush() if it is not
276  * desired to actually wait for all pending jobs to finish.
277  */
278 void drm_sched_entity_kill(struct drm_sched_entity *entity)
279 {
280 	struct drm_sched_job *job;
281 	struct dma_fence *prev;
282 
283 	if (!entity->rq)
284 		return;
285 
286 	spin_lock(&entity->lock);
287 	entity->stopped = true;
288 	drm_sched_rq_remove_entity(entity->rq, entity);
289 	spin_unlock(&entity->lock);
290 
291 	/* Make sure this entity is not used by the scheduler at the moment */
292 	wait_for_completion(&entity->entity_idle);
293 
294 	/* The entity is guaranteed to not be used by the scheduler */
295 	prev = rcu_dereference_check(entity->last_scheduled, true);
296 	dma_fence_get(prev);
297 	while ((job = drm_sched_entity_queue_pop(entity))) {
298 		struct drm_sched_fence *s_fence = job->s_fence;
299 
300 		dma_fence_get(&s_fence->finished);
301 		if (!prev ||
302 		    dma_fence_add_callback(prev, &job->finish_cb,
303 					   drm_sched_entity_kill_jobs_cb)) {
304 			/*
305 			 * Adding callback above failed.
306 			 * dma_fence_put() checks for NULL.
307 			 */
308 			dma_fence_put(prev);
309 			drm_sched_entity_kill_jobs_cb(NULL, &job->finish_cb);
310 		}
311 
312 		prev = &s_fence->finished;
313 	}
314 	dma_fence_put(prev);
315 }
316 EXPORT_SYMBOL(drm_sched_entity_kill);
317 
318 /**
319  * drm_sched_entity_flush - Flush a context entity
320  *
321  * @entity: scheduler entity
322  * @timeout: time to wait in for Q to become empty in jiffies.
323  *
324  * Splitting drm_sched_entity_fini() into two functions, The first one does the
325  * waiting, removes the entity from the runqueue and returns an error when the
326  * process was killed.
327  *
328  * Returns the remaining time in jiffies left from the input timeout
329  */
330 long drm_sched_entity_flush(struct drm_sched_entity *entity, long timeout)
331 {
332 	struct drm_gpu_scheduler *sched;
333 	struct task_struct *last_user;
334 	long ret = timeout;
335 
336 	if (!entity->rq)
337 		return 0;
338 
339 	sched = container_of(entity->rq, typeof(*sched), rq);
340 	/*
341 	 * The client will not queue more jobs during this fini - consume
342 	 * existing queued ones, or discard them on SIGKILL.
343 	 */
344 	if (current->flags & PF_EXITING) {
345 		if (timeout)
346 			ret = wait_event_timeout(
347 					sched->job_scheduled,
348 					drm_sched_entity_is_idle(entity),
349 					timeout);
350 	} else {
351 		wait_event_killable(sched->job_scheduled,
352 				    drm_sched_entity_is_idle(entity));
353 	}
354 
355 	/* For a killed process disallow further enqueueing of jobs. */
356 	last_user = cmpxchg(&entity->last_user, current->group_leader, NULL);
357 	if (last_user == current->group_leader &&
358 	    (current->flags & PF_EXITING) && (current->exit_code == SIGKILL))
359 		drm_sched_entity_kill(entity);
360 
361 	return ret;
362 }
363 EXPORT_SYMBOL(drm_sched_entity_flush);
364 
365 /**
366  * drm_sched_entity_fini - Destroy a context entity
367  *
368  * @entity: scheduler entity
369  *
370  * Cleanups up @entity which has been initialized by drm_sched_entity_init().
371  *
372  * If there are potentially job still in flight or getting newly queued
373  * drm_sched_entity_flush() must be called first. This function then goes over
374  * the entity and signals all jobs with an error code if the process was killed.
375  */
376 void drm_sched_entity_fini(struct drm_sched_entity *entity)
377 {
378 	/*
379 	 * If consumption of existing jobs wasn't completed forcefully remove
380 	 * them. Also makes sure that the scheduler won't touch this entity any
381 	 * more.
382 	 */
383 	drm_sched_entity_kill(entity);
384 
385 	if (entity->dependency) {
386 		dma_fence_remove_callback(entity->dependency, &entity->cb);
387 		dma_fence_put(entity->dependency);
388 		entity->dependency = NULL;
389 	}
390 
391 	dma_fence_put(rcu_dereference_check(entity->last_scheduled, true));
392 	RCU_INIT_POINTER(entity->last_scheduled, NULL);
393 	drm_sched_entity_stats_put(entity->stats);
394 }
395 EXPORT_SYMBOL(drm_sched_entity_fini);
396 
397 /**
398  * drm_sched_entity_destroy - Destroy a context entity
399  * @entity: scheduler entity
400  *
401  * Calls drm_sched_entity_flush() and drm_sched_entity_fini() as a
402  * convenience wrapper.
403  */
404 void drm_sched_entity_destroy(struct drm_sched_entity *entity)
405 {
406 	drm_sched_entity_flush(entity, MAX_WAIT_SCHED_ENTITY_Q_EMPTY);
407 	drm_sched_entity_fini(entity);
408 }
409 EXPORT_SYMBOL(drm_sched_entity_destroy);
410 
411 /*
412  * drm_sched_entity_wakeup - callback to clear the entity's dependency and
413  * wake up the scheduler
414  */
415 static void drm_sched_entity_wakeup(struct dma_fence *f,
416 				    struct dma_fence_cb *cb)
417 {
418 	struct drm_sched_entity *entity =
419 		container_of(cb, struct drm_sched_entity, cb);
420 	struct drm_gpu_scheduler *sched =
421 		container_of(entity->rq, typeof(*sched), rq);
422 
423 	entity->dependency = NULL;
424 	dma_fence_put(f);
425 	drm_sched_wakeup(sched);
426 }
427 
428 /**
429  * drm_sched_entity_set_priority - Sets priority of the entity
430  *
431  * @entity: scheduler entity
432  * @priority: scheduler priority
433  *
434  * Update the priority of runqueues used for the entity.
435  */
436 void drm_sched_entity_set_priority(struct drm_sched_entity *entity,
437 				   enum drm_sched_priority priority)
438 {
439 	spin_lock(&entity->lock);
440 	entity->priority = priority;
441 	spin_unlock(&entity->lock);
442 }
443 EXPORT_SYMBOL(drm_sched_entity_set_priority);
444 
445 /*
446  * Add a callback to the current dependency of the entity to wake up the
447  * scheduler when the entity becomes available.
448  */
449 static bool drm_sched_entity_add_dependency_cb(struct drm_sched_entity *entity,
450 					       struct drm_sched_job *sched_job)
451 {
452 	struct drm_gpu_scheduler *sched =
453 		container_of(entity->rq, typeof(*sched), rq);
454 	struct dma_fence *fence = entity->dependency;
455 	struct drm_sched_fence *s_fence;
456 
457 	if (fence->context == entity->fence_context ||
458 	    fence->context == entity->fence_context + 1) {
459 		/*
460 		 * Fence is a scheduled/finished fence from a job
461 		 * which belongs to the same entity, we can ignore
462 		 * fences from ourself
463 		 */
464 		dma_fence_put(entity->dependency);
465 		return false;
466 	}
467 
468 	s_fence = to_drm_sched_fence(fence);
469 	if (!fence->error && s_fence && s_fence->sched == sched &&
470 	    !test_bit(DRM_SCHED_FENCE_DONT_PIPELINE, &fence->flags)) {
471 
472 		/*
473 		 * Fence is from the same scheduler, only need to wait for
474 		 * it to be scheduled
475 		 */
476 		fence = dma_fence_get(&s_fence->scheduled);
477 		dma_fence_put(entity->dependency);
478 		entity->dependency = fence;
479 	}
480 
481 	if (trace_drm_sched_job_unschedulable_enabled() &&
482 	    !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &entity->dependency->flags))
483 		trace_drm_sched_job_unschedulable(sched_job, entity->dependency);
484 
485 	if (!dma_fence_add_callback(entity->dependency, &entity->cb,
486 				    drm_sched_entity_wakeup))
487 		return true;
488 
489 	dma_fence_put(entity->dependency);
490 	return false;
491 }
492 
493 static struct dma_fence *
494 drm_sched_job_dependency(struct drm_sched_job *job,
495 			 struct drm_sched_entity *entity)
496 {
497 	struct dma_fence *f;
498 
499 	/* We keep the fence around, so we can iterate over all dependencies
500 	 * in drm_sched_entity_kill_jobs_cb() to ensure all deps are signaled
501 	 * before killing the job.
502 	 */
503 	f = xa_load(&job->dependencies, job->last_dependency);
504 	if (f) {
505 		job->last_dependency++;
506 		return dma_fence_get(f);
507 	}
508 
509 	if (job->sched->ops->prepare_job)
510 		return job->sched->ops->prepare_job(job, entity);
511 
512 	return NULL;
513 }
514 
515 struct drm_sched_job *drm_sched_entity_pop_job(struct drm_sched_entity *entity)
516 {
517 	struct drm_sched_job *sched_job;
518 
519 	sched_job = drm_sched_entity_queue_peek(entity);
520 	if (!sched_job)
521 		return NULL;
522 
523 	while ((entity->dependency =
524 			drm_sched_job_dependency(sched_job, entity))) {
525 		if (drm_sched_entity_add_dependency_cb(entity, sched_job))
526 			return NULL;
527 	}
528 
529 	/* skip jobs from entity that marked guilty */
530 	if (entity->guilty && atomic_read(entity->guilty))
531 		dma_fence_set_error(&sched_job->s_fence->finished, -ECANCELED);
532 
533 	dma_fence_put(rcu_dereference_check(entity->last_scheduled, true));
534 	rcu_assign_pointer(entity->last_scheduled,
535 			   dma_fence_get(&sched_job->s_fence->finished));
536 
537 	/*
538 	 * If the queue is empty we allow drm_sched_entity_select_rq() to
539 	 * locklessly access ->last_scheduled. This only works if we set the
540 	 * pointer before we dequeue and if we a write barrier here.
541 	 */
542 	smp_wmb();
543 
544 	spsc_queue_pop(&entity->job_queue);
545 
546 	drm_sched_rq_pop_entity(entity);
547 
548 	/* Jobs and entities might have different lifecycles. Since we're
549 	 * removing the job from the entities queue, set the jobs entity pointer
550 	 * to NULL to prevent any future access of the entity through this job.
551 	 */
552 	sched_job->entity = NULL;
553 
554 	return sched_job;
555 }
556 
557 void drm_sched_entity_select_rq(struct drm_sched_entity *entity)
558 {
559 	struct dma_fence *fence;
560 	struct drm_gpu_scheduler *sched;
561 	struct drm_sched_rq *rq;
562 
563 	/* single possible engine and already selected */
564 	if (!entity->sched_list)
565 		return;
566 
567 	/* queue non-empty, stay on the same engine */
568 	if (spsc_queue_count(&entity->job_queue))
569 		return;
570 
571 	/*
572 	 * Only when the queue is empty are we guaranteed that
573 	 * drm_sched_run_job_work() cannot change entity->last_scheduled. To
574 	 * enforce ordering we need a read barrier here. See
575 	 * drm_sched_entity_pop_job() for the other side.
576 	 */
577 	smp_rmb();
578 
579 	fence = rcu_dereference_check(entity->last_scheduled, true);
580 
581 	/* stay on the same engine if the previous job hasn't finished */
582 	if (fence && !dma_fence_is_signaled(fence))
583 		return;
584 
585 	spin_lock(&entity->lock);
586 	sched = drm_sched_pick_best(entity->sched_list, entity->num_sched_list);
587 	rq = sched ? &sched->rq : NULL;
588 	if (rq != entity->rq) {
589 		drm_sched_rq_remove_entity(entity->rq, entity);
590 		entity->rq = rq;
591 	}
592 
593 	if (entity->num_sched_list == 1)
594 		entity->sched_list = NULL;
595 
596 	spin_unlock(&entity->lock);
597 }
598 
599 /**
600  * drm_sched_entity_push_job - Submit a job to the entity's job queue
601  * @sched_job: job to submit
602  *
603  * Note: To guarantee that the order of insertion to queue matches the job's
604  * fence sequence number this function should be called with drm_sched_job_arm()
605  * under common lock for the struct drm_sched_entity that was set up for
606  * @sched_job in drm_sched_job_init().
607  */
608 void drm_sched_entity_push_job(struct drm_sched_job *sched_job)
609 {
610 	struct drm_sched_entity *entity = sched_job->entity;
611 	struct drm_gpu_scheduler *sched =
612 		container_of(entity->rq, typeof(*sched), rq);
613 	bool first;
614 
615 	trace_drm_sched_job_queue(sched_job, entity);
616 
617 	if (trace_drm_sched_job_add_dep_enabled()) {
618 		struct dma_fence *entry;
619 		unsigned long index;
620 
621 		xa_for_each(&sched_job->dependencies, index, entry)
622 			trace_drm_sched_job_add_dep(sched_job, entry);
623 	}
624 	atomic_inc(sched->score);
625 	WRITE_ONCE(entity->last_user, current->group_leader);
626 
627 	/*
628 	 * After the sched_job is pushed into the entity queue, it may be
629 	 * completed and freed up at any time. We can no longer access it.
630 	 */
631 	first = spsc_queue_push(&entity->job_queue, &sched_job->queue_node);
632 
633 	/* first job wakes up scheduler */
634 	if (first) {
635 		sched = drm_sched_rq_add_entity(entity);
636 		if (sched)
637 			drm_sched_wakeup(sched);
638 	}
639 }
640 EXPORT_SYMBOL(drm_sched_entity_push_job);
641