Commit 46015e6b authored by Qianfeng Rong's avatar Qianfeng Rong Committed by Florian Westphal
Browse files

netfilter: ebtables: Use vmalloc_array() to improve code

Remove array_size() calls and replace vmalloc() with vmalloc_array() to
simplify the code.  vmalloc_array() is also optimized better, uses fewer
instructions, and handles overflow more concisely[1].

[1]: https://lore.kernel.org/lkml/abc66ec5-85a4-47e1-9759-2f60ab111971@vivo.com/


Signed-off-by: default avatarQianfeng Rong <rongqianfeng@vivo.com>
Signed-off-by: default avatarFlorian Westphal <fw@strlen.de>
parent cd8a4cfa
Loading
Loading
Loading
Loading
+7 −7
Original line number Diff line number Diff line
@@ -920,8 +920,8 @@ static int translate_table(struct net *net, const char *name,
		 * if an error occurs
		 */
		newinfo->chainstack =
			vmalloc(array_size(nr_cpu_ids,
					   sizeof(*(newinfo->chainstack))));
			vmalloc_array(nr_cpu_ids,
				      sizeof(*(newinfo->chainstack)));
		if (!newinfo->chainstack)
			return -ENOMEM;
		for_each_possible_cpu(i) {
@@ -938,7 +938,7 @@ static int translate_table(struct net *net, const char *name,
			}
		}

		cl_s = vmalloc(array_size(udc_cnt, sizeof(*cl_s)));
		cl_s = vmalloc_array(udc_cnt, sizeof(*cl_s));
		if (!cl_s)
			return -ENOMEM;
		i = 0; /* the i'th udc */
@@ -1018,8 +1018,8 @@ static int do_replace_finish(struct net *net, struct ebt_replace *repl,
	 * the check on the size is done later, when we have the lock
	 */
	if (repl->num_counters) {
		unsigned long size = repl->num_counters * sizeof(*counterstmp);
		counterstmp = vmalloc(size);
		counterstmp = vmalloc_array(repl->num_counters,
					    sizeof(*counterstmp));
		if (!counterstmp)
			return -ENOMEM;
	}
@@ -1386,7 +1386,7 @@ static int do_update_counters(struct net *net, const char *name,
	if (num_counters == 0)
		return -EINVAL;

	tmp = vmalloc(array_size(num_counters, sizeof(*tmp)));
	tmp = vmalloc_array(num_counters, sizeof(*tmp));
	if (!tmp)
		return -ENOMEM;

@@ -1526,7 +1526,7 @@ static int copy_counters_to_user(struct ebt_table *t,
	if (num_counters != nentries)
		return -EINVAL;

	counterstmp = vmalloc(array_size(nentries, sizeof(*counterstmp)));
	counterstmp = vmalloc_array(nentries, sizeof(*counterstmp));
	if (!counterstmp)
		return -ENOMEM;