Introduce devm_kmemdup_array() helper
PATCH 1/5 devres: Introduce devm_kmemdup_array()
In reply to Raag JadavIntroduce '_array' variant of devm_kmemdup() for the users which lack multiplication overflow check.
--- include/linux/device.h | 10 ++++++++++ 1 file changed, 10 insertions(+)
include/linux/device.h +10 -0
@@ -358,6 +358,16 @@ char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) __malloc;const char *devm_kstrdup_const (struct device *dev,const char *s,gfp_t gfp);void *devm_kmemdup (struct device *dev,const void *src,size_t len,gfp_t gfp)__realloc_size (3 ); +static inline void *devm_kmemdup_array (struct device *dev,const void *src, +size_t n,size_t size,gfp_t flags) +{ +size_t bytes; + +if (unlikely (check_mul_overflow (n, size, &bytes))) +return NULL ; + +return devm_kmemdup (dev, src, bytes, flags); +}unsigned long devm_get_free_pages (struct device *dev,gfp_t gfp_mask,unsigned int order);
Signature · 1 line
2.35.3
cover Introduce devm_kmemdup_array() helper
This series introduces devm_kmemdup_array() helper with multiplication overflow check and uses it across drivers.
Raag Jadav (5):
devres: Introduce devm_kmemdup_array()
pinctrl: intel: copy communities using devm_kmemdup_array()
pinctrl: pxa2xx: use devm_kmemdup_array()
input: sparse-keymap: use devm_kmemdup_array()
ASoC: Intel: avs: use devm_kmemdup_array()
drivers/input/sparse-keymap.c | 3 +-- drivers/pinctrl/intel/pinctrl-intel.c | 6 ++---- drivers/pinctrl/pxa/pinctrl-pxa2xx.c | 8 ++++---- include/linux/device.h | 10 ++++++++++ sound/soc/intel/avs/boards/da7219.c | 3 ++- sound/soc/intel/avs/boards/es8336.c | 3 ++- sound/soc/intel/avs/boards/nau8825.c | 3 ++- sound/soc/intel/avs/boards/rt274.c | 3 ++- sound/soc/intel/avs/boards/rt286.c | 3 ++- sound/soc/intel/avs/boards/rt298.c | 3 ++- sound/soc/intel/avs/boards/rt5663.c | 3 ++- sound/soc/intel/avs/boards/rt5682.c | 2 +- 12 files changed, 32 insertions(+), 18 deletions(-)
Signature · 1 line
2.35.3
PATCH 4/5 input: sparse-keymap: use devm_kmemdup_array()
Convert to use devm_kmemdup_array() which is more robust.
--- drivers/input/sparse-keymap.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/input/sparse-keymap.c +1 -2
@@ -176,8 +176,7 @@ int sparse_keymap_setup(struct input_dev *dev,for (e = keymap; e->type !=KE_END ; e++) map_size++; - map =devm_kmemdup (&dev->dev, keymap, map_size *sizeof (*map), -GFP_KERNEL ); + map =devm_kmemdup_array (&dev->dev, keymap, map_size,sizeof (*map),GFP_KERNEL );if (!map)return -ENOMEM ;
Signature · 1 line
2.35.3
PATCH 2/5 pinctrl: intel: copy communities using devm_kmemdup_array()
In reply to Raag JadavCopy communities using devm_kmemdup_array() instead of doing it manually.
--- drivers/pinctrl/intel/pinctrl-intel.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/pinctrl/intel/pinctrl-intel.c +2 -4
@@ -1577,8 +1577,8 @@ int intel_pinctrl_probe(struct platform_device *pdev,* to the registers. */ pctrl->ncommunities = pctrl->soc->ncommunities; - pctrl->communities =devm_kcalloc (dev, pctrl->ncommunities, -sizeof (*pctrl->communities),GFP_KERNEL ); + pctrl->communities =devm_kmemdup_array (dev, pctrl->soc->communities, pctrl->ncommunities, +sizeof (*pctrl->communities),GFP_KERNEL );if (!pctrl->communities)return -ENOMEM ;
@@ -1588,8 +1588,6 @@ int intel_pinctrl_probe(struct platform_device *pdev,u32 offset;u32 value; - *community = pctrl->soc->communities[i]; - regs =devm_platform_ioremap_resource (pdev, community->barno);if (IS_ERR (regs))return PTR_ERR (regs);
Signature · 1 line
2.35.3
PATCH 3/5 pinctrl: pxa2xx: use devm_kmemdup_array()
In reply to Raag JadavConvert to use devm_kmemdup_array() which is more robust.
--- drivers/pinctrl/pxa/pinctrl-pxa2xx.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/pinctrl/pxa/pinctrl-pxa2xx.c +4 -4
@@ -281,9 +281,8 @@ static int pxa2xx_build_functions(struct pxa_pinctrl *pctl)for (df = pctl->ppins[i].functions; df->name; df++)if (!pxa2xx_find_function (pctl, df->name, functions)) (functions + pctl->nfuncs++)->name = df->name; - pctl->functions =devm_kmemdup (pctl->dev, functions, - pctl->nfuncs *sizeof (*functions), -GFP_KERNEL ); + pctl->functions =devm_kmemdup_array (pctl->dev, functions, pctl->nfuncs, +sizeof (*functions),GFP_KERNEL );if (!pctl->functions)return -ENOMEM ;
@@ -314,7 +313,8 @@ static int pxa2xx_build_groups(struct pxa_pinctrl *pctl) pctl->ppins[j].pin.name; func = pctl->functions + i; func->ngroups = ngroups; - func->groups =devm_kmemdup (pctl->dev, gtmp, ngroups *sizeof (*gtmp),GFP_KERNEL ); + func->groups =devm_kmemdup_array (pctl->dev, gtmp, ngroups, +sizeof (*gtmp),GFP_KERNEL );if (!func->groups)return -ENOMEM ; }
Signature · 1 line
2.35.3
PATCH 5/5 ASoC: Intel: avs: use devm_kmemdup_array()
In reply to Raag JadavConvert to use devm_kmemdup_array() which is more robust.
--- sound/soc/intel/avs/boards/da7219.c | 3 ++- sound/soc/intel/avs/boards/es8336.c | 3 ++- sound/soc/intel/avs/boards/nau8825.c | 3 ++- sound/soc/intel/avs/boards/rt274.c | 3 ++- sound/soc/intel/avs/boards/rt286.c | 3 ++- sound/soc/intel/avs/boards/rt298.c | 3 ++- sound/soc/intel/avs/boards/rt5663.c | 3 ++- sound/soc/intel/avs/boards/rt5682.c | 2 +- 8 files changed, 15 insertions(+), 8 deletions(-)
sound/soc/intel/avs/boards/da7219.c +2 -1
@@ -113,7 +113,8 @@ static int avs_da7219_codec_init(struct snd_soc_pcm_runtime *runtime) } num_pins =ARRAY_SIZE (card_headset_pins); - pins =devm_kmemdup (card->dev, card_headset_pins,sizeof (*pins) * num_pins,GFP_KERNEL ); + pins =devm_kmemdup_array (card->dev, card_headset_pins, num_pins, +sizeof (*pins),GFP_KERNEL );if (!pins)return -ENOMEM ;
sound/soc/intel/avs/boards/es8336.c +2 -1
@@ -109,7 +109,8 @@ static int avs_es8336_codec_init(struct snd_soc_pcm_runtime *runtime) data =snd_soc_card_get_drvdata (card); num_pins =ARRAY_SIZE (card_headset_pins); - pins =devm_kmemdup (card->dev, card_headset_pins,sizeof (*pins) * num_pins,GFP_KERNEL ); + pins =devm_kmemdup_array (card->dev, card_headset_pins, num_pins, +sizeof (*pins),GFP_KERNEL );if (!pins)return -ENOMEM ;
sound/soc/intel/avs/boards/nau8825.c +2 -1
@@ -88,7 +88,8 @@ static int avs_nau8825_codec_init(struct snd_soc_pcm_runtime *runtime) jack =snd_soc_card_get_drvdata (card); num_pins =ARRAY_SIZE (card_headset_pins); - pins =devm_kmemdup (card->dev, card_headset_pins,sizeof (*pins) * num_pins,GFP_KERNEL ); + pins =devm_kmemdup_array (card->dev, card_headset_pins, num_pins, +sizeof (*pins),GFP_KERNEL );if (!pins)return -ENOMEM ;
sound/soc/intel/avs/boards/rt274.c +2 -1
@@ -98,7 +98,8 @@ static int avs_rt274_codec_init(struct snd_soc_pcm_runtime *runtime) jack =snd_soc_card_get_drvdata (card); num_pins =ARRAY_SIZE (card_headset_pins); - pins =devm_kmemdup (card->dev, card_headset_pins,sizeof (*pins) * num_pins,GFP_KERNEL ); + pins =devm_kmemdup_array (card->dev, card_headset_pins, num_pins, +sizeof (*pins),GFP_KERNEL );if (!pins)return -ENOMEM ;
sound/soc/intel/avs/boards/rt286.c +2 -1
@@ -59,7 +59,8 @@ static int avs_rt286_codec_init(struct snd_soc_pcm_runtime *runtime) jack =snd_soc_card_get_drvdata (card); num_pins =ARRAY_SIZE (card_headset_pins); - pins =devm_kmemdup (card->dev, card_headset_pins,sizeof (*pins) * num_pins,GFP_KERNEL ); + pins =devm_kmemdup_array (card->dev, card_headset_pins, num_pins, +sizeof (*pins),GFP_KERNEL );if (!pins)return -ENOMEM ;
sound/soc/intel/avs/boards/rt298.c +2 -1
@@ -70,7 +70,8 @@ static int avs_rt298_codec_init(struct snd_soc_pcm_runtime *runtime) jack =snd_soc_card_get_drvdata (card); num_pins =ARRAY_SIZE (card_headset_pins); - pins =devm_kmemdup (card->dev, card_headset_pins,sizeof (*pins) * num_pins,GFP_KERNEL ); + pins =devm_kmemdup_array (card->dev, card_headset_pins, num_pins, +sizeof (*pins),GFP_KERNEL );if (!pins)return -ENOMEM ;
sound/soc/intel/avs/boards/rt5663.c +2 -1
@@ -65,7 +65,8 @@ static int avs_rt5663_codec_init(struct snd_soc_pcm_runtime *runtime) jack = &priv->jack; num_pins =ARRAY_SIZE (card_headset_pins); - pins =devm_kmemdup (card->dev, card_headset_pins,sizeof (*pins) * num_pins,GFP_KERNEL ); + pins =devm_kmemdup_array (card->dev, card_headset_pins, num_pins, +sizeof (*pins),GFP_KERNEL );if (!pins)return -ENOMEM ;
sound/soc/intel/avs/boards/rt5682.c +1 -1
@@ -102,7 +102,7 @@ static int avs_rt5682_codec_init(struct snd_soc_pcm_runtime *runtime) jack =snd_soc_card_get_drvdata (card); num_pins =ARRAY_SIZE (card_jack_pins); - pins =devm_kmemdup (card->dev, card_jack_pins,sizeof (*pins) * num_pins,GFP_KERNEL ); + pins =devm_kmemdup_array (card->dev, card_jack_pins, num_pins,sizeof (*pins),GFP_KERNEL );if (!pins)return -ENOMEM ;
Signature · 1 line
2.35.3
Dmitry Torokhov
maintainerRe: [PATCH v1 1/5] devres: Introduce devm_kmemdup_array()
In reply to Raag JadavHi Raag,
Quoted 2 lines · Raag Jadav
Introduce '
_array' variant ofdevm_kmemdup()for the users which lack multiplication overflow check.
I am not sure that this new helper is needed. Unlike allocators for brand new objects, such as kmalloc_array(), devm_kmemdup() makes a copy of already existing object, which is supposed to be a valid object and therefore will have a reasonable size. So there should be no chance for hitting this overflow unless the caller is completely confused and calls devm_kmemdup() with random arguments (in which case all bets are off).
Quoted 31 lines
Suggested-by Andy Shevchenko <[email protected]>Signed-off-by Raag Jadav <[email protected]>--- include/linux/device.h | 10 ++++++++++ 1 file changed, 10 insertions(+)include/linux/device.h +10
-0@@ -358,6 +358,16 @@ char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) __malloc;const char *devm_kstrdup_const (struct device *dev,const char *s,gfp_t gfp);void *devm_kmemdup (struct device *dev,const void *src,size_t len,gfp_t gfp)__realloc_size (3 ); +static inline void *devm_kmemdup_array (struct device *dev,const void *src, +size_t n,size_t size,gfp_t flags) +{ +size_t bytes; + +if (unlikely (check_mul_overflow (n, size, &bytes))) +return NULL ; + +return devm_kmemdup (dev, src, bytes, flags); +}unsigned long devm_get_free_pages (struct device *dev,gfp_t gfp_mask,unsigned int order);Signature · 1 line
2.35.3
Thanks.
Signature · 1 line
Dmitry
Re: [PATCH v1 1/5] devres: Introduce devm_kmemdup_array()
Quoted 10 lines · Dmitry Torokhov
Quoted 2 lines · Raag Jadav
Introduce '
_array' variant ofdevm_kmemdup()for the users which lack multiplication overflow check.I am not sure that this new helper is needed. Unlike allocators for brand new objects, such as
kmalloc_array(),devm_kmemdup()makes a copy of already existing object, which is supposed to be a valid object and therefore will have a reasonable size. So there should be no chance for hitting this overflow unless the caller is completely confused and callsdevm_kmemdup()with random arguments (in which case all bets are off).
Don't we want to have a code more robust even if all what you say applies? Also this makes the call consistent with zillions of others from the alloc family of calls in the Linux kernel.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v1 1/5] devres: Introduce devm_kmemdup_array()
Quoted 12 lines · Andy Shevchenko
Quoted 10 lines · Dmitry Torokhov
Quoted 2 lines · Raag Jadav
Introduce '
_array' variant ofdevm_kmemdup()for the users which lack multiplication overflow check.I am not sure that this new helper is needed. Unlike allocators for brand new objects, such as
kmalloc_array(),devm_kmemdup()makes a copy of already existing object, which is supposed to be a valid object and therefore will have a reasonable size. So there should be no chance for hitting this overflow unless the caller is completely confused and callsdevm_kmemdup()with random arguments (in which case all bets are off).
Quoted 3 lines
Don't we want to have a code more robust even if all what you say applies? Also this makes the call consistent with zillions of others from the alloc family of calls in the Linux kernel.
Agree. Although shooting in the foot is never the expectation, it is atleast better than having to debug such unexpected cases.
Raag
Dmitry Torokhov
maintainerRe: [PATCH v1 1/5] devres: Introduce devm_kmemdup_array()
Quoted 13 lines · Raag Jadav
Quoted 12 lines · Andy Shevchenko
Quoted 10 lines · Dmitry Torokhov
Quoted 2 lines · Raag Jadav
Introduce '
_array' variant ofdevm_kmemdup()for the users which lack multiplication overflow check.I am not sure that this new helper is needed. Unlike allocators for brand new objects, such as
kmalloc_array(),devm_kmemdup()makes a copy of already existing object, which is supposed to be a valid object and therefore will have a reasonable size. So there should be no chance for hitting this overflow unless the caller is completely confused and callsdevm_kmemdup()with random arguments (in which case all bets are off).
Quoted 3 lines
Quoted 3 lines
Don't we want to have a code more robust even if all what you say applies? Also this makes the call consistent with zillions of others from the alloc family of calls in the Linux kernel.
Having a clean API is fine, just do not bill it as something that is "safer". As I mentioned, unlike other allocators this one is supposed to operate with a valid source object and size passed to devm_kmemdup() should not exceed the size of the source object. There is no chance of overflowing.
Quoted 3 lines
Agree. Although shooting in the foot is never the expectation, it is atleast better than having to debug such unexpected cases.
Then maybe have a BUG() there instead of returning NULL? I know BUG()s are frowned upon, but I think in this case overflow is really an indicator of a hard error by the caller which is passing garbage arguments to this function.
Hm, I see we have kmemdup_array() already. Ok. How about making your devm_kmemdup_array() be similar to kmemdup_array()?
static inline void *devm_kmemdup_array(struct device *dev, const void *src,
size_t n, size_t size, gfp_t flags)
{
return devm_kmemdup(dev, src, size_mul(size, n), flags);
}This will trigger a warning on a too large order of allocation in mm/page_alloc.c::__alloc_pages_noprof().
Thanks.
Signature · 1 line
Dmitry
Re: [PATCH v1 1/5] devres: Introduce devm_kmemdup_array()
Quoted 4 lines · Dmitry Torokhov
Quoted 3 lines · Raag Jadav
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Dmitry Torokhov
On Sun, Nov 24, 2024 at 01:35:23AM +0530, Raag Jadav wrote:
...
Quoted 14 lines
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Quoted 2 lines
Introduce '
_array' variant ofdevm_kmemdup()for the users which lack multiplication overflow check.I am not sure that this new helper is needed. Unlike allocators for brand new objects, such as
kmalloc_array(),devm_kmemdup()makes a copy of already existing object, which is supposed to be a valid object and therefore will have a reasonable size. So there should be no chance for hitting this overflow unless the caller is completely confused and callsdevm_kmemdup()with random arguments (in which case all bets are off).Quoted 3 lines
Quoted 3 lines
Don't we want to have a code more robust even if all what you say applies? Also this makes the call consistent with zillions of others from the alloc family of calls in the Linux kernel.
Quoted 5 lines
Having a clean API is fine, just do not bill it as something that is "safer". As I mentioned, unlike other allocators this one is supposed to operate with a valid source object and size passed to
devm_kmemdup()should not exceed the size of the source object. There is no chance of overflowing.
Agree.
Quoted 11 lines
Quoted 2 lines
Agree. Although shooting in the foot is never the expectation, it is atleast better than having to debug such unexpected cases.
Then maybe have a
BUG()there instead of returning NULL? I know BUG()s are frowned upon, but I think in this case overflow is really an indicator of a hard error by the caller which is passing garbage arguments to this function.Hm, I see we have
kmemdup_array()already. Ok. How about making yourdevm_kmemdup_array()be similar tokmemdup_array()?
Quoted 8 lines
static inline void *devm_kmemdup_array(struct device *dev, const void *src,
size_tn,size_tsize,gfp_tflags){ return devm_kmemdup(dev, src, size_mul(size, n), flags); }This will trigger a warning on a too large order of allocation in mm/page_alloc.c::__alloc_pages_noprof().
This is nice! I have overlooked that kmemdup_array() uses size_mul() instead of a check. Raag, can you rebuild your series on this?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v1 5/5] ASoC: Intel: avs: use devm_kmemdup_array()
In reply to Raag JadavQuoted 1 line · Raag Jadav
Convert to use
devm_kmemdup_array()which is more robust.
Re: [PATCH v1 1/5] devres: Introduce devm_kmemdup_array()
In reply to Andy ShevchenkoQuoted 47 lines · Andy Shevchenko
Quoted 4 lines · Dmitry Torokhov
Quoted 3 lines · Raag Jadav
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Dmitry Torokhov
On Sun, Nov 24, 2024 at 01:35:23AM +0530, Raag Jadav wrote:
...
Quoted 14 lines
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Quoted 2 lines
Introduce '
_array' variant ofdevm_kmemdup()for the users which lack multiplication overflow check.I am not sure that this new helper is needed. Unlike allocators for brand new objects, such as
kmalloc_array(),devm_kmemdup()makes a copy of already existing object, which is supposed to be a valid object and therefore will have a reasonable size. So there should be no chance for hitting this overflow unless the caller is completely confused and callsdevm_kmemdup()with random arguments (in which case all bets are off).Quoted 3 lines
Quoted 3 lines
Don't we want to have a code more robust even if all what you say applies? Also this makes the call consistent with zillions of others from the alloc family of calls in the Linux kernel.
Quoted 5 lines
Having a clean API is fine, just do not bill it as something that is "safer". As I mentioned, unlike other allocators this one is supposed to operate with a valid source object and size passed to
devm_kmemdup()should not exceed the size of the source object. There is no chance of overflowing.Agree.
Quoted 17 lines
Quoted 2 lines
Agree. Although shooting in the foot is never the expectation, it is atleast better than having to debug such unexpected cases.
Then maybe have a
BUG()there instead of returning NULL? I know BUG()s are frowned upon, but I think in this case overflow is really an indicator of a hard error by the caller which is passing garbage arguments to this function.Hm, I see we have
kmemdup_array()already. Ok. How about making yourdevm_kmemdup_array()be similar tokmemdup_array()?static inline void *devm_kmemdup_array(struct device *dev, const void *src,
size_tn,size_tsize,gfp_tflags){ return devm_kmemdup(dev, src, size_mul(size, n), flags); }
Quoted 5 lines
Quoted 2 lines
This will trigger a warning on a too large order of allocation in mm/page_alloc.c::__alloc_pages_noprof().
This is nice! I have overlooked that
kmemdup_array()usessize_mul()instead of a check. Raag, can you rebuild your series on this?
Sure.
Raag