drm_gem.c 46 KB

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