Re: [PATCH 2/3] drm/i915: Refactor duplicate object vmap functions

Chris Wilson
2015-10-08T13:06:41Z

On Thu, Oct 08, 2015 at 03:58:13PM +0300, Ville Syrjälä wrote:
> > + pages = kmalloc(n*sizeof(*pages), GFP_TEMPORARY);
>
> Random driveby: kmalloc_array()

That would be scary, imagine having enough pages in the object to
overflow unsigned long :)

> Also __GFP_NOWARN?

True.

> I wonder if the drm_malloc stuff should do the kmalloc attempt
> regardless fo the size, so we wouldn't have to do it here?

It's a pattern I've reused in a few places, so probably worthy of being
refactored. Something like

-static __inline__ void *drm_malloc_ab(size_t nmemb, size_t size)
+static __inline__ void *drm_malloc_ab(size_t nmemb, size_t size, gfp_t gfp)
{
if (size != 0 && nmemb > SIZE_MAX / size)
return NULL;

if (size * nmemb <= PAGE_SIZE)
- return kmalloc(nmemb * size, GFP_KERNEL);
+ return kmalloc(nmemb * size, gfp);
+
+ if (gfp & GFP_TEMPORARY) {
+ void *ptr = kmalloc(nmemb * size, gfp | __GFP_NOWARN);
+ if (ptr)
+ return ptr;
+ }

return __vmalloc(size * nmemb,
- GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
+ gfp | __GFP_HIGHMEM, PAGE_KERNEL);
}

Would need to call it my_drm_malloc_ab() to avoid having to update
everywhere.
-Chris

--
Chris Wilson, Intel Open Source Technology Centre