ashmem.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* mm/ashmem.c
  3. *
  4. * Anonymous Shared Memory Subsystem, ashmem
  5. *
  6. * Copyright (C) 2008 Google, Inc.
  7. *
  8. * Robert Love <rlove@google.com>
  9. */
  10. #define pr_fmt(fmt) "ashmem: " fmt
  11. #include <linux/init.h>
  12. #include <linux/export.h>
  13. #include <linux/file.h>
  14. #include <linux/fs.h>
  15. #include <linux/falloc.h>
  16. #include <linux/miscdevice.h>
  17. #include <linux/security.h>
  18. #include <linux/mm.h>
  19. #include <linux/mman.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/personality.h>
  22. #include <linux/bitops.h>
  23. #include <linux/mutex.h>
  24. #include <linux/shmem_fs.h>
  25. #include <linux/module.h>
  26. #include "ashmem.h"
  27. #define ASHMEM_NAME_PREFIX "dev/ashmem/"
  28. #define ASHMEM_NAME_PREFIX_LEN (sizeof(ASHMEM_NAME_PREFIX) - 1)
  29. #define ASHMEM_FULL_NAME_LEN (ASHMEM_NAME_LEN + ASHMEM_NAME_PREFIX_LEN)
  30. /**
  31. * struct ashmem_area - The anonymous shared memory area
  32. * @name: The optional name in /proc/pid/maps
  33. * @unpinned_list: The list of all ashmem areas
  34. * @file: The shmem-based backing file
  35. * @size: The size of the mapping, in bytes
  36. * @prot_mask: The allowed protection bits, as vm_flags
  37. *
  38. * The lifecycle of this structure is from our parent file's open() until
  39. * its release(). It is also protected by 'ashmem_mutex'
  40. *
  41. * Warning: Mappings do NOT pin this structure; It dies on close()
  42. */
  43. struct ashmem_area {
  44. char name[ASHMEM_FULL_NAME_LEN];
  45. struct list_head unpinned_list;
  46. struct file *file;
  47. size_t size;
  48. unsigned long prot_mask;
  49. };
  50. /**
  51. * struct ashmem_range - A range of unpinned/evictable pages
  52. * @lru: The entry in the LRU list
  53. * @unpinned: The entry in its area's unpinned list
  54. * @asma: The associated anonymous shared memory area.
  55. * @pgstart: The starting page (inclusive)
  56. * @pgend: The ending page (inclusive)
  57. * @purged: The purge status (ASHMEM_NOT or ASHMEM_WAS_PURGED)
  58. *
  59. * The lifecycle of this structure is from unpin to pin.
  60. * It is protected by 'ashmem_mutex'
  61. */
  62. struct ashmem_range {
  63. struct list_head lru;
  64. struct list_head unpinned;
  65. struct ashmem_area *asma;
  66. size_t pgstart;
  67. size_t pgend;
  68. unsigned int purged;
  69. };
  70. /* LRU list of unpinned pages, protected by ashmem_mutex */
  71. static LIST_HEAD(ashmem_lru_list);
  72. static atomic_t ashmem_shrink_inflight = ATOMIC_INIT(0);
  73. static DECLARE_WAIT_QUEUE_HEAD(ashmem_shrink_wait);
  74. /*
  75. * long lru_count - The count of pages on our LRU list.
  76. *
  77. * This is protected by ashmem_mutex.
  78. */
  79. static unsigned long lru_count;
  80. /*
  81. * ashmem_mutex - protects the list of and each individual ashmem_area
  82. *
  83. * Lock Ordering: ashmex_mutex -> i_mutex -> i_alloc_sem
  84. */
  85. static DEFINE_MUTEX(ashmem_mutex);
  86. static struct kmem_cache *ashmem_area_cachep __read_mostly;
  87. static struct kmem_cache *ashmem_range_cachep __read_mostly;
  88. static inline unsigned long range_size(struct ashmem_range *range)
  89. {
  90. return range->pgend - range->pgstart + 1;
  91. }
  92. static inline bool range_on_lru(struct ashmem_range *range)
  93. {
  94. return range->purged == ASHMEM_NOT_PURGED;
  95. }
  96. static inline bool page_range_subsumes_range(struct ashmem_range *range,
  97. size_t start, size_t end)
  98. {
  99. return (range->pgstart >= start) && (range->pgend <= end);
  100. }
  101. static inline bool page_range_subsumed_by_range(struct ashmem_range *range,
  102. size_t start, size_t end)
  103. {
  104. return (range->pgstart <= start) && (range->pgend >= end);
  105. }
  106. static inline bool page_in_range(struct ashmem_range *range, size_t page)
  107. {
  108. return (range->pgstart <= page) && (range->pgend >= page);
  109. }
  110. static inline bool page_range_in_range(struct ashmem_range *range,
  111. size_t start, size_t end)
  112. {
  113. return page_in_range(range, start) || page_in_range(range, end) ||
  114. page_range_subsumes_range(range, start, end);
  115. }
  116. static inline bool range_before_page(struct ashmem_range *range, size_t page)
  117. {
  118. return range->pgend < page;
  119. }
  120. #define PROT_MASK (PROT_EXEC | PROT_READ | PROT_WRITE)
  121. /**
  122. * lru_add() - Adds a range of memory to the LRU list
  123. * @range: The memory range being added.
  124. *
  125. * The range is first added to the end (tail) of the LRU list.
  126. * After this, the size of the range is added to @lru_count
  127. */
  128. static inline void lru_add(struct ashmem_range *range)
  129. {
  130. list_add_tail(&range->lru, &ashmem_lru_list);
  131. lru_count += range_size(range);
  132. }
  133. /**
  134. * lru_del() - Removes a range of memory from the LRU list
  135. * @range: The memory range being removed
  136. *
  137. * The range is first deleted from the LRU list.
  138. * After this, the size of the range is removed from @lru_count
  139. */
  140. static inline void lru_del(struct ashmem_range *range)
  141. {
  142. list_del(&range->lru);
  143. lru_count -= range_size(range);
  144. }
  145. /**
  146. * range_alloc() - Allocates and initializes a new ashmem_range structure
  147. * @asma: The associated ashmem_area
  148. * @prev_range: The previous ashmem_range in the sorted asma->unpinned list
  149. * @purged: Initial purge status (ASMEM_NOT_PURGED or ASHMEM_WAS_PURGED)
  150. * @start: The starting page (inclusive)
  151. * @end: The ending page (inclusive)
  152. *
  153. * This function is protected by ashmem_mutex.
  154. */
  155. static void range_alloc(struct ashmem_area *asma,
  156. struct ashmem_range *prev_range, unsigned int purged,
  157. size_t start, size_t end,
  158. struct ashmem_range **new_range)
  159. {
  160. struct ashmem_range *range = *new_range;
  161. *new_range = NULL;
  162. range->asma = asma;
  163. range->pgstart = start;
  164. range->pgend = end;
  165. range->purged = purged;
  166. list_add_tail(&range->unpinned, &prev_range->unpinned);
  167. if (range_on_lru(range))
  168. lru_add(range);
  169. }
  170. /**
  171. * range_del() - Deletes and dealloctes an ashmem_range structure
  172. * @range: The associated ashmem_range that has previously been allocated
  173. */
  174. static void range_del(struct ashmem_range *range)
  175. {
  176. list_del(&range->unpinned);
  177. if (range_on_lru(range))
  178. lru_del(range);
  179. kmem_cache_free(ashmem_range_cachep, range);
  180. }
  181. /**
  182. * range_shrink() - Shrinks an ashmem_range
  183. * @range: The associated ashmem_range being shrunk
  184. * @start: The starting byte of the new range
  185. * @end: The ending byte of the new range
  186. *
  187. * This does not modify the data inside the existing range in any way - It
  188. * simply shrinks the boundaries of the range.
  189. *
  190. * Theoretically, with a little tweaking, this could eventually be changed
  191. * to range_resize, and expand the lru_count if the new range is larger.
  192. */
  193. static inline void range_shrink(struct ashmem_range *range,
  194. size_t start, size_t end)
  195. {
  196. size_t pre = range_size(range);
  197. range->pgstart = start;
  198. range->pgend = end;
  199. if (range_on_lru(range))
  200. lru_count -= pre - range_size(range);
  201. }
  202. /**
  203. * ashmem_open() - Opens an Anonymous Shared Memory structure
  204. * @inode: The backing file's index node(?)
  205. * @file: The backing file
  206. *
  207. * Please note that the ashmem_area is not returned by this function - It is
  208. * instead written to "file->private_data".
  209. *
  210. * Return: 0 if successful, or another code if unsuccessful.
  211. */
  212. static int ashmem_open(struct inode *inode, struct file *file)
  213. {
  214. struct ashmem_area *asma;
  215. int ret;
  216. ret = generic_file_open(inode, file);
  217. if (ret)
  218. return ret;
  219. asma = kmem_cache_zalloc(ashmem_area_cachep, GFP_KERNEL);
  220. if (!asma)
  221. return -ENOMEM;
  222. INIT_LIST_HEAD(&asma->unpinned_list);
  223. memcpy(asma->name, ASHMEM_NAME_PREFIX, ASHMEM_NAME_PREFIX_LEN);
  224. asma->prot_mask = PROT_MASK;
  225. file->private_data = asma;
  226. return 0;
  227. }
  228. /**
  229. * ashmem_release() - Releases an Anonymous Shared Memory structure
  230. * @ignored: The backing file's Index Node(?) - It is ignored here.
  231. * @file: The backing file
  232. *
  233. * Return: 0 if successful. If it is anything else, go have a coffee and
  234. * try again.
  235. */
  236. static int ashmem_release(struct inode *ignored, struct file *file)
  237. {
  238. struct ashmem_area *asma = file->private_data;
  239. struct ashmem_range *range, *next;
  240. mutex_lock(&ashmem_mutex);
  241. list_for_each_entry_safe(range, next, &asma->unpinned_list, unpinned)
  242. range_del(range);
  243. mutex_unlock(&ashmem_mutex);
  244. if (asma->file)
  245. fput(asma->file);
  246. kmem_cache_free(ashmem_area_cachep, asma);
  247. return 0;
  248. }
  249. static ssize_t ashmem_read_iter(struct kiocb *iocb, struct iov_iter *iter)
  250. {
  251. struct ashmem_area *asma = iocb->ki_filp->private_data;
  252. int ret = 0;
  253. mutex_lock(&ashmem_mutex);
  254. /* If size is not set, or set to 0, always return EOF. */
  255. if (asma->size == 0)
  256. goto out_unlock;
  257. if (!asma->file) {
  258. ret = -EBADF;
  259. goto out_unlock;
  260. }
  261. /*
  262. * asma and asma->file are used outside the lock here. We assume
  263. * once asma->file is set it will never be changed, and will not
  264. * be destroyed until all references to the file are dropped and
  265. * ashmem_release is called.
  266. */
  267. mutex_unlock(&ashmem_mutex);
  268. ret = vfs_iter_read(asma->file, iter, &iocb->ki_pos, 0);
  269. mutex_lock(&ashmem_mutex);
  270. if (ret > 0)
  271. asma->file->f_pos = iocb->ki_pos;
  272. out_unlock:
  273. mutex_unlock(&ashmem_mutex);
  274. return ret;
  275. }
  276. static loff_t ashmem_llseek(struct file *file, loff_t offset, int origin)
  277. {
  278. struct ashmem_area *asma = file->private_data;
  279. loff_t ret;
  280. mutex_lock(&ashmem_mutex);
  281. if (asma->size == 0) {
  282. mutex_unlock(&ashmem_mutex);
  283. return -EINVAL;
  284. }
  285. if (!asma->file) {
  286. mutex_unlock(&ashmem_mutex);
  287. return -EBADF;
  288. }
  289. mutex_unlock(&ashmem_mutex);
  290. ret = vfs_llseek(asma->file, offset, origin);
  291. if (ret < 0)
  292. return ret;
  293. /** Copy f_pos from backing file, since f_ops->llseek() sets it */
  294. file->f_pos = asma->file->f_pos;
  295. return ret;
  296. }
  297. static inline vm_flags_t calc_vm_may_flags(unsigned long prot)
  298. {
  299. return _calc_vm_trans(prot, PROT_READ, VM_MAYREAD) |
  300. _calc_vm_trans(prot, PROT_WRITE, VM_MAYWRITE) |
  301. _calc_vm_trans(prot, PROT_EXEC, VM_MAYEXEC);
  302. }
  303. static int ashmem_vmfile_mmap(struct file *file, struct vm_area_struct *vma)
  304. {
  305. /* do not allow to mmap ashmem backing shmem file directly */
  306. return -EPERM;
  307. }
  308. static unsigned long
  309. ashmem_vmfile_get_unmapped_area(struct file *file, unsigned long addr,
  310. unsigned long len, unsigned long pgoff,
  311. unsigned long flags)
  312. {
  313. return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
  314. }
  315. static int ashmem_mmap(struct file *file, struct vm_area_struct *vma)
  316. {
  317. static struct file_operations vmfile_fops;
  318. struct ashmem_area *asma = file->private_data;
  319. int ret = 0;
  320. mutex_lock(&ashmem_mutex);
  321. /* user needs to SET_SIZE before mapping */
  322. if (!asma->size) {
  323. ret = -EINVAL;
  324. goto out;
  325. }
  326. /* requested mapping size larger than object size */
  327. if (vma->vm_end - vma->vm_start > PAGE_ALIGN(asma->size)) {
  328. ret = -EINVAL;
  329. goto out;
  330. }
  331. /* requested protection bits must match our allowed protection mask */
  332. if ((vma->vm_flags & ~calc_vm_prot_bits(asma->prot_mask, 0)) &
  333. calc_vm_prot_bits(PROT_MASK, 0)) {
  334. ret = -EPERM;
  335. goto out;
  336. }
  337. vma->vm_flags &= ~calc_vm_may_flags(~asma->prot_mask);
  338. if (!asma->file) {
  339. char *name = ASHMEM_NAME_DEF;
  340. struct file *vmfile;
  341. if (asma->name[ASHMEM_NAME_PREFIX_LEN] != '\0')
  342. name = asma->name;
  343. /* ... and allocate the backing shmem file */
  344. vmfile = shmem_file_setup(name, asma->size, vma->vm_flags);
  345. if (IS_ERR(vmfile)) {
  346. ret = PTR_ERR(vmfile);
  347. goto out;
  348. }
  349. vmfile->f_mode |= FMODE_LSEEK;
  350. asma->file = vmfile;
  351. /*
  352. * override mmap operation of the vmfile so that it can't be
  353. * remapped which would lead to creation of a new vma with no
  354. * asma permission checks. Have to override get_unmapped_area
  355. * as well to prevent VM_BUG_ON check for f_ops modification.
  356. */
  357. if (!vmfile_fops.mmap) {
  358. vmfile_fops = *vmfile->f_op;
  359. vmfile_fops.mmap = ashmem_vmfile_mmap;
  360. vmfile_fops.get_unmapped_area =
  361. ashmem_vmfile_get_unmapped_area;
  362. }
  363. vmfile->f_op = &vmfile_fops;
  364. }
  365. get_file(asma->file);
  366. /*
  367. * XXX - Reworked to use shmem_zero_setup() instead of
  368. * shmem_set_file while we're in staging. -jstultz
  369. */
  370. if (vma->vm_flags & VM_SHARED) {
  371. ret = shmem_zero_setup(vma);
  372. if (ret) {
  373. fput(asma->file);
  374. goto out;
  375. }
  376. } else {
  377. vma_set_anonymous(vma);
  378. }
  379. if (vma->vm_file)
  380. fput(vma->vm_file);
  381. vma->vm_file = asma->file;
  382. out:
  383. mutex_unlock(&ashmem_mutex);
  384. return ret;
  385. }
  386. /*
  387. * ashmem_shrink - our cache shrinker, called from mm/vmscan.c
  388. *
  389. * 'nr_to_scan' is the number of objects to scan for freeing.
  390. *
  391. * 'gfp_mask' is the mask of the allocation that got us into this mess.
  392. *
  393. * Return value is the number of objects freed or -1 if we cannot
  394. * proceed without risk of deadlock (due to gfp_mask).
  395. *
  396. * We approximate LRU via least-recently-unpinned, jettisoning unpinned partial
  397. * chunks of ashmem regions LRU-wise one-at-a-time until we hit 'nr_to_scan'
  398. * pages freed.
  399. */
  400. static unsigned long
  401. ashmem_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
  402. {
  403. unsigned long freed = 0;
  404. /* We might recurse into filesystem code, so bail out if necessary */
  405. if (!(sc->gfp_mask & __GFP_FS))
  406. return SHRINK_STOP;
  407. if (!mutex_trylock(&ashmem_mutex))
  408. return -1;
  409. while (!list_empty(&ashmem_lru_list)) {
  410. struct ashmem_range *range =
  411. list_first_entry(&ashmem_lru_list, typeof(*range), lru);
  412. loff_t start = range->pgstart * PAGE_SIZE;
  413. loff_t end = (range->pgend + 1) * PAGE_SIZE;
  414. struct file *f = range->asma->file;
  415. get_file(f);
  416. atomic_inc(&ashmem_shrink_inflight);
  417. range->purged = ASHMEM_WAS_PURGED;
  418. lru_del(range);
  419. freed += range_size(range);
  420. mutex_unlock(&ashmem_mutex);
  421. f->f_op->fallocate(f,
  422. FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
  423. start, end - start);
  424. fput(f);
  425. if (atomic_dec_and_test(&ashmem_shrink_inflight))
  426. wake_up_all(&ashmem_shrink_wait);
  427. if (!mutex_trylock(&ashmem_mutex))
  428. goto out;
  429. if (--sc->nr_to_scan <= 0)
  430. break;
  431. }
  432. mutex_unlock(&ashmem_mutex);
  433. out:
  434. return freed;
  435. }
  436. static unsigned long
  437. ashmem_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
  438. {
  439. /*
  440. * note that lru_count is count of pages on the lru, not a count of
  441. * objects on the list. This means the scan function needs to return the
  442. * number of pages freed, not the number of objects scanned.
  443. */
  444. return lru_count;
  445. }
  446. static struct shrinker ashmem_shrinker = {
  447. .count_objects = ashmem_shrink_count,
  448. .scan_objects = ashmem_shrink_scan,
  449. /*
  450. * XXX (dchinner): I wish people would comment on why they need on
  451. * significant changes to the default value here
  452. */
  453. .seeks = DEFAULT_SEEKS * 4,
  454. };
  455. static int set_prot_mask(struct ashmem_area *asma, unsigned long prot)
  456. {
  457. int ret = 0;
  458. mutex_lock(&ashmem_mutex);
  459. /* the user can only remove, not add, protection bits */
  460. if ((asma->prot_mask & prot) != prot) {
  461. ret = -EINVAL;
  462. goto out;
  463. }
  464. /* does the application expect PROT_READ to imply PROT_EXEC? */
  465. if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
  466. prot |= PROT_EXEC;
  467. asma->prot_mask = prot;
  468. out:
  469. mutex_unlock(&ashmem_mutex);
  470. return ret;
  471. }
  472. static int set_name(struct ashmem_area *asma, void __user *name)
  473. {
  474. int len;
  475. int ret = 0;
  476. char local_name[ASHMEM_NAME_LEN];
  477. /*
  478. * Holding the ashmem_mutex while doing a copy_from_user might cause
  479. * an data abort which would try to access mmap_sem. If another
  480. * thread has invoked ashmem_mmap then it will be holding the
  481. * semaphore and will be waiting for ashmem_mutex, there by leading to
  482. * deadlock. We'll release the mutex and take the name to a local
  483. * variable that does not need protection and later copy the local
  484. * variable to the structure member with lock held.
  485. */
  486. len = strncpy_from_user(local_name, name, ASHMEM_NAME_LEN);
  487. if (len < 0)
  488. return len;
  489. if (len == ASHMEM_NAME_LEN)
  490. local_name[ASHMEM_NAME_LEN - 1] = '\0';
  491. mutex_lock(&ashmem_mutex);
  492. /* cannot change an existing mapping's name */
  493. if (asma->file)
  494. ret = -EINVAL;
  495. else
  496. strcpy(asma->name + ASHMEM_NAME_PREFIX_LEN, local_name);
  497. mutex_unlock(&ashmem_mutex);
  498. return ret;
  499. }
  500. static int get_name(struct ashmem_area *asma, void __user *name)
  501. {
  502. int ret = 0;
  503. size_t len;
  504. /*
  505. * Have a local variable to which we'll copy the content
  506. * from asma with the lock held. Later we can copy this to the user
  507. * space safely without holding any locks. So even if we proceed to
  508. * wait for mmap_sem, it won't lead to deadlock.
  509. */
  510. char local_name[ASHMEM_NAME_LEN];
  511. mutex_lock(&ashmem_mutex);
  512. if (asma->name[ASHMEM_NAME_PREFIX_LEN] != '\0') {
  513. /*
  514. * Copying only `len', instead of ASHMEM_NAME_LEN, bytes
  515. * prevents us from revealing one user's stack to another.
  516. */
  517. len = strlen(asma->name + ASHMEM_NAME_PREFIX_LEN) + 1;
  518. memcpy(local_name, asma->name + ASHMEM_NAME_PREFIX_LEN, len);
  519. } else {
  520. len = sizeof(ASHMEM_NAME_DEF);
  521. memcpy(local_name, ASHMEM_NAME_DEF, len);
  522. }
  523. mutex_unlock(&ashmem_mutex);
  524. /*
  525. * Now we are just copying from the stack variable to userland
  526. * No lock held
  527. */
  528. if (copy_to_user(name, local_name, len))
  529. ret = -EFAULT;
  530. return ret;
  531. }
  532. /*
  533. * ashmem_pin - pin the given ashmem region, returning whether it was
  534. * previously purged (ASHMEM_WAS_PURGED) or not (ASHMEM_NOT_PURGED).
  535. *
  536. * Caller must hold ashmem_mutex.
  537. */
  538. static int ashmem_pin(struct ashmem_area *asma, size_t pgstart, size_t pgend,
  539. struct ashmem_range **new_range)
  540. {
  541. struct ashmem_range *range, *next;
  542. int ret = ASHMEM_NOT_PURGED;
  543. list_for_each_entry_safe(range, next, &asma->unpinned_list, unpinned) {
  544. /* moved past last applicable page; we can short circuit */
  545. if (range_before_page(range, pgstart))
  546. break;
  547. /*
  548. * The user can ask us to pin pages that span multiple ranges,
  549. * or to pin pages that aren't even unpinned, so this is messy.
  550. *
  551. * Four cases:
  552. * 1. The requested range subsumes an existing range, so we
  553. * just remove the entire matching range.
  554. * 2. The requested range overlaps the start of an existing
  555. * range, so we just update that range.
  556. * 3. The requested range overlaps the end of an existing
  557. * range, so we just update that range.
  558. * 4. The requested range punches a hole in an existing range,
  559. * so we have to update one side of the range and then
  560. * create a new range for the other side.
  561. */
  562. if (page_range_in_range(range, pgstart, pgend)) {
  563. ret |= range->purged;
  564. /* Case #1: Easy. Just nuke the whole thing. */
  565. if (page_range_subsumes_range(range, pgstart, pgend)) {
  566. range_del(range);
  567. continue;
  568. }
  569. /* Case #2: We overlap from the start, so adjust it */
  570. if (range->pgstart >= pgstart) {
  571. range_shrink(range, pgend + 1, range->pgend);
  572. continue;
  573. }
  574. /* Case #3: We overlap from the rear, so adjust it */
  575. if (range->pgend <= pgend) {
  576. range_shrink(range, range->pgstart,
  577. pgstart - 1);
  578. continue;
  579. }
  580. /*
  581. * Case #4: We eat a chunk out of the middle. A bit
  582. * more complicated, we allocate a new range for the
  583. * second half and adjust the first chunk's endpoint.
  584. */
  585. range_alloc(asma, range, range->purged,
  586. pgend + 1, range->pgend, new_range);
  587. range_shrink(range, range->pgstart, pgstart - 1);
  588. break;
  589. }
  590. }
  591. return ret;
  592. }
  593. /*
  594. * ashmem_unpin - unpin the given range of pages. Returns zero on success.
  595. *
  596. * Caller must hold ashmem_mutex.
  597. */
  598. static int ashmem_unpin(struct ashmem_area *asma, size_t pgstart, size_t pgend,
  599. struct ashmem_range **new_range)
  600. {
  601. struct ashmem_range *range, *next;
  602. unsigned int purged = ASHMEM_NOT_PURGED;
  603. restart:
  604. list_for_each_entry_safe(range, next, &asma->unpinned_list, unpinned) {
  605. /* short circuit: this is our insertion point */
  606. if (range_before_page(range, pgstart))
  607. break;
  608. /*
  609. * The user can ask us to unpin pages that are already entirely
  610. * or partially pinned. We handle those two cases here.
  611. */
  612. if (page_range_subsumed_by_range(range, pgstart, pgend))
  613. return 0;
  614. if (page_range_in_range(range, pgstart, pgend)) {
  615. pgstart = min(range->pgstart, pgstart);
  616. pgend = max(range->pgend, pgend);
  617. purged |= range->purged;
  618. range_del(range);
  619. goto restart;
  620. }
  621. }
  622. range_alloc(asma, range, purged, pgstart, pgend, new_range);
  623. return 0;
  624. }
  625. /*
  626. * ashmem_get_pin_status - Returns ASHMEM_IS_UNPINNED if _any_ pages in the
  627. * given interval are unpinned and ASHMEM_IS_PINNED otherwise.
  628. *
  629. * Caller must hold ashmem_mutex.
  630. */
  631. static int ashmem_get_pin_status(struct ashmem_area *asma, size_t pgstart,
  632. size_t pgend)
  633. {
  634. struct ashmem_range *range;
  635. int ret = ASHMEM_IS_PINNED;
  636. list_for_each_entry(range, &asma->unpinned_list, unpinned) {
  637. if (range_before_page(range, pgstart))
  638. break;
  639. if (page_range_in_range(range, pgstart, pgend)) {
  640. ret = ASHMEM_IS_UNPINNED;
  641. break;
  642. }
  643. }
  644. return ret;
  645. }
  646. static int ashmem_pin_unpin(struct ashmem_area *asma, unsigned long cmd,
  647. void __user *p)
  648. {
  649. struct ashmem_pin pin;
  650. size_t pgstart, pgend;
  651. int ret = -EINVAL;
  652. struct ashmem_range *range = NULL;
  653. if (copy_from_user(&pin, p, sizeof(pin)))
  654. return -EFAULT;
  655. if (cmd == ASHMEM_PIN || cmd == ASHMEM_UNPIN) {
  656. range = kmem_cache_zalloc(ashmem_range_cachep, GFP_KERNEL);
  657. if (!range)
  658. return -ENOMEM;
  659. }
  660. mutex_lock(&ashmem_mutex);
  661. wait_event(ashmem_shrink_wait, !atomic_read(&ashmem_shrink_inflight));
  662. if (!asma->file)
  663. goto out_unlock;
  664. /* per custom, you can pass zero for len to mean "everything onward" */
  665. if (!pin.len)
  666. pin.len = PAGE_ALIGN(asma->size) - pin.offset;
  667. if ((pin.offset | pin.len) & ~PAGE_MASK)
  668. goto out_unlock;
  669. if (((__u32)-1) - pin.offset < pin.len)
  670. goto out_unlock;
  671. if (PAGE_ALIGN(asma->size) < pin.offset + pin.len)
  672. goto out_unlock;
  673. pgstart = pin.offset / PAGE_SIZE;
  674. pgend = pgstart + (pin.len / PAGE_SIZE) - 1;
  675. switch (cmd) {
  676. case ASHMEM_PIN:
  677. ret = ashmem_pin(asma, pgstart, pgend, &range);
  678. break;
  679. case ASHMEM_UNPIN:
  680. ret = ashmem_unpin(asma, pgstart, pgend, &range);
  681. break;
  682. case ASHMEM_GET_PIN_STATUS:
  683. ret = ashmem_get_pin_status(asma, pgstart, pgend);
  684. break;
  685. }
  686. out_unlock:
  687. mutex_unlock(&ashmem_mutex);
  688. if (range)
  689. kmem_cache_free(ashmem_range_cachep, range);
  690. return ret;
  691. }
  692. static long ashmem_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  693. {
  694. struct ashmem_area *asma = file->private_data;
  695. long ret = -ENOTTY;
  696. switch (cmd) {
  697. case ASHMEM_SET_NAME:
  698. ret = set_name(asma, (void __user *)arg);
  699. break;
  700. case ASHMEM_GET_NAME:
  701. ret = get_name(asma, (void __user *)arg);
  702. break;
  703. case ASHMEM_SET_SIZE:
  704. ret = -EINVAL;
  705. mutex_lock(&ashmem_mutex);
  706. if (!asma->file) {
  707. ret = 0;
  708. asma->size = (size_t)arg;
  709. }
  710. mutex_unlock(&ashmem_mutex);
  711. break;
  712. case ASHMEM_GET_SIZE:
  713. ret = asma->size;
  714. break;
  715. case ASHMEM_SET_PROT_MASK:
  716. ret = set_prot_mask(asma, arg);
  717. break;
  718. case ASHMEM_GET_PROT_MASK:
  719. ret = asma->prot_mask;
  720. break;
  721. case ASHMEM_PIN:
  722. case ASHMEM_UNPIN:
  723. case ASHMEM_GET_PIN_STATUS:
  724. ret = ashmem_pin_unpin(asma, cmd, (void __user *)arg);
  725. break;
  726. case ASHMEM_PURGE_ALL_CACHES:
  727. ret = -EPERM;
  728. if (capable(CAP_SYS_ADMIN)) {
  729. struct shrink_control sc = {
  730. .gfp_mask = GFP_KERNEL,
  731. .nr_to_scan = LONG_MAX,
  732. };
  733. ret = ashmem_shrink_count(&ashmem_shrinker, &sc);
  734. ashmem_shrink_scan(&ashmem_shrinker, &sc);
  735. }
  736. break;
  737. }
  738. return ret;
  739. }
  740. /* support of 32bit userspace on 64bit platforms */
  741. #ifdef CONFIG_COMPAT
  742. static long compat_ashmem_ioctl(struct file *file, unsigned int cmd,
  743. unsigned long arg)
  744. {
  745. switch (cmd) {
  746. case COMPAT_ASHMEM_SET_SIZE:
  747. cmd = ASHMEM_SET_SIZE;
  748. break;
  749. case COMPAT_ASHMEM_SET_PROT_MASK:
  750. cmd = ASHMEM_SET_PROT_MASK;
  751. break;
  752. }
  753. return ashmem_ioctl(file, cmd, arg);
  754. }
  755. #endif
  756. #ifdef CONFIG_PROC_FS
  757. static void ashmem_show_fdinfo(struct seq_file *m, struct file *file)
  758. {
  759. struct ashmem_area *asma = file->private_data;
  760. mutex_lock(&ashmem_mutex);
  761. if (asma->file)
  762. seq_printf(m, "inode:\t%ld\n", file_inode(asma->file)->i_ino);
  763. if (asma->name[ASHMEM_NAME_PREFIX_LEN] != '\0')
  764. seq_printf(m, "name:\t%s\n",
  765. asma->name + ASHMEM_NAME_PREFIX_LEN);
  766. mutex_unlock(&ashmem_mutex);
  767. }
  768. #endif
  769. static const struct file_operations ashmem_fops = {
  770. .owner = THIS_MODULE,
  771. .open = ashmem_open,
  772. .release = ashmem_release,
  773. .read_iter = ashmem_read_iter,
  774. .llseek = ashmem_llseek,
  775. .mmap = ashmem_mmap,
  776. .unlocked_ioctl = ashmem_ioctl,
  777. #ifdef CONFIG_COMPAT
  778. .compat_ioctl = compat_ashmem_ioctl,
  779. #endif
  780. #ifdef CONFIG_PROC_FS
  781. .show_fdinfo = ashmem_show_fdinfo,
  782. #endif
  783. };
  784. static struct miscdevice ashmem_misc = {
  785. .minor = MISC_DYNAMIC_MINOR,
  786. .name = "ashmem",
  787. .fops = &ashmem_fops,
  788. };
  789. static int __init ashmem_init(void)
  790. {
  791. int ret = -ENOMEM;
  792. ashmem_area_cachep = kmem_cache_create("ashmem_area_cache",
  793. sizeof(struct ashmem_area),
  794. 0, 0, NULL);
  795. if (!ashmem_area_cachep) {
  796. pr_err("failed to create slab cache\n");
  797. goto out;
  798. }
  799. ashmem_range_cachep = kmem_cache_create("ashmem_range_cache",
  800. sizeof(struct ashmem_range),
  801. 0, 0, NULL);
  802. if (!ashmem_range_cachep) {
  803. pr_err("failed to create slab cache\n");
  804. goto out_free1;
  805. }
  806. ret = misc_register(&ashmem_misc);
  807. if (ret) {
  808. pr_err("failed to register misc device!\n");
  809. goto out_free2;
  810. }
  811. ret = register_shrinker(&ashmem_shrinker);
  812. if (ret) {
  813. pr_err("failed to register shrinker!\n");
  814. goto out_demisc;
  815. }
  816. pr_info("initialized\n");
  817. return 0;
  818. out_demisc:
  819. misc_deregister(&ashmem_misc);
  820. out_free2:
  821. kmem_cache_destroy(ashmem_range_cachep);
  822. out_free1:
  823. kmem_cache_destroy(ashmem_area_cachep);
  824. out:
  825. return ret;
  826. }
  827. device_initcall(ashmem_init);
  828. MODULE_LICENSE("GPL v2");