ashmem.c 23 KB

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