文档章节

Android ION overview

Jerikc
 Jerikc
发布于 2015/01/14 19:07
字数 893
阅读 1143
收藏 2
ION

ION is the memory manager of Android, it could be used by graphic and multimedia stacks to allocate buffers.

ION include a buffer sharing mechanism between process and drivers.

ION define opaque handles to manage underline buffers.

ION handles are only map in kernel if that is needed by drivers, it help to save logical address space.

In a same way ION handles aren't mmaped by default in userland but all helpers functions are provided.

ION files

In an Android kernel you can found ION files here:

  • include/linux/ion.h
  • drivers/gpu/ion/*


ION architecture

ION define different type of heaps:

  • ION_HEAP_TYPE_SYSTEM : memory allocated via vmalloc
  • ION_HEAP_TYPE_SYSTEM_CONTIG: memory allocated via kmalloc
  • ION_HEAP_TYPE_CARVEOUT: memory allocated from a prereserved carveout heap, allocations are physically contiguous
  • ION_HEAP_TYPE_CHUNK
  • ION_HEAP_TYPE_DMA: memory allocated via DMA API

ION architecture diagram

diagram generated from this file

Heaps could be defined in board configuration file with those structures:

/**
 * struct ion_platform_heap - defines a heap in the given platform
 * @type:       type of the heap from ion_heap_type enum
 * @id:         unique identifier for heap.  When allocating higher numbers
 *              will be allocated from first.  At allocation these are passed
 *              as a bit mask and therefore can not exceed ION_NUM_HEAP_IDS.
 * @name:       used for debug purposes
 * @base:       base address of heap in physical memory if applicable
 * @size:       size of the heap in bytes if applicable
 * @align:      required alignment in physical memory if applicable
 * @priv:       private info passed from the board file
 *
 * Provided by the board file.
 */
struct ion_platform_heap {
        enum ion_heap_type type;
        unsigned int id;
        const char *name;
        ion_phys_addr_t base;
        size_t size;
        ion_phys_addr_t align;
        void *priv;
};

/**
 * struct ion_platform_data - array of platform heaps passed from board file
 * @nr:         number of structures in the array
 * @heaps:      array of platform_heap structions
 *
 * Provided by the board file in the form of platform data to a platform device.
 */
struct ion_platform_data {
        int nr;
        struct ion_platform_heap *heaps;
};

Multiple heaps of each type could be instantiated, and unique ID is used to distinguish and order them.

ION client could select which type of heaps they want to use by setting the correct heap mask when calling ion_alloc. The first heap (i.e. the heap with the lowest ID) is used to do the allocation.

If an ION client want to be more selective it could specify exactly which heap(s) to use (or not) with "flags" parameter of ion_alloc.

ION APIs

Userland API

ioctl are defined to interact with application in userland:

  • ION_IOC_ALLOC: allocate memory
  • ION_IOC_FREE: free memory
  • ION_IOC_MAP: get a file descriptor to mmap
  • ION_IOC_SHARE: creates a file descriptor to use to share an allocation
  • ION_IOC_IMPORT: imports a shared file descriptor
  • ION_IOC_CUSTOM: call architecture specific ion ioctl
  • ION_IOC_SYNC - syncs a shared file descriptors to memory

ION_IOC_MAP and ION_IOC_SHARE are using the same code inside ION core.

A suggestion could be to use only ION_IOC_SHARE to avoid any possible confusion with real map or mmap operations.

Kernel API

Kernel drivers can register themself as "client" of ION and specify with a mask which type of heap(s) they want to use:

  • ion_client_create: allocate a client and returns it
  • ion_client_destroy: free's a client and all it's handles

Buffer allocation and release functions:

  • ion_alloc: allocate ion memory, it return an opaque handle it
  • ion_free: free a handle

ION use opaque handle to manipulate buffers, drivers must be able to get access to the underline buffer, ION provide lot of function for that:

  • ion_phys: returns the physical address and len of a handle
  • ion_map_kernel: create mapping for the given handle
  • ion_unmap_kernel: destroy a kernel mapping for a handle
  • ion_map_dma: create a dma mapping for a given handle, return an sglist
  • ion_unmap_dma: destroy a dma mapping for a handle

Instead of share a buffer address between drivers ION allow to use handles, the same mechanism could be used to share buffer between process in useland:

  • ion_share: given a handle, obtain a buffer to pass to other clients
  • ion_import: given an buffer in another client, import it
  • ion_import_fd: given an fd obtained via ION_IOC_SHARE ioctl, import it


Heap API

Heap interface is describe in drivers/gpu/ion/ion_priv.h file.

This API is not exported to drivers or userland.

