i915_gem_execbuffer.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  1. /*
  2. * Copyright © 2008,2010 Intel Corporation
  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 (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. * Chris Wilson <chris@chris-wilson.co.uk>
  26. *
  27. */
  28. #include <drm/drmP.h>
  29. #include <drm/i915_drm.h>
  30. #include "i915_drv.h"
  31. #include "i915_trace.h"
  32. #include "intel_drv.h"
  33. #include <linux/dma_remapping.h>
  34. struct eb_objects {
  35. struct list_head objects;
  36. int and;
  37. union {
  38. struct drm_i915_gem_object *lut[0];
  39. struct hlist_head buckets[0];
  40. };
  41. };
  42. static struct eb_objects *
  43. eb_create(struct drm_i915_gem_execbuffer2 *args)
  44. {
  45. struct eb_objects *eb = NULL;
  46. if (args->flags & I915_EXEC_HANDLE_LUT) {
  47. int size = args->buffer_count;
  48. size *= sizeof(struct drm_i915_gem_object *);
  49. size += sizeof(struct eb_objects);
  50. eb = kmalloc(size, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
  51. }
  52. if (eb == NULL) {
  53. int size = args->buffer_count;
  54. int count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
  55. BUILD_BUG_ON_NOT_POWER_OF_2(PAGE_SIZE / sizeof(struct hlist_head));
  56. while (count > 2*size)
  57. count >>= 1;
  58. eb = kzalloc(count*sizeof(struct hlist_head) +
  59. sizeof(struct eb_objects),
  60. GFP_TEMPORARY);
  61. if (eb == NULL)
  62. return eb;
  63. eb->and = count - 1;
  64. } else
  65. eb->and = -args->buffer_count;
  66. INIT_LIST_HEAD(&eb->objects);
  67. return eb;
  68. }
  69. static void
  70. eb_reset(struct eb_objects *eb)
  71. {
  72. if (eb->and >= 0)
  73. memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
  74. }
  75. static int
  76. eb_lookup_objects(struct eb_objects *eb,
  77. struct drm_i915_gem_exec_object2 *exec,
  78. const struct drm_i915_gem_execbuffer2 *args,
  79. struct drm_file *file)
  80. {
  81. int i;
  82. spin_lock(&file->table_lock);
  83. for (i = 0; i < args->buffer_count; i++) {
  84. struct drm_i915_gem_object *obj;
  85. obj = to_intel_bo(idr_find(&file->object_idr, exec[i].handle));
  86. if (obj == NULL) {
  87. spin_unlock(&file->table_lock);
  88. DRM_DEBUG("Invalid object handle %d at index %d\n",
  89. exec[i].handle, i);
  90. return -ENOENT;
  91. }
  92. if (!list_empty(&obj->exec_list)) {
  93. spin_unlock(&file->table_lock);
  94. DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
  95. obj, exec[i].handle, i);
  96. return -EINVAL;
  97. }
  98. drm_gem_object_reference(&obj->base);
  99. list_add_tail(&obj->exec_list, &eb->objects);
  100. obj->exec_entry = &exec[i];
  101. if (eb->and < 0) {
  102. eb->lut[i] = obj;
  103. } else {
  104. uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
  105. obj->exec_handle = handle;
  106. hlist_add_head(&obj->exec_node,
  107. &eb->buckets[handle & eb->and]);
  108. }
  109. }
  110. spin_unlock(&file->table_lock);
  111. return 0;
  112. }
  113. static struct drm_i915_gem_object *
  114. eb_get_object(struct eb_objects *eb, unsigned long handle)
  115. {
  116. if (eb->and < 0) {
  117. if (handle >= -eb->and)
  118. return NULL;
  119. return eb->lut[handle];
  120. } else {
  121. struct hlist_head *head;
  122. struct hlist_node *node;
  123. head = &eb->buckets[handle & eb->and];
  124. hlist_for_each(node, head) {
  125. struct drm_i915_gem_object *obj;
  126. obj = hlist_entry(node, struct drm_i915_gem_object, exec_node);
  127. if (obj->exec_handle == handle)
  128. return obj;
  129. }
  130. return NULL;
  131. }
  132. }
  133. static void
  134. eb_destroy(struct eb_objects *eb)
  135. {
  136. while (!list_empty(&eb->objects)) {
  137. struct drm_i915_gem_object *obj;
  138. obj = list_first_entry(&eb->objects,
  139. struct drm_i915_gem_object,
  140. exec_list);
  141. list_del_init(&obj->exec_list);
  142. drm_gem_object_unreference(&obj->base);
  143. }
  144. kfree(eb);
  145. }
  146. static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
  147. {
  148. return (obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
  149. !obj->map_and_fenceable ||
  150. obj->cache_level != I915_CACHE_NONE);
  151. }
  152. static int
  153. i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
  154. struct eb_objects *eb,
  155. struct drm_i915_gem_relocation_entry *reloc)
  156. {
  157. struct drm_device *dev = obj->base.dev;
  158. struct drm_gem_object *target_obj;
  159. struct drm_i915_gem_object *target_i915_obj;
  160. uint32_t target_offset;
  161. int ret = -EINVAL;
  162. /* we've already hold a reference to all valid objects */
  163. target_obj = &eb_get_object(eb, reloc->target_handle)->base;
  164. if (unlikely(target_obj == NULL))
  165. return -ENOENT;
  166. target_i915_obj = to_intel_bo(target_obj);
  167. target_offset = i915_gem_obj_ggtt_offset(target_i915_obj);
  168. /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
  169. * pipe_control writes because the gpu doesn't properly redirect them
  170. * through the ppgtt for non_secure batchbuffers. */
  171. if (unlikely(IS_GEN6(dev) &&
  172. reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
  173. !target_i915_obj->has_global_gtt_mapping)) {
  174. i915_gem_gtt_bind_object(target_i915_obj,
  175. target_i915_obj->cache_level);
  176. }
  177. /* Validate that the target is in a valid r/w GPU domain */
  178. if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
  179. DRM_DEBUG("reloc with multiple write domains: "
  180. "obj %p target %d offset %d "
  181. "read %08x write %08x",
  182. obj, reloc->target_handle,
  183. (int) reloc->offset,
  184. reloc->read_domains,
  185. reloc->write_domain);
  186. return ret;
  187. }
  188. if (unlikely((reloc->write_domain | reloc->read_domains)
  189. & ~I915_GEM_GPU_DOMAINS)) {
  190. DRM_DEBUG("reloc with read/write non-GPU domains: "
  191. "obj %p target %d offset %d "
  192. "read %08x write %08x",
  193. obj, reloc->target_handle,
  194. (int) reloc->offset,
  195. reloc->read_domains,
  196. reloc->write_domain);
  197. return ret;
  198. }
  199. target_obj->pending_read_domains |= reloc->read_domains;
  200. target_obj->pending_write_domain |= reloc->write_domain;
  201. /* If the relocation already has the right value in it, no
  202. * more work needs to be done.
  203. */
  204. if (target_offset == reloc->presumed_offset)
  205. return 0;
  206. /* Check that the relocation address is valid... */
  207. if (unlikely(reloc->offset > obj->base.size - 4)) {
  208. DRM_DEBUG("Relocation beyond object bounds: "
  209. "obj %p target %d offset %d size %d.\n",
  210. obj, reloc->target_handle,
  211. (int) reloc->offset,
  212. (int) obj->base.size);
  213. return ret;
  214. }
  215. if (unlikely(reloc->offset & 3)) {
  216. DRM_DEBUG("Relocation not 4-byte aligned: "
  217. "obj %p target %d offset %d.\n",
  218. obj, reloc->target_handle,
  219. (int) reloc->offset);
  220. return ret;
  221. }
  222. /* We can't wait for rendering with pagefaults disabled */
  223. if (obj->active && in_atomic())
  224. return -EFAULT;
  225. reloc->delta += target_offset;
  226. if (use_cpu_reloc(obj)) {
  227. uint32_t page_offset = offset_in_page(reloc->offset);
  228. char *vaddr;
  229. ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  230. if (ret)
  231. return ret;
  232. vaddr = kmap_atomic(i915_gem_object_get_page(obj,
  233. reloc->offset >> PAGE_SHIFT));
  234. *(uint32_t *)(vaddr + page_offset) = reloc->delta;
  235. kunmap_atomic(vaddr);
  236. } else {
  237. struct drm_i915_private *dev_priv = dev->dev_private;
  238. uint32_t __iomem *reloc_entry;
  239. void __iomem *reloc_page;
  240. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  241. if (ret)
  242. return ret;
  243. ret = i915_gem_object_put_fence(obj);
  244. if (ret)
  245. return ret;
  246. /* Map the page containing the relocation we're going to perform. */
  247. reloc->offset += i915_gem_obj_ggtt_offset(obj);
  248. reloc_page = io_mapping_map_atomic_wc(dev_priv->gtt.mappable,
  249. reloc->offset & PAGE_MASK);
  250. reloc_entry = (uint32_t __iomem *)
  251. (reloc_page + offset_in_page(reloc->offset));
  252. iowrite32(reloc->delta, reloc_entry);
  253. io_mapping_unmap_atomic(reloc_page);
  254. }
  255. /* and update the user's relocation entry */
  256. reloc->presumed_offset = target_offset;
  257. return 0;
  258. }
  259. static int
  260. i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
  261. struct eb_objects *eb)
  262. {
  263. #define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
  264. struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
  265. struct drm_i915_gem_relocation_entry __user *user_relocs;
  266. struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  267. int remain, ret;
  268. user_relocs = to_user_ptr(entry->relocs_ptr);
  269. remain = entry->relocation_count;
  270. while (remain) {
  271. struct drm_i915_gem_relocation_entry *r = stack_reloc;
  272. int count = remain;
  273. if (count > ARRAY_SIZE(stack_reloc))
  274. count = ARRAY_SIZE(stack_reloc);
  275. remain -= count;
  276. if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0])))
  277. return -EFAULT;
  278. do {
  279. u64 offset = r->presumed_offset;
  280. ret = i915_gem_execbuffer_relocate_entry(obj, eb, r);
  281. if (ret)
  282. return ret;
  283. if (r->presumed_offset != offset &&
  284. __copy_to_user_inatomic(&user_relocs->presumed_offset,
  285. &r->presumed_offset,
  286. sizeof(r->presumed_offset))) {
  287. return -EFAULT;
  288. }
  289. user_relocs++;
  290. r++;
  291. } while (--count);
  292. }
  293. return 0;
  294. #undef N_RELOC
  295. }
  296. static int
  297. i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
  298. struct eb_objects *eb,
  299. struct drm_i915_gem_relocation_entry *relocs)
  300. {
  301. const struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  302. int i, ret;
  303. for (i = 0; i < entry->relocation_count; i++) {
  304. ret = i915_gem_execbuffer_relocate_entry(obj, eb, &relocs[i]);
  305. if (ret)
  306. return ret;
  307. }
  308. return 0;
  309. }
  310. static int
  311. i915_gem_execbuffer_relocate(struct eb_objects *eb)
  312. {
  313. struct drm_i915_gem_object *obj;
  314. int ret = 0;
  315. /* This is the fast path and we cannot handle a pagefault whilst
  316. * holding the struct mutex lest the user pass in the relocations
  317. * contained within a mmaped bo. For in such a case we, the page
  318. * fault handler would call i915_gem_fault() and we would try to
  319. * acquire the struct mutex again. Obviously this is bad and so
  320. * lockdep complains vehemently.
  321. */
  322. pagefault_disable();
  323. list_for_each_entry(obj, &eb->objects, exec_list) {
  324. ret = i915_gem_execbuffer_relocate_object(obj, eb);
  325. if (ret)
  326. break;
  327. }
  328. pagefault_enable();
  329. return ret;
  330. }
  331. #define __EXEC_OBJECT_HAS_PIN (1<<31)
  332. #define __EXEC_OBJECT_HAS_FENCE (1<<30)
  333. static int
  334. need_reloc_mappable(struct drm_i915_gem_object *obj)
  335. {
  336. struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  337. return entry->relocation_count && !use_cpu_reloc(obj);
  338. }
  339. static int
  340. i915_gem_execbuffer_reserve_object(struct drm_i915_gem_object *obj,
  341. struct intel_ring_buffer *ring,
  342. bool *need_reloc)
  343. {
  344. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  345. struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  346. bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
  347. bool need_fence, need_mappable;
  348. int ret;
  349. need_fence =
  350. has_fenced_gpu_access &&
  351. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  352. obj->tiling_mode != I915_TILING_NONE;
  353. need_mappable = need_fence || need_reloc_mappable(obj);
  354. /* FIXME: vm plubming */
  355. ret = i915_gem_object_pin(obj, &dev_priv->gtt.base, entry->alignment,
  356. need_mappable, false);
  357. if (ret)
  358. return ret;
  359. entry->flags |= __EXEC_OBJECT_HAS_PIN;
  360. if (has_fenced_gpu_access) {
  361. if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
  362. ret = i915_gem_object_get_fence(obj);
  363. if (ret)
  364. return ret;
  365. if (i915_gem_object_pin_fence(obj))
  366. entry->flags |= __EXEC_OBJECT_HAS_FENCE;
  367. obj->pending_fenced_gpu_access = true;
  368. }
  369. }
  370. /* Ensure ppgtt mapping exists if needed */
  371. if (dev_priv->mm.aliasing_ppgtt && !obj->has_aliasing_ppgtt_mapping) {
  372. i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
  373. obj, obj->cache_level);
  374. obj->has_aliasing_ppgtt_mapping = 1;
  375. }
  376. if (entry->offset != i915_gem_obj_ggtt_offset(obj)) {
  377. entry->offset = i915_gem_obj_ggtt_offset(obj);
  378. *need_reloc = true;
  379. }
  380. if (entry->flags & EXEC_OBJECT_WRITE) {
  381. obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
  382. obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
  383. }
  384. if (entry->flags & EXEC_OBJECT_NEEDS_GTT &&
  385. !obj->has_global_gtt_mapping)
  386. i915_gem_gtt_bind_object(obj, obj->cache_level);
  387. return 0;
  388. }
  389. static void
  390. i915_gem_execbuffer_unreserve_object(struct drm_i915_gem_object *obj)
  391. {
  392. struct drm_i915_gem_exec_object2 *entry;
  393. if (!i915_gem_obj_ggtt_bound(obj))
  394. return;
  395. entry = obj->exec_entry;
  396. if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
  397. i915_gem_object_unpin_fence(obj);
  398. if (entry->flags & __EXEC_OBJECT_HAS_PIN)
  399. i915_gem_object_unpin(obj);
  400. entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
  401. }
  402. static int
  403. i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
  404. struct list_head *objects,
  405. bool *need_relocs)
  406. {
  407. struct drm_i915_gem_object *obj;
  408. struct list_head ordered_objects;
  409. bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
  410. int retry;
  411. INIT_LIST_HEAD(&ordered_objects);
  412. while (!list_empty(objects)) {
  413. struct drm_i915_gem_exec_object2 *entry;
  414. bool need_fence, need_mappable;
  415. obj = list_first_entry(objects,
  416. struct drm_i915_gem_object,
  417. exec_list);
  418. entry = obj->exec_entry;
  419. need_fence =
  420. has_fenced_gpu_access &&
  421. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  422. obj->tiling_mode != I915_TILING_NONE;
  423. need_mappable = need_fence || need_reloc_mappable(obj);
  424. if (need_mappable)
  425. list_move(&obj->exec_list, &ordered_objects);
  426. else
  427. list_move_tail(&obj->exec_list, &ordered_objects);
  428. obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
  429. obj->base.pending_write_domain = 0;
  430. obj->pending_fenced_gpu_access = false;
  431. }
  432. list_splice(&ordered_objects, objects);
  433. /* Attempt to pin all of the buffers into the GTT.
  434. * This is done in 3 phases:
  435. *
  436. * 1a. Unbind all objects that do not match the GTT constraints for
  437. * the execbuffer (fenceable, mappable, alignment etc).
  438. * 1b. Increment pin count for already bound objects.
  439. * 2. Bind new objects.
  440. * 3. Decrement pin count.
  441. *
  442. * This avoid unnecessary unbinding of later objects in order to make
  443. * room for the earlier objects *unless* we need to defragment.
  444. */
  445. retry = 0;
  446. do {
  447. int ret = 0;
  448. /* Unbind any ill-fitting objects or pin. */
  449. list_for_each_entry(obj, objects, exec_list) {
  450. struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  451. bool need_fence, need_mappable;
  452. if (!i915_gem_obj_ggtt_bound(obj))
  453. continue;
  454. need_fence =
  455. has_fenced_gpu_access &&
  456. entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  457. obj->tiling_mode != I915_TILING_NONE;
  458. need_mappable = need_fence || need_reloc_mappable(obj);
  459. if ((entry->alignment &&
  460. i915_gem_obj_ggtt_offset(obj) & (entry->alignment - 1)) ||
  461. (need_mappable && !obj->map_and_fenceable))
  462. ret = i915_gem_object_unbind(obj);
  463. else
  464. ret = i915_gem_execbuffer_reserve_object(obj, ring, need_relocs);
  465. if (ret)
  466. goto err;
  467. }
  468. /* Bind fresh objects */
  469. list_for_each_entry(obj, objects, exec_list) {
  470. if (i915_gem_obj_ggtt_bound(obj))
  471. continue;
  472. ret = i915_gem_execbuffer_reserve_object(obj, ring, need_relocs);
  473. if (ret)
  474. goto err;
  475. }
  476. err: /* Decrement pin count for bound objects */
  477. list_for_each_entry(obj, objects, exec_list)
  478. i915_gem_execbuffer_unreserve_object(obj);
  479. if (ret != -ENOSPC || retry++)
  480. return ret;
  481. ret = i915_gem_evict_everything(ring->dev);
  482. if (ret)
  483. return ret;
  484. } while (1);
  485. }
  486. static int
  487. i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
  488. struct drm_i915_gem_execbuffer2 *args,
  489. struct drm_file *file,
  490. struct intel_ring_buffer *ring,
  491. struct eb_objects *eb,
  492. struct drm_i915_gem_exec_object2 *exec)
  493. {
  494. struct drm_i915_gem_relocation_entry *reloc;
  495. struct drm_i915_gem_object *obj;
  496. bool need_relocs;
  497. int *reloc_offset;
  498. int i, total, ret;
  499. int count = args->buffer_count;
  500. /* We may process another execbuffer during the unlock... */
  501. while (!list_empty(&eb->objects)) {
  502. obj = list_first_entry(&eb->objects,
  503. struct drm_i915_gem_object,
  504. exec_list);
  505. list_del_init(&obj->exec_list);
  506. drm_gem_object_unreference(&obj->base);
  507. }
  508. mutex_unlock(&dev->struct_mutex);
  509. total = 0;
  510. for (i = 0; i < count; i++)
  511. total += exec[i].relocation_count;
  512. reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset));
  513. reloc = drm_malloc_ab(total, sizeof(*reloc));
  514. if (reloc == NULL || reloc_offset == NULL) {
  515. drm_free_large(reloc);
  516. drm_free_large(reloc_offset);
  517. mutex_lock(&dev->struct_mutex);
  518. return -ENOMEM;
  519. }
  520. total = 0;
  521. for (i = 0; i < count; i++) {
  522. struct drm_i915_gem_relocation_entry __user *user_relocs;
  523. u64 invalid_offset = (u64)-1;
  524. int j;
  525. user_relocs = to_user_ptr(exec[i].relocs_ptr);
  526. if (copy_from_user(reloc+total, user_relocs,
  527. exec[i].relocation_count * sizeof(*reloc))) {
  528. ret = -EFAULT;
  529. mutex_lock(&dev->struct_mutex);
  530. goto err;
  531. }
  532. /* As we do not update the known relocation offsets after
  533. * relocating (due to the complexities in lock handling),
  534. * we need to mark them as invalid now so that we force the
  535. * relocation processing next time. Just in case the target
  536. * object is evicted and then rebound into its old
  537. * presumed_offset before the next execbuffer - if that
  538. * happened we would make the mistake of assuming that the
  539. * relocations were valid.
  540. */
  541. for (j = 0; j < exec[i].relocation_count; j++) {
  542. if (copy_to_user(&user_relocs[j].presumed_offset,
  543. &invalid_offset,
  544. sizeof(invalid_offset))) {
  545. ret = -EFAULT;
  546. mutex_lock(&dev->struct_mutex);
  547. goto err;
  548. }
  549. }
  550. reloc_offset[i] = total;
  551. total += exec[i].relocation_count;
  552. }
  553. ret = i915_mutex_lock_interruptible(dev);
  554. if (ret) {
  555. mutex_lock(&dev->struct_mutex);
  556. goto err;
  557. }
  558. /* reacquire the objects */
  559. eb_reset(eb);
  560. ret = eb_lookup_objects(eb, exec, args, file);
  561. if (ret)
  562. goto err;
  563. need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  564. ret = i915_gem_execbuffer_reserve(ring, &eb->objects, &need_relocs);
  565. if (ret)
  566. goto err;
  567. list_for_each_entry(obj, &eb->objects, exec_list) {
  568. int offset = obj->exec_entry - exec;
  569. ret = i915_gem_execbuffer_relocate_object_slow(obj, eb,
  570. reloc + reloc_offset[offset]);
  571. if (ret)
  572. goto err;
  573. }
  574. /* Leave the user relocations as are, this is the painfully slow path,
  575. * and we want to avoid the complication of dropping the lock whilst
  576. * having buffers reserved in the aperture and so causing spurious
  577. * ENOSPC for random operations.
  578. */
  579. err:
  580. drm_free_large(reloc);
  581. drm_free_large(reloc_offset);
  582. return ret;
  583. }
  584. static int
  585. i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
  586. struct list_head *objects)
  587. {
  588. struct drm_i915_gem_object *obj;
  589. uint32_t flush_domains = 0;
  590. int ret;
  591. list_for_each_entry(obj, objects, exec_list) {
  592. ret = i915_gem_object_sync(obj, ring);
  593. if (ret)
  594. return ret;
  595. if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
  596. i915_gem_clflush_object(obj);
  597. flush_domains |= obj->base.write_domain;
  598. }
  599. if (flush_domains & I915_GEM_DOMAIN_CPU)
  600. i915_gem_chipset_flush(ring->dev);
  601. if (flush_domains & I915_GEM_DOMAIN_GTT)
  602. wmb();
  603. /* Unconditionally invalidate gpu caches and ensure that we do flush
  604. * any residual writes from the previous batch.
  605. */
  606. return intel_ring_invalidate_all_caches(ring);
  607. }
  608. static bool
  609. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  610. {
  611. if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
  612. return false;
  613. return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
  614. }
  615. static int
  616. validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
  617. int count)
  618. {
  619. int i;
  620. int relocs_total = 0;
  621. int relocs_max = INT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
  622. for (i = 0; i < count; i++) {
  623. char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
  624. int length; /* limited by fault_in_pages_readable() */
  625. if (exec[i].flags & __EXEC_OBJECT_UNKNOWN_FLAGS)
  626. return -EINVAL;
  627. /* First check for malicious input causing overflow in
  628. * the worst case where we need to allocate the entire
  629. * relocation tree as a single array.
  630. */
  631. if (exec[i].relocation_count > relocs_max - relocs_total)
  632. return -EINVAL;
  633. relocs_total += exec[i].relocation_count;
  634. length = exec[i].relocation_count *
  635. sizeof(struct drm_i915_gem_relocation_entry);
  636. /*
  637. * We must check that the entire relocation array is safe
  638. * to read, but since we may need to update the presumed
  639. * offsets during execution, check for full write access.
  640. */
  641. if (!access_ok(VERIFY_WRITE, ptr, length))
  642. return -EFAULT;
  643. if (likely(!i915_prefault_disable)) {
  644. if (fault_in_multipages_readable(ptr, length))
  645. return -EFAULT;
  646. }
  647. }
  648. return 0;
  649. }
  650. static void
  651. i915_gem_execbuffer_move_to_active(struct list_head *objects,
  652. struct intel_ring_buffer *ring)
  653. {
  654. struct drm_i915_gem_object *obj;
  655. list_for_each_entry(obj, objects, exec_list) {
  656. u32 old_read = obj->base.read_domains;
  657. u32 old_write = obj->base.write_domain;
  658. obj->base.write_domain = obj->base.pending_write_domain;
  659. if (obj->base.write_domain == 0)
  660. obj->base.pending_read_domains |= obj->base.read_domains;
  661. obj->base.read_domains = obj->base.pending_read_domains;
  662. obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
  663. i915_gem_object_move_to_active(obj, ring);
  664. if (obj->base.write_domain) {
  665. obj->dirty = 1;
  666. obj->last_write_seqno = intel_ring_get_seqno(ring);
  667. if (obj->pin_count) /* check for potential scanout */
  668. intel_mark_fb_busy(obj, ring);
  669. }
  670. trace_i915_gem_object_change_domain(obj, old_read, old_write);
  671. }
  672. }
  673. static void
  674. i915_gem_execbuffer_retire_commands(struct drm_device *dev,
  675. struct drm_file *file,
  676. struct intel_ring_buffer *ring,
  677. struct drm_i915_gem_object *obj)
  678. {
  679. /* Unconditionally force add_request to emit a full flush. */
  680. ring->gpu_caches_dirty = true;
  681. /* Add a breadcrumb for the completion of the batch buffer */
  682. (void)__i915_add_request(ring, file, obj, NULL);
  683. }
  684. static int
  685. i915_reset_gen7_sol_offsets(struct drm_device *dev,
  686. struct intel_ring_buffer *ring)
  687. {
  688. drm_i915_private_t *dev_priv = dev->dev_private;
  689. int ret, i;
  690. if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
  691. return 0;
  692. ret = intel_ring_begin(ring, 4 * 3);
  693. if (ret)
  694. return ret;
  695. for (i = 0; i < 4; i++) {
  696. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  697. intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
  698. intel_ring_emit(ring, 0);
  699. }
  700. intel_ring_advance(ring);
  701. return 0;
  702. }
  703. static int
  704. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  705. struct drm_file *file,
  706. struct drm_i915_gem_execbuffer2 *args,
  707. struct drm_i915_gem_exec_object2 *exec)
  708. {
  709. drm_i915_private_t *dev_priv = dev->dev_private;
  710. struct eb_objects *eb;
  711. struct drm_i915_gem_object *batch_obj;
  712. struct drm_clip_rect *cliprects = NULL;
  713. struct intel_ring_buffer *ring;
  714. u32 ctx_id = i915_execbuffer2_get_context_id(*args);
  715. u32 exec_start, exec_len;
  716. u32 mask, flags;
  717. int ret, mode, i;
  718. bool need_relocs;
  719. if (!i915_gem_check_execbuffer(args))
  720. return -EINVAL;
  721. ret = validate_exec_list(exec, args->buffer_count);
  722. if (ret)
  723. return ret;
  724. flags = 0;
  725. if (args->flags & I915_EXEC_SECURE) {
  726. if (!file->is_master || !capable(CAP_SYS_ADMIN))
  727. return -EPERM;
  728. flags |= I915_DISPATCH_SECURE;
  729. }
  730. if (args->flags & I915_EXEC_IS_PINNED)
  731. flags |= I915_DISPATCH_PINNED;
  732. switch (args->flags & I915_EXEC_RING_MASK) {
  733. case I915_EXEC_DEFAULT:
  734. case I915_EXEC_RENDER:
  735. ring = &dev_priv->ring[RCS];
  736. break;
  737. case I915_EXEC_BSD:
  738. ring = &dev_priv->ring[VCS];
  739. if (ctx_id != DEFAULT_CONTEXT_ID) {
  740. DRM_DEBUG("Ring %s doesn't support contexts\n",
  741. ring->name);
  742. return -EPERM;
  743. }
  744. break;
  745. case I915_EXEC_BLT:
  746. ring = &dev_priv->ring[BCS];
  747. if (ctx_id != DEFAULT_CONTEXT_ID) {
  748. DRM_DEBUG("Ring %s doesn't support contexts\n",
  749. ring->name);
  750. return -EPERM;
  751. }
  752. break;
  753. case I915_EXEC_VEBOX:
  754. ring = &dev_priv->ring[VECS];
  755. if (ctx_id != DEFAULT_CONTEXT_ID) {
  756. DRM_DEBUG("Ring %s doesn't support contexts\n",
  757. ring->name);
  758. return -EPERM;
  759. }
  760. break;
  761. default:
  762. DRM_DEBUG("execbuf with unknown ring: %d\n",
  763. (int)(args->flags & I915_EXEC_RING_MASK));
  764. return -EINVAL;
  765. }
  766. if (!intel_ring_initialized(ring)) {
  767. DRM_DEBUG("execbuf with invalid ring: %d\n",
  768. (int)(args->flags & I915_EXEC_RING_MASK));
  769. return -EINVAL;
  770. }
  771. mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  772. mask = I915_EXEC_CONSTANTS_MASK;
  773. switch (mode) {
  774. case I915_EXEC_CONSTANTS_REL_GENERAL:
  775. case I915_EXEC_CONSTANTS_ABSOLUTE:
  776. case I915_EXEC_CONSTANTS_REL_SURFACE:
  777. if (ring == &dev_priv->ring[RCS] &&
  778. mode != dev_priv->relative_constants_mode) {
  779. if (INTEL_INFO(dev)->gen < 4)
  780. return -EINVAL;
  781. if (INTEL_INFO(dev)->gen > 5 &&
  782. mode == I915_EXEC_CONSTANTS_REL_SURFACE)
  783. return -EINVAL;
  784. /* The HW changed the meaning on this bit on gen6 */
  785. if (INTEL_INFO(dev)->gen >= 6)
  786. mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
  787. }
  788. break;
  789. default:
  790. DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
  791. return -EINVAL;
  792. }
  793. if (args->buffer_count < 1) {
  794. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  795. return -EINVAL;
  796. }
  797. if (args->num_cliprects != 0) {
  798. if (ring != &dev_priv->ring[RCS]) {
  799. DRM_DEBUG("clip rectangles are only valid with the render ring\n");
  800. return -EINVAL;
  801. }
  802. if (INTEL_INFO(dev)->gen >= 5) {
  803. DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
  804. return -EINVAL;
  805. }
  806. if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
  807. DRM_DEBUG("execbuf with %u cliprects\n",
  808. args->num_cliprects);
  809. return -EINVAL;
  810. }
  811. cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
  812. GFP_KERNEL);
  813. if (cliprects == NULL) {
  814. ret = -ENOMEM;
  815. goto pre_mutex_err;
  816. }
  817. if (copy_from_user(cliprects,
  818. to_user_ptr(args->cliprects_ptr),
  819. sizeof(*cliprects)*args->num_cliprects)) {
  820. ret = -EFAULT;
  821. goto pre_mutex_err;
  822. }
  823. }
  824. ret = i915_mutex_lock_interruptible(dev);
  825. if (ret)
  826. goto pre_mutex_err;
  827. if (dev_priv->ums.mm_suspended) {
  828. mutex_unlock(&dev->struct_mutex);
  829. ret = -EBUSY;
  830. goto pre_mutex_err;
  831. }
  832. eb = eb_create(args);
  833. if (eb == NULL) {
  834. mutex_unlock(&dev->struct_mutex);
  835. ret = -ENOMEM;
  836. goto pre_mutex_err;
  837. }
  838. /* Look up object handles */
  839. ret = eb_lookup_objects(eb, exec, args, file);
  840. if (ret)
  841. goto err;
  842. /* take note of the batch buffer before we might reorder the lists */
  843. batch_obj = list_entry(eb->objects.prev,
  844. struct drm_i915_gem_object,
  845. exec_list);
  846. /* Move the objects en-masse into the GTT, evicting if necessary. */
  847. need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  848. ret = i915_gem_execbuffer_reserve(ring, &eb->objects, &need_relocs);
  849. if (ret)
  850. goto err;
  851. /* The objects are in their final locations, apply the relocations. */
  852. if (need_relocs)
  853. ret = i915_gem_execbuffer_relocate(eb);
  854. if (ret) {
  855. if (ret == -EFAULT) {
  856. ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring,
  857. eb, exec);
  858. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  859. }
  860. if (ret)
  861. goto err;
  862. }
  863. /* Set the pending read domains for the batch buffer to COMMAND */
  864. if (batch_obj->base.pending_write_domain) {
  865. DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
  866. ret = -EINVAL;
  867. goto err;
  868. }
  869. batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  870. /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
  871. * batch" bit. Hence we need to pin secure batches into the global gtt.
  872. * hsw should have this fixed, but let's be paranoid and do it
  873. * unconditionally for now. */
  874. if (flags & I915_DISPATCH_SECURE && !batch_obj->has_global_gtt_mapping)
  875. i915_gem_gtt_bind_object(batch_obj, batch_obj->cache_level);
  876. ret = i915_gem_execbuffer_move_to_gpu(ring, &eb->objects);
  877. if (ret)
  878. goto err;
  879. ret = i915_switch_context(ring, file, ctx_id);
  880. if (ret)
  881. goto err;
  882. if (ring == &dev_priv->ring[RCS] &&
  883. mode != dev_priv->relative_constants_mode) {
  884. ret = intel_ring_begin(ring, 4);
  885. if (ret)
  886. goto err;
  887. intel_ring_emit(ring, MI_NOOP);
  888. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  889. intel_ring_emit(ring, INSTPM);
  890. intel_ring_emit(ring, mask << 16 | mode);
  891. intel_ring_advance(ring);
  892. dev_priv->relative_constants_mode = mode;
  893. }
  894. if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
  895. ret = i915_reset_gen7_sol_offsets(dev, ring);
  896. if (ret)
  897. goto err;
  898. }
  899. exec_start = i915_gem_obj_ggtt_offset(batch_obj) + args->batch_start_offset;
  900. exec_len = args->batch_len;
  901. if (cliprects) {
  902. for (i = 0; i < args->num_cliprects; i++) {
  903. ret = i915_emit_box(dev, &cliprects[i],
  904. args->DR1, args->DR4);
  905. if (ret)
  906. goto err;
  907. ret = ring->dispatch_execbuffer(ring,
  908. exec_start, exec_len,
  909. flags);
  910. if (ret)
  911. goto err;
  912. }
  913. } else {
  914. ret = ring->dispatch_execbuffer(ring,
  915. exec_start, exec_len,
  916. flags);
  917. if (ret)
  918. goto err;
  919. }
  920. trace_i915_gem_ring_dispatch(ring, intel_ring_get_seqno(ring), flags);
  921. i915_gem_execbuffer_move_to_active(&eb->objects, ring);
  922. i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
  923. err:
  924. eb_destroy(eb);
  925. mutex_unlock(&dev->struct_mutex);
  926. pre_mutex_err:
  927. kfree(cliprects);
  928. return ret;
  929. }
  930. /*
  931. * Legacy execbuffer just creates an exec2 list from the original exec object
  932. * list array and passes it to the real function.
  933. */
  934. int
  935. i915_gem_execbuffer(struct drm_device *dev, void *data,
  936. struct drm_file *file)
  937. {
  938. struct drm_i915_gem_execbuffer *args = data;
  939. struct drm_i915_gem_execbuffer2 exec2;
  940. struct drm_i915_gem_exec_object *exec_list = NULL;
  941. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  942. int ret, i;
  943. if (args->buffer_count < 1) {
  944. DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  945. return -EINVAL;
  946. }
  947. /* Copy in the exec list from userland */
  948. exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  949. exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  950. if (exec_list == NULL || exec2_list == NULL) {
  951. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  952. args->buffer_count);
  953. drm_free_large(exec_list);
  954. drm_free_large(exec2_list);
  955. return -ENOMEM;
  956. }
  957. ret = copy_from_user(exec_list,
  958. to_user_ptr(args->buffers_ptr),
  959. sizeof(*exec_list) * args->buffer_count);
  960. if (ret != 0) {
  961. DRM_DEBUG("copy %d exec entries failed %d\n",
  962. args->buffer_count, ret);
  963. drm_free_large(exec_list);
  964. drm_free_large(exec2_list);
  965. return -EFAULT;
  966. }
  967. for (i = 0; i < args->buffer_count; i++) {
  968. exec2_list[i].handle = exec_list[i].handle;
  969. exec2_list[i].relocation_count = exec_list[i].relocation_count;
  970. exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  971. exec2_list[i].alignment = exec_list[i].alignment;
  972. exec2_list[i].offset = exec_list[i].offset;
  973. if (INTEL_INFO(dev)->gen < 4)
  974. exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  975. else
  976. exec2_list[i].flags = 0;
  977. }
  978. exec2.buffers_ptr = args->buffers_ptr;
  979. exec2.buffer_count = args->buffer_count;
  980. exec2.batch_start_offset = args->batch_start_offset;
  981. exec2.batch_len = args->batch_len;
  982. exec2.DR1 = args->DR1;
  983. exec2.DR4 = args->DR4;
  984. exec2.num_cliprects = args->num_cliprects;
  985. exec2.cliprects_ptr = args->cliprects_ptr;
  986. exec2.flags = I915_EXEC_RENDER;
  987. i915_execbuffer2_set_context_id(exec2, 0);
  988. ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
  989. if (!ret) {
  990. /* Copy the new buffer offsets back to the user's exec list. */
  991. for (i = 0; i < args->buffer_count; i++)
  992. exec_list[i].offset = exec2_list[i].offset;
  993. /* ... and back out to userspace */
  994. ret = copy_to_user(to_user_ptr(args->buffers_ptr),
  995. exec_list,
  996. sizeof(*exec_list) * args->buffer_count);
  997. if (ret) {
  998. ret = -EFAULT;
  999. DRM_DEBUG("failed to copy %d exec entries "
  1000. "back to user (%d)\n",
  1001. args->buffer_count, ret);
  1002. }
  1003. }
  1004. drm_free_large(exec_list);
  1005. drm_free_large(exec2_list);
  1006. return ret;
  1007. }
  1008. int
  1009. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  1010. struct drm_file *file)
  1011. {
  1012. struct drm_i915_gem_execbuffer2 *args = data;
  1013. struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1014. int ret;
  1015. if (args->buffer_count < 1 ||
  1016. args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
  1017. DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
  1018. return -EINVAL;
  1019. }
  1020. exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
  1021. GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
  1022. if (exec2_list == NULL)
  1023. exec2_list = drm_malloc_ab(sizeof(*exec2_list),
  1024. args->buffer_count);
  1025. if (exec2_list == NULL) {
  1026. DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1027. args->buffer_count);
  1028. return -ENOMEM;
  1029. }
  1030. ret = copy_from_user(exec2_list,
  1031. to_user_ptr(args->buffers_ptr),
  1032. sizeof(*exec2_list) * args->buffer_count);
  1033. if (ret != 0) {
  1034. DRM_DEBUG("copy %d exec entries failed %d\n",
  1035. args->buffer_count, ret);
  1036. drm_free_large(exec2_list);
  1037. return -EFAULT;
  1038. }
  1039. ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
  1040. if (!ret) {
  1041. /* Copy the new buffer offsets back to the user's exec list. */
  1042. ret = copy_to_user(to_user_ptr(args->buffers_ptr),
  1043. exec2_list,
  1044. sizeof(*exec2_list) * args->buffer_count);
  1045. if (ret) {
  1046. ret = -EFAULT;
  1047. DRM_DEBUG("failed to copy %d exec entries "
  1048. "back to user (%d)\n",
  1049. args->buffer_count, ret);
  1050. }
  1051. }
  1052. drm_free_large(exec2_list);
  1053. return ret;
  1054. }