Commit c26aed40 authored by Eric Dumazet's avatar Eric Dumazet Committed by David S. Miller

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: default avatarEric Dumazet <eric.dumazet@gmail.com>
Acked-by: default avatarChangli Gao <xiaosuo@gmail.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 8c1592d6
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
#include <asm/uaccess.h> #include <asm/uaccess.h>
#include <asm/unaligned.h> #include <asm/unaligned.h>
#include <linux/filter.h> #include <linux/filter.h>
#include <linux/reciprocal_div.h>
enum { enum {
BPF_S_RET_K = 1, BPF_S_RET_K = 1,
...@@ -205,7 +206,7 @@ unsigned int sk_run_filter(struct sk_buff *skb, const struct sock_filter *fentry ...@@ -205,7 +206,7 @@ unsigned int sk_run_filter(struct sk_buff *skb, const struct sock_filter *fentry
A /= X; A /= X;
continue; continue;
case BPF_S_ALU_DIV_K: case BPF_S_ALU_DIV_K:
A /= K; A = reciprocal_divide(A, K);
continue; continue;
case BPF_S_ALU_AND_X: case BPF_S_ALU_AND_X:
A &= X; A &= X;
...@@ -506,6 +507,7 @@ int sk_chk_filter(struct sock_filter *filter, int flen) ...@@ -506,6 +507,7 @@ int sk_chk_filter(struct sock_filter *filter, int flen)
/* check for division by zero */ /* check for division by zero */
if (ftest->k == 0) if (ftest->k == 0)
return -EINVAL; return -EINVAL;
ftest->k = reciprocal_value(ftest->k);
break; break;
case BPF_S_LD_MEM: case BPF_S_LD_MEM:
case BPF_S_LDX_MEM: case BPF_S_LDX_MEM:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment