binder_alloc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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 "binder_alloc.h"
  29. #include "binder_trace.h"
  30. static DEFINE_MUTEX(binder_alloc_mmap_lock);
  31. enum {
  32. BINDER_DEBUG_OPEN_CLOSE = 1U << 1,
  33. BINDER_DEBUG_BUFFER_ALLOC = 1U << 2,
  34. BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 3,
  35. };
  36. static uint32_t binder_alloc_debug_mask;
  37. module_param_named(debug_mask, binder_alloc_debug_mask,
  38. uint, 0644);
  39. #define binder_alloc_debug(mask, x...) \
  40. do { \
  41. if (binder_alloc_debug_mask & mask) \
  42. pr_info(x); \
  43. } while (0)
  44. static size_t binder_alloc_buffer_size(struct binder_alloc *alloc,
  45. struct binder_buffer *buffer)
  46. {
  47. if (list_is_last(&buffer->entry, &alloc->buffers))
  48. return alloc->buffer +
  49. alloc->buffer_size - (void *)buffer->data;
  50. return (size_t)list_entry(buffer->entry.next,
  51. struct binder_buffer, entry) - (size_t)buffer->data;
  52. }
  53. static void binder_insert_free_buffer(struct binder_alloc *alloc,
  54. struct binder_buffer *new_buffer)
  55. {
  56. struct rb_node **p = &alloc->free_buffers.rb_node;
  57. struct rb_node *parent = NULL;
  58. struct binder_buffer *buffer;
  59. size_t buffer_size;
  60. size_t new_buffer_size;
  61. BUG_ON(!new_buffer->free);
  62. new_buffer_size = binder_alloc_buffer_size(alloc, new_buffer);
  63. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  64. "%d: add free buffer, size %zd, at %pK\n",
  65. alloc->pid, new_buffer_size, new_buffer);
  66. while (*p) {
  67. parent = *p;
  68. buffer = rb_entry(parent, struct binder_buffer, rb_node);
  69. BUG_ON(!buffer->free);
  70. buffer_size = binder_alloc_buffer_size(alloc, buffer);
  71. if (new_buffer_size < buffer_size)
  72. p = &parent->rb_left;
  73. else
  74. p = &parent->rb_right;
  75. }
  76. rb_link_node(&new_buffer->rb_node, parent, p);
  77. rb_insert_color(&new_buffer->rb_node, &alloc->free_buffers);
  78. }
  79. static void binder_insert_allocated_buffer_locked(
  80. struct binder_alloc *alloc, struct binder_buffer *new_buffer)
  81. {
  82. struct rb_node **p = &alloc->allocated_buffers.rb_node;
  83. struct rb_node *parent = NULL;
  84. struct binder_buffer *buffer;
  85. BUG_ON(new_buffer->free);
  86. while (*p) {
  87. parent = *p;
  88. buffer = rb_entry(parent, struct binder_buffer, rb_node);
  89. BUG_ON(buffer->free);
  90. if (new_buffer < buffer)
  91. p = &parent->rb_left;
  92. else if (new_buffer > buffer)
  93. p = &parent->rb_right;
  94. else
  95. BUG();
  96. }
  97. rb_link_node(&new_buffer->rb_node, parent, p);
  98. rb_insert_color(&new_buffer->rb_node, &alloc->allocated_buffers);
  99. }
  100. static struct binder_buffer *binder_alloc_prepare_to_free_locked(
  101. struct binder_alloc *alloc,
  102. uintptr_t user_ptr)
  103. {
  104. struct rb_node *n = alloc->allocated_buffers.rb_node;
  105. struct binder_buffer *buffer;
  106. struct binder_buffer *kern_ptr;
  107. kern_ptr = (struct binder_buffer *)(user_ptr - alloc->user_buffer_offset
  108. - offsetof(struct binder_buffer, data));
  109. while (n) {
  110. buffer = rb_entry(n, struct binder_buffer, rb_node);
  111. BUG_ON(buffer->free);
  112. if (kern_ptr < buffer)
  113. n = n->rb_left;
  114. else if (kern_ptr > buffer)
  115. n = n->rb_right;
  116. else {
  117. /*
  118. * Guard against user threads attempting to
  119. * free the buffer twice
  120. */
  121. if (buffer->free_in_progress) {
  122. pr_err("%d:%d FREE_BUFFER u%016llx user freed buffer twice\n",
  123. alloc->pid, current->pid, (u64)user_ptr);
  124. return NULL;
  125. }
  126. buffer->free_in_progress = 1;
  127. return buffer;
  128. }
  129. }
  130. return NULL;
  131. }
  132. /**
  133. * binder_alloc_buffer_lookup() - get buffer given user ptr
  134. * @alloc: binder_alloc for this proc
  135. * @user_ptr: User pointer to buffer data
  136. *
  137. * Validate userspace pointer to buffer data and return buffer corresponding to
  138. * that user pointer. Search the rb tree for buffer that matches user data
  139. * pointer.
  140. *
  141. * Return: Pointer to buffer or NULL
  142. */
  143. struct binder_buffer *binder_alloc_prepare_to_free(struct binder_alloc *alloc,
  144. uintptr_t user_ptr)
  145. {
  146. struct binder_buffer *buffer;
  147. mutex_lock(&alloc->mutex);
  148. buffer = binder_alloc_prepare_to_free_locked(alloc, user_ptr);
  149. mutex_unlock(&alloc->mutex);
  150. return buffer;
  151. }
  152. static int binder_update_page_range(struct binder_alloc *alloc, int allocate,
  153. void *start, void *end,
  154. struct vm_area_struct *vma)
  155. {
  156. void *page_addr;
  157. unsigned long user_page_addr;
  158. struct page **page;
  159. struct mm_struct *mm;
  160. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  161. "%d: %s pages %pK-%pK\n", alloc->pid,
  162. allocate ? "allocate" : "free", start, end);
  163. if (end <= start)
  164. return 0;
  165. trace_binder_update_page_range(alloc, allocate, start, end);
  166. if (vma)
  167. mm = NULL;
  168. else
  169. mm = get_task_mm(alloc->tsk);
  170. if (mm) {
  171. down_write(&mm->mmap_sem);
  172. vma = alloc->vma;
  173. if (vma && mm != alloc->vma_vm_mm) {
  174. pr_err("%d: vma mm and task mm mismatch\n",
  175. alloc->pid);
  176. vma = NULL;
  177. }
  178. }
  179. if (allocate == 0)
  180. goto free_range;
  181. if (vma == NULL) {
  182. pr_err("%d: binder_alloc_buf failed to map pages in userspace, no vma\n",
  183. alloc->pid);
  184. goto err_no_vma;
  185. }
  186. for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
  187. int ret;
  188. page = &alloc->pages[(page_addr - alloc->buffer) / PAGE_SIZE];
  189. BUG_ON(*page);
  190. *page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
  191. if (*page == NULL) {
  192. pr_err("%d: binder_alloc_buf failed for page at %pK\n",
  193. alloc->pid, page_addr);
  194. goto err_alloc_page_failed;
  195. }
  196. ret = map_kernel_range_noflush((unsigned long)page_addr,
  197. PAGE_SIZE, PAGE_KERNEL, page);
  198. flush_cache_vmap((unsigned long)page_addr,
  199. (unsigned long)page_addr + PAGE_SIZE);
  200. if (ret != 1) {
  201. pr_err("%d: binder_alloc_buf failed to map page at %pK in kernel\n",
  202. alloc->pid, page_addr);
  203. goto err_map_kernel_failed;
  204. }
  205. user_page_addr =
  206. (uintptr_t)page_addr + alloc->user_buffer_offset;
  207. ret = vm_insert_page(vma, user_page_addr, page[0]);
  208. if (ret) {
  209. pr_err("%d: binder_alloc_buf failed to map page at %lx in userspace\n",
  210. alloc->pid, user_page_addr);
  211. goto err_vm_insert_page_failed;
  212. }
  213. /* vm_insert_page does not seem to increment the refcount */
  214. }
  215. if (mm) {
  216. up_write(&mm->mmap_sem);
  217. mmput(mm);
  218. }
  219. return 0;
  220. free_range:
  221. for (page_addr = end - PAGE_SIZE; page_addr >= start;
  222. page_addr -= PAGE_SIZE) {
  223. page = &alloc->pages[(page_addr - alloc->buffer) / PAGE_SIZE];
  224. if (vma)
  225. zap_page_range(vma, (uintptr_t)page_addr +
  226. alloc->user_buffer_offset, PAGE_SIZE);
  227. err_vm_insert_page_failed:
  228. unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
  229. err_map_kernel_failed:
  230. __free_page(*page);
  231. *page = NULL;
  232. err_alloc_page_failed:
  233. ;
  234. }
  235. err_no_vma:
  236. if (mm) {
  237. up_write(&mm->mmap_sem);
  238. mmput(mm);
  239. }
  240. return vma ? -ENOMEM : -ESRCH;
  241. }
  242. struct binder_buffer *binder_alloc_new_buf_locked(struct binder_alloc *alloc,
  243. size_t data_size,
  244. size_t offsets_size,
  245. size_t extra_buffers_size,
  246. int is_async)
  247. {
  248. struct rb_node *n = alloc->free_buffers.rb_node;
  249. struct binder_buffer *buffer;
  250. size_t buffer_size;
  251. struct rb_node *best_fit = NULL;
  252. void *has_page_addr;
  253. void *end_page_addr;
  254. size_t size, data_offsets_size;
  255. int ret;
  256. if (alloc->vma == NULL) {
  257. pr_err("%d: binder_alloc_buf, no vma\n",
  258. alloc->pid);
  259. return ERR_PTR(-ESRCH);
  260. }
  261. data_offsets_size = ALIGN(data_size, sizeof(void *)) +
  262. ALIGN(offsets_size, sizeof(void *));
  263. if (data_offsets_size < data_size || data_offsets_size < offsets_size) {
  264. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  265. "%d: got transaction with invalid size %zd-%zd\n",
  266. alloc->pid, data_size, offsets_size);
  267. return ERR_PTR(-EINVAL);
  268. }
  269. size = data_offsets_size + ALIGN(extra_buffers_size, sizeof(void *));
  270. if (size < data_offsets_size || size < extra_buffers_size) {
  271. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  272. "%d: got transaction with invalid extra_buffers_size %zd\n",
  273. alloc->pid, extra_buffers_size);
  274. return ERR_PTR(-EINVAL);
  275. }
  276. if (is_async &&
  277. alloc->free_async_space < size + sizeof(struct binder_buffer)) {
  278. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  279. "%d: binder_alloc_buf size %zd failed, no async space left\n",
  280. alloc->pid, size);
  281. return ERR_PTR(-ENOSPC);
  282. }
  283. while (n) {
  284. buffer = rb_entry(n, struct binder_buffer, rb_node);
  285. BUG_ON(!buffer->free);
  286. buffer_size = binder_alloc_buffer_size(alloc, buffer);
  287. if (size < buffer_size) {
  288. best_fit = n;
  289. n = n->rb_left;
  290. } else if (size > buffer_size)
  291. n = n->rb_right;
  292. else {
  293. best_fit = n;
  294. break;
  295. }
  296. }
  297. if (best_fit == NULL) {
  298. pr_err("%d: binder_alloc_buf size %zd failed, no address space\n",
  299. alloc->pid, size);
  300. return ERR_PTR(-ENOSPC);
  301. }
  302. if (n == NULL) {
  303. buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
  304. buffer_size = binder_alloc_buffer_size(alloc, buffer);
  305. }
  306. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  307. "%d: binder_alloc_buf size %zd got buffer %pK size %zd\n",
  308. alloc->pid, size, buffer, buffer_size);
  309. has_page_addr =
  310. (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK);
  311. if (n == NULL) {
  312. if (size + sizeof(struct binder_buffer) + 4 >= buffer_size)
  313. buffer_size = size; /* no room for other buffers */
  314. else
  315. buffer_size = size + sizeof(struct binder_buffer);
  316. }
  317. end_page_addr =
  318. (void *)PAGE_ALIGN((uintptr_t)buffer->data + buffer_size);
  319. if (end_page_addr > has_page_addr)
  320. end_page_addr = has_page_addr;
  321. ret = binder_update_page_range(alloc, 1,
  322. (void *)PAGE_ALIGN((uintptr_t)buffer->data), end_page_addr, NULL);
  323. if (ret)
  324. return ERR_PTR(ret);
  325. rb_erase(best_fit, &alloc->free_buffers);
  326. buffer->free = 0;
  327. buffer->free_in_progress = 0;
  328. binder_insert_allocated_buffer_locked(alloc, buffer);
  329. if (buffer_size != size) {
  330. struct binder_buffer *new_buffer = (void *)buffer->data + size;
  331. list_add(&new_buffer->entry, &buffer->entry);
  332. new_buffer->free = 1;
  333. binder_insert_free_buffer(alloc, new_buffer);
  334. }
  335. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  336. "%d: binder_alloc_buf size %zd got %pK\n",
  337. alloc->pid, size, buffer);
  338. buffer->data_size = data_size;
  339. buffer->offsets_size = offsets_size;
  340. buffer->async_transaction = is_async;
  341. buffer->extra_buffers_size = extra_buffers_size;
  342. if (is_async) {
  343. alloc->free_async_space -= size + sizeof(struct binder_buffer);
  344. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
  345. "%d: binder_alloc_buf size %zd async free %zd\n",
  346. alloc->pid, size, alloc->free_async_space);
  347. }
  348. return buffer;
  349. }
  350. /**
  351. * binder_alloc_new_buf() - Allocate a new binder buffer
  352. * @alloc: binder_alloc for this proc
  353. * @data_size: size of user data buffer
  354. * @offsets_size: user specified buffer offset
  355. * @extra_buffers_size: size of extra space for meta-data (eg, security context)
  356. * @is_async: buffer for async transaction
  357. *
  358. * Allocate a new buffer given the requested sizes. Returns
  359. * the kernel version of the buffer pointer. The size allocated
  360. * is the sum of the three given sizes (each rounded up to
  361. * pointer-sized boundary)
  362. *
  363. * Return: The allocated buffer or %NULL if error
  364. */
  365. struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc,
  366. size_t data_size,
  367. size_t offsets_size,
  368. size_t extra_buffers_size,
  369. int is_async)
  370. {
  371. struct binder_buffer *buffer;
  372. mutex_lock(&alloc->mutex);
  373. buffer = binder_alloc_new_buf_locked(alloc, data_size, offsets_size,
  374. extra_buffers_size, is_async);
  375. mutex_unlock(&alloc->mutex);
  376. return buffer;
  377. }
  378. static void *buffer_start_page(struct binder_buffer *buffer)
  379. {
  380. return (void *)((uintptr_t)buffer & PAGE_MASK);
  381. }
  382. static void *buffer_end_page(struct binder_buffer *buffer)
  383. {
  384. return (void *)(((uintptr_t)(buffer + 1) - 1) & PAGE_MASK);
  385. }
  386. static void binder_delete_free_buffer(struct binder_alloc *alloc,
  387. struct binder_buffer *buffer)
  388. {
  389. struct binder_buffer *prev, *next = NULL;
  390. int free_page_end = 1;
  391. int free_page_start = 1;
  392. BUG_ON(alloc->buffers.next == &buffer->entry);
  393. prev = list_entry(buffer->entry.prev, struct binder_buffer, entry);
  394. BUG_ON(!prev->free);
  395. if (buffer_end_page(prev) == buffer_start_page(buffer)) {
  396. free_page_start = 0;
  397. if (buffer_end_page(prev) == buffer_end_page(buffer))
  398. free_page_end = 0;
  399. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  400. "%d: merge free, buffer %pK share page with %pK\n",
  401. alloc->pid, buffer, prev);
  402. }
  403. if (!list_is_last(&buffer->entry, &alloc->buffers)) {
  404. next = list_entry(buffer->entry.next,
  405. struct binder_buffer, entry);
  406. if (buffer_start_page(next) == buffer_end_page(buffer)) {
  407. free_page_end = 0;
  408. if (buffer_start_page(next) ==
  409. buffer_start_page(buffer))
  410. free_page_start = 0;
  411. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  412. "%d: merge free, buffer %pK share page with %pK\n",
  413. alloc->pid, buffer, prev);
  414. }
  415. }
  416. list_del(&buffer->entry);
  417. if (free_page_start || free_page_end) {
  418. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  419. "%d: merge free, buffer %pK do not share page%s%s with %pK or %pK\n",
  420. alloc->pid, buffer, free_page_start ? "" : " end",
  421. free_page_end ? "" : " start", prev, next);
  422. binder_update_page_range(alloc, 0, free_page_start ?
  423. buffer_start_page(buffer) : buffer_end_page(buffer),
  424. (free_page_end ? buffer_end_page(buffer) :
  425. buffer_start_page(buffer)) + PAGE_SIZE, NULL);
  426. }
  427. }
  428. static void binder_free_buf_locked(struct binder_alloc *alloc,
  429. struct binder_buffer *buffer)
  430. {
  431. size_t size, buffer_size;
  432. buffer_size = binder_alloc_buffer_size(alloc, buffer);
  433. size = ALIGN(buffer->data_size, sizeof(void *)) +
  434. ALIGN(buffer->offsets_size, sizeof(void *)) +
  435. ALIGN(buffer->extra_buffers_size, sizeof(void *));
  436. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  437. "%d: binder_free_buf %pK size %zd buffer_size %zd\n",
  438. alloc->pid, buffer, size, buffer_size);
  439. BUG_ON(buffer->free);
  440. BUG_ON(size > buffer_size);
  441. BUG_ON(buffer->transaction != NULL);
  442. BUG_ON((void *)buffer < alloc->buffer);
  443. BUG_ON((void *)buffer > alloc->buffer + alloc->buffer_size);
  444. if (buffer->async_transaction) {
  445. alloc->free_async_space += size + sizeof(struct binder_buffer);
  446. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
  447. "%d: binder_free_buf size %zd async free %zd\n",
  448. alloc->pid, size, alloc->free_async_space);
  449. }
  450. binder_update_page_range(alloc, 0,
  451. (void *)PAGE_ALIGN((uintptr_t)buffer->data),
  452. (void *)(((uintptr_t)buffer->data + buffer_size) & PAGE_MASK),
  453. NULL);
  454. rb_erase(&buffer->rb_node, &alloc->allocated_buffers);
  455. buffer->free = 1;
  456. if (!list_is_last(&buffer->entry, &alloc->buffers)) {
  457. struct binder_buffer *next = list_entry(buffer->entry.next,
  458. struct binder_buffer, entry);
  459. if (next->free) {
  460. rb_erase(&next->rb_node, &alloc->free_buffers);
  461. binder_delete_free_buffer(alloc, next);
  462. }
  463. }
  464. if (alloc->buffers.next != &buffer->entry) {
  465. struct binder_buffer *prev = list_entry(buffer->entry.prev,
  466. struct binder_buffer, entry);
  467. if (prev->free) {
  468. binder_delete_free_buffer(alloc, buffer);
  469. rb_erase(&prev->rb_node, &alloc->free_buffers);
  470. buffer = prev;
  471. }
  472. }
  473. binder_insert_free_buffer(alloc, buffer);
  474. }
  475. /**
  476. * binder_alloc_free_buf() - free a binder buffer
  477. * @alloc: binder_alloc for this proc
  478. * @buffer: kernel pointer to buffer
  479. *
  480. * Free the buffer allocated via binder_alloc_new_buffer()
  481. */
  482. void binder_alloc_free_buf(struct binder_alloc *alloc,
  483. struct binder_buffer *buffer)
  484. {
  485. mutex_lock(&alloc->mutex);
  486. binder_free_buf_locked(alloc, buffer);
  487. mutex_unlock(&alloc->mutex);
  488. }
  489. /**
  490. * binder_alloc_mmap_handler() - map virtual address space for proc
  491. * @alloc: alloc structure for this proc
  492. * @vma: vma passed to mmap()
  493. *
  494. * Called by binder_mmap() to initialize the space specified in
  495. * vma for allocating binder buffers
  496. *
  497. * Return:
  498. * 0 = success
  499. * -EBUSY = address space already mapped
  500. * -ENOMEM = failed to map memory to given address space
  501. */
  502. int binder_alloc_mmap_handler(struct binder_alloc *alloc,
  503. struct vm_area_struct *vma)
  504. {
  505. int ret;
  506. struct vm_struct *area;
  507. const char *failure_string;
  508. struct binder_buffer *buffer;
  509. mutex_lock(&binder_alloc_mmap_lock);
  510. if (alloc->buffer) {
  511. ret = -EBUSY;
  512. failure_string = "already mapped";
  513. goto err_already_mapped;
  514. }
  515. area = get_vm_area(vma->vm_end - vma->vm_start, VM_IOREMAP);
  516. if (area == NULL) {
  517. ret = -ENOMEM;
  518. failure_string = "get_vm_area";
  519. goto err_get_vm_area_failed;
  520. }
  521. alloc->buffer = area->addr;
  522. alloc->user_buffer_offset =
  523. vma->vm_start - (uintptr_t)alloc->buffer;
  524. mutex_unlock(&binder_alloc_mmap_lock);
  525. #ifdef CONFIG_CPU_CACHE_VIPT
  526. if (cache_is_vipt_aliasing()) {
  527. while (CACHE_COLOUR(
  528. (vma->vm_start ^ (uint32_t)alloc->buffer))) {
  529. pr_info("%s: %d %lx-%lx maps %pK bad alignment\n",
  530. __func__, alloc->pid, vma->vm_start,
  531. vma->vm_end, alloc->buffer);
  532. vma->vm_start += PAGE_SIZE;
  533. }
  534. }
  535. #endif
  536. alloc->pages = kzalloc(sizeof(alloc->pages[0]) *
  537. ((vma->vm_end - vma->vm_start) / PAGE_SIZE),
  538. GFP_KERNEL);
  539. if (alloc->pages == NULL) {
  540. ret = -ENOMEM;
  541. failure_string = "alloc page array";
  542. goto err_alloc_pages_failed;
  543. }
  544. alloc->buffer_size = vma->vm_end - vma->vm_start;
  545. if (binder_update_page_range(alloc, 1, alloc->buffer,
  546. alloc->buffer + PAGE_SIZE, vma)) {
  547. ret = -ENOMEM;
  548. failure_string = "alloc small buf";
  549. goto err_alloc_small_buf_failed;
  550. }
  551. buffer = alloc->buffer;
  552. INIT_LIST_HEAD(&alloc->buffers);
  553. list_add(&buffer->entry, &alloc->buffers);
  554. buffer->free = 1;
  555. binder_insert_free_buffer(alloc, buffer);
  556. alloc->free_async_space = alloc->buffer_size / 2;
  557. barrier();
  558. alloc->vma = vma;
  559. alloc->vma_vm_mm = vma->vm_mm;
  560. return 0;
  561. err_alloc_small_buf_failed:
  562. kfree(alloc->pages);
  563. alloc->pages = NULL;
  564. err_alloc_pages_failed:
  565. mutex_lock(&binder_alloc_mmap_lock);
  566. vfree(alloc->buffer);
  567. alloc->buffer = NULL;
  568. err_get_vm_area_failed:
  569. err_already_mapped:
  570. mutex_unlock(&binder_alloc_mmap_lock);
  571. pr_err("%s: %d %lx-%lx %s failed %d\n", __func__,
  572. alloc->pid, vma->vm_start, vma->vm_end, failure_string, ret);
  573. return ret;
  574. }
  575. void binder_alloc_deferred_release(struct binder_alloc *alloc)
  576. {
  577. struct rb_node *n;
  578. int buffers, page_count;
  579. BUG_ON(alloc->vma);
  580. buffers = 0;
  581. mutex_lock(&alloc->mutex);
  582. while ((n = rb_first(&alloc->allocated_buffers))) {
  583. struct binder_buffer *buffer;
  584. buffer = rb_entry(n, struct binder_buffer, rb_node);
  585. /* Transaction should already have been freed */
  586. BUG_ON(buffer->transaction);
  587. binder_free_buf_locked(alloc, buffer);
  588. buffers++;
  589. }
  590. page_count = 0;
  591. if (alloc->pages) {
  592. int i;
  593. for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
  594. void *page_addr;
  595. if (!alloc->pages[i])
  596. continue;
  597. page_addr = alloc->buffer + i * PAGE_SIZE;
  598. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  599. "%s: %d: page %d at %pK not freed\n",
  600. __func__, alloc->pid, i, page_addr);
  601. unmap_kernel_range((unsigned long)page_addr, PAGE_SIZE);
  602. __free_page(alloc->pages[i]);
  603. page_count++;
  604. }
  605. kfree(alloc->pages);
  606. vfree(alloc->buffer);
  607. }
  608. mutex_unlock(&alloc->mutex);
  609. binder_alloc_debug(BINDER_DEBUG_OPEN_CLOSE,
  610. "%s: %d buffers %d, pages %d\n",
  611. __func__, alloc->pid, buffers, page_count);
  612. }
  613. static void print_binder_buffer(struct seq_file *m, const char *prefix,
  614. struct binder_buffer *buffer)
  615. {
  616. seq_printf(m, "%s %d: %pK size %zd:%zd %s\n",
  617. prefix, buffer->debug_id, buffer->data,
  618. buffer->data_size, buffer->offsets_size,
  619. buffer->transaction ? "active" : "delivered");
  620. }
  621. /**
  622. * binder_alloc_print_allocated() - print buffer info
  623. * @m: seq_file for output via seq_printf()
  624. * @alloc: binder_alloc for this proc
  625. *
  626. * Prints information about every buffer associated with
  627. * the binder_alloc state to the given seq_file
  628. */
  629. void binder_alloc_print_allocated(struct seq_file *m,
  630. struct binder_alloc *alloc)
  631. {
  632. struct rb_node *n;
  633. mutex_lock(&alloc->mutex);
  634. for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
  635. print_binder_buffer(m, " buffer",
  636. rb_entry(n, struct binder_buffer, rb_node));
  637. mutex_unlock(&alloc->mutex);
  638. }
  639. /**
  640. * binder_alloc_get_allocated_count() - return count of buffers
  641. * @alloc: binder_alloc for this proc
  642. *
  643. * Return: count of allocated buffers
  644. */
  645. int binder_alloc_get_allocated_count(struct binder_alloc *alloc)
  646. {
  647. struct rb_node *n;
  648. int count = 0;
  649. mutex_lock(&alloc->mutex);
  650. for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
  651. count++;
  652. mutex_unlock(&alloc->mutex);
  653. return count;
  654. }
  655. /**
  656. * binder_alloc_vma_close() - invalidate address space
  657. * @alloc: binder_alloc for this proc
  658. *
  659. * Called from binder_vma_close() when releasing address space.
  660. * Clears alloc->vma to prevent new incoming transactions from
  661. * allocating more buffers.
  662. */
  663. void binder_alloc_vma_close(struct binder_alloc *alloc)
  664. {
  665. WRITE_ONCE(alloc->vma, NULL);
  666. WRITE_ONCE(alloc->vma_vm_mm, NULL);
  667. }
  668. /**
  669. * binder_alloc_init() - called by binder_open() for per-proc initialization
  670. * @alloc: binder_alloc for this proc
  671. *
  672. * Called from binder_open() to initialize binder_alloc fields for
  673. * new binder proc
  674. */
  675. void binder_alloc_init(struct binder_alloc *alloc)
  676. {
  677. alloc->tsk = current->group_leader;
  678. alloc->pid = current->group_leader->pid;
  679. mutex_init(&alloc->mutex);
  680. }