binder_alloc.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  1. /* binder_alloc.c
  2. *
  3. * Android IPC Subsystem
  4. *
  5. * Copyright (C) 2007-2017 Google, Inc.
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <asm/cacheflush.h>
  19. #include <linux/list.h>
  20. #include <linux/sched/mm.h>
  21. #include <linux/module.h>
  22. #include <linux/rtmutex.h>
  23. #include <linux/rbtree.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/slab.h>
  27. #include <linux/sched.h>
  28. #include <linux/list_lru.h>
  29. #include "binder_alloc.h"
  30. #include "binder_trace.h"
  31. struct list_lru binder_alloc_lru;
  32. static DEFINE_MUTEX(binder_alloc_mmap_lock);
  33. enum {
  34. BINDER_DEBUG_OPEN_CLOSE = 1U << 1,
  35. BINDER_DEBUG_BUFFER_ALLOC = 1U << 2,
  36. BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 3,
  37. };
  38. static uint32_t binder_alloc_debug_mask;
  39. module_param_named(debug_mask, binder_alloc_debug_mask,
  40. uint, 0644);
  41. #define binder_alloc_debug(mask, x...) \
  42. do { \
  43. if (binder_alloc_debug_mask & mask) \
  44. pr_info(x); \
  45. } while (0)
  46. static struct binder_buffer *binder_buffer_next(struct binder_buffer *buffer)
  47. {
  48. return list_entry(buffer->entry.next, struct binder_buffer, entry);
  49. }
  50. static struct binder_buffer *binder_buffer_prev(struct binder_buffer *buffer)
  51. {
  52. return list_entry(buffer->entry.prev, struct binder_buffer, entry);
  53. }
  54. static size_t binder_alloc_buffer_size(struct binder_alloc *alloc,
  55. struct binder_buffer *buffer)
  56. {
  57. if (list_is_last(&buffer->entry, &alloc->buffers))
  58. return (u8 *)alloc->buffer +
  59. alloc->buffer_size - (u8 *)buffer->data;
  60. return (u8 *)binder_buffer_next(buffer)->data - (u8 *)buffer->data;
  61. }
  62. static void binder_insert_free_buffer(struct binder_alloc *alloc,
  63. struct binder_buffer *new_buffer)
  64. {
  65. struct rb_node **p = &alloc->free_buffers.rb_node;
  66. struct rb_node *parent = NULL;
  67. struct binder_buffer *buffer;
  68. size_t buffer_size;
  69. size_t new_buffer_size;
  70. BUG_ON(!new_buffer->free);
  71. new_buffer_size = binder_alloc_buffer_size(alloc, new_buffer);
  72. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  73. "%d: add free buffer, size %zd, at %pK\n",
  74. alloc->pid, new_buffer_size, new_buffer);
  75. while (*p) {
  76. parent = *p;
  77. buffer = rb_entry(parent, struct binder_buffer, rb_node);
  78. BUG_ON(!buffer->free);
  79. buffer_size = binder_alloc_buffer_size(alloc, buffer);
  80. if (new_buffer_size < buffer_size)
  81. p = &parent->rb_left;
  82. else
  83. p = &parent->rb_right;
  84. }
  85. rb_link_node(&new_buffer->rb_node, parent, p);
  86. rb_insert_color(&new_buffer->rb_node, &alloc->free_buffers);
  87. }
  88. static void binder_insert_allocated_buffer_locked(
  89. struct binder_alloc *alloc, struct binder_buffer *new_buffer)
  90. {
  91. struct rb_node **p = &alloc->allocated_buffers.rb_node;
  92. struct rb_node *parent = NULL;
  93. struct binder_buffer *buffer;
  94. BUG_ON(new_buffer->free);
  95. while (*p) {
  96. parent = *p;
  97. buffer = rb_entry(parent, struct binder_buffer, rb_node);
  98. BUG_ON(buffer->free);
  99. if (new_buffer->data < buffer->data)
  100. p = &parent->rb_left;
  101. else if (new_buffer->data > buffer->data)
  102. p = &parent->rb_right;
  103. else
  104. BUG();
  105. }
  106. rb_link_node(&new_buffer->rb_node, parent, p);
  107. rb_insert_color(&new_buffer->rb_node, &alloc->allocated_buffers);
  108. }
  109. static struct binder_buffer *binder_alloc_prepare_to_free_locked(
  110. struct binder_alloc *alloc,
  111. uintptr_t user_ptr)
  112. {
  113. struct rb_node *n = alloc->allocated_buffers.rb_node;
  114. struct binder_buffer *buffer;
  115. void *kern_ptr;
  116. kern_ptr = (void *)(user_ptr - alloc->user_buffer_offset);
  117. while (n) {
  118. buffer = rb_entry(n, struct binder_buffer, rb_node);
  119. BUG_ON(buffer->free);
  120. if (kern_ptr < buffer->data)
  121. n = n->rb_left;
  122. else if (kern_ptr > buffer->data)
  123. n = n->rb_right;
  124. else {
  125. /*
  126. * Guard against user threads attempting to
  127. * free the buffer twice
  128. */
  129. if (buffer->free_in_progress) {
  130. pr_err("%d:%d FREE_BUFFER u%016llx user freed buffer twice\n",
  131. alloc->pid, current->pid, (u64)user_ptr);
  132. return NULL;
  133. }
  134. buffer->free_in_progress = 1;
  135. return buffer;
  136. }
  137. }
  138. return NULL;
  139. }
  140. /**
  141. * binder_alloc_buffer_lookup() - get buffer given user ptr
  142. * @alloc: binder_alloc for this proc
  143. * @user_ptr: User pointer to buffer data
  144. *
  145. * Validate userspace pointer to buffer data and return buffer corresponding to
  146. * that user pointer. Search the rb tree for buffer that matches user data
  147. * pointer.
  148. *
  149. * Return: Pointer to buffer or NULL
  150. */
  151. struct binder_buffer *binder_alloc_prepare_to_free(struct binder_alloc *alloc,
  152. uintptr_t user_ptr)
  153. {
  154. struct binder_buffer *buffer;
  155. mutex_lock(&alloc->mutex);
  156. buffer = binder_alloc_prepare_to_free_locked(alloc, user_ptr);
  157. mutex_unlock(&alloc->mutex);
  158. return buffer;
  159. }
  160. static int binder_update_page_range(struct binder_alloc *alloc, int allocate,
  161. void *start, void *end,
  162. struct vm_area_struct *vma)
  163. {
  164. void *page_addr;
  165. unsigned long user_page_addr;
  166. struct binder_lru_page *page;
  167. struct mm_struct *mm = NULL;
  168. bool need_mm = false;
  169. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  170. "%d: %s pages %pK-%pK\n", alloc->pid,
  171. allocate ? "allocate" : "free", start, end);
  172. if (end <= start)
  173. return 0;
  174. trace_binder_update_page_range(alloc, allocate, start, end);
  175. if (allocate == 0)
  176. goto free_range;
  177. for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
  178. page = &alloc->pages[(page_addr - alloc->buffer) / PAGE_SIZE];
  179. if (!page->page_ptr) {
  180. need_mm = true;
  181. break;
  182. }
  183. }
  184. if (!vma && need_mm)
  185. mm = get_task_mm(alloc->tsk);
  186. if (mm) {
  187. down_write(&mm->mmap_sem);
  188. vma = alloc->vma;
  189. if (vma && mm != alloc->vma_vm_mm) {
  190. pr_err("%d: vma mm and task mm mismatch\n",
  191. alloc->pid);
  192. vma = NULL;
  193. }
  194. }
  195. if (!vma && need_mm) {
  196. pr_err("%d: binder_alloc_buf failed to map pages in userspace, no vma\n",
  197. alloc->pid);
  198. goto err_no_vma;
  199. }
  200. for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
  201. int ret;
  202. bool on_lru;
  203. size_t index;
  204. index = (page_addr - alloc->buffer) / PAGE_SIZE;
  205. page = &alloc->pages[index];
  206. if (page->page_ptr) {
  207. trace_binder_alloc_lru_start(alloc, index);
  208. on_lru = list_lru_del(&binder_alloc_lru, &page->lru);
  209. WARN_ON(!on_lru);
  210. trace_binder_alloc_lru_end(alloc, index);
  211. continue;
  212. }
  213. if (WARN_ON(!vma))
  214. goto err_page_ptr_cleared;
  215. trace_binder_alloc_page_start(alloc, index);
  216. page->page_ptr = alloc_page(GFP_KERNEL |
  217. __GFP_HIGHMEM |
  218. __GFP_ZERO);
  219. if (!page->page_ptr) {
  220. pr_err("%d: binder_alloc_buf failed for page at %pK\n",
  221. alloc->pid, page_addr);
  222. goto err_alloc_page_failed;
  223. }
  224. page->alloc = alloc;
  225. INIT_LIST_HEAD(&page->lru);
  226. ret = map_kernel_range_noflush((unsigned long)page_addr,
  227. PAGE_SIZE, PAGE_KERNEL,
  228. &page->page_ptr);
  229. flush_cache_vmap((unsigned long)page_addr,
  230. (unsigned long)page_addr + PAGE_SIZE);
  231. if (ret != 1) {
  232. pr_err("%d: binder_alloc_buf failed to map page at %pK in kernel\n",
  233. alloc->pid, page_addr);
  234. goto err_map_kernel_failed;
  235. }
  236. user_page_addr =
  237. (uintptr_t)page_addr + alloc->user_buffer_offset;
  238. ret = vm_insert_page(vma, user_page_addr, page[0].page_ptr);
  239. if (ret) {
  240. pr_err("%d: binder_alloc_buf failed to map page at %lx in userspace\n",
  241. alloc->pid, user_page_addr);
  242. goto err_vm_insert_page_failed;
  243. }
  244. trace_binder_alloc_page_end(alloc, index);
  245. /* vm_insert_page does not seem to increment the refcount */
  246. }
  247. if (mm) {
  248. up_write(&mm->mmap_sem);
  249. mmput(mm);
  250. }
  251. return 0;
  252. free_range:
  253. for (page_addr = end - PAGE_SIZE; page_addr >= start;
  254. page_addr -= PAGE_SIZE) {
  255. bool ret;
  256. size_t index;
  257. index = (page_addr - alloc->buffer) / PAGE_SIZE;
  258. page = &alloc->pages[index];
  259. trace_binder_free_lru_start(alloc, index);
  260. ret = list_lru_add(&binder_alloc_lru, &page->lru);
  261. WARN_ON(!ret);
  262. trace_binder_free_lru_end(alloc, index);
  263. continue;
  264. err_vm_insert_page_failed:
  265. unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
  266. err_map_kernel_failed:
  267. __free_page(page->page_ptr);
  268. page->page_ptr = NULL;
  269. err_alloc_page_failed:
  270. err_page_ptr_cleared:
  271. ;
  272. }
  273. err_no_vma:
  274. if (mm) {
  275. up_write(&mm->mmap_sem);
  276. mmput(mm);
  277. }
  278. return vma ? -ENOMEM : -ESRCH;
  279. }
  280. struct binder_buffer *binder_alloc_new_buf_locked(struct binder_alloc *alloc,
  281. size_t data_size,
  282. size_t offsets_size,
  283. size_t extra_buffers_size,
  284. int is_async)
  285. {
  286. struct rb_node *n = alloc->free_buffers.rb_node;
  287. struct binder_buffer *buffer;
  288. size_t buffer_size;
  289. struct rb_node *best_fit = NULL;
  290. void *has_page_addr;
  291. void *end_page_addr;
  292. size_t size, data_offsets_size;
  293. int ret;
  294. if (alloc->vma == NULL) {
  295. pr_err("%d: binder_alloc_buf, no vma\n",
  296. alloc->pid);
  297. return ERR_PTR(-ESRCH);
  298. }
  299. data_offsets_size = ALIGN(data_size, sizeof(void *)) +
  300. ALIGN(offsets_size, sizeof(void *));
  301. if (data_offsets_size < data_size || data_offsets_size < offsets_size) {
  302. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  303. "%d: got transaction with invalid size %zd-%zd\n",
  304. alloc->pid, data_size, offsets_size);
  305. return ERR_PTR(-EINVAL);
  306. }
  307. size = data_offsets_size + ALIGN(extra_buffers_size, sizeof(void *));
  308. if (size < data_offsets_size || size < extra_buffers_size) {
  309. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  310. "%d: got transaction with invalid extra_buffers_size %zd\n",
  311. alloc->pid, extra_buffers_size);
  312. return ERR_PTR(-EINVAL);
  313. }
  314. if (is_async &&
  315. alloc->free_async_space < size + sizeof(struct binder_buffer)) {
  316. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  317. "%d: binder_alloc_buf size %zd failed, no async space left\n",
  318. alloc->pid, size);
  319. return ERR_PTR(-ENOSPC);
  320. }
  321. /* Pad 0-size buffers so they get assigned unique addresses */
  322. size = max(size, sizeof(void *));
  323. while (n) {
  324. buffer = rb_entry(n, struct binder_buffer, rb_node);
  325. BUG_ON(!buffer->free);
  326. buffer_size = binder_alloc_buffer_size(alloc, buffer);
  327. if (size < buffer_size) {
  328. best_fit = n;
  329. n = n->rb_left;
  330. } else if (size > buffer_size)
  331. n = n->rb_right;
  332. else {
  333. best_fit = n;
  334. break;
  335. }
  336. }
  337. if (best_fit == NULL) {
  338. size_t allocated_buffers = 0;
  339. size_t largest_alloc_size = 0;
  340. size_t total_alloc_size = 0;
  341. size_t free_buffers = 0;
  342. size_t largest_free_size = 0;
  343. size_t total_free_size = 0;
  344. for (n = rb_first(&alloc->allocated_buffers); n != NULL;
  345. n = rb_next(n)) {
  346. buffer = rb_entry(n, struct binder_buffer, rb_node);
  347. buffer_size = binder_alloc_buffer_size(alloc, buffer);
  348. allocated_buffers++;
  349. total_alloc_size += buffer_size;
  350. if (buffer_size > largest_alloc_size)
  351. largest_alloc_size = buffer_size;
  352. }
  353. for (n = rb_first(&alloc->free_buffers); n != NULL;
  354. n = rb_next(n)) {
  355. buffer = rb_entry(n, struct binder_buffer, rb_node);
  356. buffer_size = binder_alloc_buffer_size(alloc, buffer);
  357. free_buffers++;
  358. total_free_size += buffer_size;
  359. if (buffer_size > largest_free_size)
  360. largest_free_size = buffer_size;
  361. }
  362. pr_err("%d: binder_alloc_buf size %zd failed, no address space\n",
  363. alloc->pid, size);
  364. pr_err("allocated: %zd (num: %zd largest: %zd), free: %zd (num: %zd largest: %zd)\n",
  365. total_alloc_size, allocated_buffers, largest_alloc_size,
  366. total_free_size, free_buffers, largest_free_size);
  367. return ERR_PTR(-ENOSPC);
  368. }
  369. if (n == NULL) {
  370. buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
  371. buffer_size = binder_alloc_buffer_size(alloc, buffer);
  372. }
  373. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  374. "%d: binder_alloc_buf size %zd got buffer %pK size %zd\n",
  375. alloc->pid, size, buffer, buffer_size);
  376. has_page_addr =
  377. (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK);
  378. WARN_ON(n && buffer_size != size);
  379. end_page_addr =
  380. (void *)PAGE_ALIGN((uintptr_t)buffer->data + size);
  381. if (end_page_addr > has_page_addr)
  382. end_page_addr = has_page_addr;
  383. ret = binder_update_page_range(alloc, 1,
  384. (void *)PAGE_ALIGN((uintptr_t)buffer->data), end_page_addr, NULL);
  385. if (ret)
  386. return ERR_PTR(ret);
  387. if (buffer_size != size) {
  388. struct binder_buffer *new_buffer;
  389. new_buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
  390. if (!new_buffer) {
  391. pr_err("%s: %d failed to alloc new buffer struct\n",
  392. __func__, alloc->pid);
  393. goto err_alloc_buf_struct_failed;
  394. }
  395. new_buffer->data = (u8 *)buffer->data + size;
  396. list_add(&new_buffer->entry, &buffer->entry);
  397. new_buffer->free = 1;
  398. binder_insert_free_buffer(alloc, new_buffer);
  399. }
  400. rb_erase(best_fit, &alloc->free_buffers);
  401. buffer->free = 0;
  402. buffer->free_in_progress = 0;
  403. binder_insert_allocated_buffer_locked(alloc, buffer);
  404. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  405. "%d: binder_alloc_buf size %zd got %pK\n",
  406. alloc->pid, size, buffer);
  407. buffer->data_size = data_size;
  408. buffer->offsets_size = offsets_size;
  409. buffer->async_transaction = is_async;
  410. buffer->extra_buffers_size = extra_buffers_size;
  411. if (is_async) {
  412. alloc->free_async_space -= size + sizeof(struct binder_buffer);
  413. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
  414. "%d: binder_alloc_buf size %zd async free %zd\n",
  415. alloc->pid, size, alloc->free_async_space);
  416. }
  417. return buffer;
  418. err_alloc_buf_struct_failed:
  419. binder_update_page_range(alloc, 0,
  420. (void *)PAGE_ALIGN((uintptr_t)buffer->data),
  421. end_page_addr, NULL);
  422. return ERR_PTR(-ENOMEM);
  423. }
  424. /**
  425. * binder_alloc_new_buf() - Allocate a new binder buffer
  426. * @alloc: binder_alloc for this proc
  427. * @data_size: size of user data buffer
  428. * @offsets_size: user specified buffer offset
  429. * @extra_buffers_size: size of extra space for meta-data (eg, security context)
  430. * @is_async: buffer for async transaction
  431. *
  432. * Allocate a new buffer given the requested sizes. Returns
  433. * the kernel version of the buffer pointer. The size allocated
  434. * is the sum of the three given sizes (each rounded up to
  435. * pointer-sized boundary)
  436. *
  437. * Return: The allocated buffer or %NULL if error
  438. */
  439. struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc,
  440. size_t data_size,
  441. size_t offsets_size,
  442. size_t extra_buffers_size,
  443. int is_async)
  444. {
  445. struct binder_buffer *buffer;
  446. mutex_lock(&alloc->mutex);
  447. buffer = binder_alloc_new_buf_locked(alloc, data_size, offsets_size,
  448. extra_buffers_size, is_async);
  449. mutex_unlock(&alloc->mutex);
  450. return buffer;
  451. }
  452. static void *buffer_start_page(struct binder_buffer *buffer)
  453. {
  454. return (void *)((uintptr_t)buffer->data & PAGE_MASK);
  455. }
  456. static void *prev_buffer_end_page(struct binder_buffer *buffer)
  457. {
  458. return (void *)(((uintptr_t)(buffer->data) - 1) & PAGE_MASK);
  459. }
  460. static void binder_delete_free_buffer(struct binder_alloc *alloc,
  461. struct binder_buffer *buffer)
  462. {
  463. struct binder_buffer *prev, *next = NULL;
  464. bool to_free = true;
  465. BUG_ON(alloc->buffers.next == &buffer->entry);
  466. prev = binder_buffer_prev(buffer);
  467. BUG_ON(!prev->free);
  468. if (prev_buffer_end_page(prev) == buffer_start_page(buffer)) {
  469. to_free = false;
  470. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  471. "%d: merge free, buffer %pK share page with %pK\n",
  472. alloc->pid, buffer->data, prev->data);
  473. }
  474. if (!list_is_last(&buffer->entry, &alloc->buffers)) {
  475. next = binder_buffer_next(buffer);
  476. if (buffer_start_page(next) == buffer_start_page(buffer)) {
  477. to_free = false;
  478. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  479. "%d: merge free, buffer %pK share page with %pK\n",
  480. alloc->pid,
  481. buffer->data,
  482. next->data);
  483. }
  484. }
  485. if (PAGE_ALIGNED(buffer->data)) {
  486. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  487. "%d: merge free, buffer start %pK is page aligned\n",
  488. alloc->pid, buffer->data);
  489. to_free = false;
  490. }
  491. if (to_free) {
  492. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  493. "%d: merge free, buffer %pK do not share page with %pK or %pK\n",
  494. alloc->pid, buffer->data,
  495. prev->data, next->data);
  496. binder_update_page_range(alloc, 0, buffer_start_page(buffer),
  497. buffer_start_page(buffer) + PAGE_SIZE,
  498. NULL);
  499. }
  500. list_del(&buffer->entry);
  501. kfree(buffer);
  502. }
  503. static void binder_free_buf_locked(struct binder_alloc *alloc,
  504. struct binder_buffer *buffer)
  505. {
  506. size_t size, buffer_size;
  507. buffer_size = binder_alloc_buffer_size(alloc, buffer);
  508. size = ALIGN(buffer->data_size, sizeof(void *)) +
  509. ALIGN(buffer->offsets_size, sizeof(void *)) +
  510. ALIGN(buffer->extra_buffers_size, sizeof(void *));
  511. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  512. "%d: binder_free_buf %pK size %zd buffer_size %zd\n",
  513. alloc->pid, buffer, size, buffer_size);
  514. BUG_ON(buffer->free);
  515. BUG_ON(size > buffer_size);
  516. BUG_ON(buffer->transaction != NULL);
  517. BUG_ON(buffer->data < alloc->buffer);
  518. BUG_ON(buffer->data > alloc->buffer + alloc->buffer_size);
  519. if (buffer->async_transaction) {
  520. alloc->free_async_space += size + sizeof(struct binder_buffer);
  521. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
  522. "%d: binder_free_buf size %zd async free %zd\n",
  523. alloc->pid, size, alloc->free_async_space);
  524. }
  525. binder_update_page_range(alloc, 0,
  526. (void *)PAGE_ALIGN((uintptr_t)buffer->data),
  527. (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK),
  528. NULL);
  529. rb_erase(&buffer->rb_node, &alloc->allocated_buffers);
  530. buffer->free = 1;
  531. if (!list_is_last(&buffer->entry, &alloc->buffers)) {
  532. struct binder_buffer *next = binder_buffer_next(buffer);
  533. if (next->free) {
  534. rb_erase(&next->rb_node, &alloc->free_buffers);
  535. binder_delete_free_buffer(alloc, next);
  536. }
  537. }
  538. if (alloc->buffers.next != &buffer->entry) {
  539. struct binder_buffer *prev = binder_buffer_prev(buffer);
  540. if (prev->free) {
  541. binder_delete_free_buffer(alloc, buffer);
  542. rb_erase(&prev->rb_node, &alloc->free_buffers);
  543. buffer = prev;
  544. }
  545. }
  546. binder_insert_free_buffer(alloc, buffer);
  547. }
  548. /**
  549. * binder_alloc_free_buf() - free a binder buffer
  550. * @alloc: binder_alloc for this proc
  551. * @buffer: kernel pointer to buffer
  552. *
  553. * Free the buffer allocated via binder_alloc_new_buffer()
  554. */
  555. void binder_alloc_free_buf(struct binder_alloc *alloc,
  556. struct binder_buffer *buffer)
  557. {
  558. mutex_lock(&alloc->mutex);
  559. binder_free_buf_locked(alloc, buffer);
  560. mutex_unlock(&alloc->mutex);
  561. }
  562. /**
  563. * binder_alloc_mmap_handler() - map virtual address space for proc
  564. * @alloc: alloc structure for this proc
  565. * @vma: vma passed to mmap()
  566. *
  567. * Called by binder_mmap() to initialize the space specified in
  568. * vma for allocating binder buffers
  569. *
  570. * Return:
  571. * 0 = success
  572. * -EBUSY = address space already mapped
  573. * -ENOMEM = failed to map memory to given address space
  574. */
  575. int binder_alloc_mmap_handler(struct binder_alloc *alloc,
  576. struct vm_area_struct *vma)
  577. {
  578. int ret;
  579. struct vm_struct *area;
  580. const char *failure_string;
  581. struct binder_buffer *buffer;
  582. mutex_lock(&binder_alloc_mmap_lock);
  583. if (alloc->buffer) {
  584. ret = -EBUSY;
  585. failure_string = "already mapped";
  586. goto err_already_mapped;
  587. }
  588. area = get_vm_area(vma->vm_end - vma->vm_start, VM_IOREMAP);
  589. if (area == NULL) {
  590. ret = -ENOMEM;
  591. failure_string = "get_vm_area";
  592. goto err_get_vm_area_failed;
  593. }
  594. alloc->buffer = area->addr;
  595. alloc->user_buffer_offset =
  596. vma->vm_start - (uintptr_t)alloc->buffer;
  597. mutex_unlock(&binder_alloc_mmap_lock);
  598. #ifdef CONFIG_CPU_CACHE_VIPT
  599. if (cache_is_vipt_aliasing()) {
  600. while (CACHE_COLOUR(
  601. (vma->vm_start ^ (uint32_t)alloc->buffer))) {
  602. pr_info("%s: %d %lx-%lx maps %pK bad alignment\n",
  603. __func__, alloc->pid, vma->vm_start,
  604. vma->vm_end, alloc->buffer);
  605. vma->vm_start += PAGE_SIZE;
  606. }
  607. }
  608. #endif
  609. alloc->pages = kzalloc(sizeof(alloc->pages[0]) *
  610. ((vma->vm_end - vma->vm_start) / PAGE_SIZE),
  611. GFP_KERNEL);
  612. if (alloc->pages == NULL) {
  613. ret = -ENOMEM;
  614. failure_string = "alloc page array";
  615. goto err_alloc_pages_failed;
  616. }
  617. alloc->buffer_size = vma->vm_end - vma->vm_start;
  618. buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
  619. if (!buffer) {
  620. ret = -ENOMEM;
  621. failure_string = "alloc buffer struct";
  622. goto err_alloc_buf_struct_failed;
  623. }
  624. buffer->data = alloc->buffer;
  625. list_add(&buffer->entry, &alloc->buffers);
  626. buffer->free = 1;
  627. binder_insert_free_buffer(alloc, buffer);
  628. alloc->free_async_space = alloc->buffer_size / 2;
  629. barrier();
  630. alloc->vma = vma;
  631. alloc->vma_vm_mm = vma->vm_mm;
  632. return 0;
  633. err_alloc_buf_struct_failed:
  634. kfree(alloc->pages);
  635. alloc->pages = NULL;
  636. err_alloc_pages_failed:
  637. mutex_lock(&binder_alloc_mmap_lock);
  638. vfree(alloc->buffer);
  639. alloc->buffer = NULL;
  640. err_get_vm_area_failed:
  641. err_already_mapped:
  642. mutex_unlock(&binder_alloc_mmap_lock);
  643. pr_err("%s: %d %lx-%lx %s failed %d\n", __func__,
  644. alloc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
  645. return ret;
  646. }
  647. void binder_alloc_deferred_release(struct binder_alloc *alloc)
  648. {
  649. struct rb_node *n;
  650. int buffers, page_count;
  651. struct binder_buffer *buffer;
  652. BUG_ON(alloc->vma);
  653. buffers = 0;
  654. mutex_lock(&alloc->mutex);
  655. while ((n = rb_first(&alloc->allocated_buffers))) {
  656. buffer = rb_entry(n, struct binder_buffer, rb_node);
  657. /* Transaction should already have been freed */
  658. BUG_ON(buffer->transaction);
  659. binder_free_buf_locked(alloc, buffer);
  660. buffers++;
  661. }
  662. while (!list_empty(&alloc->buffers)) {
  663. buffer = list_first_entry(&alloc->buffers,
  664. struct binder_buffer, entry);
  665. WARN_ON(!buffer->free);
  666. list_del(&buffer->entry);
  667. WARN_ON_ONCE(!list_empty(&alloc->buffers));
  668. kfree(buffer);
  669. }
  670. page_count = 0;
  671. if (alloc->pages) {
  672. int i;
  673. for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
  674. void *page_addr;
  675. bool on_lru;
  676. if (!alloc->pages[i].page_ptr)
  677. continue;
  678. on_lru = list_lru_del(&binder_alloc_lru,
  679. &alloc->pages[i].lru);
  680. page_addr = alloc->buffer + i * PAGE_SIZE;
  681. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  682. "%s: %d: page %d at %pK %s\n",
  683. __func__, alloc->pid, i, page_addr,
  684. on_lru ? "on lru" : "active");
  685. unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
  686. __free_page(alloc->pages[i].page_ptr);
  687. page_count++;
  688. }
  689. kfree(alloc->pages);
  690. vfree(alloc->buffer);
  691. }
  692. mutex_unlock(&alloc->mutex);
  693. binder_alloc_debug(BINDER_DEBUG_OPEN_CLOSE,
  694. "%s: %d buffers %d, pages %d\n",
  695. __func__, alloc->pid, buffers, page_count);
  696. }
  697. static void print_binder_buffer(struct seq_file *m, const char *prefix,
  698. struct binder_buffer *buffer)
  699. {
  700. seq_printf(m, "%s %d: %pK size %zd:%zd:%zd %s\n",
  701. prefix, buffer->debug_id, buffer->data,
  702. buffer->data_size, buffer->offsets_size,
  703. buffer->extra_buffers_size,
  704. buffer->transaction ? "active" : "delivered");
  705. }
  706. /**
  707. * binder_alloc_print_allocated() - print buffer info
  708. * @m: seq_file for output via seq_printf()
  709. * @alloc: binder_alloc for this proc
  710. *
  711. * Prints information about every buffer associated with
  712. * the binder_alloc state to the given seq_file
  713. */
  714. void binder_alloc_print_allocated(struct seq_file *m,
  715. struct binder_alloc *alloc)
  716. {
  717. struct rb_node *n;
  718. mutex_lock(&alloc->mutex);
  719. for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
  720. print_binder_buffer(m, " buffer",
  721. rb_entry(n, struct binder_buffer, rb_node));
  722. mutex_unlock(&alloc->mutex);
  723. }
  724. /**
  725. * binder_alloc_print_pages() - print page usage
  726. * @m: seq_file for output via seq_printf()
  727. * @alloc: binder_alloc for this proc
  728. */
  729. void binder_alloc_print_pages(struct seq_file *m,
  730. struct binder_alloc *alloc)
  731. {
  732. struct binder_lru_page *page;
  733. int i;
  734. int active = 0;
  735. int lru = 0;
  736. int free = 0;
  737. mutex_lock(&alloc->mutex);
  738. for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
  739. page = &alloc->pages[i];
  740. if (!page->page_ptr)
  741. free++;
  742. else if (list_empty(&page->lru))
  743. active++;
  744. else
  745. lru++;
  746. }
  747. mutex_unlock(&alloc->mutex);
  748. seq_printf(m, " pages: %d:%d:%d\n", active, lru, free);
  749. }
  750. /**
  751. * binder_alloc_get_allocated_count() - return count of buffers
  752. * @alloc: binder_alloc for this proc
  753. *
  754. * Return: count of allocated buffers
  755. */
  756. int binder_alloc_get_allocated_count(struct binder_alloc *alloc)
  757. {
  758. struct rb_node *n;
  759. int count = 0;
  760. mutex_lock(&alloc->mutex);
  761. for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
  762. count++;
  763. mutex_unlock(&alloc->mutex);
  764. return count;
  765. }
  766. /**
  767. * binder_alloc_vma_close() - invalidate address space
  768. * @alloc: binder_alloc for this proc
  769. *
  770. * Called from binder_vma_close() when releasing address space.
  771. * Clears alloc->vma to prevent new incoming transactions from
  772. * allocating more buffers.
  773. */
  774. void binder_alloc_vma_close(struct binder_alloc *alloc)
  775. {
  776. WRITE_ONCE(alloc->vma, NULL);
  777. WRITE_ONCE(alloc->vma_vm_mm, NULL);
  778. }
  779. /**
  780. * binder_alloc_free_page() - shrinker callback to free pages
  781. * @item: item to free
  782. * @lock: lock protecting the item
  783. * @cb_arg: callback argument
  784. *
  785. * Called from list_lru_walk() in binder_shrink_scan() to free
  786. * up pages when the system is under memory pressure.
  787. */
  788. enum lru_status binder_alloc_free_page(struct list_head *item,
  789. struct list_lru_one *lru,
  790. spinlock_t *lock,
  791. void *cb_arg)
  792. {
  793. struct mm_struct *mm = NULL;
  794. struct binder_lru_page *page = container_of(item,
  795. struct binder_lru_page,
  796. lru);
  797. struct binder_alloc *alloc;
  798. uintptr_t page_addr;
  799. size_t index;
  800. struct vm_area_struct *vma;
  801. alloc = page->alloc;
  802. if (!mutex_trylock(&alloc->mutex))
  803. goto err_get_alloc_mutex_failed;
  804. if (!page->page_ptr)
  805. goto err_page_already_freed;
  806. index = page - alloc->pages;
  807. page_addr = (uintptr_t)alloc->buffer + index * PAGE_SIZE;
  808. vma = alloc->vma;
  809. if (vma) {
  810. mm = get_task_mm(alloc->tsk);
  811. if (!mm)
  812. goto err_get_task_mm_failed;
  813. if (!down_write_trylock(&mm->mmap_sem))
  814. goto err_down_write_mmap_sem_failed;
  815. }
  816. list_lru_isolate(lru, item);
  817. spin_unlock(lock);
  818. if (vma) {
  819. trace_binder_unmap_user_start(alloc, index);
  820. zap_page_range(vma,
  821. page_addr + alloc->user_buffer_offset,
  822. PAGE_SIZE);
  823. trace_binder_unmap_user_end(alloc, index);
  824. up_write(&mm->mmap_sem);
  825. mmput(mm);
  826. }
  827. trace_binder_unmap_kernel_start(alloc, index);
  828. unmap_kernel_range(page_addr, PAGE_SIZE);
  829. __free_page(page->page_ptr);
  830. page->page_ptr = NULL;
  831. trace_binder_unmap_kernel_end(alloc, index);
  832. spin_lock(lock);
  833. mutex_unlock(&alloc->mutex);
  834. return LRU_REMOVED_RETRY;
  835. err_down_write_mmap_sem_failed:
  836. mmput_async(mm);
  837. err_get_task_mm_failed:
  838. err_page_already_freed:
  839. mutex_unlock(&alloc->mutex);
  840. err_get_alloc_mutex_failed:
  841. return LRU_SKIP;
  842. }
  843. static unsigned long
  844. binder_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
  845. {
  846. unsigned long ret = list_lru_count(&binder_alloc_lru);
  847. return ret;
  848. }
  849. static unsigned long
  850. binder_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
  851. {
  852. unsigned long ret;
  853. ret = list_lru_walk(&binder_alloc_lru, binder_alloc_free_page,
  854. NULL, sc->nr_to_scan);
  855. return ret;
  856. }
  857. struct shrinker binder_shrinker = {
  858. .count_objects = binder_shrink_count,
  859. .scan_objects = binder_shrink_scan,
  860. .seeks = DEFAULT_SEEKS,
  861. };
  862. /**
  863. * binder_alloc_init() - called by binder_open() for per-proc initialization
  864. * @alloc: binder_alloc for this proc
  865. *
  866. * Called from binder_open() to initialize binder_alloc fields for
  867. * new binder proc
  868. */
  869. void binder_alloc_init(struct binder_alloc *alloc)
  870. {
  871. alloc->tsk = current->group_leader;
  872. alloc->pid = current->group_leader->pid;
  873. mutex_init(&alloc->mutex);
  874. INIT_LIST_HEAD(&alloc->buffers);
  875. }
  876. void binder_alloc_shrinker_init(void)
  877. {
  878. list_lru_init(&binder_alloc_lru);
  879. register_shrinker(&binder_shrinker);
  880. }