lkml.org 
[lkml]   [2019]   [Dec]   [20]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: [RFC PATCH v1] devres: align devres.data strictly only for devm_kmalloc()
On Fri, Dec 20, 2019 at 11:19:27AM +0100, Marc Gonzalez wrote:
> Would anyone else have any suggestions, comments, insights, recommendations,
> improvements, guidance, or wisdom? :-)

Flip devres upside down!

**WARNING, wear protective glasses when reading the below**


struct devres {
struct devres_node node;
void *data;
};

/*
* We place struct devres at the tail of the memory allocation
* such that data retains the ARCH_KMALLOC_MINALIGN alignment.
* struct devres itself is just 4 pointers and should therefore
* only require trivial alignment.
*/
static inline struct devres *data2devres(void *data)
{
return (struct devres *)(data + ksize(data) - sizeof(struct devres));
}

void *alloc_dr(...)
{
struct devres *dr;
void *data;

data = kmalloc(size + sizeof(struct devres), GFP_KERNEL);
dr = data2devres(data);
WARN_ON((unsigned long)dr & __alignof(*dr)-1);
INIT_LIST_HEAD(&dr->node.entry);
dr->node.release = release;
dr->data = data;

return dr;
}

void devres_free(void *data)
{
if (data) {
struct devres *dr = data2devres(data);
BUG_ON(!list_empty(dr->node.entry));
kfree(data);
}
}

static int release_nodes(...)
{
...
list_for_each_entry_safe_reverse(dr, ...) {
...
kfree(dr->data);
}
}

\
 
 \ /
  Last update: 2019-12-20 15:07    [W:0.087 / U:1.556 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site