linux/drivers/net
Hannes Frederic Sowa 809fa972fd reciprocal_divide: update/correction of the algorithm
Jakub Zawadzki noticed that some divisions by reciprocal_divide()
were not correct [1][2], which he could also show with BPF code
after divisions are transformed into reciprocal_value() for runtime
invariance which can be passed to reciprocal_divide() later on;
reverse in BPF dump ended up with a different, off-by-one K in
some situations.

This has been fixed by Eric Dumazet in commit aee636c480
("bpf: do not use reciprocal divide"). This follow-up patch
improves reciprocal_value() and reciprocal_divide() to work in
all cases by using Granlund and Montgomery method, so that also
future use is safe and without any non-obvious side-effects.
Known problems with the old implementation were that division by 1
always returned 0 and some off-by-ones when the dividend and divisor
where very large. This seemed to not be problematic with its
current users, as far as we can tell. Eric Dumazet checked for
the slab usage, we cannot surely say so in the case of flex_array.
Still, in order to fix that, we propose an extension from the
original implementation from commit 6a2d7a955d resp. [3][4],
by using the algorithm proposed in "Division by Invariant Integers
Using Multiplication" [5], Torbjörn Granlund and Peter L.
Montgomery, that is, pseudocode for q = n/d where q, n, d is in
u32 universe:

1) Initialization:

  int l = ceil(log_2 d)
  uword m' = floor((1<<32)*((1<<l)-d)/d)+1
  int sh_1 = min(l,1)
  int sh_2 = max(l-1,0)

