ashmem.c 23 KB

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