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