drm_gem.c 42 KB

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