2) For q = n/d, all uword:

  uword t = (n*m')>>32
  q = (t+((n-t)>>sh_1))>>sh_2

The assembler implementation from Agner Fog [6] also helped a lot
while implementing. We have tested the implementation on x86_64,
ppc64, i686, s390x; on x86_64/haswell we're still half the latency
compared to normal divide.

Joint work with Daniel Borkmann.

  [1] http://www.wireshark.org/~darkjames/reciprocal-buggy.c
  [2] http://www.wireshark.org/~darkjames/set-and-dump-filter-k-bug.c
  [3] https://gmplib.org/~tege/division-paper.pdf
  [4] http://homepage.cs.uiowa.edu/~jones/bcd/divide.html
  [5] http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.1.2556
  [6] http://www.agner.org/optimize/asmlib.zip

Reported-by: Jakub Zawadzki <darkjames-ws@darkjames.pl>
Cc: Eric Dumazet <eric.dumazet@gmail.com>
Cc: Austin S Hemmelgarn <ahferroin7@gmail.com>
Cc: linux-kernel@vger.kernel.org
Cc: Jesse Gross <jesse@nicira.com>
Cc: Jamal Hadi Salim <jhs@mojatatu.com>
Cc: Stephen Hemminger <stephen@networkplumber.org>
Cc: Matt Mackall <mpm@selenic.com>
Cc: Pekka Enberg <penberg@kernel.org>
Cc: Christoph Lameter <cl@linux-foundation.org>
Cc: Andy Gospodarek <andy@greyhouse.net>
Cc: Veaceslav Falico <vfalico@redhat.com>
Cc: Jay Vosburgh <fubar@us.ibm.com>
Cc: Jakub Zawadzki <darkjames-ws@darkjames.pl>
Signed-off-by: Daniel Borkmann <dborkman@redhat.com>
Signed-off-by: Hannes Frederic Sowa <hannes@stressinduktion.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-01-21 23:17:20 -08:00
..
appletalk PTR_RET is now PTR_ERR_OR_ZERO(): Replace most. 2013-07-15 11:25:01 +09:30
arcnet drivers/net: delete non-required instances of include <linux/init.h> 2014-01-16 11:53:26 -08:00
bonding reciprocal_divide: update/correction of the algorithm 2014-01-21 23:17:20 -08:00
caif drivers/net: delete non-required instances of include <linux/init.h> 2014-01-16 11:53:26 -08:00
can drivers/net: delete non-required instances of include <linux/init.h> 2014-01-16 11:53:26 -08:00
cris
dsa
ethernet net/mlx4_core: Remove unnecessary validation for port number 2014-01-21 23:14:27 -08:00
fddi drivers/net: delete non-required instances of include <linux/init.h> 2014-01-16 11:53:26 -08:00
hamradio Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/davem/net 2014-01-06 17:37:45 -05:00
hippi drivers/net: delete non-required instances of include <linux/init.h> 2014-01-16 11:53:26 -08:00
hyperv Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/davem/net 2014-01-06 17:37:45 -05:00
ieee802154 net: ieee802154: remove unnecessary spi_set_drvdata() 2013-12-10 22:31:32 -05:00
irda drivers/net: delete non-required instances of include <linux/init.h> 2014-01-16 11:53:26 -08:00
phy phy: cleanup 10g code 2014-01-19 19:55:50 -08:00
plip net: plip: slight optimization of addr compare 2013-12-31 16:48:33 -05:00
ppp ppp: slight optimization of addr compare 2013-12-26 13:31:33 -05:00
slip slip/slcan: added locking in wakeup function 2013-09-20 15:38:26 -04:00
team random32: add prandom_u32_max and convert open coded users 2014-01-21 23:17:20 -08:00
usb Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net 2014-01-18 00:55:41 -08:00
vmxnet3 drivers/net: delete non-required instances of include <linux/init.h> 2014-01-16 11:53:26 -08:00
wan drivers/net: delete non-required instances of include <linux/init.h> 2014-01-16 11:53:26 -08:00
wimax wimax: Remove extern from function prototypes 2013-09-24 16:46:22 -07:00
wireless Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wireless-next into for-davem 2014-01-17 14:43:17 -05:00
xen-netback xen-netback: use new skb_checksum_setup function 2014-01-14 14:24:19 -08:00
Kconfig virtio-net: fix build error when CONFIG_AVERAGE is not enabled 2014-01-17 18:58:38 -08:00
LICENSE.SRC
Makefile packet: nlmon: virtual netlink monitoring device for packet sockets 2013-06-24 16:39:05 -07:00
Space.c drivers: net: Include new header file in sbni.c 2013-12-19 18:51:20 -05:00
dummy.c net: Explicitly initialize u64_stats_sync structures for lockdep 2013-11-06 12:40:25 +01:00
eql.c eql: use __dev_get_by_name instead of dev_get_by_name to find interface 2014-01-14 18:50:46 -08:00
ifb.c net: Explicitly initialize u64_stats_sync structures for lockdep 2013-11-06 12:40:25 +01:00
loopback.c drivers/net: delete non-required instances of include <linux/init.h> 2014-01-16 11:53:26 -08:00
macvlan.c Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net 2014-01-14 14:42:42 -08:00
macvtap.c drivers/net: delete non-required instances of include <linux/init.h> 2014-01-16 11:53:26 -08:00
mdio.c mdio: unused ethtool functions 2013-12-26 13:29:35 -05:00
mii.c
netconsole.c Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net 2013-11-04 13:48:30 -05:00
nlmon.c net: Explicitly initialize u64_stats_sync structures for lockdep 2013-11-06 12:40:25 +01:00
ntb_netdev.c
rionet.c rapidio/rionet: rework driver initialization and removal 2013-07-03 16:08:04 -07:00
sb1000.c
sungem_phy.c
tun.c Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net 2014-01-14 14:42:42 -08:00
veth.c Merge branch 'core-locking-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip 2013-11-14 16:30:30 +09:00
virtio_net.c virtio-net: initial rx sysfs support, export mergeable rx buffer size 2014-01-16 23:46:07 -08:00
vxlan.c net: Add GRO support for vxlan traffic 2014-01-21 18:05:04 -08:00
xen-netfront.c xen-netfront: add support for IPv6 offloads 2014-01-16 16:22:54 -08:00