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