Hi,
I am trying to export an ion_buffer allocate from kernel space to multiple user-space clients. Eg: Allow multiple process to mmap framebuffer allocated using ion by fb driver. The following is the pseudo-code for that. Is this fine? there a cleaner way to do it?
Or is it expected to share buffers across process only by user-space sharing fds using sockets/binder and not directly in kernel.
fb driver init/probe: (init process context) ------------------------------------------------- /* Create an ion client and allocate framebuffer */ init_client = ion_client_create(idev,...); init_hdl = ion_alloc(init_client,...); /* Create a global dma_buf instance for the buffer */ fd = ion_share_dma_buf(init_client, init_hdl); // - Inc refcount of ion_buffer // - Create a dma_buf and anon-file for the ion buffer // - Get a free fd and install to anon file g_dma_buf = dma_buf_get(fd); // - Get the dma_buf pointer and inc refcount of anon_file dma_buf_put(g_dma_buf); // - Dec extra refcount of anon_file which happened in prev command put_unused_fd(fd); // - Free up the fd as fd is not exported to user-space here.
fb driver exit: (init process context) ------------------------------------------ /* Free the dma_buf reference */ dma_buf_put(g_dma_buf); // - Dec refcount of anon_file. Free the dma_buf and dec refcount of ion_buffer if anon_file refcount = 0 /* Free the framebuffer and destroy the ion client created for init process */ ion_free(init_client, init_hdl); ion_client_destroy(init_client);
fb device open: (user process context) ----------------------------------------------- /* Create an ion client for the user process */ p_client = ion_client_create(idev,...);
fb device ioctl to import ion handle for the fb: (user process context) ----------------------------------------------------------------------------------- /* Import a ion_handle from the global dma_buf */ fd = dma_buf_fd(g_dmabuf, O_CLOEXEC); // - Get ref to anon file // - Get a free fd and install to anon file p_hdl = ion_import_dma_buf(p_client, fd); // - Inc refcount of ion_buffer // - create a ion_handle for the buffer for this process/client dma_buf_put(g_dmabuf); // - Free the anon file reference taken in first step put_unused_fd(fd); // - Free up the fd as fd is not exported to user-space here.
fb device release: (user process context) --------------------------------------------------- /* Destroy the client created */ ion_client_destroy(p_client);
- Nishanth Peethambaran