/**
 * struct ion_heap_ops - ops to operate on a given heap
 * @allocate:           allocate memory
 * @free:               free memory
 * @phys                get physical address of a buffer (only define on physically contiguous heaps)
 * @map_dma             map the memory for dma to a scatterlist
 * @unmap_dma           unmap the memory for dma
 * @map_kernel          map memory to the kernel
 * @unmap_kernel        unmap memory to the kernel
 * @map_user            map memory to userspace
 */
struct ion_heap_ops {
        int (*allocate) (struct ion_heap *heap, struct ion_buffer *buffer, unsigned long len,unsigned long align, unsigned long flags);
        void (*free) (struct ion_buffer *buffer);
        int (*phys) (struct ion_heap *heap, struct ion_buffer *buffer, ion_phys_addr_t *addr, size_t *len);
        struct scatterlist *(*map_dma) (struct ion_heap *heap, struct ion_buffer *buffer);
        void (*unmap_dma) (struct ion_heap *heap, struct ion_buffer *buffer);
        void * (*map_kernel) (struct ion_heap *heap, struct ion_buffer *buffer);
        void (*unmap_kernel) (struct ion_heap *heap, struct ion_buffer *buffer);
        int (*map_user) (struct ion_heap *mapper, struct ion_buffer *buffer, struct vm_area_struct *vma);
};

ION debug

ION provided a debugfs interface in /sys/kernel/debug/ion/ directory.

Each heap has it own debugfs entry where clients memory usage is displayed: /sys/kernel/debug/ion/<<heap name>>

$cat /sys/kernel/debug/ion/ion-heap-1 
          client              pid             size
        test_ion             2890            16384

Each client identify by pid have also a debugfs entry: /sys/kernel/debug/ion/<<pid>> where memory usage displayed.

$cat /sys/kernel/debug/ion/2890 
       heap_name:    size_in_bytes
      ion-heap-1:    40960 11


本文转载自:https://wiki.linaro.org/BenjaminGaignard/ion

Jerikc
粉丝 98
博文 246
码字总数 22757
作品 0
浦东
程序员
私信 提问
Integrating the ION memory allocator

As part of the Android + Graphics micro-conference at the 2013 Linux Plumbers Conference, we'll be discussing the ION memory allocator and how its functionality might be upstrea......

Jerikc
2015/01/14
0
0
The Android ION memory allocator

Back in December 2011, LWN reviewed the list of Android kernel patches in the linux-next staging directory. The merging of these drivers, one of which is a memory allocator call......

Jerikc
2014/10/21
0
0
关于Android中ION的libion

在高通的OpenCL SDK中,其Android.mk文件中,有判断当前kernel的版本,如果大于4.12,那么就使用libion.so,否则则使用ion kernle uapi: 从Andriod P开始,Kernel 4.14已推到AOSP, libion...

willhua
2018/12/07
0
0
H5优化-安卓列表卡顿优化(基于angular 1.2.28)

[问题描述] 近日发现APP部分列表功能在部分安卓机器上有卡顿效果,影响用户体验; 目前APP涉及到的技术点: angular 1.2.28 ionic 1.0.1 with ion-infinite-scroll & ion-refresher 问题: ...

rayph
2016/01/15
39
4
RAMpage攻击:再次利用安卓DRAM RowHammer漏洞

        RAMpage即CVE-2018-9442可以使没有权限的安卓APP利用之前泄漏的Drameer攻击获取root权限。Drameer攻击是针对安卓设备DRAM Rowhammer硬件漏洞的攻击变种。   DRAM RowHammer漏...

嘶吼RoarTalk
2018/07/06
0
0

没有更多内容

加载失败,请刷新页面

加载更多

OSChina 周五乱弹 —— 要步入《攻壳机动队》的世界了么?

Osc乱弹歌单(2019)请戳(这里) 【今日歌曲】 @ nnnm:说到音乐,这段一直是纯音乐在循环。每次听到久石让的Summer时心上都会莫名的感觉着轻松。《菊次郞的夏天》竟然是北野武的片子,没有...

小小编辑
5分钟前
8
0
C++ STL set::find的用法

参考: http://blog.csdn.net/lihao21/article/details/6302196 /* class for function predicate * - operator() returns whether a person is less than another person */ c......

shzwork
15分钟前
0
0
抽象同步队列AQS——AbstractQueuedSynchronizer锁详解

AQS——锁的底层支持 谈到并发,不得不谈ReentrantLock;而谈到ReentrantLock,不得不谈AbstractQueuedSynchronizer(AQS)! 类如其名,抽象的队列式的同步器,AQS定义了一套多线程访问共享资...

须臾之余
今天
3
0
springboot配置百度UEditor 富文本详解

富文本简介 UEditor是由百度web前端研发部开发所见即所得富文本web编辑器,具有轻量,可定制,注重用户体验等特点,开源基于MIT协议,允许自由使用和修改代码... 准备工作 ueditor需要单独文...

wotrd
昨天
4
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部