2017-11-07 13:07:54 -07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2013-12-13 20:38:38 -07:00
|
|
|
/*
|
|
|
|
* drivers/staging/android/ion/ion_heap.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Google, Inc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/err.h>
|
2013-12-13 15:24:39 -07:00
|
|
|
#include <linux/freezer.h>
|
|
|
|
#include <linux/kthread.h>
|
2013-12-13 15:24:26 -07:00
|
|
|
#include <linux/mm.h>
|
2013-12-13 15:24:39 -07:00
|
|
|
#include <linux/rtmutex.h>
|
|
|
|
#include <linux/sched.h>
|
2017-02-01 10:07:51 -07:00
|
|
|
#include <uapi/linux/sched/types.h>
|
2013-12-13 15:24:26 -07:00
|
|
|
#include <linux/scatterlist.h>
|
|
|
|
#include <linux/vmalloc.h>
|
2013-12-13 20:38:38 -07:00
|
|
|
#include "ion.h"
|
|
|
|
|
2013-12-13 15:24:26 -07:00
|
|
|
void *ion_heap_map_kernel(struct ion_heap *heap,
|
|
|
|
struct ion_buffer *buffer)
|
|
|
|
{
|
|
|
|
struct scatterlist *sg;
|
|
|
|
int i, j;
|
|
|
|
void *vaddr;
|
|
|
|
pgprot_t pgprot;
|
|
|
|
struct sg_table *table = buffer->sg_table;
|
|
|
|
int npages = PAGE_ALIGN(buffer->size) / PAGE_SIZE;
|
treewide: Use array_size() in vmalloc()
The vmalloc() function has no 2-factor argument form, so multiplication
factors need to be wrapped in array_size(). This patch replaces cases of:
vmalloc(a * b)
with:
vmalloc(array_size(a, b))
as well as handling cases of:
vmalloc(a * b * c)
with:
vmalloc(array3_size(a, b, c))
This does, however, attempt to ignore constant size factors like:
vmalloc(4 * 1024)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
vmalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
vmalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
vmalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
vmalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
vmalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
vmalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
vmalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
vmalloc(
- sizeof(TYPE) * (COUNT_ID)
+ array_size(COUNT_ID, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT_ID
+ array_size(COUNT_ID, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * (COUNT_CONST)
+ array_size(COUNT_CONST, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT_CONST
+ array_size(COUNT_CONST, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT_ID)
+ array_size(COUNT_ID, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT_ID
+ array_size(COUNT_ID, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT_CONST)
+ array_size(COUNT_CONST, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT_CONST
+ array_size(COUNT_CONST, sizeof(THING))
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
vmalloc(
- SIZE * COUNT
+ array_size(COUNT, SIZE)
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
vmalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
vmalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
vmalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
vmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
vmalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
vmalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
vmalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
vmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
vmalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
vmalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
vmalloc(C1 * C2 * C3, ...)
|
vmalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants.
@@
expression E1, E2;
constant C1, C2;
@@
(
vmalloc(C1 * C2, ...)
|
vmalloc(
- E1 * E2
+ array_size(E1, E2)
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-12 15:27:11 -06:00
|
|
|
struct page **pages = vmalloc(array_size(npages,
|
|
|
|
sizeof(struct page *)));
|
2013-12-13 15:24:26 -07:00
|
|
|
struct page **tmp = pages;
|
|
|
|
|
|
|
|
if (!pages)
|
2013-12-13 20:26:28 -07:00
|
|
|
return NULL;
|
2013-12-13 15:24:26 -07:00
|
|
|
|
|
|
|
if (buffer->flags & ION_FLAG_CACHED)
|
|
|
|
pgprot = PAGE_KERNEL;
|
|
|
|
else
|
|
|
|
pgprot = pgprot_writecombine(PAGE_KERNEL);
|
|
|
|
|
|
|
|
for_each_sg(table->sgl, sg, table->nents, i) {
|
2013-12-13 15:25:02 -07:00
|
|
|
int npages_this_entry = PAGE_ALIGN(sg->length) / PAGE_SIZE;
|
2013-12-13 15:24:26 -07:00
|
|
|
struct page *page = sg_page(sg);
|
2014-04-30 10:30:23 -06:00
|
|
|
|
2013-12-13 15:24:26 -07:00
|
|
|
BUG_ON(i >= npages);
|
2013-12-13 20:26:33 -07:00
|
|
|
for (j = 0; j < npages_this_entry; j++)
|
2013-12-13 15:24:26 -07:00
|
|
|
*(tmp++) = page++;
|
|
|
|
}
|
|
|
|
vaddr = vmap(pages, npages, VM_MAP, pgprot);
|
|
|
|
vfree(pages);
|
|
|
|
|
2015-10-19 11:07:51 -06:00
|
|
|
if (!vaddr)
|
2013-12-13 15:24:48 -07:00
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
2013-12-13 15:24:26 -07:00
|
|
|
return vaddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ion_heap_unmap_kernel(struct ion_heap *heap,
|
|
|
|
struct ion_buffer *buffer)
|
|
|
|
{
|
|
|
|
vunmap(buffer->vaddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ion_heap_map_user(struct ion_heap *heap, struct ion_buffer *buffer,
|
|
|
|
struct vm_area_struct *vma)
|
|
|
|
{
|
|
|
|
struct sg_table *table = buffer->sg_table;
|
|
|
|
unsigned long addr = vma->vm_start;
|
|
|
|
unsigned long offset = vma->vm_pgoff * PAGE_SIZE;
|
|
|
|
struct scatterlist *sg;
|
|
|
|
int i;
|
2013-12-13 20:26:23 -07:00
|
|
|
int ret;
|
2013-12-13 15:24:26 -07:00
|
|
|
|
|
|
|
for_each_sg(table->sgl, sg, table->nents, i) {
|
|
|
|
struct page *page = sg_page(sg);
|
|
|
|
unsigned long remainder = vma->vm_end - addr;
|
2013-12-13 15:25:02 -07:00
|
|
|
unsigned long len = sg->length;
|
2013-12-13 15:24:26 -07:00
|
|
|
|
2013-12-13 15:25:02 -07:00
|
|
|
if (offset >= sg->length) {
|
|
|
|
offset -= sg->length;
|
2013-12-13 15:24:26 -07:00
|
|
|
continue;
|
|
|
|
} else if (offset) {
|
|
|
|
page += offset / PAGE_SIZE;
|
2013-12-13 15:25:02 -07:00
|
|
|
len = sg->length - offset;
|
2013-12-13 15:24:26 -07:00
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
len = min(len, remainder);
|
2013-12-13 20:26:23 -07:00
|
|
|
ret = remap_pfn_range(vma, addr, page_to_pfn(page), len,
|
2016-08-16 21:02:00 -06:00
|
|
|
vma->vm_page_prot);
|
2013-12-13 20:26:23 -07:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2013-12-13 15:24:26 -07:00
|
|
|
addr += len;
|
|
|
|
if (addr >= vma->vm_end)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-13 20:26:21 -07:00
|
|
|
static int ion_heap_clear_pages(struct page **pages, int num, pgprot_t pgprot)
|
|
|
|
{
|
|
|
|
void *addr = vm_map_ram(pages, num, -1, pgprot);
|
2014-04-30 10:30:23 -06:00
|
|
|
|
2013-12-13 20:26:21 -07:00
|
|
|
if (!addr)
|
|
|
|
return -ENOMEM;
|
|
|
|
memset(addr, 0, PAGE_SIZE * num);
|
|
|
|
vm_unmap_ram(addr, num);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-13 20:26:30 -07:00
|
|
|
static int ion_heap_sglist_zero(struct scatterlist *sgl, unsigned int nents,
|
2016-08-16 21:02:00 -06:00
|
|
|
pgprot_t pgprot)
|
2013-12-13 20:26:30 -07:00
|
|
|
{
|
|
|
|
int p = 0;
|
|
|
|
int ret = 0;
|
|
|
|
struct sg_page_iter piter;
|
|
|
|
struct page *pages[32];
|
|
|
|
|
|
|
|
for_each_sg_page(sgl, &piter, nents, 0) {
|
|
|
|
pages[p++] = sg_page_iter_page(&piter);
|
|
|
|
if (p == ARRAY_SIZE(pages)) {
|
|
|
|
ret = ion_heap_clear_pages(pages, p, pgprot);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
p = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p)
|
|
|
|
ret = ion_heap_clear_pages(pages, p, pgprot);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-13 15:24:32 -07:00
|
|
|
int ion_heap_buffer_zero(struct ion_buffer *buffer)
|
|
|
|
{
|
|
|
|
struct sg_table *table = buffer->sg_table;
|
|
|
|
pgprot_t pgprot;
|
|
|
|
|
|
|
|
if (buffer->flags & ION_FLAG_CACHED)
|
|
|
|
pgprot = PAGE_KERNEL;
|
|
|
|
else
|
|
|
|
pgprot = pgprot_writecombine(PAGE_KERNEL);
|
|
|
|
|
2013-12-13 20:26:30 -07:00
|
|
|
return ion_heap_sglist_zero(table->sgl, table->nents, pgprot);
|
|
|
|
}
|
2013-12-13 15:24:32 -07:00
|
|
|
|
2013-12-13 20:26:30 -07:00
|
|
|
int ion_heap_pages_zero(struct page *page, size_t size, pgprot_t pgprot)
|
|
|
|
{
|
|
|
|
struct scatterlist sg;
|
|
|
|
|
|
|
|
sg_init_table(&sg, 1);
|
|
|
|
sg_set_page(&sg, page, size, 0);
|
|
|
|
return ion_heap_sglist_zero(&sg, 1, pgprot);
|
2013-12-13 15:24:32 -07:00
|
|
|
}
|
|
|
|
|
2013-12-13 20:26:33 -07:00
|
|
|
void ion_heap_freelist_add(struct ion_heap *heap, struct ion_buffer *buffer)
|
2013-12-13 15:24:39 -07:00
|
|
|
{
|
2013-12-17 18:04:29 -07:00
|
|
|
spin_lock(&heap->free_lock);
|
2013-12-13 15:24:39 -07:00
|
|
|
list_add(&buffer->list, &heap->free_list);
|
|
|
|
heap->free_list_size += buffer->size;
|
2013-12-17 18:04:29 -07:00
|
|
|
spin_unlock(&heap->free_lock);
|
2013-12-13 15:24:39 -07:00
|
|
|
wake_up(&heap->waitqueue);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ion_heap_freelist_size(struct ion_heap *heap)
|
|
|
|
{
|
|
|
|
size_t size;
|
|
|
|
|
2013-12-17 18:04:29 -07:00
|
|
|
spin_lock(&heap->free_lock);
|
2013-12-13 15:24:39 -07:00
|
|
|
size = heap->free_list_size;
|
2013-12-17 18:04:29 -07:00
|
|
|
spin_unlock(&heap->free_lock);
|
2013-12-13 15:24:39 -07:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2014-02-17 14:58:39 -07:00
|
|
|
static size_t _ion_heap_freelist_drain(struct ion_heap *heap, size_t size,
|
2016-08-16 21:02:00 -06:00
|
|
|
bool skip_pools)
|
2013-12-13 15:24:39 -07:00
|
|
|
{
|
2013-12-17 18:04:29 -07:00
|
|
|
struct ion_buffer *buffer;
|
2013-12-13 15:24:39 -07:00
|
|
|
size_t total_drained = 0;
|
|
|
|
|
|
|
|
if (ion_heap_freelist_size(heap) == 0)
|
|
|
|
return 0;
|
|
|
|
|
2013-12-17 18:04:29 -07:00
|
|
|
spin_lock(&heap->free_lock);
|
2013-12-13 15:24:39 -07:00
|
|
|
if (size == 0)
|
|
|
|
size = heap->free_list_size;
|
|
|
|
|
2013-12-17 18:04:29 -07:00
|
|
|
while (!list_empty(&heap->free_list)) {
|
2013-12-13 15:24:39 -07:00
|
|
|
if (total_drained >= size)
|
|
|
|
break;
|
2013-12-17 18:04:29 -07:00
|
|
|
buffer = list_first_entry(&heap->free_list, struct ion_buffer,
|
|
|
|
list);
|
2013-12-13 15:24:39 -07:00
|
|
|
list_del(&buffer->list);
|
|
|
|
heap->free_list_size -= buffer->size;
|
2014-02-17 14:58:39 -07:00
|
|
|
if (skip_pools)
|
|
|
|
buffer->private_flags |= ION_PRIV_FLAG_SHRINKER_FREE;
|
2013-12-13 15:24:39 -07:00
|
|
|
total_drained += buffer->size;
|
2013-12-17 18:04:29 -07:00
|
|
|
spin_unlock(&heap->free_lock);
|
2013-12-13 20:26:17 -07:00
|
|
|
ion_buffer_destroy(buffer);
|
2013-12-17 18:04:29 -07:00
|
|
|
spin_lock(&heap->free_lock);
|
2013-12-13 15:24:39 -07:00
|
|
|
}
|
2013-12-17 18:04:29 -07:00
|
|
|
spin_unlock(&heap->free_lock);
|
2013-12-13 15:24:39 -07:00
|
|
|
|
|
|
|
return total_drained;
|
|
|
|
}
|
|
|
|
|
2014-02-17 14:58:39 -07:00
|
|
|
size_t ion_heap_freelist_drain(struct ion_heap *heap, size_t size)
|
|
|
|
{
|
|
|
|
return _ion_heap_freelist_drain(heap, size, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ion_heap_freelist_shrink(struct ion_heap *heap, size_t size)
|
|
|
|
{
|
|
|
|
return _ion_heap_freelist_drain(heap, size, true);
|
|
|
|
}
|
|
|
|
|
2013-12-13 20:26:28 -07:00
|
|
|
static int ion_heap_deferred_free(void *data)
|
2013-12-13 15:24:39 -07:00
|
|
|
{
|
|
|
|
struct ion_heap *heap = data;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
struct ion_buffer *buffer;
|
|
|
|
|
|
|
|
wait_event_freezable(heap->waitqueue,
|
|
|
|
ion_heap_freelist_size(heap) > 0);
|
|
|
|
|
2013-12-17 18:04:29 -07:00
|
|
|
spin_lock(&heap->free_lock);
|
2013-12-13 15:24:39 -07:00
|
|
|
if (list_empty(&heap->free_list)) {
|
2013-12-17 18:04:29 -07:00
|
|
|
spin_unlock(&heap->free_lock);
|
2013-12-13 15:24:39 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
buffer = list_first_entry(&heap->free_list, struct ion_buffer,
|
|
|
|
list);
|
|
|
|
list_del(&buffer->list);
|
|
|
|
heap->free_list_size -= buffer->size;
|
2013-12-17 18:04:29 -07:00
|
|
|
spin_unlock(&heap->free_lock);
|
2013-12-13 15:24:39 -07:00
|
|
|
ion_buffer_destroy(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ion_heap_init_deferred_free(struct ion_heap *heap)
|
|
|
|
{
|
|
|
|
struct sched_param param = { .sched_priority = 0 };
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&heap->free_list);
|
|
|
|
init_waitqueue_head(&heap->waitqueue);
|
|
|
|
heap->task = kthread_run(ion_heap_deferred_free, heap,
|
|
|
|
"%s", heap->name);
|
|
|
|
if (IS_ERR(heap->task)) {
|
|
|
|
pr_err("%s: creating thread for deferred free failed\n",
|
|
|
|
__func__);
|
2014-01-26 23:47:05 -07:00
|
|
|
return PTR_ERR_OR_ZERO(heap->task);
|
2013-12-13 15:24:39 -07:00
|
|
|
}
|
2014-01-22 07:20:03 -07:00
|
|
|
sched_setscheduler(heap->task, SCHED_IDLE, ¶m);
|
2013-12-13 15:24:39 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-17 14:58:38 -07:00
|
|
|
static unsigned long ion_heap_shrink_count(struct shrinker *shrinker,
|
2016-08-16 21:02:00 -06:00
|
|
|
struct shrink_control *sc)
|
2014-02-17 14:58:38 -07:00
|
|
|
{
|
|
|
|
struct ion_heap *heap = container_of(shrinker, struct ion_heap,
|
|
|
|
shrinker);
|
|
|
|
int total = 0;
|
|
|
|
|
|
|
|
total = ion_heap_freelist_size(heap) / PAGE_SIZE;
|
|
|
|
if (heap->ops->shrink)
|
|
|
|
total += heap->ops->shrink(heap, sc->gfp_mask, 0);
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long ion_heap_shrink_scan(struct shrinker *shrinker,
|
2016-08-16 21:02:00 -06:00
|
|
|
struct shrink_control *sc)
|
2014-02-17 14:58:38 -07:00
|
|
|
{
|
|
|
|
struct ion_heap *heap = container_of(shrinker, struct ion_heap,
|
|
|
|
shrinker);
|
|
|
|
int freed = 0;
|
|
|
|
int to_scan = sc->nr_to_scan;
|
|
|
|
|
|
|
|
if (to_scan == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* shrink the free list first, no point in zeroing the memory if we're
|
2014-02-17 14:58:39 -07:00
|
|
|
* just going to reclaim it. Also, skip any possible page pooling.
|
2014-02-17 14:58:38 -07:00
|
|
|
*/
|
|
|
|
if (heap->flags & ION_HEAP_FLAG_DEFER_FREE)
|
2014-02-17 14:58:39 -07:00
|
|
|
freed = ion_heap_freelist_shrink(heap, to_scan * PAGE_SIZE) /
|
2014-02-17 14:58:38 -07:00
|
|
|
PAGE_SIZE;
|
|
|
|
|
|
|
|
to_scan -= freed;
|
|
|
|
if (to_scan <= 0)
|
|
|
|
return freed;
|
|
|
|
|
|
|
|
if (heap->ops->shrink)
|
|
|
|
freed += heap->ops->shrink(heap, sc->gfp_mask, to_scan);
|
|
|
|
return freed;
|
|
|
|
}
|
|
|
|
|
2017-12-29 05:37:03 -07:00
|
|
|
int ion_heap_init_shrinker(struct ion_heap *heap)
|
2014-02-17 14:58:38 -07:00
|
|
|
{
|
|
|
|
heap->shrinker.count_objects = ion_heap_shrink_count;
|
|
|
|
heap->shrinker.scan_objects = ion_heap_shrink_scan;
|
|
|
|
heap->shrinker.seeks = DEFAULT_SEEKS;
|
|
|
|
heap->shrinker.batch = 0;
|
2017-12-29 05:37:03 -07:00
|
|
|
|
|
|
|
return register_shrinker(&heap->shrinker);
|
2014-02-17 14:58:38 -07:00
|
|
|
}
|