Skip to content

Commit f28e15b

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: x_tables: pass xt_counters struct to counter allocator
Keeps some noise away from a followup patch. Signed-off-by: Florian Westphal <[email protected]> Acked-by: Eric Dumazet <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 4d31eef commit f28e15b

File tree

5 files changed

+34
-38
lines changed

5 files changed

+34
-38
lines changed

include/linux/netfilter/x_tables.h

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -404,32 +404,7 @@ static inline unsigned long ifname_compare_aligned(const char *_a,
404404
}
405405

406406

407-
/* On SMP, ip(6)t_entry->counters.pcnt holds address of the
408-
* real (percpu) counter. On !SMP, its just the packet count,
409-
* so nothing needs to be done there.
410-
*
411-
* xt_percpu_counter_alloc returns the address of the percpu
412-
* counter, or 0 on !SMP. We force an alignment of 16 bytes
413-
* so that bytes/packets share a common cache line.
414-
*
415-
* Hence caller must use IS_ERR_VALUE to check for error, this
416-
* allows us to return 0 for single core systems without forcing
417-
* callers to deal with SMP vs. NONSMP issues.
418-
*/
419-
static inline unsigned long xt_percpu_counter_alloc(void)
420-
{
421-
if (nr_cpu_ids > 1) {
422-
void __percpu *res = __alloc_percpu(sizeof(struct xt_counters),
423-
sizeof(struct xt_counters));
424-
425-
if (res == NULL)
426-
return -ENOMEM;
427-
428-
return (__force unsigned long) res;
429-
}
430-
431-
return 0;
432-
}
407+
bool xt_percpu_counter_alloc(struct xt_counters *counters);
433408
void xt_percpu_counter_free(struct xt_counters *cnt);
434409

435410
static inline struct xt_counters *

net/ipv4/netfilter/arp_tables.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -415,13 +415,10 @@ find_check_entry(struct arpt_entry *e, const char *name, unsigned int size)
415415
{
416416
struct xt_entry_target *t;
417417
struct xt_target *target;
418-
unsigned long pcnt;
419418
int ret;
420419

421-
pcnt = xt_percpu_counter_alloc();
422-
if (IS_ERR_VALUE(pcnt))
420+
if (!xt_percpu_counter_alloc(&e->counters))
423421
return -ENOMEM;
424-
e->counters.pcnt = pcnt;
425422

426423
t = arpt_get_target(e);
427424
target = xt_request_find_target(NFPROTO_ARP, t->u.user.name,

net/ipv4/netfilter/ip_tables.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -539,12 +539,9 @@ find_check_entry(struct ipt_entry *e, struct net *net, const char *name,
539539
unsigned int j;
540540
struct xt_mtchk_param mtpar;
541541
struct xt_entry_match *ematch;
542-
unsigned long pcnt;
543542

544-
pcnt = xt_percpu_counter_alloc();
545-
if (IS_ERR_VALUE(pcnt))
543+
if (!xt_percpu_counter_alloc(&e->counters))
546544
return -ENOMEM;
547-
e->counters.pcnt = pcnt;
548545

549546
j = 0;
550547
mtpar.net = net;

net/ipv6/netfilter/ip6_tables.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -570,12 +570,9 @@ find_check_entry(struct ip6t_entry *e, struct net *net, const char *name,
570570
unsigned int j;
571571
struct xt_mtchk_param mtpar;
572572
struct xt_entry_match *ematch;
573-
unsigned long pcnt;
574573

575-
pcnt = xt_percpu_counter_alloc();
576-
if (IS_ERR_VALUE(pcnt))
574+
if (!xt_percpu_counter_alloc(&e->counters))
577575
return -ENOMEM;
578-
e->counters.pcnt = pcnt;
579576

580577
j = 0;
581578
mtpar.net = net;

net/netfilter/x_tables.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,6 +1615,36 @@ void xt_proto_fini(struct net *net, u_int8_t af)
16151615
}
16161616
EXPORT_SYMBOL_GPL(xt_proto_fini);
16171617

1618+
/**
1619+
* xt_percpu_counter_alloc - allocate x_tables rule counter
1620+
*
1621+
* @counter: pointer to counter struct inside the ip(6)/arpt_entry struct
1622+
*
1623+
* On SMP, the packet counter [ ip(6)t_entry->counters.pcnt ] will then
1624+
* contain the address of the real (percpu) counter.
1625+
*
1626+
* Rule evaluation needs to use xt_get_this_cpu_counter() helper
1627+
* to fetch the real percpu counter.
1628+
*
1629+
* returns false on error.
1630+
*/
1631+
bool xt_percpu_counter_alloc(struct xt_counters *counter)
1632+
{
1633+
void __percpu *res;
1634+
1635+
if (nr_cpu_ids <= 1)
1636+
return true;
1637+
1638+
res = __alloc_percpu(sizeof(struct xt_counters),
1639+
sizeof(struct xt_counters));
1640+
if (!res)
1641+
return false;
1642+
1643+
counter->pcnt = (__force unsigned long)res;
1644+
return true;
1645+
}
1646+
EXPORT_SYMBOL_GPL(xt_percpu_counter_alloc);
1647+
16181648
void xt_percpu_counter_free(struct xt_counters *counters)
16191649
{
16201650
unsigned long pcnt = counters->pcnt;

0 commit comments

Comments
 (0)