drm_gem.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  1. /*
  2. * Copyright © 2008 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 <[email protected]>
  25. *
  26. */
  27. #include <linux/dma-buf.h>
  28. #include <linux/file.h>
  29. #include <linux/fs.h>
  30. #include <linux/iosys-map.h>
  31. #include <linux/mem_encrypt.h>
  32. #include <linux/mm.h>
  33. #include <linux/mman.h>
  34. #include <linux/module.h>
  35. #include <linux/pagemap.h>
  36. #include <linux/pagevec.h>
  37. #include <linux/shmem_fs.h>
  38. #include <linux/slab.h>
  39. #include <linux/string_helpers.h>
  40. #include <linux/types.h>
  41. #include <linux/uaccess.h>
  42. #include <drm/drm.h>
  43. #include <drm/drm_device.h>
  44. #include <drm/drm_drv.h>
  45. #include <drm/drm_file.h>
  46. #include <drm/drm_gem.h>
  47. #include <drm/drm_managed.h>
  48. #include <drm/drm_print.h>
  49. #include <drm/drm_vma_manager.h>
  50. #include "drm_internal.h"
  51. /** @file drm_gem.c
  52. *
  53. * This file provides some of the base ioctls and library routines for
  54. * the graphics memory manager implemented by each device driver.
  55. *
  56. * Because various devices have different requirements in terms of
  57. * synchronization and migration strategies, implementing that is left up to
  58. * the driver, and all that the general API provides should be generic --
  59. * allocating objects, reading/writing data with the cpu, freeing objects.
  60. * Even there, platform-dependent optimizations for reading/writing data with
  61. * the CPU mean we'll likely hook those out to driver-specific calls. However,
  62. * the DRI2 implementation wants to have at least allocate/mmap be generic.
  63. *
  64. * The goal was to have swap-backed object allocation managed through
  65. * struct file. However, file descriptors as handles to a struct file have
  66. * two major failings:
  67. * - Process limits prevent more than 1024 or so being used at a time by
  68. * default.
  69. * - Inability to allocate high fds will aggravate the X Server's select()
  70. * handling, and likely that of many GL client applications as well.
  71. *
  72. * This led to a plan of using our own integer IDs (called handles, following
  73. * DRM terminology) to mimic fds, and implement the fd syscalls we need as
  74. * ioctls. The objects themselves will still include the struct file so
  75. * that we can transition to fds if the required kernel infrastructure shows
  76. * up at a later date, and as our interface with shmfs for memory allocation.
  77. */
  78. static void
  79. drm_gem_init_release(struct drm_device *dev, void *ptr)
  80. {
  81. drm_vma_offset_manager_destroy(dev->vma_offset_manager);
  82. }
  83. /**
  84. * drm_gem_init - Initialize the GEM device fields
  85. * @dev: drm_devic structure to initialize
  86. */
  87. int
  88. drm_gem_init(struct drm_device *dev)
  89. {
  90. struct drm_vma_offset_manager *vma_offset_manager;
  91. mutex_init(&dev->object_name_lock);
  92. idr_init_base(&dev->object_name_idr, 1);
  93. vma_offset_manager = drmm_kzalloc(dev, sizeof(*vma_offset_manager),
  94. GFP_KERNEL);
  95. if (!vma_offset_manager) {
  96. DRM_ERROR("out of memory\n");
  97. return -ENOMEM;
  98. }
  99. dev->vma_offset_manager = vma_offset_manager;
  100. drm_vma_offset_manager_init(vma_offset_manager,
  101. DRM_FILE_PAGE_OFFSET_START,
  102. DRM_FILE_PAGE_OFFSET_SIZE);
  103. return drmm_add_action(dev, drm_gem_init_release, NULL);
  104. }
  105. /**
  106. * drm_gem_object_init - initialize an allocated shmem-backed GEM object
  107. * @dev: drm_device the object should be initialized for
  108. * @obj: drm_gem_object to initialize
  109. * @size: object size
  110. *
  111. * Initialize an already allocated GEM object of the specified size with
  112. * shmfs backing store.
  113. */
  114. int drm_gem_object_init(struct drm_device *dev,
  115. struct drm_gem_object *obj, size_t size)
  116. {
  117. struct file *filp;
  118. drm_gem_private_object_init(dev, obj, size);
  119. filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
  120. if (IS_ERR(filp))
  121. return PTR_ERR(filp);
  122. obj->filp = filp;
  123. return 0;
  124. }
  125. EXPORT_SYMBOL(drm_gem_object_init);
  126. /**
  127. * drm_gem_private_object_init - initialize an allocated private GEM object
  128. * @dev: drm_device the object should be initialized for
  129. * @obj: drm_gem_object to initialize
  130. * @size: object size
  131. *
  132. * Initialize an already allocated GEM object of the specified size with
  133. * no GEM provided backing store. Instead the caller is responsible for
  134. * backing the object and handling it.
  135. */
  136. void drm_gem_private_object_init(struct drm_device *dev,
  137. struct drm_gem_object *obj, size_t size)
  138. {
  139. BUG_ON((size & (PAGE_SIZE - 1)) != 0);
  140. obj->dev = dev;
  141. obj->filp = NULL;
  142. kref_init(&obj->refcount);
  143. obj->handle_count = 0;
  144. obj->size = size;
  145. dma_resv_init(&obj->_resv);
  146. if (!obj->resv)
  147. obj->resv = &obj->_resv;
  148. drm_vma_node_reset(&obj->vma_node);
  149. INIT_LIST_HEAD(&obj->lru_node);
  150. }
  151. EXPORT_SYMBOL(drm_gem_private_object_init);
  152. /**
  153. * drm_gem_object_handle_free - release resources bound to userspace handles
  154. * @obj: GEM object to clean up.
  155. *
  156. * Called after the last handle to the object has been closed
  157. *
  158. * Removes any name for the object. Note that this must be
  159. * called before drm_gem_object_free or we'll be touching
  160. * freed memory
  161. */
  162. static void drm_gem_object_handle_free(struct drm_gem_object *obj)
  163. {
  164. struct drm_device *dev = obj->dev;
  165. /* Remove any name for this object */
  166. if (obj->name) {
  167. idr_remove(&dev->object_name_idr, obj->name);
  168. obj->name = 0;
  169. }
  170. }
  171. static void drm_gem_object_exported_dma_buf_free(struct drm_gem_object *obj)
  172. {
  173. /* Unbreak the reference cycle if we have an exported dma_buf. */
  174. if (obj->dma_buf) {
  175. dma_buf_put(obj->dma_buf);
  176. obj->dma_buf = NULL;
  177. }
  178. }
  179. static void
  180. drm_gem_object_handle_put_unlocked(struct drm_gem_object *obj)
  181. {
  182. struct drm_device *dev = obj->dev;
  183. bool final = false;
  184. if (WARN_ON(READ_ONCE(obj->handle_count) == 0))
  185. return;
  186. /*
  187. * Must bump handle count first as this may be the last
  188. * ref, in which case the object would disappear before we
  189. * checked for a name
  190. */
  191. mutex_lock(&dev->object_name_lock);
  192. if (--obj->handle_count == 0) {
  193. drm_gem_object_handle_free(obj);
  194. drm_gem_object_exported_dma_buf_free(obj);
  195. final = true;
  196. }
  197. mutex_unlock(&dev->object_name_lock);
  198. if (final)
  199. drm_gem_object_put(obj);
  200. }
  201. /*
  202. * Called at device or object close to release the file's
  203. * handle references on objects.
  204. */
  205. static int
  206. drm_gem_object_release_handle(int id, void *ptr, void *data)
  207. {
  208. struct drm_file *file_priv = data;
  209. struct drm_gem_object *obj = ptr;
  210. if (obj->funcs->close)
  211. obj->funcs->close(obj, file_priv);
  212. drm_prime_remove_buf_handle(&file_priv->prime, id);
  213. drm_vma_node_revoke(&obj->vma_node, file_priv);
  214. drm_gem_object_handle_put_unlocked(obj);
  215. return 0;
  216. }
  217. /**
  218. * drm_gem_handle_delete - deletes the given file-private handle
  219. * @filp: drm file-private structure to use for the handle look up
  220. * @handle: userspace handle to delete
  221. *
  222. * Removes the GEM handle from the @filp lookup table which has been added with
  223. * drm_gem_handle_create(). If this is the last handle also cleans up linked
  224. * resources like GEM names.
  225. */
  226. int
  227. drm_gem_handle_delete(struct drm_file *filp, u32 handle)
  228. {
  229. struct drm_gem_object *obj;
  230. spin_lock(&filp->table_lock);
  231. /* Check if we currently have a reference on the object */
  232. obj = idr_replace(&filp->object_idr, NULL, handle);
  233. spin_unlock(&filp->table_lock);
  234. if (IS_ERR_OR_NULL(obj))
  235. return -EINVAL;
  236. /* Release driver's reference and decrement refcount. */
  237. drm_gem_object_release_handle(handle, obj, filp);
  238. /* And finally make the handle available for future allocations. */
  239. spin_lock(&filp->table_lock);
  240. idr_remove(&filp->object_idr, handle);
  241. spin_unlock(&filp->table_lock);
  242. return 0;
  243. }
  244. EXPORT_SYMBOL(drm_gem_handle_delete);
  245. /**
  246. * drm_gem_dumb_map_offset - return the fake mmap offset for a gem object
  247. * @file: drm file-private structure containing the gem object
  248. * @dev: corresponding drm_device
  249. * @handle: gem object handle
  250. * @offset: return location for the fake mmap offset
  251. *
  252. * This implements the &drm_driver.dumb_map_offset kms driver callback for
  253. * drivers which use gem to manage their backing storage.
  254. *
  255. * Returns:
  256. * 0 on success or a negative error code on failure.
  257. */
  258. int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
  259. u32 handle, u64 *offset)
  260. {
  261. struct drm_gem_object *obj;
  262. int ret;
  263. obj = drm_gem_object_lookup(file, handle);
  264. if (!obj)
  265. return -ENOENT;
  266. /* Don't allow imported objects to be mapped */
  267. if (obj->import_attach) {
  268. ret = -EINVAL;
  269. goto out;
  270. }
  271. ret = drm_gem_create_mmap_offset(obj);
  272. if (ret)
  273. goto out;
  274. *offset = drm_vma_node_offset_addr(&obj->vma_node);
  275. out:
  276. drm_gem_object_put(obj);
  277. return ret;
  278. }
  279. EXPORT_SYMBOL_GPL(drm_gem_dumb_map_offset);
  280. int drm_gem_dumb_destroy(struct drm_file *file,
  281. struct drm_device *dev,
  282. u32 handle)
  283. {
  284. return drm_gem_handle_delete(file, handle);
  285. }
  286. /**
  287. * drm_gem_handle_create_tail - internal functions to create a handle
  288. * @file_priv: drm file-private structure to register the handle for
  289. * @obj: object to register
  290. * @handlep: pointer to return the created handle to the caller
  291. *
  292. * This expects the &drm_device.object_name_lock to be held already and will
  293. * drop it before returning. Used to avoid races in establishing new handles
  294. * when importing an object from either an flink name or a dma-buf.
  295. *
  296. * Handles must be release again through drm_gem_handle_delete(). This is done
  297. * when userspace closes @file_priv for all attached handles, or through the
  298. * GEM_CLOSE ioctl for individual handles.
  299. */
  300. int
  301. drm_gem_handle_create_tail(struct drm_file *file_priv,
  302. struct drm_gem_object *obj,
  303. u32 *handlep)
  304. {
  305. struct drm_device *dev = obj->dev;
  306. u32 handle;
  307. int ret;
  308. WARN_ON(!mutex_is_locked(&dev->object_name_lock));
  309. if (obj->handle_count++ == 0)
  310. drm_gem_object_get(obj);
  311. /*
  312. * Get the user-visible handle using idr. Preload and perform
  313. * allocation under our spinlock.
  314. */
  315. idr_preload(GFP_KERNEL);
  316. spin_lock(&file_priv->table_lock);
  317. ret = idr_alloc(&file_priv->object_idr, obj, 1, 0, GFP_NOWAIT);
  318. spin_unlock(&file_priv->table_lock);
  319. idr_preload_end();
  320. mutex_unlock(&dev->object_name_lock);
  321. if (ret < 0)
  322. goto err_unref;
  323. handle = ret;
  324. ret = drm_vma_node_allow(&obj->vma_node, file_priv);
  325. if (ret)
  326. goto err_remove;
  327. if (obj->funcs->open) {
  328. ret = obj->funcs->open(obj, file_priv);
  329. if (ret)
  330. goto err_revoke;
  331. }
  332. *handlep = handle;
  333. return 0;
  334. err_revoke:
  335. drm_vma_node_revoke(&obj->vma_node, file_priv);
  336. err_remove:
  337. spin_lock(&file_priv->table_lock);
  338. idr_remove(&file_priv->object_idr, handle);
  339. spin_unlock(&file_priv->table_lock);
  340. err_unref:
  341. drm_gem_object_handle_put_unlocked(obj);
  342. return ret;
  343. }
  344. /**
  345. * drm_gem_handle_create - create a gem handle for an object
  346. * @file_priv: drm file-private structure to register the handle for
  347. * @obj: object to register
  348. * @handlep: pointer to return the created handle to the caller
  349. *
  350. * Create a handle for this object. This adds a handle reference to the object,
  351. * which includes a regular reference count. Callers will likely want to
  352. * dereference the object afterwards.
  353. *
  354. * Since this publishes @obj to userspace it must be fully set up by this point,
  355. * drivers must call this last in their buffer object creation callbacks.
  356. */
  357. int drm_gem_handle_create(struct drm_file *file_priv,
  358. struct drm_gem_object *obj,
  359. u32 *handlep)
  360. {
  361. mutex_lock(&obj->dev->object_name_lock);
  362. return drm_gem_handle_create_tail(file_priv, obj, handlep);
  363. }
  364. EXPORT_SYMBOL(drm_gem_handle_create);
  365. /**
  366. * drm_gem_free_mmap_offset - release a fake mmap offset for an object
  367. * @obj: obj in question
  368. *
  369. * This routine frees fake offsets allocated by drm_gem_create_mmap_offset().
  370. *
  371. * Note that drm_gem_object_release() already calls this function, so drivers
  372. * don't have to take care of releasing the mmap offset themselves when freeing
  373. * the GEM object.
  374. */
  375. void
  376. drm_gem_free_mmap_offset(struct drm_gem_object *obj)
  377. {
  378. struct drm_device *dev = obj->dev;
  379. drm_vma_offset_remove(dev->vma_offset_manager, &obj->vma_node);
  380. }
  381. EXPORT_SYMBOL(drm_gem_free_mmap_offset);
  382. /**
  383. * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object
  384. * @obj: obj in question
  385. * @size: the virtual size
  386. *
  387. * GEM memory mapping works by handing back to userspace a fake mmap offset
  388. * it can use in a subsequent mmap(2) call. The DRM core code then looks
  389. * up the object based on the offset and sets up the various memory mapping
  390. * structures.
  391. *
  392. * This routine allocates and attaches a fake offset for @obj, in cases where
  393. * the virtual size differs from the physical size (ie. &drm_gem_object.size).
  394. * Otherwise just use drm_gem_create_mmap_offset().
  395. *
  396. * This function is idempotent and handles an already allocated mmap offset
  397. * transparently. Drivers do not need to check for this case.
  398. */
  399. int
  400. drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size)
  401. {
  402. struct drm_device *dev = obj->dev;
  403. return drm_vma_offset_add(dev->vma_offset_manager, &obj->vma_node,
  404. size / PAGE_SIZE);
  405. }
  406. EXPORT_SYMBOL(drm_gem_create_mmap_offset_size);
  407. /**
  408. * drm_gem_create_mmap_offset - create a fake mmap offset for an object
  409. * @obj: obj in question
  410. *
  411. * GEM memory mapping works by handing back to userspace a fake mmap offset
  412. * it can use in a subsequent mmap(2) call. The DRM core code then looks
  413. * up the object based on the offset and sets up the various memory mapping
  414. * structures.
  415. *
  416. * This routine allocates and attaches a fake offset for @obj.
  417. *
  418. * Drivers can call drm_gem_free_mmap_offset() before freeing @obj to release
  419. * the fake offset again.
  420. */
  421. int drm_gem_create_mmap_offset(struct drm_gem_object *obj)
  422. {
  423. return drm_gem_create_mmap_offset_size(obj, obj->size);
  424. }
  425. EXPORT_SYMBOL(drm_gem_create_mmap_offset);
  426. /*
  427. * Move pages to appropriate lru and release the pagevec, decrementing the
  428. * ref count of those pages.
  429. */
  430. static void drm_gem_check_release_pagevec(struct pagevec *pvec)
  431. {
  432. check_move_unevictable_pages(pvec);
  433. __pagevec_release(pvec);
  434. cond_resched();
  435. }
  436. /**
  437. * drm_gem_get_pages - helper to allocate backing pages for a GEM object
  438. * from shmem
  439. * @obj: obj in question
  440. *
  441. * This reads the page-array of the shmem-backing storage of the given gem
  442. * object. An array of pages is returned. If a page is not allocated or
  443. * swapped-out, this will allocate/swap-in the required pages. Note that the
  444. * whole object is covered by the page-array and pinned in memory.
  445. *
  446. * Use drm_gem_put_pages() to release the array and unpin all pages.
  447. *
  448. * This uses the GFP-mask set on the shmem-mapping (see mapping_set_gfp_mask()).
  449. * If you require other GFP-masks, you have to do those allocations yourself.
  450. *
  451. * Note that you are not allowed to change gfp-zones during runtime. That is,
  452. * shmem_read_mapping_page_gfp() must be called with the same gfp_zone(gfp) as
  453. * set during initialization. If you have special zone constraints, set them
  454. * after drm_gem_object_init() via mapping_set_gfp_mask(). shmem-core takes care
  455. * to keep pages in the required zone during swap-in.
  456. *
  457. * This function is only valid on objects initialized with
  458. * drm_gem_object_init(), but not for those initialized with
  459. * drm_gem_private_object_init() only.
  460. */
  461. struct page **drm_gem_get_pages(struct drm_gem_object *obj)
  462. {
  463. struct address_space *mapping;
  464. struct page *p, **pages;
  465. struct pagevec pvec;
  466. int i, npages;
  467. if (WARN_ON(!obj->filp))
  468. return ERR_PTR(-EINVAL);
  469. /* This is the shared memory object that backs the GEM resource */
  470. mapping = obj->filp->f_mapping;
  471. /* We already BUG_ON() for non-page-aligned sizes in
  472. * drm_gem_object_init(), so we should never hit this unless
  473. * driver author is doing something really wrong:
  474. */
  475. WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
  476. npages = obj->size >> PAGE_SHIFT;
  477. pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
  478. if (pages == NULL)
  479. return ERR_PTR(-ENOMEM);
  480. mapping_set_unevictable(mapping);
  481. for (i = 0; i < npages; i++) {
  482. p = shmem_read_mapping_page(mapping, i);
  483. if (IS_ERR(p))
  484. goto fail;
  485. pages[i] = p;
  486. /* Make sure shmem keeps __GFP_DMA32 allocated pages in the
  487. * correct region during swapin. Note that this requires
  488. * __GFP_DMA32 to be set in mapping_gfp_mask(inode->i_mapping)
  489. * so shmem can relocate pages during swapin if required.
  490. */
  491. BUG_ON(mapping_gfp_constraint(mapping, __GFP_DMA32) &&
  492. (page_to_pfn(p) >= 0x00100000UL));
  493. }
  494. return pages;
  495. fail:
  496. mapping_clear_unevictable(mapping);
  497. pagevec_init(&pvec);
  498. while (i--) {
  499. if (!pagevec_add(&pvec, pages[i]))
  500. drm_gem_check_release_pagevec(&pvec);
  501. }
  502. if (pagevec_count(&pvec))
  503. drm_gem_check_release_pagevec(&pvec);
  504. kvfree(pages);
  505. return ERR_CAST(p);
  506. }
  507. EXPORT_SYMBOL(drm_gem_get_pages);
  508. /**
  509. * drm_gem_put_pages - helper to free backing pages for a GEM object
  510. * @obj: obj in question
  511. * @pages: pages to free
  512. * @dirty: if true, pages will be marked as dirty
  513. * @accessed: if true, the pages will be marked as accessed
  514. */
  515. void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
  516. bool dirty, bool accessed)
  517. {
  518. int i, npages;
  519. struct address_space *mapping;
  520. struct pagevec pvec;
  521. mapping = file_inode(obj->filp)->i_mapping;
  522. mapping_clear_unevictable(mapping);
  523. /* We already BUG_ON() for non-page-aligned sizes in
  524. * drm_gem_object_init(), so we should never hit this unless
  525. * driver author is doing something really wrong:
  526. */
  527. WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0);
  528. npages = obj->size >> PAGE_SHIFT;
  529. pagevec_init(&pvec);
  530. for (i = 0; i < npages; i++) {
  531. if (!pages[i])
  532. continue;
  533. if (dirty)
  534. set_page_dirty(pages[i]);
  535. if (accessed)
  536. mark_page_accessed(pages[i]);
  537. /* Undo the reference we took when populating the table */
  538. if (!pagevec_add(&pvec, pages[i]))
  539. drm_gem_check_release_pagevec(&pvec);
  540. }
  541. if (pagevec_count(&pvec))
  542. drm_gem_check_release_pagevec(&pvec);
  543. kvfree(pages);
  544. }
  545. EXPORT_SYMBOL(drm_gem_put_pages);
  546. static int objects_lookup(struct drm_file *filp, u32 *handle, int count,
  547. struct drm_gem_object **objs)
  548. {
  549. int i, ret = 0;
  550. struct drm_gem_object *obj;
  551. spin_lock(&filp->table_lock);
  552. for (i = 0; i < count; i++) {
  553. /* Check if we currently have a reference on the object */
  554. obj = idr_find(&filp->object_idr, handle[i]);
  555. if (!obj) {
  556. ret = -ENOENT;
  557. break;
  558. }
  559. drm_gem_object_get(obj);
  560. objs[i] = obj;
  561. }
  562. spin_unlock(&filp->table_lock);
  563. return ret;
  564. }
  565. /**
  566. * drm_gem_objects_lookup - look up GEM objects from an array of handles
  567. * @filp: DRM file private date
  568. * @bo_handles: user pointer to array of userspace handle
  569. * @count: size of handle array
  570. * @objs_out: returned pointer to array of drm_gem_object pointers
  571. *
  572. * Takes an array of userspace handles and returns a newly allocated array of
  573. * GEM objects.
  574. *
  575. * For a single handle lookup, use drm_gem_object_lookup().
  576. *
  577. * Returns:
  578. *
  579. * @objs filled in with GEM object pointers. Returned GEM objects need to be
  580. * released with drm_gem_object_put(). -ENOENT is returned on a lookup
  581. * failure. 0 is returned on success.
  582. *
  583. */
  584. int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
  585. int count, struct drm_gem_object ***objs_out)
  586. {
  587. int ret;
  588. u32 *handles;
  589. struct drm_gem_object **objs;
  590. if (!count)
  591. return 0;
  592. objs = kvmalloc_array(count, sizeof(struct drm_gem_object *),
  593. GFP_KERNEL | __GFP_ZERO);
  594. if (!objs)
  595. return -ENOMEM;
  596. *objs_out = objs;
  597. handles = kvmalloc_array(count, sizeof(u32), GFP_KERNEL);
  598. if (!handles) {
  599. ret = -ENOMEM;
  600. goto out;
  601. }
  602. if (copy_from_user(handles, bo_handles, count * sizeof(u32))) {
  603. ret = -EFAULT;
  604. DRM_DEBUG("Failed to copy in GEM handles\n");
  605. goto out;
  606. }
  607. ret = objects_lookup(filp, handles, count, objs);
  608. out:
  609. kvfree(handles);
  610. return ret;
  611. }
  612. EXPORT_SYMBOL(drm_gem_objects_lookup);
  613. /**
  614. * drm_gem_object_lookup - look up a GEM object from its handle
  615. * @filp: DRM file private date
  616. * @handle: userspace handle
  617. *
  618. * Returns:
  619. *
  620. * A reference to the object named by the handle if such exists on @filp, NULL
  621. * otherwise.
  622. *
  623. * If looking up an array of handles, use drm_gem_objects_lookup().
  624. */
  625. struct drm_gem_object *
  626. drm_gem_object_lookup(struct drm_file *filp, u32 handle)
  627. {
  628. struct drm_gem_object *obj = NULL;
  629. objects_lookup(filp, &handle, 1, &obj);
  630. return obj;
  631. }
  632. EXPORT_SYMBOL(drm_gem_object_lookup);
  633. /**
  634. * drm_gem_dma_resv_wait - Wait on GEM object's reservation's objects
  635. * shared and/or exclusive fences.
  636. * @filep: DRM file private date
  637. * @handle: userspace handle
  638. * @wait_all: if true, wait on all fences, else wait on just exclusive fence
  639. * @timeout: timeout value in jiffies or zero to return immediately
  640. *
  641. * Returns:
  642. *
  643. * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
  644. * greater than 0 on success.
  645. */
  646. long drm_gem_dma_resv_wait(struct drm_file *filep, u32 handle,
  647. bool wait_all, unsigned long timeout)
  648. {
  649. long ret;
  650. struct drm_gem_object *obj;
  651. obj = drm_gem_object_lookup(filep, handle);
  652. if (!obj) {
  653. DRM_DEBUG("Failed to look up GEM BO %d\n", handle);
  654. return -EINVAL;
  655. }
  656. ret = dma_resv_wait_timeout(obj->resv, dma_resv_usage_rw(wait_all),
  657. true, timeout);
  658. if (ret == 0)
  659. ret = -ETIME;
  660. else if (ret > 0)
  661. ret = 0;
  662. drm_gem_object_put(obj);
  663. return ret;
  664. }
  665. EXPORT_SYMBOL(drm_gem_dma_resv_wait);
  666. /**
  667. * drm_gem_close_ioctl - implementation of the GEM_CLOSE ioctl
  668. * @dev: drm_device
  669. * @data: ioctl data
  670. * @file_priv: drm file-private structure
  671. *
  672. * Releases the handle to an mm object.
  673. */
  674. int
  675. drm_gem_close_ioctl(struct drm_device *dev, void *data,
  676. struct drm_file *file_priv)
  677. {
  678. struct drm_gem_close *args = data;
  679. int ret;
  680. if (!drm_core_check_feature(dev, DRIVER_GEM))
  681. return -EOPNOTSUPP;
  682. ret = drm_gem_handle_delete(file_priv, args->handle);
  683. return ret;
  684. }
  685. /**
  686. * drm_gem_flink_ioctl - implementation of the GEM_FLINK ioctl
  687. * @dev: drm_device
  688. * @data: ioctl data
  689. * @file_priv: drm file-private structure
  690. *
  691. * Create a global name for an object, returning the name.
  692. *
  693. * Note that the name does not hold a reference; when the object
  694. * is freed, the name goes away.
  695. */
  696. int
  697. drm_gem_flink_ioctl(struct drm_device *dev, void *data,
  698. struct drm_file *file_priv)
  699. {
  700. struct drm_gem_flink *args = data;
  701. struct drm_gem_object *obj;
  702. int ret;
  703. if (!drm_core_check_feature(dev, DRIVER_GEM))
  704. return -EOPNOTSUPP;
  705. obj = drm_gem_object_lookup(file_priv, args->handle);
  706. if (obj == NULL)
  707. return -ENOENT;
  708. mutex_lock(&dev->object_name_lock);
  709. /* prevent races with concurrent gem_close. */
  710. if (obj->handle_count == 0) {
  711. ret = -ENOENT;
  712. goto err;
  713. }
  714. if (!obj->name) {
  715. ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_KERNEL);
  716. if (ret < 0)
  717. goto err;
  718. obj->name = ret;
  719. }
  720. args->name = (uint64_t) obj->name;
  721. ret = 0;
  722. err:
  723. mutex_unlock(&dev->object_name_lock);
  724. drm_gem_object_put(obj);
  725. return ret;
  726. }
  727. /**
  728. * drm_gem_open_ioctl - implementation of the GEM_OPEN ioctl
  729. * @dev: drm_device
  730. * @data: ioctl data
  731. * @file_priv: drm file-private structure
  732. *
  733. * Open an object using the global name, returning a handle and the size.
  734. *
  735. * This handle (of course) holds a reference to the object, so the object
  736. * will not go away until the handle is deleted.
  737. */
  738. int
  739. drm_gem_open_ioctl(struct drm_device *dev, void *data,
  740. struct drm_file *file_priv)
  741. {
  742. struct drm_gem_open *args = data;
  743. struct drm_gem_object *obj;
  744. int ret;
  745. u32 handle;
  746. if (!drm_core_check_feature(dev, DRIVER_GEM))
  747. return -EOPNOTSUPP;
  748. mutex_lock(&dev->object_name_lock);
  749. obj = idr_find(&dev->object_name_idr, (int) args->name);
  750. if (obj) {
  751. drm_gem_object_get(obj);
  752. } else {
  753. mutex_unlock(&dev->object_name_lock);
  754. return -ENOENT;
  755. }
  756. /* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
  757. ret = drm_gem_handle_create_tail(file_priv, obj, &handle);
  758. if (ret)
  759. goto err;
  760. args->handle = handle;
  761. args->size = obj->size;
  762. err:
  763. drm_gem_object_put(obj);
  764. return ret;
  765. }
  766. /**
  767. * drm_gem_open - initializes GEM file-private structures at devnode open time
  768. * @dev: drm_device which is being opened by userspace
  769. * @file_private: drm file-private structure to set up
  770. *
  771. * Called at device open time, sets up the structure for handling refcounting
  772. * of mm objects.
  773. */
  774. void
  775. drm_gem_open(struct drm_device *dev, struct drm_file *file_private)
  776. {
  777. idr_init_base(&file_private->object_idr, 1);
  778. spin_lock_init(&file_private->table_lock);
  779. }
  780. /**
  781. * drm_gem_release - release file-private GEM resources
  782. * @dev: drm_device which is being closed by userspace
  783. * @file_private: drm file-private structure to clean up
  784. *
  785. * Called at close time when the filp is going away.
  786. *
  787. * Releases any remaining references on objects by this filp.
  788. */
  789. void
  790. drm_gem_release(struct drm_device *dev, struct drm_file *file_private)
  791. {
  792. idr_for_each(&file_private->object_idr,
  793. &drm_gem_object_release_handle, file_private);
  794. idr_destroy(&file_private->object_idr);
  795. }
  796. /**
  797. * drm_gem_object_release - release GEM buffer object resources
  798. * @obj: GEM buffer object
  799. *
  800. * This releases any structures and resources used by @obj and is the inverse of
  801. * drm_gem_object_init().
  802. */
  803. void
  804. drm_gem_object_release(struct drm_gem_object *obj)
  805. {
  806. WARN_ON(obj->dma_buf);
  807. if (obj->filp)
  808. fput(obj->filp);
  809. dma_resv_fini(&obj->_resv);
  810. drm_gem_free_mmap_offset(obj);
  811. drm_gem_lru_remove(obj);
  812. }
  813. EXPORT_SYMBOL(drm_gem_object_release);
  814. /**
  815. * drm_gem_object_free - free a GEM object
  816. * @kref: kref of the object to free
  817. *
  818. * Called after the last reference to the object has been lost.
  819. *
  820. * Frees the object
  821. */
  822. void
  823. drm_gem_object_free(struct kref *kref)
  824. {
  825. struct drm_gem_object *obj =
  826. container_of(kref, struct drm_gem_object, refcount);
  827. if (WARN_ON(!obj->funcs->free))
  828. return;
  829. obj->funcs->free(obj);
  830. }
  831. EXPORT_SYMBOL(drm_gem_object_free);
  832. /**
  833. * drm_gem_vm_open - vma->ops->open implementation for GEM
  834. * @vma: VM area structure
  835. *
  836. * This function implements the #vm_operations_struct open() callback for GEM
  837. * drivers. This must be used together with drm_gem_vm_close().
  838. */
  839. void drm_gem_vm_open(struct vm_area_struct *vma)
  840. {
  841. struct drm_gem_object *obj = vma->vm_private_data;
  842. drm_gem_object_get(obj);
  843. }
  844. EXPORT_SYMBOL(drm_gem_vm_open);
  845. /**
  846. * drm_gem_vm_close - vma->ops->close implementation for GEM
  847. * @vma: VM area structure
  848. *
  849. * This function implements the #vm_operations_struct close() callback for GEM
  850. * drivers. This must be used together with drm_gem_vm_open().
  851. */
  852. void drm_gem_vm_close(struct vm_area_struct *vma)
  853. {
  854. struct drm_gem_object *obj = vma->vm_private_data;
  855. drm_gem_object_put(obj);
  856. }
  857. EXPORT_SYMBOL(drm_gem_vm_close);
  858. /**
  859. * drm_gem_mmap_obj - memory map a GEM object
  860. * @obj: the GEM object to map
  861. * @obj_size: the object size to be mapped, in bytes
  862. * @vma: VMA for the area to be mapped
  863. *
  864. * Set up the VMA to prepare mapping of the GEM object using the GEM object's
  865. * vm_ops. Depending on their requirements, GEM objects can either
  866. * provide a fault handler in their vm_ops (in which case any accesses to
  867. * the object will be trapped, to perform migration, GTT binding, surface
  868. * register allocation, or performance monitoring), or mmap the buffer memory
  869. * synchronously after calling drm_gem_mmap_obj.
  870. *
  871. * This function is mainly intended to implement the DMABUF mmap operation, when
  872. * the GEM object is not looked up based on its fake offset. To implement the
  873. * DRM mmap operation, drivers should use the drm_gem_mmap() function.
  874. *
  875. * drm_gem_mmap_obj() assumes the user is granted access to the buffer while
  876. * drm_gem_mmap() prevents unprivileged users from mapping random objects. So
  877. * callers must verify access restrictions before calling this helper.
  878. *
  879. * Return 0 or success or -EINVAL if the object size is smaller than the VMA
  880. * size, or if no vm_ops are provided.
  881. */
  882. int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
  883. struct vm_area_struct *vma)
  884. {
  885. int ret;
  886. /* Check for valid size. */
  887. if (obj_size < vma->vm_end - vma->vm_start)
  888. return -EINVAL;
  889. /* Take a ref for this mapping of the object, so that the fault
  890. * handler can dereference the mmap offset's pointer to the object.
  891. * This reference is cleaned up by the corresponding vm_close
  892. * (which should happen whether the vma was created by this call, or
  893. * by a vm_open due to mremap or partial unmap or whatever).
  894. */
  895. drm_gem_object_get(obj);
  896. vma->vm_private_data = obj;
  897. vma->vm_ops = obj->funcs->vm_ops;
  898. if (obj->funcs->mmap) {
  899. ret = obj->funcs->mmap(obj, vma);
  900. if (ret)
  901. goto err_drm_gem_object_put;
  902. WARN_ON(!(vma->vm_flags & VM_DONTEXPAND));
  903. } else {
  904. if (!vma->vm_ops) {
  905. ret = -EINVAL;
  906. goto err_drm_gem_object_put;
  907. }
  908. vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP);
  909. vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
  910. vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
  911. }
  912. return 0;
  913. err_drm_gem_object_put:
  914. drm_gem_object_put(obj);
  915. return ret;
  916. }
  917. EXPORT_SYMBOL(drm_gem_mmap_obj);
  918. /**
  919. * drm_gem_mmap - memory map routine for GEM objects
  920. * @filp: DRM file pointer
  921. * @vma: VMA for the area to be mapped
  922. *
  923. * If a driver supports GEM object mapping, mmap calls on the DRM file
  924. * descriptor will end up here.
  925. *
  926. * Look up the GEM object based on the offset passed in (vma->vm_pgoff will
  927. * contain the fake offset we created when the GTT map ioctl was called on
  928. * the object) and map it with a call to drm_gem_mmap_obj().
  929. *
  930. * If the caller is not granted access to the buffer object, the mmap will fail
  931. * with EACCES. Please see the vma manager for more information.
  932. */
  933. int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
  934. {
  935. struct drm_file *priv = filp->private_data;
  936. struct drm_device *dev = priv->minor->dev;
  937. struct drm_gem_object *obj = NULL;
  938. struct drm_vma_offset_node *node;
  939. int ret;
  940. if (drm_dev_is_unplugged(dev))
  941. return -ENODEV;
  942. drm_vma_offset_lock_lookup(dev->vma_offset_manager);
  943. node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager,
  944. vma->vm_pgoff,
  945. vma_pages(vma));
  946. if (likely(node)) {
  947. obj = container_of(node, struct drm_gem_object, vma_node);
  948. /*
  949. * When the object is being freed, after it hits 0-refcnt it
  950. * proceeds to tear down the object. In the process it will
  951. * attempt to remove the VMA offset and so acquire this
  952. * mgr->vm_lock. Therefore if we find an object with a 0-refcnt
  953. * that matches our range, we know it is in the process of being
  954. * destroyed and will be freed as soon as we release the lock -
  955. * so we have to check for the 0-refcnted object and treat it as
  956. * invalid.
  957. */
  958. if (!kref_get_unless_zero(&obj->refcount))
  959. obj = NULL;
  960. }
  961. drm_vma_offset_unlock_lookup(dev->vma_offset_manager);
  962. if (!obj)
  963. return -EINVAL;
  964. if (!drm_vma_node_is_allowed(node, priv)) {
  965. drm_gem_object_put(obj);
  966. return -EACCES;
  967. }
  968. ret = drm_gem_mmap_obj(obj, drm_vma_node_size(node) << PAGE_SHIFT,
  969. vma);
  970. drm_gem_object_put(obj);
  971. return ret;
  972. }
  973. EXPORT_SYMBOL(drm_gem_mmap);
  974. void drm_gem_print_info(struct drm_printer *p, unsigned int indent,
  975. const struct drm_gem_object *obj)
  976. {
  977. drm_printf_indent(p, indent, "name=%d\n", obj->name);
  978. drm_printf_indent(p, indent, "refcount=%u\n",
  979. kref_read(&obj->refcount));
  980. drm_printf_indent(p, indent, "start=%08lx\n",
  981. drm_vma_node_start(&obj->vma_node));
  982. drm_printf_indent(p, indent, "size=%zu\n", obj->size);
  983. drm_printf_indent(p, indent, "imported=%s\n",
  984. str_yes_no(obj->import_attach));
  985. if (obj->funcs->print_info)
  986. obj->funcs->print_info(p, indent, obj);
  987. }
  988. int drm_gem_pin(struct drm_gem_object *obj)
  989. {
  990. if (obj->funcs->pin)
  991. return obj->funcs->pin(obj);
  992. else
  993. return 0;
  994. }
  995. void drm_gem_unpin(struct drm_gem_object *obj)
  996. {
  997. if (obj->funcs->unpin)
  998. obj->funcs->unpin(obj);
  999. }
  1000. int drm_gem_vmap(struct drm_gem_object *obj, struct iosys_map *map)
  1001. {
  1002. int ret;
  1003. if (!obj->funcs->vmap)
  1004. return -EOPNOTSUPP;
  1005. ret = obj->funcs->vmap(obj, map);
  1006. if (ret)
  1007. return ret;
  1008. else if (iosys_map_is_null(map))
  1009. return -ENOMEM;
  1010. return 0;
  1011. }
  1012. EXPORT_SYMBOL(drm_gem_vmap);
  1013. void drm_gem_vunmap(struct drm_gem_object *obj, struct iosys_map *map)
  1014. {
  1015. if (iosys_map_is_null(map))
  1016. return;
  1017. if (obj->funcs->vunmap)
  1018. obj->funcs->vunmap(obj, map);
  1019. /* Always set the mapping to NULL. Callers may rely on this. */
  1020. iosys_map_clear(map);
  1021. }
  1022. EXPORT_SYMBOL(drm_gem_vunmap);
  1023. /**
  1024. * drm_gem_lock_reservations - Sets up the ww context and acquires
  1025. * the lock on an array of GEM objects.
  1026. *
  1027. * Once you've locked your reservations, you'll want to set up space
  1028. * for your shared fences (if applicable), submit your job, then
  1029. * drm_gem_unlock_reservations().
  1030. *
  1031. * @objs: drm_gem_objects to lock
  1032. * @count: Number of objects in @objs
  1033. * @acquire_ctx: struct ww_acquire_ctx that will be initialized as
  1034. * part of tracking this set of locked reservations.
  1035. */
  1036. int
  1037. drm_gem_lock_reservations(struct drm_gem_object **objs, int count,
  1038. struct ww_acquire_ctx *acquire_ctx)
  1039. {
  1040. int contended = -1;
  1041. int i, ret;
  1042. ww_acquire_init(acquire_ctx, &reservation_ww_class);
  1043. retry:
  1044. if (contended != -1) {
  1045. struct drm_gem_object *obj = objs[contended];
  1046. ret = dma_resv_lock_slow_interruptible(obj->resv,
  1047. acquire_ctx);
  1048. if (ret) {
  1049. ww_acquire_fini(acquire_ctx);
  1050. return ret;
  1051. }
  1052. }
  1053. for (i = 0; i < count; i++) {
  1054. if (i == contended)
  1055. continue;
  1056. ret = dma_resv_lock_interruptible(objs[i]->resv,
  1057. acquire_ctx);
  1058. if (ret) {
  1059. int j;
  1060. for (j = 0; j < i; j++)
  1061. dma_resv_unlock(objs[j]->resv);
  1062. if (contended != -1 && contended >= i)
  1063. dma_resv_unlock(objs[contended]->resv);
  1064. if (ret == -EDEADLK) {
  1065. contended = i;
  1066. goto retry;
  1067. }
  1068. ww_acquire_fini(acquire_ctx);
  1069. return ret;
  1070. }
  1071. }
  1072. ww_acquire_done(acquire_ctx);
  1073. return 0;
  1074. }
  1075. EXPORT_SYMBOL(drm_gem_lock_reservations);
  1076. void
  1077. drm_gem_unlock_reservations(struct drm_gem_object **objs, int count,
  1078. struct ww_acquire_ctx *acquire_ctx)
  1079. {
  1080. int i;
  1081. for (i = 0; i < count; i++)
  1082. dma_resv_unlock(objs[i]->resv);
  1083. ww_acquire_fini(acquire_ctx);
  1084. }
  1085. EXPORT_SYMBOL(drm_gem_unlock_reservations);
  1086. /**
  1087. * drm_gem_lru_init - initialize a LRU
  1088. *
  1089. * @lru: The LRU to initialize
  1090. * @lock: The lock protecting the LRU
  1091. */
  1092. void
  1093. drm_gem_lru_init(struct drm_gem_lru *lru, struct mutex *lock)
  1094. {
  1095. lru->lock = lock;
  1096. lru->count = 0;
  1097. INIT_LIST_HEAD(&lru->list);
  1098. }
  1099. EXPORT_SYMBOL(drm_gem_lru_init);
  1100. static void
  1101. drm_gem_lru_remove_locked(struct drm_gem_object *obj)
  1102. {
  1103. obj->lru->count -= obj->size >> PAGE_SHIFT;
  1104. WARN_ON(obj->lru->count < 0);
  1105. list_del(&obj->lru_node);
  1106. obj->lru = NULL;
  1107. }
  1108. /**
  1109. * drm_gem_lru_remove - remove object from whatever LRU it is in
  1110. *
  1111. * If the object is currently in any LRU, remove it.
  1112. *
  1113. * @obj: The GEM object to remove from current LRU
  1114. */
  1115. void
  1116. drm_gem_lru_remove(struct drm_gem_object *obj)
  1117. {
  1118. struct drm_gem_lru *lru = obj->lru;
  1119. if (!lru)
  1120. return;
  1121. mutex_lock(lru->lock);
  1122. drm_gem_lru_remove_locked(obj);
  1123. mutex_unlock(lru->lock);
  1124. }
  1125. EXPORT_SYMBOL(drm_gem_lru_remove);
  1126. static void
  1127. drm_gem_lru_move_tail_locked(struct drm_gem_lru *lru, struct drm_gem_object *obj)
  1128. {
  1129. lockdep_assert_held_once(lru->lock);
  1130. if (obj->lru)
  1131. drm_gem_lru_remove_locked(obj);
  1132. lru->count += obj->size >> PAGE_SHIFT;
  1133. list_add_tail(&obj->lru_node, &lru->list);
  1134. obj->lru = lru;
  1135. }
  1136. /**
  1137. * drm_gem_lru_move_tail - move the object to the tail of the LRU
  1138. *
  1139. * If the object is already in this LRU it will be moved to the
  1140. * tail. Otherwise it will be removed from whichever other LRU
  1141. * it is in (if any) and moved into this LRU.
  1142. *
  1143. * @lru: The LRU to move the object into.
  1144. * @obj: The GEM object to move into this LRU
  1145. */
  1146. void
  1147. drm_gem_lru_move_tail(struct drm_gem_lru *lru, struct drm_gem_object *obj)
  1148. {
  1149. mutex_lock(lru->lock);
  1150. drm_gem_lru_move_tail_locked(lru, obj);
  1151. mutex_unlock(lru->lock);
  1152. }
  1153. EXPORT_SYMBOL(drm_gem_lru_move_tail);
  1154. /**
  1155. * drm_gem_lru_scan - helper to implement shrinker.scan_objects
  1156. *
  1157. * If the shrink callback succeeds, it is expected that the driver
  1158. * move the object out of this LRU.
  1159. *
  1160. * If the LRU possibly contain active buffers, it is the responsibility
  1161. * of the shrink callback to check for this (ie. dma_resv_test_signaled())
  1162. * or if necessary block until the buffer becomes idle.
  1163. *
  1164. * @lru: The LRU to scan
  1165. * @nr_to_scan: The number of pages to try to reclaim
  1166. * @remaining: The number of pages left to reclaim, should be initialized by caller
  1167. * @shrink: Callback to try to shrink/reclaim the object.
  1168. */
  1169. unsigned long
  1170. drm_gem_lru_scan(struct drm_gem_lru *lru,
  1171. unsigned int nr_to_scan,
  1172. unsigned long *remaining,
  1173. bool (*shrink)(struct drm_gem_object *obj))
  1174. {
  1175. struct drm_gem_lru still_in_lru;
  1176. struct drm_gem_object *obj;
  1177. unsigned freed = 0;
  1178. drm_gem_lru_init(&still_in_lru, lru->lock);
  1179. mutex_lock(lru->lock);
  1180. while (freed < nr_to_scan) {
  1181. obj = list_first_entry_or_null(&lru->list, typeof(*obj), lru_node);
  1182. if (!obj)
  1183. break;
  1184. drm_gem_lru_move_tail_locked(&still_in_lru, obj);
  1185. /*
  1186. * If it's in the process of being freed, gem_object->free()
  1187. * may be blocked on lock waiting to remove it. So just
  1188. * skip it.
  1189. */
  1190. if (!kref_get_unless_zero(&obj->refcount))
  1191. continue;
  1192. /*
  1193. * Now that we own a reference, we can drop the lock for the
  1194. * rest of the loop body, to reduce contention with other
  1195. * code paths that need the LRU lock
  1196. */
  1197. mutex_unlock(lru->lock);
  1198. /*
  1199. * Note that this still needs to be trylock, since we can
  1200. * hit shrinker in response to trying to get backing pages
  1201. * for this obj (ie. while it's lock is already held)
  1202. */
  1203. if (!dma_resv_trylock(obj->resv)) {
  1204. *remaining += obj->size >> PAGE_SHIFT;
  1205. goto tail;
  1206. }
  1207. if (shrink(obj)) {
  1208. freed += obj->size >> PAGE_SHIFT;
  1209. /*
  1210. * If we succeeded in releasing the object's backing
  1211. * pages, we expect the driver to have moved the object
  1212. * out of this LRU
  1213. */
  1214. WARN_ON(obj->lru == &still_in_lru);
  1215. WARN_ON(obj->lru == lru);
  1216. }
  1217. dma_resv_unlock(obj->resv);
  1218. tail:
  1219. drm_gem_object_put(obj);
  1220. mutex_lock(lru->lock);
  1221. }
  1222. /*
  1223. * Move objects we've skipped over out of the temporary still_in_lru
  1224. * back into this LRU
  1225. */
  1226. list_for_each_entry (obj, &still_in_lru.list, lru_node)
  1227. obj->lru = lru;
  1228. list_splice_tail(&still_in_lru.list, &lru->list);
  1229. lru->count += still_in_lru.count;
  1230. mutex_unlock(lru->lock);
  1231. return freed;
  1232. }
  1233. EXPORT_SYMBOL(drm_gem_lru_scan);