binder_alloc.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* binder_alloc.c
  3. *
  4. * Android IPC Subsystem
  5. *
  6. * Copyright (C) 2007-2017 Google, Inc.
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/list.h>
  10. #include <linux/sched/mm.h>
  11. #include <linux/module.h>
  12. #include <linux/rtmutex.h>
  13. #include <linux/rbtree.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/slab.h>
  17. #include <linux/sched.h>
  18. #include <linux/list_lru.h>
  19. #include <linux/ratelimit.h>
  20. #include <asm/cacheflush.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/highmem.h>
  23. #include <linux/sizes.h>
  24. #include "binder_alloc.h"
  25. #include "binder_trace.h"
  26. struct list_lru binder_freelist;
  27. static DEFINE_MUTEX(binder_alloc_mmap_lock);
  28. enum {
  29. BINDER_DEBUG_USER_ERROR = 1U << 0,
  30. BINDER_DEBUG_OPEN_CLOSE = 1U << 1,
  31. BINDER_DEBUG_BUFFER_ALLOC = 1U << 2,
  32. BINDER_DEBUG_BUFFER_ALLOC_ASYNC = 1U << 3,
  33. };
  34. static uint32_t binder_alloc_debug_mask = BINDER_DEBUG_USER_ERROR;
  35. module_param_named(debug_mask, binder_alloc_debug_mask,
  36. uint, 0644);
  37. #define binder_alloc_debug(mask, x...) \
  38. do { \
  39. if (binder_alloc_debug_mask & mask) \
  40. pr_info_ratelimited(x); \
  41. } while (0)
  42. static struct binder_buffer *binder_buffer_next(struct binder_buffer *buffer)
  43. {
  44. return list_entry(buffer->entry.next, struct binder_buffer, entry);
  45. }
  46. static struct binder_buffer *binder_buffer_prev(struct binder_buffer *buffer)
  47. {
  48. return list_entry(buffer->entry.prev, struct binder_buffer, entry);
  49. }
  50. static size_t binder_alloc_buffer_size(struct binder_alloc *alloc,
  51. struct binder_buffer *buffer)
  52. {
  53. if (list_is_last(&buffer->entry, &alloc->buffers))
  54. return alloc->buffer + alloc->buffer_size - buffer->user_data;
  55. return binder_buffer_next(buffer)->user_data - buffer->user_data;
  56. }
  57. static void binder_insert_free_buffer(struct binder_alloc *alloc,
  58. struct binder_buffer *new_buffer)
  59. {
  60. struct rb_node **p = &alloc->free_buffers.rb_node;
  61. struct rb_node *parent = NULL;
  62. struct binder_buffer *buffer;
  63. size_t buffer_size;
  64. size_t new_buffer_size;
  65. BUG_ON(!new_buffer->free);
  66. new_buffer_size = binder_alloc_buffer_size(alloc, new_buffer);
  67. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  68. "%d: add free buffer, size %zd, at %pK\n",
  69. alloc->pid, new_buffer_size, new_buffer);
  70. while (*p) {
  71. parent = *p;
  72. buffer = rb_entry(parent, struct binder_buffer, rb_node);
  73. BUG_ON(!buffer->free);
  74. buffer_size = binder_alloc_buffer_size(alloc, buffer);
  75. if (new_buffer_size < buffer_size)
  76. p = &parent->rb_left;
  77. else
  78. p = &parent->rb_right;
  79. }
  80. rb_link_node(&new_buffer->rb_node, parent, p);
  81. rb_insert_color(&new_buffer->rb_node, &alloc->free_buffers);
  82. }
  83. static void binder_insert_allocated_buffer_locked(
  84. struct binder_alloc *alloc, struct binder_buffer *new_buffer)
  85. {
  86. struct rb_node **p = &alloc->allocated_buffers.rb_node;
  87. struct rb_node *parent = NULL;
  88. struct binder_buffer *buffer;
  89. BUG_ON(new_buffer->free);
  90. while (*p) {
  91. parent = *p;
  92. buffer = rb_entry(parent, struct binder_buffer, rb_node);
  93. BUG_ON(buffer->free);
  94. if (new_buffer->user_data < buffer->user_data)
  95. p = &parent->rb_left;
  96. else if (new_buffer->user_data > buffer->user_data)
  97. p = &parent->rb_right;
  98. else
  99. BUG();
  100. }
  101. rb_link_node(&new_buffer->rb_node, parent, p);
  102. rb_insert_color(&new_buffer->rb_node, &alloc->allocated_buffers);
  103. }
  104. static struct binder_buffer *binder_alloc_prepare_to_free_locked(
  105. struct binder_alloc *alloc,
  106. unsigned long user_ptr)
  107. {
  108. struct rb_node *n = alloc->allocated_buffers.rb_node;
  109. struct binder_buffer *buffer;
  110. while (n) {
  111. buffer = rb_entry(n, struct binder_buffer, rb_node);
  112. BUG_ON(buffer->free);
  113. if (user_ptr < buffer->user_data) {
  114. n = n->rb_left;
  115. } else if (user_ptr > buffer->user_data) {
  116. n = n->rb_right;
  117. } else {
  118. /*
  119. * Guard against user threads attempting to
  120. * free the buffer when in use by kernel or
  121. * after it's already been freed.
  122. */
  123. if (!buffer->allow_user_free)
  124. return ERR_PTR(-EPERM);
  125. buffer->allow_user_free = 0;
  126. return buffer;
  127. }
  128. }
  129. return NULL;
  130. }
  131. /**
  132. * binder_alloc_prepare_to_free() - get buffer given user ptr
  133. * @alloc: binder_alloc for this proc
  134. * @user_ptr: User pointer to buffer data
  135. *
  136. * Validate userspace pointer to buffer data and return buffer corresponding to
  137. * that user pointer. Search the rb tree for buffer that matches user data
  138. * pointer.
  139. *
  140. * Return: Pointer to buffer or NULL
  141. */
  142. struct binder_buffer *binder_alloc_prepare_to_free(struct binder_alloc *alloc,
  143. unsigned long user_ptr)
  144. {
  145. struct binder_buffer *buffer;
  146. spin_lock(&alloc->lock);
  147. buffer = binder_alloc_prepare_to_free_locked(alloc, user_ptr);
  148. spin_unlock(&alloc->lock);
  149. return buffer;
  150. }
  151. static inline void
  152. binder_set_installed_page(struct binder_lru_page *lru_page,
  153. struct page *page)
  154. {
  155. /* Pairs with acquire in binder_get_installed_page() */
  156. smp_store_release(&lru_page->page_ptr, page);
  157. }
  158. static inline struct page *
  159. binder_get_installed_page(struct binder_lru_page *lru_page)
  160. {
  161. /* Pairs with release in binder_set_installed_page() */
  162. return smp_load_acquire(&lru_page->page_ptr);
  163. }
  164. static void binder_lru_freelist_add(struct binder_alloc *alloc,
  165. unsigned long start, unsigned long end)
  166. {
  167. struct binder_lru_page *page;
  168. unsigned long page_addr;
  169. trace_binder_update_page_range(alloc, false, start, end);
  170. for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
  171. size_t index;
  172. int ret;
  173. index = (page_addr - alloc->buffer) / PAGE_SIZE;
  174. page = &alloc->pages[index];
  175. if (!binder_get_installed_page(page))
  176. continue;
  177. trace_binder_free_lru_start(alloc, index);
  178. ret = list_lru_add_obj(&binder_freelist, &page->lru);
  179. WARN_ON(!ret);
  180. trace_binder_free_lru_end(alloc, index);
  181. }
  182. }
  183. static int binder_install_single_page(struct binder_alloc *alloc,
  184. struct binder_lru_page *lru_page,
  185. unsigned long addr)
  186. {
  187. struct page *page;
  188. int ret = 0;
  189. if (!mmget_not_zero(alloc->mm))
  190. return -ESRCH;
  191. /*
  192. * Protected with mmap_sem in write mode as multiple tasks
  193. * might race to install the same page.
  194. */
  195. mmap_write_lock(alloc->mm);
  196. if (binder_get_installed_page(lru_page))
  197. goto out;
  198. if (!alloc->vma) {
  199. pr_err("%d: %s failed, no vma\n", alloc->pid, __func__);
  200. ret = -ESRCH;
  201. goto out;
  202. }
  203. page = alloc_page(GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
  204. if (!page) {
  205. pr_err("%d: failed to allocate page\n", alloc->pid);
  206. ret = -ENOMEM;
  207. goto out;
  208. }
  209. ret = vm_insert_page(alloc->vma, addr, page);
  210. if (ret) {
  211. pr_err("%d: %s failed to insert page at offset %lx with %d\n",
  212. alloc->pid, __func__, addr - alloc->buffer, ret);
  213. __free_page(page);
  214. ret = -ENOMEM;
  215. goto out;
  216. }
  217. /* Mark page installation complete and safe to use */
  218. binder_set_installed_page(lru_page, page);
  219. out:
  220. mmap_write_unlock(alloc->mm);
  221. mmput_async(alloc->mm);
  222. return ret;
  223. }
  224. static int binder_install_buffer_pages(struct binder_alloc *alloc,
  225. struct binder_buffer *buffer,
  226. size_t size)
  227. {
  228. struct binder_lru_page *page;
  229. unsigned long start, final;
  230. unsigned long page_addr;
  231. start = buffer->user_data & PAGE_MASK;
  232. final = PAGE_ALIGN(buffer->user_data + size);
  233. for (page_addr = start; page_addr < final; page_addr += PAGE_SIZE) {
  234. unsigned long index;
  235. int ret;
  236. index = (page_addr - alloc->buffer) / PAGE_SIZE;
  237. page = &alloc->pages[index];
  238. if (binder_get_installed_page(page))
  239. continue;
  240. trace_binder_alloc_page_start(alloc, index);
  241. ret = binder_install_single_page(alloc, page, page_addr);
  242. if (ret)
  243. return ret;
  244. trace_binder_alloc_page_end(alloc, index);
  245. }
  246. return 0;
  247. }
  248. /* The range of pages should exclude those shared with other buffers */
  249. static void binder_lru_freelist_del(struct binder_alloc *alloc,
  250. unsigned long start, unsigned long end)
  251. {
  252. struct binder_lru_page *page;
  253. unsigned long page_addr;
  254. trace_binder_update_page_range(alloc, true, start, end);
  255. for (page_addr = start; page_addr < end; page_addr += PAGE_SIZE) {
  256. unsigned long index;
  257. bool on_lru;
  258. index = (page_addr - alloc->buffer) / PAGE_SIZE;
  259. page = &alloc->pages[index];
  260. if (page->page_ptr) {
  261. trace_binder_alloc_lru_start(alloc, index);
  262. on_lru = list_lru_del_obj(&binder_freelist, &page->lru);
  263. WARN_ON(!on_lru);
  264. trace_binder_alloc_lru_end(alloc, index);
  265. continue;
  266. }
  267. if (index + 1 > alloc->pages_high)
  268. alloc->pages_high = index + 1;
  269. }
  270. }
  271. static inline void binder_alloc_set_vma(struct binder_alloc *alloc,
  272. struct vm_area_struct *vma)
  273. {
  274. /* pairs with smp_load_acquire in binder_alloc_get_vma() */
  275. smp_store_release(&alloc->vma, vma);
  276. }
  277. static inline struct vm_area_struct *binder_alloc_get_vma(
  278. struct binder_alloc *alloc)
  279. {
  280. /* pairs with smp_store_release in binder_alloc_set_vma() */
  281. return smp_load_acquire(&alloc->vma);
  282. }
  283. static void debug_no_space_locked(struct binder_alloc *alloc)
  284. {
  285. size_t largest_alloc_size = 0;
  286. struct binder_buffer *buffer;
  287. size_t allocated_buffers = 0;
  288. size_t largest_free_size = 0;
  289. size_t total_alloc_size = 0;
  290. size_t total_free_size = 0;
  291. size_t free_buffers = 0;
  292. size_t buffer_size;
  293. struct rb_node *n;
  294. for (n = rb_first(&alloc->allocated_buffers); n; n = rb_next(n)) {
  295. buffer = rb_entry(n, struct binder_buffer, rb_node);
  296. buffer_size = binder_alloc_buffer_size(alloc, buffer);
  297. allocated_buffers++;
  298. total_alloc_size += buffer_size;
  299. if (buffer_size > largest_alloc_size)
  300. largest_alloc_size = buffer_size;
  301. }
  302. for (n = rb_first(&alloc->free_buffers); n; n = rb_next(n)) {
  303. buffer = rb_entry(n, struct binder_buffer, rb_node);
  304. buffer_size = binder_alloc_buffer_size(alloc, buffer);
  305. free_buffers++;
  306. total_free_size += buffer_size;
  307. if (buffer_size > largest_free_size)
  308. largest_free_size = buffer_size;
  309. }
  310. binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
  311. "allocated: %zd (num: %zd largest: %zd), free: %zd (num: %zd largest: %zd)\n",
  312. total_alloc_size, allocated_buffers,
  313. largest_alloc_size, total_free_size,
  314. free_buffers, largest_free_size);
  315. }
  316. static bool debug_low_async_space_locked(struct binder_alloc *alloc)
  317. {
  318. /*
  319. * Find the amount and size of buffers allocated by the current caller;
  320. * The idea is that once we cross the threshold, whoever is responsible
  321. * for the low async space is likely to try to send another async txn,
  322. * and at some point we'll catch them in the act. This is more efficient
  323. * than keeping a map per pid.
  324. */
  325. struct binder_buffer *buffer;
  326. size_t total_alloc_size = 0;
  327. int pid = current->tgid;
  328. size_t num_buffers = 0;
  329. struct rb_node *n;
  330. /*
  331. * Only start detecting spammers once we have less than 20% of async
  332. * space left (which is less than 10% of total buffer size).
  333. */
  334. if (alloc->free_async_space >= alloc->buffer_size / 10) {
  335. alloc->oneway_spam_detected = false;
  336. return false;
  337. }
  338. for (n = rb_first(&alloc->allocated_buffers); n != NULL;
  339. n = rb_next(n)) {
  340. buffer = rb_entry(n, struct binder_buffer, rb_node);
  341. if (buffer->pid != pid)
  342. continue;
  343. if (!buffer->async_transaction)
  344. continue;
  345. total_alloc_size += binder_alloc_buffer_size(alloc, buffer);
  346. num_buffers++;
  347. }
  348. /*
  349. * Warn if this pid has more than 50 transactions, or more than 50% of
  350. * async space (which is 25% of total buffer size). Oneway spam is only
  351. * detected when the threshold is exceeded.
  352. */
  353. if (num_buffers > 50 || total_alloc_size > alloc->buffer_size / 4) {
  354. binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
  355. "%d: pid %d spamming oneway? %zd buffers allocated for a total size of %zd\n",
  356. alloc->pid, pid, num_buffers, total_alloc_size);
  357. if (!alloc->oneway_spam_detected) {
  358. alloc->oneway_spam_detected = true;
  359. return true;
  360. }
  361. }
  362. return false;
  363. }
  364. /* Callers preallocate @new_buffer, it is freed by this function if unused */
  365. static struct binder_buffer *binder_alloc_new_buf_locked(
  366. struct binder_alloc *alloc,
  367. struct binder_buffer *new_buffer,
  368. size_t size,
  369. int is_async)
  370. {
  371. struct rb_node *n = alloc->free_buffers.rb_node;
  372. struct rb_node *best_fit = NULL;
  373. struct binder_buffer *buffer;
  374. unsigned long next_used_page;
  375. unsigned long curr_last_page;
  376. size_t buffer_size;
  377. if (is_async && alloc->free_async_space < size) {
  378. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  379. "%d: binder_alloc_buf size %zd failed, no async space left\n",
  380. alloc->pid, size);
  381. buffer = ERR_PTR(-ENOSPC);
  382. goto out;
  383. }
  384. while (n) {
  385. buffer = rb_entry(n, struct binder_buffer, rb_node);
  386. BUG_ON(!buffer->free);
  387. buffer_size = binder_alloc_buffer_size(alloc, buffer);
  388. if (size < buffer_size) {
  389. best_fit = n;
  390. n = n->rb_left;
  391. } else if (size > buffer_size) {
  392. n = n->rb_right;
  393. } else {
  394. best_fit = n;
  395. break;
  396. }
  397. }
  398. if (unlikely(!best_fit)) {
  399. binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
  400. "%d: binder_alloc_buf size %zd failed, no address space\n",
  401. alloc->pid, size);
  402. debug_no_space_locked(alloc);
  403. buffer = ERR_PTR(-ENOSPC);
  404. goto out;
  405. }
  406. if (buffer_size != size) {
  407. /* Found an oversized buffer and needs to be split */
  408. buffer = rb_entry(best_fit, struct binder_buffer, rb_node);
  409. buffer_size = binder_alloc_buffer_size(alloc, buffer);
  410. WARN_ON(n || buffer_size == size);
  411. new_buffer->user_data = buffer->user_data + size;
  412. list_add(&new_buffer->entry, &buffer->entry);
  413. new_buffer->free = 1;
  414. binder_insert_free_buffer(alloc, new_buffer);
  415. new_buffer = NULL;
  416. }
  417. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  418. "%d: binder_alloc_buf size %zd got buffer %pK size %zd\n",
  419. alloc->pid, size, buffer, buffer_size);
  420. /*
  421. * Now we remove the pages from the freelist. A clever calculation
  422. * with buffer_size determines if the last page is shared with an
  423. * adjacent in-use buffer. In such case, the page has been already
  424. * removed from the freelist so we trim our range short.
  425. */
  426. next_used_page = (buffer->user_data + buffer_size) & PAGE_MASK;
  427. curr_last_page = PAGE_ALIGN(buffer->user_data + size);
  428. binder_lru_freelist_del(alloc, PAGE_ALIGN(buffer->user_data),
  429. min(next_used_page, curr_last_page));
  430. rb_erase(&buffer->rb_node, &alloc->free_buffers);
  431. buffer->free = 0;
  432. buffer->allow_user_free = 0;
  433. binder_insert_allocated_buffer_locked(alloc, buffer);
  434. buffer->async_transaction = is_async;
  435. buffer->oneway_spam_suspect = false;
  436. if (is_async) {
  437. alloc->free_async_space -= size;
  438. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
  439. "%d: binder_alloc_buf size %zd async free %zd\n",
  440. alloc->pid, size, alloc->free_async_space);
  441. if (debug_low_async_space_locked(alloc))
  442. buffer->oneway_spam_suspect = true;
  443. }
  444. out:
  445. /* Discard possibly unused new_buffer */
  446. kfree(new_buffer);
  447. return buffer;
  448. }
  449. /* Calculate the sanitized total size, returns 0 for invalid request */
  450. static inline size_t sanitized_size(size_t data_size,
  451. size_t offsets_size,
  452. size_t extra_buffers_size)
  453. {
  454. size_t total, tmp;
  455. /* Align to pointer size and check for overflows */
  456. tmp = ALIGN(data_size, sizeof(void *)) +
  457. ALIGN(offsets_size, sizeof(void *));
  458. if (tmp < data_size || tmp < offsets_size)
  459. return 0;
  460. total = tmp + ALIGN(extra_buffers_size, sizeof(void *));
  461. if (total < tmp || total < extra_buffers_size)
  462. return 0;
  463. /* Pad 0-sized buffers so they get a unique address */
  464. total = max(total, sizeof(void *));
  465. return total;
  466. }
  467. /**
  468. * binder_alloc_new_buf() - Allocate a new binder buffer
  469. * @alloc: binder_alloc for this proc
  470. * @data_size: size of user data buffer
  471. * @offsets_size: user specified buffer offset
  472. * @extra_buffers_size: size of extra space for meta-data (eg, security context)
  473. * @is_async: buffer for async transaction
  474. *
  475. * Allocate a new buffer given the requested sizes. Returns
  476. * the kernel version of the buffer pointer. The size allocated
  477. * is the sum of the three given sizes (each rounded up to
  478. * pointer-sized boundary)
  479. *
  480. * Return: The allocated buffer or %ERR_PTR(-errno) if error
  481. */
  482. struct binder_buffer *binder_alloc_new_buf(struct binder_alloc *alloc,
  483. size_t data_size,
  484. size_t offsets_size,
  485. size_t extra_buffers_size,
  486. int is_async)
  487. {
  488. struct binder_buffer *buffer, *next;
  489. size_t size;
  490. int ret;
  491. /* Check binder_alloc is fully initialized */
  492. if (!binder_alloc_get_vma(alloc)) {
  493. binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
  494. "%d: binder_alloc_buf, no vma\n",
  495. alloc->pid);
  496. return ERR_PTR(-ESRCH);
  497. }
  498. size = sanitized_size(data_size, offsets_size, extra_buffers_size);
  499. if (unlikely(!size)) {
  500. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  501. "%d: got transaction with invalid size %zd-%zd-%zd\n",
  502. alloc->pid, data_size, offsets_size,
  503. extra_buffers_size);
  504. return ERR_PTR(-EINVAL);
  505. }
  506. /* Preallocate the next buffer */
  507. next = kzalloc(sizeof(*next), GFP_KERNEL);
  508. if (!next)
  509. return ERR_PTR(-ENOMEM);
  510. spin_lock(&alloc->lock);
  511. buffer = binder_alloc_new_buf_locked(alloc, next, size, is_async);
  512. if (IS_ERR(buffer)) {
  513. spin_unlock(&alloc->lock);
  514. goto out;
  515. }
  516. buffer->data_size = data_size;
  517. buffer->offsets_size = offsets_size;
  518. buffer->extra_buffers_size = extra_buffers_size;
  519. buffer->pid = current->tgid;
  520. spin_unlock(&alloc->lock);
  521. ret = binder_install_buffer_pages(alloc, buffer, size);
  522. if (ret) {
  523. binder_alloc_free_buf(alloc, buffer);
  524. buffer = ERR_PTR(ret);
  525. }
  526. out:
  527. return buffer;
  528. }
  529. static unsigned long buffer_start_page(struct binder_buffer *buffer)
  530. {
  531. return buffer->user_data & PAGE_MASK;
  532. }
  533. static unsigned long prev_buffer_end_page(struct binder_buffer *buffer)
  534. {
  535. return (buffer->user_data - 1) & PAGE_MASK;
  536. }
  537. static void binder_delete_free_buffer(struct binder_alloc *alloc,
  538. struct binder_buffer *buffer)
  539. {
  540. struct binder_buffer *prev, *next;
  541. if (PAGE_ALIGNED(buffer->user_data))
  542. goto skip_freelist;
  543. BUG_ON(alloc->buffers.next == &buffer->entry);
  544. prev = binder_buffer_prev(buffer);
  545. BUG_ON(!prev->free);
  546. if (prev_buffer_end_page(prev) == buffer_start_page(buffer))
  547. goto skip_freelist;
  548. if (!list_is_last(&buffer->entry, &alloc->buffers)) {
  549. next = binder_buffer_next(buffer);
  550. if (buffer_start_page(next) == buffer_start_page(buffer))
  551. goto skip_freelist;
  552. }
  553. binder_lru_freelist_add(alloc, buffer_start_page(buffer),
  554. buffer_start_page(buffer) + PAGE_SIZE);
  555. skip_freelist:
  556. list_del(&buffer->entry);
  557. kfree(buffer);
  558. }
  559. static void binder_free_buf_locked(struct binder_alloc *alloc,
  560. struct binder_buffer *buffer)
  561. {
  562. size_t size, buffer_size;
  563. buffer_size = binder_alloc_buffer_size(alloc, buffer);
  564. size = ALIGN(buffer->data_size, sizeof(void *)) +
  565. ALIGN(buffer->offsets_size, sizeof(void *)) +
  566. ALIGN(buffer->extra_buffers_size, sizeof(void *));
  567. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  568. "%d: binder_free_buf %pK size %zd buffer_size %zd\n",
  569. alloc->pid, buffer, size, buffer_size);
  570. BUG_ON(buffer->free);
  571. BUG_ON(size > buffer_size);
  572. BUG_ON(buffer->transaction != NULL);
  573. BUG_ON(buffer->user_data < alloc->buffer);
  574. BUG_ON(buffer->user_data > alloc->buffer + alloc->buffer_size);
  575. if (buffer->async_transaction) {
  576. alloc->free_async_space += buffer_size;
  577. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC_ASYNC,
  578. "%d: binder_free_buf size %zd async free %zd\n",
  579. alloc->pid, size, alloc->free_async_space);
  580. }
  581. binder_lru_freelist_add(alloc, PAGE_ALIGN(buffer->user_data),
  582. (buffer->user_data + buffer_size) & PAGE_MASK);
  583. rb_erase(&buffer->rb_node, &alloc->allocated_buffers);
  584. buffer->free = 1;
  585. if (!list_is_last(&buffer->entry, &alloc->buffers)) {
  586. struct binder_buffer *next = binder_buffer_next(buffer);
  587. if (next->free) {
  588. rb_erase(&next->rb_node, &alloc->free_buffers);
  589. binder_delete_free_buffer(alloc, next);
  590. }
  591. }
  592. if (alloc->buffers.next != &buffer->entry) {
  593. struct binder_buffer *prev = binder_buffer_prev(buffer);
  594. if (prev->free) {
  595. binder_delete_free_buffer(alloc, buffer);
  596. rb_erase(&prev->rb_node, &alloc->free_buffers);
  597. buffer = prev;
  598. }
  599. }
  600. binder_insert_free_buffer(alloc, buffer);
  601. }
  602. /**
  603. * binder_alloc_get_page() - get kernel pointer for given buffer offset
  604. * @alloc: binder_alloc for this proc
  605. * @buffer: binder buffer to be accessed
  606. * @buffer_offset: offset into @buffer data
  607. * @pgoffp: address to copy final page offset to
  608. *
  609. * Lookup the struct page corresponding to the address
  610. * at @buffer_offset into @buffer->user_data. If @pgoffp is not
  611. * NULL, the byte-offset into the page is written there.
  612. *
  613. * The caller is responsible to ensure that the offset points
  614. * to a valid address within the @buffer and that @buffer is
  615. * not freeable by the user. Since it can't be freed, we are
  616. * guaranteed that the corresponding elements of @alloc->pages[]
  617. * cannot change.
  618. *
  619. * Return: struct page
  620. */
  621. static struct page *binder_alloc_get_page(struct binder_alloc *alloc,
  622. struct binder_buffer *buffer,
  623. binder_size_t buffer_offset,
  624. pgoff_t *pgoffp)
  625. {
  626. binder_size_t buffer_space_offset = buffer_offset +
  627. (buffer->user_data - alloc->buffer);
  628. pgoff_t pgoff = buffer_space_offset & ~PAGE_MASK;
  629. size_t index = buffer_space_offset >> PAGE_SHIFT;
  630. struct binder_lru_page *lru_page;
  631. lru_page = &alloc->pages[index];
  632. *pgoffp = pgoff;
  633. return lru_page->page_ptr;
  634. }
  635. /**
  636. * binder_alloc_clear_buf() - zero out buffer
  637. * @alloc: binder_alloc for this proc
  638. * @buffer: binder buffer to be cleared
  639. *
  640. * memset the given buffer to 0
  641. */
  642. static void binder_alloc_clear_buf(struct binder_alloc *alloc,
  643. struct binder_buffer *buffer)
  644. {
  645. size_t bytes = binder_alloc_buffer_size(alloc, buffer);
  646. binder_size_t buffer_offset = 0;
  647. while (bytes) {
  648. unsigned long size;
  649. struct page *page;
  650. pgoff_t pgoff;
  651. page = binder_alloc_get_page(alloc, buffer,
  652. buffer_offset, &pgoff);
  653. size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
  654. memset_page(page, pgoff, 0, size);
  655. bytes -= size;
  656. buffer_offset += size;
  657. }
  658. }
  659. /**
  660. * binder_alloc_free_buf() - free a binder buffer
  661. * @alloc: binder_alloc for this proc
  662. * @buffer: kernel pointer to buffer
  663. *
  664. * Free the buffer allocated via binder_alloc_new_buf()
  665. */
  666. void binder_alloc_free_buf(struct binder_alloc *alloc,
  667. struct binder_buffer *buffer)
  668. {
  669. /*
  670. * We could eliminate the call to binder_alloc_clear_buf()
  671. * from binder_alloc_deferred_release() by moving this to
  672. * binder_free_buf_locked(). However, that could
  673. * increase contention for the alloc->lock if clear_on_free
  674. * is used frequently for large buffers. This lock is not
  675. * needed for correctness here.
  676. */
  677. if (buffer->clear_on_free) {
  678. binder_alloc_clear_buf(alloc, buffer);
  679. buffer->clear_on_free = false;
  680. }
  681. spin_lock(&alloc->lock);
  682. binder_free_buf_locked(alloc, buffer);
  683. spin_unlock(&alloc->lock);
  684. }
  685. /**
  686. * binder_alloc_mmap_handler() - map virtual address space for proc
  687. * @alloc: alloc structure for this proc
  688. * @vma: vma passed to mmap()
  689. *
  690. * Called by binder_mmap() to initialize the space specified in
  691. * vma for allocating binder buffers
  692. *
  693. * Return:
  694. * 0 = success
  695. * -EBUSY = address space already mapped
  696. * -ENOMEM = failed to map memory to given address space
  697. */
  698. int binder_alloc_mmap_handler(struct binder_alloc *alloc,
  699. struct vm_area_struct *vma)
  700. {
  701. struct binder_buffer *buffer;
  702. const char *failure_string;
  703. int ret, i;
  704. if (unlikely(vma->vm_mm != alloc->mm)) {
  705. ret = -EINVAL;
  706. failure_string = "invalid vma->vm_mm";
  707. goto err_invalid_mm;
  708. }
  709. mutex_lock(&binder_alloc_mmap_lock);
  710. if (alloc->buffer_size) {
  711. ret = -EBUSY;
  712. failure_string = "already mapped";
  713. goto err_already_mapped;
  714. }
  715. alloc->buffer_size = min_t(unsigned long, vma->vm_end - vma->vm_start,
  716. SZ_4M);
  717. mutex_unlock(&binder_alloc_mmap_lock);
  718. alloc->buffer = vma->vm_start;
  719. alloc->pages = kvcalloc(alloc->buffer_size / PAGE_SIZE,
  720. sizeof(alloc->pages[0]),
  721. GFP_KERNEL);
  722. if (alloc->pages == NULL) {
  723. ret = -ENOMEM;
  724. failure_string = "alloc page array";
  725. goto err_alloc_pages_failed;
  726. }
  727. for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
  728. alloc->pages[i].alloc = alloc;
  729. INIT_LIST_HEAD(&alloc->pages[i].lru);
  730. }
  731. buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
  732. if (!buffer) {
  733. ret = -ENOMEM;
  734. failure_string = "alloc buffer struct";
  735. goto err_alloc_buf_struct_failed;
  736. }
  737. buffer->user_data = alloc->buffer;
  738. list_add(&buffer->entry, &alloc->buffers);
  739. buffer->free = 1;
  740. binder_insert_free_buffer(alloc, buffer);
  741. alloc->free_async_space = alloc->buffer_size / 2;
  742. /* Signal binder_alloc is fully initialized */
  743. binder_alloc_set_vma(alloc, vma);
  744. return 0;
  745. err_alloc_buf_struct_failed:
  746. kvfree(alloc->pages);
  747. alloc->pages = NULL;
  748. err_alloc_pages_failed:
  749. alloc->buffer = 0;
  750. mutex_lock(&binder_alloc_mmap_lock);
  751. alloc->buffer_size = 0;
  752. err_already_mapped:
  753. mutex_unlock(&binder_alloc_mmap_lock);
  754. err_invalid_mm:
  755. binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
  756. "%s: %d %lx-%lx %s failed %d\n", __func__,
  757. alloc->pid, vma->vm_start, vma->vm_end,
  758. failure_string, ret);
  759. return ret;
  760. }
  761. void binder_alloc_deferred_release(struct binder_alloc *alloc)
  762. {
  763. struct rb_node *n;
  764. int buffers, page_count;
  765. struct binder_buffer *buffer;
  766. buffers = 0;
  767. spin_lock(&alloc->lock);
  768. BUG_ON(alloc->vma);
  769. while ((n = rb_first(&alloc->allocated_buffers))) {
  770. buffer = rb_entry(n, struct binder_buffer, rb_node);
  771. /* Transaction should already have been freed */
  772. BUG_ON(buffer->transaction);
  773. if (buffer->clear_on_free) {
  774. binder_alloc_clear_buf(alloc, buffer);
  775. buffer->clear_on_free = false;
  776. }
  777. binder_free_buf_locked(alloc, buffer);
  778. buffers++;
  779. }
  780. while (!list_empty(&alloc->buffers)) {
  781. buffer = list_first_entry(&alloc->buffers,
  782. struct binder_buffer, entry);
  783. WARN_ON(!buffer->free);
  784. list_del(&buffer->entry);
  785. WARN_ON_ONCE(!list_empty(&alloc->buffers));
  786. kfree(buffer);
  787. }
  788. page_count = 0;
  789. if (alloc->pages) {
  790. int i;
  791. for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
  792. bool on_lru;
  793. if (!alloc->pages[i].page_ptr)
  794. continue;
  795. on_lru = list_lru_del_obj(&binder_freelist,
  796. &alloc->pages[i].lru);
  797. binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
  798. "%s: %d: page %d %s\n",
  799. __func__, alloc->pid, i,
  800. on_lru ? "on lru" : "active");
  801. __free_page(alloc->pages[i].page_ptr);
  802. page_count++;
  803. }
  804. }
  805. spin_unlock(&alloc->lock);
  806. kvfree(alloc->pages);
  807. if (alloc->mm)
  808. mmdrop(alloc->mm);
  809. binder_alloc_debug(BINDER_DEBUG_OPEN_CLOSE,
  810. "%s: %d buffers %d, pages %d\n",
  811. __func__, alloc->pid, buffers, page_count);
  812. }
  813. /**
  814. * binder_alloc_print_allocated() - print buffer info
  815. * @m: seq_file for output via seq_printf()
  816. * @alloc: binder_alloc for this proc
  817. *
  818. * Prints information about every buffer associated with
  819. * the binder_alloc state to the given seq_file
  820. */
  821. void binder_alloc_print_allocated(struct seq_file *m,
  822. struct binder_alloc *alloc)
  823. {
  824. struct binder_buffer *buffer;
  825. struct rb_node *n;
  826. spin_lock(&alloc->lock);
  827. for (n = rb_first(&alloc->allocated_buffers); n; n = rb_next(n)) {
  828. buffer = rb_entry(n, struct binder_buffer, rb_node);
  829. seq_printf(m, " buffer %d: %lx size %zd:%zd:%zd %s\n",
  830. buffer->debug_id,
  831. buffer->user_data - alloc->buffer,
  832. buffer->data_size, buffer->offsets_size,
  833. buffer->extra_buffers_size,
  834. buffer->transaction ? "active" : "delivered");
  835. }
  836. spin_unlock(&alloc->lock);
  837. }
  838. /**
  839. * binder_alloc_print_pages() - print page usage
  840. * @m: seq_file for output via seq_printf()
  841. * @alloc: binder_alloc for this proc
  842. */
  843. void binder_alloc_print_pages(struct seq_file *m,
  844. struct binder_alloc *alloc)
  845. {
  846. struct binder_lru_page *page;
  847. int i;
  848. int active = 0;
  849. int lru = 0;
  850. int free = 0;
  851. spin_lock(&alloc->lock);
  852. /*
  853. * Make sure the binder_alloc is fully initialized, otherwise we might
  854. * read inconsistent state.
  855. */
  856. if (binder_alloc_get_vma(alloc) != NULL) {
  857. for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
  858. page = &alloc->pages[i];
  859. if (!page->page_ptr)
  860. free++;
  861. else if (list_empty(&page->lru))
  862. active++;
  863. else
  864. lru++;
  865. }
  866. }
  867. spin_unlock(&alloc->lock);
  868. seq_printf(m, " pages: %d:%d:%d\n", active, lru, free);
  869. seq_printf(m, " pages high watermark: %zu\n", alloc->pages_high);
  870. }
  871. /**
  872. * binder_alloc_get_allocated_count() - return count of buffers
  873. * @alloc: binder_alloc for this proc
  874. *
  875. * Return: count of allocated buffers
  876. */
  877. int binder_alloc_get_allocated_count(struct binder_alloc *alloc)
  878. {
  879. struct rb_node *n;
  880. int count = 0;
  881. spin_lock(&alloc->lock);
  882. for (n = rb_first(&alloc->allocated_buffers); n != NULL; n = rb_next(n))
  883. count++;
  884. spin_unlock(&alloc->lock);
  885. return count;
  886. }
  887. /**
  888. * binder_alloc_vma_close() - invalidate address space
  889. * @alloc: binder_alloc for this proc
  890. *
  891. * Called from binder_vma_close() when releasing address space.
  892. * Clears alloc->vma to prevent new incoming transactions from
  893. * allocating more buffers.
  894. */
  895. void binder_alloc_vma_close(struct binder_alloc *alloc)
  896. {
  897. binder_alloc_set_vma(alloc, NULL);
  898. }
  899. /**
  900. * binder_alloc_free_page() - shrinker callback to free pages
  901. * @item: item to free
  902. * @lock: lock protecting the item
  903. * @cb_arg: callback argument
  904. *
  905. * Called from list_lru_walk() in binder_shrink_scan() to free
  906. * up pages when the system is under memory pressure.
  907. */
  908. enum lru_status binder_alloc_free_page(struct list_head *item,
  909. struct list_lru_one *lru,
  910. spinlock_t *lock,
  911. void *cb_arg)
  912. __must_hold(lock)
  913. {
  914. struct binder_lru_page *page = container_of(item, typeof(*page), lru);
  915. struct binder_alloc *alloc = page->alloc;
  916. struct mm_struct *mm = alloc->mm;
  917. struct vm_area_struct *vma;
  918. struct page *page_to_free;
  919. unsigned long page_addr;
  920. size_t index;
  921. if (!mmget_not_zero(mm))
  922. goto err_mmget;
  923. if (!mmap_read_trylock(mm))
  924. goto err_mmap_read_lock_failed;
  925. if (!spin_trylock(&alloc->lock))
  926. goto err_get_alloc_lock_failed;
  927. if (!page->page_ptr)
  928. goto err_page_already_freed;
  929. index = page - alloc->pages;
  930. page_addr = alloc->buffer + index * PAGE_SIZE;
  931. vma = vma_lookup(mm, page_addr);
  932. if (vma && vma != binder_alloc_get_vma(alloc))
  933. goto err_invalid_vma;
  934. trace_binder_unmap_kernel_start(alloc, index);
  935. page_to_free = page->page_ptr;
  936. page->page_ptr = NULL;
  937. trace_binder_unmap_kernel_end(alloc, index);
  938. list_lru_isolate(lru, item);
  939. spin_unlock(&alloc->lock);
  940. spin_unlock(lock);
  941. if (vma) {
  942. trace_binder_unmap_user_start(alloc, index);
  943. zap_page_range_single(vma, page_addr, PAGE_SIZE, NULL);
  944. trace_binder_unmap_user_end(alloc, index);
  945. }
  946. mmap_read_unlock(mm);
  947. mmput_async(mm);
  948. __free_page(page_to_free);
  949. spin_lock(lock);
  950. return LRU_REMOVED_RETRY;
  951. err_invalid_vma:
  952. err_page_already_freed:
  953. spin_unlock(&alloc->lock);
  954. err_get_alloc_lock_failed:
  955. mmap_read_unlock(mm);
  956. err_mmap_read_lock_failed:
  957. mmput_async(mm);
  958. err_mmget:
  959. return LRU_SKIP;
  960. }
  961. static unsigned long
  962. binder_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
  963. {
  964. return list_lru_count(&binder_freelist);
  965. }
  966. static unsigned long
  967. binder_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
  968. {
  969. return list_lru_walk(&binder_freelist, binder_alloc_free_page,
  970. NULL, sc->nr_to_scan);
  971. }
  972. static struct shrinker *binder_shrinker;
  973. /**
  974. * binder_alloc_init() - called by binder_open() for per-proc initialization
  975. * @alloc: binder_alloc for this proc
  976. *
  977. * Called from binder_open() to initialize binder_alloc fields for
  978. * new binder proc
  979. */
  980. void binder_alloc_init(struct binder_alloc *alloc)
  981. {
  982. alloc->pid = current->group_leader->pid;
  983. alloc->mm = current->mm;
  984. mmgrab(alloc->mm);
  985. spin_lock_init(&alloc->lock);
  986. INIT_LIST_HEAD(&alloc->buffers);
  987. }
  988. int binder_alloc_shrinker_init(void)
  989. {
  990. int ret;
  991. ret = list_lru_init(&binder_freelist);
  992. if (ret)
  993. return ret;
  994. binder_shrinker = shrinker_alloc(0, "android-binder");
  995. if (!binder_shrinker) {
  996. list_lru_destroy(&binder_freelist);
  997. return -ENOMEM;
  998. }
  999. binder_shrinker->count_objects = binder_shrink_count;
  1000. binder_shrinker->scan_objects = binder_shrink_scan;
  1001. shrinker_register(binder_shrinker);
  1002. return 0;
  1003. }
  1004. void binder_alloc_shrinker_exit(void)
  1005. {
  1006. shrinker_free(binder_shrinker);
  1007. list_lru_destroy(&binder_freelist);
  1008. }
  1009. /**
  1010. * check_buffer() - verify that buffer/offset is safe to access
  1011. * @alloc: binder_alloc for this proc
  1012. * @buffer: binder buffer to be accessed
  1013. * @offset: offset into @buffer data
  1014. * @bytes: bytes to access from offset
  1015. *
  1016. * Check that the @offset/@bytes are within the size of the given
  1017. * @buffer and that the buffer is currently active and not freeable.
  1018. * Offsets must also be multiples of sizeof(u32). The kernel is
  1019. * allowed to touch the buffer in two cases:
  1020. *
  1021. * 1) when the buffer is being created:
  1022. * (buffer->free == 0 && buffer->allow_user_free == 0)
  1023. * 2) when the buffer is being torn down:
  1024. * (buffer->free == 0 && buffer->transaction == NULL).
  1025. *
  1026. * Return: true if the buffer is safe to access
  1027. */
  1028. static inline bool check_buffer(struct binder_alloc *alloc,
  1029. struct binder_buffer *buffer,
  1030. binder_size_t offset, size_t bytes)
  1031. {
  1032. size_t buffer_size = binder_alloc_buffer_size(alloc, buffer);
  1033. return buffer_size >= bytes &&
  1034. offset <= buffer_size - bytes &&
  1035. IS_ALIGNED(offset, sizeof(u32)) &&
  1036. !buffer->free &&
  1037. (!buffer->allow_user_free || !buffer->transaction);
  1038. }
  1039. /**
  1040. * binder_alloc_copy_user_to_buffer() - copy src user to tgt user
  1041. * @alloc: binder_alloc for this proc
  1042. * @buffer: binder buffer to be accessed
  1043. * @buffer_offset: offset into @buffer data
  1044. * @from: userspace pointer to source buffer
  1045. * @bytes: bytes to copy
  1046. *
  1047. * Copy bytes from source userspace to target buffer.
  1048. *
  1049. * Return: bytes remaining to be copied
  1050. */
  1051. unsigned long
  1052. binder_alloc_copy_user_to_buffer(struct binder_alloc *alloc,
  1053. struct binder_buffer *buffer,
  1054. binder_size_t buffer_offset,
  1055. const void __user *from,
  1056. size_t bytes)
  1057. {
  1058. if (!check_buffer(alloc, buffer, buffer_offset, bytes))
  1059. return bytes;
  1060. while (bytes) {
  1061. unsigned long size;
  1062. unsigned long ret;
  1063. struct page *page;
  1064. pgoff_t pgoff;
  1065. void *kptr;
  1066. page = binder_alloc_get_page(alloc, buffer,
  1067. buffer_offset, &pgoff);
  1068. size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
  1069. kptr = kmap_local_page(page) + pgoff;
  1070. ret = copy_from_user(kptr, from, size);
  1071. kunmap_local(kptr);
  1072. if (ret)
  1073. return bytes - size + ret;
  1074. bytes -= size;
  1075. from += size;
  1076. buffer_offset += size;
  1077. }
  1078. return 0;
  1079. }
  1080. static int binder_alloc_do_buffer_copy(struct binder_alloc *alloc,
  1081. bool to_buffer,
  1082. struct binder_buffer *buffer,
  1083. binder_size_t buffer_offset,
  1084. void *ptr,
  1085. size_t bytes)
  1086. {
  1087. /* All copies must be 32-bit aligned and 32-bit size */
  1088. if (!check_buffer(alloc, buffer, buffer_offset, bytes))
  1089. return -EINVAL;
  1090. while (bytes) {
  1091. unsigned long size;
  1092. struct page *page;
  1093. pgoff_t pgoff;
  1094. page = binder_alloc_get_page(alloc, buffer,
  1095. buffer_offset, &pgoff);
  1096. size = min_t(size_t, bytes, PAGE_SIZE - pgoff);
  1097. if (to_buffer)
  1098. memcpy_to_page(page, pgoff, ptr, size);
  1099. else
  1100. memcpy_from_page(ptr, page, pgoff, size);
  1101. bytes -= size;
  1102. pgoff = 0;
  1103. ptr = ptr + size;
  1104. buffer_offset += size;
  1105. }
  1106. return 0;
  1107. }
  1108. int binder_alloc_copy_to_buffer(struct binder_alloc *alloc,
  1109. struct binder_buffer *buffer,
  1110. binder_size_t buffer_offset,
  1111. void *src,
  1112. size_t bytes)
  1113. {
  1114. return binder_alloc_do_buffer_copy(alloc, true, buffer, buffer_offset,
  1115. src, bytes);
  1116. }
  1117. int binder_alloc_copy_from_buffer(struct binder_alloc *alloc,
  1118. void *dest,
  1119. struct binder_buffer *buffer,
  1120. binder_size_t buffer_offset,
  1121. size_t bytes)
  1122. {
  1123. return binder_alloc_do_buffer_copy(alloc, false, buffer, buffer_offset,
  1124. dest, bytes);
  1125. }