diff options
author | Eric Dumazet <eric.dumazet@gmail.com> | 2010-11-18 17:04:46 -0500 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2010-11-19 13:06:54 -0500 |
commit | c26aed40f4fd18f86bcc6aba557cab700b129b73 (patch) | |
tree | e723d358ddd6facc3af4f37be02fabcf71e3e605 /net | |
parent | 8c1592d68bc89248bfd0ee287648f41c1370d826 (diff) |
filter: use reciprocal divide
At compile time, we can replace the DIV_K instruction (divide by a
constant value) by a reciprocal divide.
At exec time, the expensive divide is replaced by a multiply, a less
expensive operation on most processors.
Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com>
Acked-by: Changli Gao <xiaosuo@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net')
-rw-r--r-- | net/core/filter.c | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/net/core/filter.c b/net/core/filter.c index c0b68f7c8036..23e0a2a9a4de 100644 --- a/net/core/filter.c +++ b/net/core/filter.c | |||
@@ -37,6 +37,7 @@ | |||
37 | #include <asm/uaccess.h> | 37 | #include <asm/uaccess.h> |
38 | #include <asm/unaligned.h> | 38 | #include <asm/unaligned.h> |
39 | #include <linux/filter.h> | 39 | #include <linux/filter.h> |
40 | #include <linux/reciprocal_div.h> | ||
40 | 41 | ||
41 | enum { | 42 | enum { |
42 | BPF_S_RET_K = 1, | 43 | BPF_S_RET_K = 1, |
@@ -205,7 +206,7 @@ unsigned int sk_run_filter(struct sk_buff *skb, const struct sock_filter *fentry | |||
205 | A /= X; | 206 | A /= X; |
206 | continue; | 207 | continue; |
207 | case BPF_S_ALU_DIV_K: | 208 | case BPF_S_ALU_DIV_K: |
208 | A /= K; | 209 | A = reciprocal_divide(A, K); |
209 | continue; | 210 | continue; |
210 | case BPF_S_ALU_AND_X: | 211 | case BPF_S_ALU_AND_X: |
211 | A &= X; | 212 | A &= X; |
@@ -506,6 +507,7 @@ int sk_chk_filter(struct sock_filter *filter, int flen) | |||
506 | /* check for division by zero */ | 507 | /* check for division by zero */ |
507 | if (ftest->k == 0) | 508 | if (ftest->k == 0) |
508 | return -EINVAL; | 509 | return -EINVAL; |
510 | ftest->k = reciprocal_value(ftest->k); | ||
509 | break; | 511 | break; |
510 | case BPF_S_LD_MEM: | 512 | case BPF_S_LD_MEM: |
511 | case BPF_S_LDX_MEM: | 513 | case BPF_S_LDX_MEM: |