In my case, I am allocating memory like this:

    struct ion_allocation_data arg_alloc;

    arg_alloc.len = len;
    arg_alloc.heap_mask = heap_mask;
    arg_alloc.flags = flags;
    arg_alloc.fd = 0;

    ret = ioctl(client, ION_IOC_ALLOC_V1, &arg_alloc);

In kernel sources I found that heap id is calculated as:

    heap_id = 1 << MAGIC_NUMBER;

MAGIC_NUMBER here is heap id (which is not equal to heap type). This MAGIC_NUMBER is calculated during kernel's boot, thanks to Laura Abbott:

SHA 2f87f50b by Laura Abbott, 04/18/2017 09:27 PM committed by Greg Kroah-Hartman, 04/18/2017 09:43 PM parent eb9751db

staging: android: ion: Rework heap registration/enumeration

The current model of Ion heap registration is based on the outdated model of board files. The replacement for board files (devicetree) isn't a good replacement for what Ion wants to do. In actuality, Ion wants to show what memory is available in the system for something else to figure out what to use. Switch to a model where Ion creates its device unconditionally and heaps are registed as available regions. Currently, only system and CMA heaps are converted over to the new model. Carveout and chunk heaps can be converted over when someone wants to figure out how.

Signed-off-by: Laura Abbott Signed-off-by: Greg Kroah-Hartman

I want to use DMA heap. Type of this heap is defined in enumeration:

    typedef enum
    * must be last so device specific heaps always
    * are at the end of this enum
    } ion_heap_type;

Now is a problem, ION_HEAP_TYPE_DMA is enumerated here as 4, and this number in not heap id. Right heap id mask, which corresponds to this heap type, in my case, is equal to (1 << 1) (once more, thanks to Laura).

So, again, the question is: "How to allocate ION's memory of predefined heap type? How to allocate the same memory on different platforms?".

  • As I can see, this thread is dead. Can anyone give me advice about how to contact Linux developers? – lol lol Feb 27 at 8:42
  • I wrote an e-mail to Abbot, but she was ignoring me. It's pity. Anyway I found the answer. – lol lol Feb 28 at 8:26

ION's driver contains ioctl's command "ION_IOC_HEAP_QUERY" argument which may be used to get information about heaps (name, type, id, etc.), enabled on concrete platform. Example of implementation was found on the following link:

int ion_query_heap_cnt(int fd, int* cnt) {
    int ret;
    struct ion_heap_query query;
    memset(&query, 0, sizeof(query));
    ret = ion_ioctl(fd, ION_IOC_HEAP_QUERY, &query);
    if (ret < 0) return ret;
    *cnt = query.cnt;
    return ret;

int ion_query_get_heaps(int fd, int cnt, void* buffers) {
    int ret;
    struct ion_heap_query query = {
        .cnt = cnt, .heaps = (uintptr_t)buffers,
    ret = ion_ioctl(fd, ION_IOC_HEAP_QUERY, &query);
    return ret;

Example of use for this API is found here:

static int find_ion_heap_id(int ion_client, char* name)
    int i, ret, cnt, heap_id = -1;
    struct ion_heap_data *data;
    ret = ion_query_heap_cnt(ion_client, &cnt);
    if (ret)
        AERR("ion count query failed with %s", strerror(errno));
        return -1;
    data = (struct ion_heap_data *)malloc(cnt * sizeof(*data));
    if (!data)
        AERR("Error allocating data %s\n", strerror(errno));
        return -1;
    ret = ion_query_get_heaps(ion_client, cnt, data);
    if (ret)
        AERR("Error querying heaps from ion %s", strerror(errno));
        for (i = 0; i < cnt; i++) {
            struct ion_heap_data *dat = (struct ion_heap_data *)data;
            if (strcmp(dat[i].name, name) == 0) {
                heap_id = dat[i].heap_id;
        if (i > cnt)
            AERR("No System Heap Found amongst %d heaps\n", cnt);
            heap_id = -1;
    return heap_id;

Following function may be simply rewritten to get heap_id from heap_type.

After we've got heap_id we need to calculate:

heap_mask = (1 << heap_id);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.