* [PATCH 0/2][next] Use flexible_array_size() helper in memcpy()
@ 2020-07-31 17:12 Gustavo A. R. Silva
2020-07-31 17:12 ` [PATCH 1/2][next] mm: memcontrol: Use flex_array_size() " Gustavo A. R. Silva
2020-07-31 17:13 ` [PATCH 2/2][next] mm: memcontrol: Use the preferred form for passing the size of a structure type Gustavo A. R. Silva
0 siblings, 2 replies; 5+ messages in thread
From: Gustavo A. R. Silva @ 2020-07-31 17:12 UTC (permalink / raw)
To: linux-kernel
Cc: Johannes Weiner, Michal Hocko, Vladimir Davydov, Andrew Morton,
cgroups, linux-mm, Gustavo A. R. Silva
Hi,
This small series aims to:
1. Make use of the flexible_array_size() helper in a call to memcpy()
2. While there, use the preferred form for passing the size of a structure type
Thanks
Gustavo A. R. Silva (2):
mm: memcontrol: Use flex_array_size() helper in memcpy()
mm: memcontrol: Use the preferred form for passing the size of a
structure type
mm/memcontrol.c | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)
--
2.27.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH 1/2][next] mm: memcontrol: Use flex_array_size() helper in memcpy()
2020-07-31 17:12 [PATCH 0/2][next] Use flexible_array_size() helper in memcpy() Gustavo A. R. Silva
@ 2020-07-31 17:12 ` Gustavo A. R. Silva
2020-08-04 13:25 ` Michal Hocko
2020-07-31 17:13 ` [PATCH 2/2][next] mm: memcontrol: Use the preferred form for passing the size of a structure type Gustavo A. R. Silva
1 sibling, 1 reply; 5+ messages in thread
From: Gustavo A. R. Silva @ 2020-07-31 17:12 UTC (permalink / raw)
To: linux-kernel
Cc: Johannes Weiner, Michal Hocko, Vladimir Davydov, Andrew Morton,
cgroups, linux-mm, Gustavo A. R. Silva
Make use of the flex_array_size() helper to calculate the size of a
flexible array member within an enclosing structure.
This helper offers defense-in-depth against potential integer
overflows, while at the same time makes it explicitly clear that
we are dealing with a flexible array member.
Also, remove unnecessary braces.
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
mm/memcontrol.c | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index e84c2b5596f2..bd7f972ceea4 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -4246,10 +4246,9 @@ static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
new->size = size;
/* Copy thresholds (if any) to new array */
- if (thresholds->primary) {
- memcpy(new->entries, thresholds->primary->entries, (size - 1) *
- sizeof(struct mem_cgroup_threshold));
- }
+ if (thresholds->primary)
+ memcpy(new->entries, thresholds->primary->entries,
+ flex_array_size(new, entries, size - 1));
/* Add new threshold */
new->entries[size - 1].eventfd = eventfd;
--
2.27.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 2/2][next] mm: memcontrol: Use the preferred form for passing the size of a structure type
2020-07-31 17:12 [PATCH 0/2][next] Use flexible_array_size() helper in memcpy() Gustavo A. R. Silva
2020-07-31 17:12 ` [PATCH 1/2][next] mm: memcontrol: Use flex_array_size() " Gustavo A. R. Silva
@ 2020-07-31 17:13 ` Gustavo A. R. Silva
2020-08-04 13:25 ` Michal Hocko
1 sibling, 1 reply; 5+ messages in thread
From: Gustavo A. R. Silva @ 2020-07-31 17:13 UTC (permalink / raw)
To: linux-kernel
Cc: Johannes Weiner, Michal Hocko, Vladimir Davydov, Andrew Morton,
cgroups, linux-mm, Gustavo A. R. Silva
Use the preferred form for passing the size of a structure type. The
alternative form where the structure type is spelled out hurts
readability and introduces an opportunity for a bug when the object
type is changed but the corresponding object identifier to which the
sizeof operator is applied is not.
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
mm/memcontrol.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index bd7f972ceea4..bd0f56785841 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -4255,7 +4255,7 @@ static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
new->entries[size - 1].threshold = threshold;
/* Sort thresholds. Registering of new threshold isn't time-critical */
- sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
+ sort(new->entries, size, sizeof(*new->entries),
compare_thresholds, NULL);
/* Find current threshold */
--
2.27.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 1/2][next] mm: memcontrol: Use flex_array_size() helper in memcpy()
2020-07-31 17:12 ` [PATCH 1/2][next] mm: memcontrol: Use flex_array_size() " Gustavo A. R. Silva
@ 2020-08-04 13:25 ` Michal Hocko
0 siblings, 0 replies; 5+ messages in thread
From: Michal Hocko @ 2020-08-04 13:25 UTC (permalink / raw)
To: Gustavo A. R. Silva
Cc: linux-kernel, Johannes Weiner, Vladimir Davydov, Andrew Morton,
cgroups, linux-mm
On Fri 31-07-20 12:12:53, Gustavo A. R. Silva wrote:
> Make use of the flex_array_size() helper to calculate the size of a
> flexible array member within an enclosing structure.
>
> This helper offers defense-in-depth against potential integer
> overflows, while at the same time makes it explicitly clear that
> we are dealing with a flexible array member.
>
> Also, remove unnecessary braces.
I was not aware of this helper. The code looks slightly better.
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Acked-by: Michal Hocko <mhocko@suse.com>
> ---
> mm/memcontrol.c | 7 +++----
> 1 file changed, 3 insertions(+), 4 deletions(-)
>
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index e84c2b5596f2..bd7f972ceea4 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -4246,10 +4246,9 @@ static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
> new->size = size;
>
> /* Copy thresholds (if any) to new array */
> - if (thresholds->primary) {
> - memcpy(new->entries, thresholds->primary->entries, (size - 1) *
> - sizeof(struct mem_cgroup_threshold));
> - }
> + if (thresholds->primary)
> + memcpy(new->entries, thresholds->primary->entries,
> + flex_array_size(new, entries, size - 1));
>
> /* Add new threshold */
> new->entries[size - 1].eventfd = eventfd;
> --
> 2.27.0
--
Michal Hocko
SUSE Labs
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 2/2][next] mm: memcontrol: Use the preferred form for passing the size of a structure type
2020-07-31 17:13 ` [PATCH 2/2][next] mm: memcontrol: Use the preferred form for passing the size of a structure type Gustavo A. R. Silva
@ 2020-08-04 13:25 ` Michal Hocko
0 siblings, 0 replies; 5+ messages in thread
From: Michal Hocko @ 2020-08-04 13:25 UTC (permalink / raw)
To: Gustavo A. R. Silva
Cc: linux-kernel, Johannes Weiner, Vladimir Davydov, Andrew Morton,
cgroups, linux-mm
On Fri 31-07-20 12:13:14, Gustavo A. R. Silva wrote:
> Use the preferred form for passing the size of a structure type. The
> alternative form where the structure type is spelled out hurts
> readability and introduces an opportunity for a bug when the object
> type is changed but the corresponding object identifier to which the
> sizeof operator is applied is not.
>
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Acked-by: Michal Hocko <mhocko@suse.com>
> ---
> mm/memcontrol.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/mm/memcontrol.c b/mm/memcontrol.c
> index bd7f972ceea4..bd0f56785841 100644
> --- a/mm/memcontrol.c
> +++ b/mm/memcontrol.c
> @@ -4255,7 +4255,7 @@ static int __mem_cgroup_usage_register_event(struct mem_cgroup *memcg,
> new->entries[size - 1].threshold = threshold;
>
> /* Sort thresholds. Registering of new threshold isn't time-critical */
> - sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
> + sort(new->entries, size, sizeof(*new->entries),
> compare_thresholds, NULL);
>
> /* Find current threshold */
> --
> 2.27.0
--
Michal Hocko
SUSE Labs
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2020-08-04 13:25 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-31 17:12 [PATCH 0/2][next] Use flexible_array_size() helper in memcpy() Gustavo A. R. Silva
2020-07-31 17:12 ` [PATCH 1/2][next] mm: memcontrol: Use flex_array_size() " Gustavo A. R. Silva
2020-08-04 13:25 ` Michal Hocko
2020-07-31 17:13 ` [PATCH 2/2][next] mm: memcontrol: Use the preferred form for passing the size of a structure type Gustavo A. R. Silva
2020-08-04 13:25 ` Michal Hocko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).