devres: Slightly optimize alloc_dr()
authorChristophe JAILLET <christophe.jaillet@wanadoo.fr>
Wed, 20 Jul 2022 21:26:36 +0000 (23:26 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 1 Sep 2022 16:17:14 +0000 (18:17 +0200)
If the gfp flag used for the memory allocation already has __GFP_ZERO,
then there is no need to explicitly clear the "struct devres_node". It is
already zeroed.

This saves a few cycles when using devm_zalloc() and co.

In the case of devres_alloc() (which calls __devres_alloc_node()), the
compiler could remove the test and the memset() because it should be able
to see that the __GFP_ZERO flag is set.
So this would make the code both faster and smaller.

Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
Link: https://lore.kernel.org/r/d255bd871484e63cdd628e819f929e2df59afb02.1658352383.git.christophe.jaillet@wanadoo.fr
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/base/devres.c

index 864d0b3..4ab2b50 100644 (file)
@@ -117,7 +117,9 @@ static __always_inline struct devres * alloc_dr(dr_release_t release,
        if (unlikely(!dr))
                return NULL;
 
-       memset(dr, 0, offsetof(struct devres, data));
+       /* No need to clear memory twice */
+       if (!(gfp & __GFP_ZERO))
+               memset(dr, 0, offsetof(struct devres, data));
 
        INIT_LIST_HEAD(&dr->node.entry);
        dr->node.release = release;