Re: [RFC][PATCH 1/5 v2] dma-buf: Add dma-buf heaps framework
From: Andrew F. Davis
Date: Tue Mar 19 2019 - 11:24:19 EST
On 3/19/19 7:08 AM, Brian Starkey wrote:
> Hi John,
>
> On Tue, Mar 05, 2019 at 12:54:29PM -0800, John Stultz wrote:
>> From: "Andrew F. Davis" <afd@xxxxxx>
>
> [snip]
>
>> +
>> +#define NUM_HEAP_MINORS 128
>> +static DEFINE_IDR(dma_heap_idr);
>> +static DEFINE_MUTEX(minor_lock); /* Protect idr accesses */
>
> I saw that Matthew Wilcox is trying to nuke idr:
> https://patchwork.freedesktop.org/series/57073/
>
> Perhaps a different data structure could be considered? (I don't have
> an informed opinion on which).
>
Looks like XArray is the suggested replacement. Should be easy enough,
the minor number would just index to our heap struct directly, I'll give
it a shot and see.
>> +
>> +dev_t dma_heap_devt;
>> +struct class *dma_heap_class;
>> +struct list_head dma_heap_list;
>> +struct dentry *dma_heap_debug_root;
>> +
>> +static int dma_heap_buffer_alloc(struct dma_heap *heap, size_t len,
>> + unsigned int flags)
>> +{
>> + len = PAGE_ALIGN(len);
>> + if (!len)
>> + return -EINVAL;
>
> I think aligning len to pages only makes sense if heaps are going to
> allocate aligned to pages too. Perhaps that's an implicit assumption?
> If so, lets document it.
>
> Why not let the heaps take care of aligning len however they want
> though?
>
This is how I originally had it, but I think we couldn't find any case
where you would want an the start or end of a buffer to not fall on a
page boundary here. It would only lead to problems. As you say though,
nothing keeping us from moving that into the heaps themselves.
> ...
>
>> +
>> +int dma_heap_add(struct dma_heap *heap)
>> +{
>> + struct device *dev_ret;
>> + int ret;
>> +
>> + if (!heap->name || !strcmp(heap->name, "")) {
>> + pr_err("dma_heap: Cannot add heap without a name\n");
>> + return -EINVAL;
>> + }
>> +
>> + if (!heap->ops || !heap->ops->allocate) {
>> + pr_err("dma_heap: Cannot add heap with invalid ops struct\n");
>> + return -EINVAL;
>> + }
>> +
>> + /* Find unused minor number */
>> + mutex_lock(&minor_lock);
>> + ret = idr_alloc(&dma_heap_idr, heap, 0, NUM_HEAP_MINORS, GFP_KERNEL);
>> + mutex_unlock(&minor_lock);
>> + if (ret < 0) {
>> + pr_err("dma_heap: Unable to get minor number for heap\n");
>> + return ret;
>> + }
>> + heap->minor = ret;
>> +
>> + /* Create device */
>> + heap->heap_devt = MKDEV(MAJOR(dma_heap_devt), heap->minor);
>> + dev_ret = device_create(dma_heap_class,
>> + NULL,
>> + heap->heap_devt,
>> + NULL,
>> + heap->name);
>> + if (IS_ERR(dev_ret)) {
>> + pr_err("dma_heap: Unable to create char device\n");
>> + return PTR_ERR(dev_ret);
>> + }
>> +
>> + /* Add device */
>> + cdev_init(&heap->heap_cdev, &dma_heap_fops);
>> + ret = cdev_add(&heap->heap_cdev, dma_heap_devt, NUM_HEAP_MINORS);
>
> Shouldn't this be s/dma_heap_devt/heap->heap_devt/ and a count of 1?
>
Hmm, strange this ever worked before..
> Also would it be better to have cdev_add/device_create the other way
> around? First create the char device, then once it's all set up
> register it with sysfs.
>
Yes that does seem to be more common, lets flip it.
>> + if (ret < 0) {
>> + device_destroy(dma_heap_class, heap->heap_devt);
>> + pr_err("dma_heap: Unable to add char device\n");
>> + return ret;
>> + }
>> +
>> + return 0;
>> +}
>> +EXPORT_SYMBOL(dma_heap_add);
>
> Until we've figured out how modules are going to work, I still think
> it would be a good idea to not export this.
>
Agree.
Andrew
> Cheers,
> -Brian
>