1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
3  *
4  * Copyright © 2018 VMware, Inc., Palo Alto, CA., USA
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sub license, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the
16  * next paragraph) shall be included in all copies or substantial portions
17  * of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25  * USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *
27  **************************************************************************/
28 #include <linux/slab.h>
29 #include "vmwgfx_validation.h"
30 #include "vmwgfx_drv.h"
31 
32 /**
33  * struct vmw_validation_bo_node - Buffer object validation metadata.
34  * @base: Metadata used for TTM reservation- and validation.
35  * @hash: A hash entry used for the duplicate detection hash table.
36  * @as_mob: Validate as mob.
37  * @cpu_blit: Validate for cpu blit access.
38  *
39  * Bit fields are used since these structures are allocated and freed in
40  * large numbers and space conservation is desired.
41  */
42 struct vmw_validation_bo_node {
43 	struct ttm_validate_buffer base;
44 	struct drm_hash_item hash;
45 	u32 as_mob : 1;
46 	u32 cpu_blit : 1;
47 };
48 
49 /**
50  * struct vmw_validation_res_node - Resource validation metadata.
51  * @head: List head for the resource validation list.
52  * @hash: A hash entry used for the duplicate detection hash table.
53  * @res: Reference counted resource pointer.
54  * @new_backup: Non ref-counted pointer to new backup buffer to be assigned
55  * to a resource.
56  * @new_backup_offset: Offset into the new backup mob for resources that can
57  * share MOBs.
58  * @no_buffer_needed: Kernel does not need to allocate a MOB during validation,
59  * the command stream provides a mob bind operation.
60  * @switching_backup: The validation process is switching backup MOB.
61  * @first_usage: True iff the resource has been seen only once in the current
62  * validation batch.
63  * @reserved: Whether the resource is currently reserved by this process.
64  * @private: Optionally additional memory for caller-private data.
65  *
66  * Bit fields are used since these structures are allocated and freed in
67  * large numbers and space conservation is desired.
68  */
69 struct vmw_validation_res_node {
70 	struct list_head head;
71 	struct drm_hash_item hash;
72 	struct vmw_resource *res;
73 	struct vmw_buffer_object *new_backup;
74 	unsigned long new_backup_offset;
75 	u32 no_buffer_needed : 1;
76 	u32 switching_backup : 1;
77 	u32 first_usage : 1;
78 	u32 reserved : 1;
79 	unsigned long private[0];
80 };
81 
82 /**
83  * vmw_validation_mem_alloc - Allocate kernel memory from the validation
84  * context based allocator
85  * @ctx: The validation context
86  * @size: The number of bytes to allocated.
87  *
88  * The memory allocated may not exceed PAGE_SIZE, and the returned
89  * address is aligned to sizeof(long). All memory allocated this way is
90  * reclaimed after validation when calling any of the exported functions:
91  * vmw_validation_unref_lists()
92  * vmw_validation_revert()
93  * vmw_validation_done()
94  *
95  * Return: Pointer to the allocated memory on success. NULL on failure.
96  */
97 void *vmw_validation_mem_alloc(struct vmw_validation_context *ctx,
98 			       unsigned int size)
99 {
100 	void *addr;
101 
102 	size = vmw_validation_align(size);
103 	if (size > PAGE_SIZE)
104 		return NULL;
105 
106 	if (ctx->mem_size_left < size) {
107 		struct page *page = alloc_page(GFP_KERNEL | __GFP_ZERO);
108 
109 		if (!page)
110 			return NULL;
111 
112 		list_add_tail(&page->lru, &ctx->page_list);
113 		ctx->page_address = page_address(page);
114 		ctx->mem_size_left = PAGE_SIZE;
115 	}
116 
117 	addr = (void *) (ctx->page_address + (PAGE_SIZE - ctx->mem_size_left));
118 	ctx->mem_size_left -= size;
119 
120 	return addr;
121 }
122 
123 /**
124  * vmw_validation_mem_free - Free all memory allocated using
125  * vmw_validation_mem_alloc()
126  * @ctx: The validation context
127  *
128  * All memory previously allocated for this context using
129  * vmw_validation_mem_alloc() is freed.
130  */
131 static void vmw_validation_mem_free(struct vmw_validation_context *ctx)
132 {
133 	struct page *entry, *next;
134 
135 	list_for_each_entry_safe(entry, next, &ctx->page_list, lru) {
136 		list_del_init(&entry->lru);
137 		__free_page(entry);
138 	}
139 
140 	ctx->mem_size_left = 0;
141 }
142 
143 /**
144  * vmw_validation_find_bo_dup - Find a duplicate buffer object entry in the
145  * validation context's lists.
146  * @ctx: The validation context to search.
147  * @vbo: The buffer object to search for.
148  *
149  * Return: Pointer to the struct vmw_validation_bo_node referencing the
150  * duplicate, or NULL if none found.
151  */
152 static struct vmw_validation_bo_node *
153 vmw_validation_find_bo_dup(struct vmw_validation_context *ctx,
154 			   struct vmw_buffer_object *vbo)
155 {
156 	struct  vmw_validation_bo_node *bo_node = NULL;
157 
158 	if (!ctx->merge_dups)
159 		return NULL;
160 
161 	if (ctx->ht) {
162 		struct drm_hash_item *hash;
163 
164 		if (!drm_ht_find_item(ctx->ht, (unsigned long) vbo, &hash))
165 			bo_node = container_of(hash, typeof(*bo_node), hash);
166 	} else {
167 		struct  vmw_validation_bo_node *entry;
168 
169 		list_for_each_entry(entry, &ctx->bo_list, base.head) {
170 			if (entry->base.bo == &vbo->base) {
171 				bo_node = entry;
172 				break;
173 			}
174 		}
175 	}
176 
177 	return bo_node;
178 }
179 
180 /**
181  * vmw_validation_find_res_dup - Find a duplicate resource entry in the
182  * validation context's lists.
183  * @ctx: The validation context to search.
184  * @vbo: The buffer object to search for.
185  *
186  * Return: Pointer to the struct vmw_validation_bo_node referencing the
187  * duplicate, or NULL if none found.
188  */
189 static struct vmw_validation_res_node *
190 vmw_validation_find_res_dup(struct vmw_validation_context *ctx,
191 			    struct vmw_resource *res)
192 {
193 	struct  vmw_validation_res_node *res_node = NULL;
194 
195 	if (!ctx->merge_dups)
196 		return NULL;
197 
198 	if (ctx->ht) {
199 		struct drm_hash_item *hash;
200 
201 		if (!drm_ht_find_item(ctx->ht, (unsigned long) res, &hash))
202 			res_node = container_of(hash, typeof(*res_node), hash);
203 	} else {
204 		struct  vmw_validation_res_node *entry;
205 
206 		list_for_each_entry(entry, &ctx->resource_ctx_list, head) {
207 			if (entry->res == res) {
208 				res_node = entry;
209 				goto out;
210 			}
211 		}
212 
213 		list_for_each_entry(entry, &ctx->resource_list, head) {
214 			if (entry->res == res) {
215 				res_node = entry;
216 				break;
217 			}
218 		}
219 
220 	}
221 out:
222 	return res_node;
223 }
224 
225 /**
226  * vmw_validation_add_bo - Add a buffer object to the validation context.
227  * @ctx: The validation context.
228  * @vbo: The buffer object.
229  * @as_mob: Validate as mob, otherwise suitable for GMR operations.
230  * @cpu_blit: Validate in a page-mappable location.
231  *
232  * Return: Zero on success, negative error code otherwise.
233  */
234 int vmw_validation_add_bo(struct vmw_validation_context *ctx,
235 			  struct vmw_buffer_object *vbo,
236 			  bool as_mob,
237 			  bool cpu_blit)
238 {
239 	struct vmw_validation_bo_node *bo_node;
240 
241 	bo_node = vmw_validation_find_bo_dup(ctx, vbo);
242 	if (bo_node) {
243 		if (bo_node->as_mob != as_mob ||
244 		    bo_node->cpu_blit != cpu_blit) {
245 			DRM_ERROR("Inconsistent buffer usage.\n");
246 			return -EINVAL;
247 		}
248 	} else {
249 		struct ttm_validate_buffer *val_buf;
250 		int ret;
251 
252 		bo_node = vmw_validation_mem_alloc(ctx, sizeof(*bo_node));
253 		if (!bo_node)
254 			return -ENOMEM;
255 
256 		if (ctx->ht) {
257 			bo_node->hash.key = (unsigned long) vbo;
258 			ret = drm_ht_insert_item(ctx->ht, &bo_node->hash);
259 			if (ret) {
260 				DRM_ERROR("Failed to initialize a buffer "
261 					  "validation entry.\n");
262 				return ret;
263 			}
264 		}
265 		val_buf = &bo_node->base;
266 		val_buf->bo = ttm_bo_get_unless_zero(&vbo->base);
267 		if (!val_buf->bo)
268 			return -ESRCH;
269 		val_buf->shared = false;
270 		list_add_tail(&val_buf->head, &ctx->bo_list);
271 		bo_node->as_mob = as_mob;
272 		bo_node->cpu_blit = cpu_blit;
273 	}
274 
275 	return 0;
276 }
277 
278 /**
279  * vmw_validation_add_resource - Add a resource to the validation context.
280  * @ctx: The validation context.
281  * @res: The resource.
282  * @priv_size: Size of private, additional metadata.
283  * @p_node: Output pointer of additional metadata address.
284  * @first_usage: Whether this was the first time this resource was seen.
285  *
286  * Return: Zero on success, negative error code otherwise.
287  */
288 int vmw_validation_add_resource(struct vmw_validation_context *ctx,
289 				struct vmw_resource *res,
290 				size_t priv_size,
291 				void **p_node,
292 				bool *first_usage)
293 {
294 	struct vmw_validation_res_node *node;
295 	int ret;
296 
297 	node = vmw_validation_find_res_dup(ctx, res);
298 	if (node) {
299 		node->first_usage = 0;
300 		goto out_fill;
301 	}
302 
303 	node = vmw_validation_mem_alloc(ctx, sizeof(*node) + priv_size);
304 	if (!node) {
305 		DRM_ERROR("Failed to allocate a resource validation "
306 			  "entry.\n");
307 		return -ENOMEM;
308 	}
309 
310 	if (ctx->ht) {
311 		node->hash.key = (unsigned long) res;
312 		ret = drm_ht_insert_item(ctx->ht, &node->hash);
313 		if (ret) {
314 			DRM_ERROR("Failed to initialize a resource validation "
315 				  "entry.\n");
316 			return ret;
317 		}
318 	}
319 	node->res = vmw_resource_reference_unless_doomed(res);
320 	if (!node->res)
321 		return -ESRCH;
322 
323 	node->first_usage = 1;
324 	if (!res->dev_priv->has_mob) {
325 		list_add_tail(&node->head, &ctx->resource_list);
326 	} else {
327 		switch (vmw_res_type(res)) {
328 		case vmw_res_context:
329 		case vmw_res_dx_context:
330 			list_add(&node->head, &ctx->resource_ctx_list);
331 			break;
332 		case vmw_res_cotable:
333 			list_add_tail(&node->head, &ctx->resource_ctx_list);
334 			break;
335 		default:
336 			list_add_tail(&node->head, &ctx->resource_list);
337 			break;
338 		}
339 	}
340 
341 out_fill:
342 	if (first_usage)
343 		*first_usage = node->first_usage;
344 	if (p_node)
345 		*p_node = &node->private;
346 
347 	return 0;
348 }
349 
350 /**
351  * vmw_validation_res_switch_backup - Register a backup MOB switch during
352  * validation.
353  * @ctx: The validation context.
354  * @val_private: The additional meta-data pointer returned when the
355  * resource was registered with the validation context. Used to identify
356  * the resource.
357  * @vbo: The new backup buffer object MOB. This buffer object needs to have
358  * already been registered with the validation context.
359  * @backup_offset: Offset into the new backup MOB.
360  */
361 void vmw_validation_res_switch_backup(struct vmw_validation_context *ctx,
362 				      void *val_private,
363 				      struct vmw_buffer_object *vbo,
364 				      unsigned long backup_offset)
365 {
366 	struct vmw_validation_res_node *val;
367 
368 	val = container_of(val_private, typeof(*val), private);
369 
370 	val->switching_backup = 1;
371 	if (val->first_usage)
372 		val->no_buffer_needed = 1;
373 
374 	val->new_backup = vbo;
375 	val->new_backup_offset = backup_offset;
376 }
377 
378 /**
379  * vmw_validation_res_reserve - Reserve all resources registered with this
380  * validation context.
381  * @ctx: The validation context.
382  * @intr: Use interruptible waits when possible.
383  *
384  * Return: Zero on success, -ERESTARTSYS if interrupted. Negative error
385  * code on failure.
386  */
387 int vmw_validation_res_reserve(struct vmw_validation_context *ctx,
388 			       bool intr)
389 {
390 	struct vmw_validation_res_node *val;
391 	int ret = 0;
392 
393 	list_splice_init(&ctx->resource_ctx_list, &ctx->resource_list);
394 
395 	list_for_each_entry(val, &ctx->resource_list, head) {
396 		struct vmw_resource *res = val->res;
397 
398 		ret = vmw_resource_reserve(res, intr, val->no_buffer_needed);
399 		if (ret)
400 			goto out_unreserve;
401 
402 		val->reserved = 1;
403 		if (res->backup) {
404 			struct vmw_buffer_object *vbo = res->backup;
405 
406 			ret = vmw_validation_add_bo
407 				(ctx, vbo, vmw_resource_needs_backup(res),
408 				 false);
409 			if (ret)
410 				goto out_unreserve;
411 		}
412 	}
413 
414 	return 0;
415 
416 out_unreserve:
417 	vmw_validation_res_unreserve(ctx, true);
418 	return ret;
419 }
420 
421 /**
422  * vmw_validation_res_unreserve - Unreserve all reserved resources
423  * registered with this validation context.
424  * @ctx: The validation context.
425  * @backoff: Whether this is a backoff- of a commit-type operation. This
426  * is used to determine whether to switch backup MOBs or not.
427  */
428 void vmw_validation_res_unreserve(struct vmw_validation_context *ctx,
429 				 bool backoff)
430 {
431 	struct vmw_validation_res_node *val;
432 
433 	list_splice_init(&ctx->resource_ctx_list, &ctx->resource_list);
434 
435 	list_for_each_entry(val, &ctx->resource_list, head) {
436 		if (val->reserved)
437 			vmw_resource_unreserve(val->res,
438 					       !backoff &&
439 					       val->switching_backup,
440 					       val->new_backup,
441 					       val->new_backup_offset);
442 	}
443 }
444 
445 /**
446  * vmw_validation_bo_validate_single - Validate a single buffer object.
447  * @bo: The TTM buffer object base.
448  * @interruptible: Whether to perform waits interruptible if possible.
449  * @validate_as_mob: Whether to validate in MOB memory.
450  *
451  * Return: Zero on success, -ERESTARTSYS if interrupted. Negative error
452  * code on failure.
453  */
454 int vmw_validation_bo_validate_single(struct ttm_buffer_object *bo,
455 				      bool interruptible,
456 				      bool validate_as_mob)
457 {
458 	struct vmw_buffer_object *vbo =
459 		container_of(bo, struct vmw_buffer_object, base);
460 	struct ttm_operation_ctx ctx = {
461 		.interruptible = interruptible,
462 		.no_wait_gpu = false
463 	};
464 	int ret;
465 
466 	if (vbo->pin_count > 0)
467 		return 0;
468 
469 	if (validate_as_mob)
470 		return ttm_bo_validate(bo, &vmw_mob_placement, &ctx);
471 
472 	/**
473 	 * Put BO in VRAM if there is space, otherwise as a GMR.
474 	 * If there is no space in VRAM and GMR ids are all used up,
475 	 * start evicting GMRs to make room. If the DMA buffer can't be
476 	 * used as a GMR, this will return -ENOMEM.
477 	 */
478 
479 	ret = ttm_bo_validate(bo, &vmw_vram_gmr_placement, &ctx);
480 	if (ret == 0 || ret == -ERESTARTSYS)
481 		return ret;
482 
483 	/**
484 	 * If that failed, try VRAM again, this time evicting
485 	 * previous contents.
486 	 */
487 
488 	ret = ttm_bo_validate(bo, &vmw_vram_placement, &ctx);
489 	return ret;
490 }
491 
492 /**
493  * vmw_validation_bo_validate - Validate all buffer objects registered with
494  * the validation context.
495  * @ctx: The validation context.
496  * @intr: Whether to perform waits interruptible if possible.
497  *
498  * Return: Zero on success, -ERESTARTSYS if interrupted,
499  * negative error code on failure.
500  */
501 int vmw_validation_bo_validate(struct vmw_validation_context *ctx, bool intr)
502 {
503 	struct vmw_validation_bo_node *entry;
504 	int ret;
505 
506 	list_for_each_entry(entry, &ctx->bo_list, base.head) {
507 		if (entry->cpu_blit) {
508 			struct ttm_operation_ctx ctx = {
509 				.interruptible = intr,
510 				.no_wait_gpu = false
511 			};
512 
513 			ret = ttm_bo_validate(entry->base.bo,
514 					      &vmw_nonfixed_placement, &ctx);
515 		} else {
516 			ret = vmw_validation_bo_validate_single
517 			(entry->base.bo, intr, entry->as_mob);
518 		}
519 		if (ret)
520 			return ret;
521 	}
522 	return 0;
523 }
524 
525 /**
526  * vmw_validation_res_validate - Validate all resources registered with the
527  * validation context.
528  * @ctx: The validation context.
529  * @intr: Whether to perform waits interruptible if possible.
530  *
531  * Before this function is called, all resource backup buffers must have
532  * been validated.
533  *
534  * Return: Zero on success, -ERESTARTSYS if interrupted,
535  * negative error code on failure.
536  */
537 int vmw_validation_res_validate(struct vmw_validation_context *ctx, bool intr)
538 {
539 	struct vmw_validation_res_node *val;
540 	int ret;
541 
542 	list_for_each_entry(val, &ctx->resource_list, head) {
543 		struct vmw_resource *res = val->res;
544 		struct vmw_buffer_object *backup = res->backup;
545 
546 		ret = vmw_resource_validate(res, intr);
547 		if (ret) {
548 			if (ret != -ERESTARTSYS)
549 				DRM_ERROR("Failed to validate resource.\n");
550 			return ret;
551 		}
552 
553 		/* Check if the resource switched backup buffer */
554 		if (backup && res->backup && (backup != res->backup)) {
555 			struct vmw_buffer_object *vbo = res->backup;
556 
557 			ret = vmw_validation_add_bo
558 				(ctx, vbo, vmw_resource_needs_backup(res),
559 				 false);
560 			if (ret)
561 				return ret;
562 		}
563 	}
564 	return 0;
565 }
566 
567 /**
568  * vmw_validation_drop_ht - Reset the hash table used for duplicate finding
569  * and unregister it from this validation context.
570  * @ctx: The validation context.
571  *
572  * The hash table used for duplicate finding is an expensive resource and
573  * may be protected by mutexes that may cause deadlocks during resource
574  * unreferencing if held. After resource- and buffer object registering,
575  * there is no longer any use for this hash table, so allow freeing it
576  * either to shorten any mutex locking time, or before resources- and
577  * buffer objects are freed during validation context cleanup.
578  */
579 void vmw_validation_drop_ht(struct vmw_validation_context *ctx)
580 {
581 	struct vmw_validation_bo_node *entry;
582 	struct vmw_validation_res_node *val;
583 
584 	if (!ctx->ht)
585 		return;
586 
587 	list_for_each_entry(entry, &ctx->bo_list, base.head)
588 		(void) drm_ht_remove_item(ctx->ht, &entry->hash);
589 
590 	list_for_each_entry(val, &ctx->resource_list, head)
591 		(void) drm_ht_remove_item(ctx->ht, &val->hash);
592 
593 	list_for_each_entry(val, &ctx->resource_ctx_list, head)
594 		(void) drm_ht_remove_item(ctx->ht, &val->hash);
595 
596 	ctx->ht = NULL;
597 }
598 
599 /**
600  * vmw_validation_unref_lists - Unregister previously registered buffer
601  * object and resources.
602  * @ctx: The validation context.
603  *
604  * Note that this function may cause buffer object- and resource destructors
605  * to be invoked.
606  */
607 void vmw_validation_unref_lists(struct vmw_validation_context *ctx)
608 {
609 	struct vmw_validation_bo_node *entry;
610 	struct vmw_validation_res_node *val;
611 
612 	list_for_each_entry(entry, &ctx->bo_list, base.head)
613 		ttm_bo_unref(&entry->base.bo);
614 
615 	list_splice_init(&ctx->resource_ctx_list, &ctx->resource_list);
616 	list_for_each_entry(val, &ctx->resource_list, head)
617 		vmw_resource_unreference(&val->res);
618 
619 	/*
620 	 * No need to detach each list entry since they are all freed with
621 	 * vmw_validation_free_mem. Just make the inaccessible.
622 	 */
623 	INIT_LIST_HEAD(&ctx->bo_list);
624 	INIT_LIST_HEAD(&ctx->resource_list);
625 
626 	vmw_validation_mem_free(ctx);
627 }
628 
629 /**
630  * vmw_validation_prepare - Prepare a validation context for command
631  * submission.
632  * @ctx: The validation context.
633  * @mutex: The mutex used to protect resource reservation.
634  * @intr: Whether to perform waits interruptible if possible.
635  *
636  * Note that the single reservation mutex @mutex is an unfortunate
637  * construct. Ideally resource reservation should be moved to per-resource
638  * ww_mutexes.
639  * If this functions doesn't return Zero to indicate success, all resources
640  * are left unreserved but still referenced.
641  * Return: Zero on success, -ERESTARTSYS if interrupted, negative error code
642  * on error.
643  */
644 int vmw_validation_prepare(struct vmw_validation_context *ctx,
645 			   struct mutex *mutex,
646 			   bool intr)
647 {
648 	int ret = 0;
649 
650 	if (mutex) {
651 		if (intr)
652 			ret = mutex_lock_interruptible(mutex);
653 		else
654 			mutex_lock(mutex);
655 		if (ret)
656 			return -ERESTARTSYS;
657 	}
658 
659 	ctx->res_mutex = mutex;
660 	ret = vmw_validation_res_reserve(ctx, intr);
661 	if (ret)
662 		goto out_no_res_reserve;
663 
664 	ret = vmw_validation_bo_reserve(ctx, intr);
665 	if (ret)
666 		goto out_no_bo_reserve;
667 
668 	ret = vmw_validation_bo_validate(ctx, intr);
669 	if (ret)
670 		goto out_no_validate;
671 
672 	ret = vmw_validation_res_validate(ctx, intr);
673 	if (ret)
674 		goto out_no_validate;
675 
676 	return 0;
677 
678 out_no_validate:
679 	vmw_validation_bo_backoff(ctx);
680 out_no_bo_reserve:
681 	vmw_validation_res_unreserve(ctx, true);
682 out_no_res_reserve:
683 	if (mutex)
684 		mutex_unlock(mutex);
685 
686 	return ret;
687 }
688 
689 /**
690  * vmw_validation_revert - Revert validation actions if command submission
691  * failed.
692  *
693  * @ctx: The validation context.
694  *
695  * The caller still needs to unref resources after a call to this function.
696  */
697 void vmw_validation_revert(struct vmw_validation_context *ctx)
698 {
699 	vmw_validation_bo_backoff(ctx);
700 	vmw_validation_res_unreserve(ctx, true);
701 	if (ctx->res_mutex)
702 		mutex_unlock(ctx->res_mutex);
703 	vmw_validation_unref_lists(ctx);
704 }
705 
706 /**
707  * vmw_validation_cone - Commit validation actions after command submission
708  * success.
709  * @ctx: The validation context.
710  * @fence: Fence with which to fence all buffer objects taking part in the
711  * command submission.
712  *
713  * The caller does NOT need to unref resources after a call to this function.
714  */
715 void vmw_validation_done(struct vmw_validation_context *ctx,
716 			 struct vmw_fence_obj *fence)
717 {
718 	vmw_validation_bo_fence(ctx, fence);
719 	vmw_validation_res_unreserve(ctx, false);
720 	if (ctx->res_mutex)
721 		mutex_unlock(ctx->res_mutex);
722 	vmw_validation_unref_lists(ctx);
723 }
724 
725 /**
726  * vmw_validation_preload_bo - Preload the validation memory allocator for a
727  * call to vmw_validation_add_bo().
728  * @ctx: Pointer to the validation context.
729  *
730  * Iff this function returns successfully, the next call to
731  * vmw_validation_add_bo() is guaranteed not to sleep. An error is not fatal
732  * but voids the guarantee.
733  *
734  * Returns: Zero if successful, %-EINVAL otherwise.
735  */
736 int vmw_validation_preload_bo(struct vmw_validation_context *ctx)
737 {
738 	unsigned int size = sizeof(struct vmw_validation_bo_node);
739 
740 	if (!vmw_validation_mem_alloc(ctx, size))
741 		return -ENOMEM;
742 
743 	ctx->mem_size_left += size;
744 	return 0;
745 }
746 
747 /**
748  * vmw_validation_preload_res - Preload the validation memory allocator for a
749  * call to vmw_validation_add_res().
750  * @ctx: Pointer to the validation context.
751  * @size: Size of the validation node extra data. See below.
752  *
753  * Iff this function returns successfully, the next call to
754  * vmw_validation_add_res() with the same or smaller @size is guaranteed not to
755  * sleep. An error is not fatal but voids the guarantee.
756  *
757  * Returns: Zero if successful, %-EINVAL otherwise.
758  */
759 int vmw_validation_preload_res(struct vmw_validation_context *ctx,
760 			       unsigned int size)
761 {
762 	size = vmw_validation_align(sizeof(struct vmw_validation_res_node) +
763 				    size) +
764 		vmw_validation_align(sizeof(struct vmw_validation_bo_node));
765 	if (!vmw_validation_mem_alloc(ctx, size))
766 		return -ENOMEM;
767 
768 	ctx->mem_size_left += size;
769 	return 0;
770 }
771