All of lore.kernel.org
 help / color / mirror / Atom feed
From: Edward Cree <ecree@solarflare.com>
To: Alexei Starovoitov <ast@fb.com>,
	David Miller <davem@davemloft.net>,
	Daniel Borkmann <daniel@iogearbox.net>
Cc: <alexei.starovoitov@gmail.com>, <netdev@vger.kernel.org>
Subject: Re: [PATCH v2 1/3] bpf: Use 1<<16 as ceiling for immediate alignment in verifier.
Date: Thu, 18 May 2017 17:38:45 +0100	[thread overview]
Message-ID: <739ef342-728c-de57-c2a2-03fa85b3a246@solarflare.com> (raw)
In-Reply-To: <c8e4ce49-5707-3d6d-c8c2-80dd8335d981@solarflare.com>

On 18/05/17 15:49, Edward Cree wrote:
> Here's one idea that seemed to work when I did a couple of experiments:
> let A = (a;am), B = (b;bm) where the m are the masks
> Σ = am + bm + a + b
> χ = Σ ^ (a + b) /* unknown carries */
> μ = χ | am | bm /* mask of result */
> then A + B = ((a + b) & ~μ; μ)
>
> The idea is that we find which bits change between the case "all x are
>  1" and "all x are 0", and those become xs too.
And now I've found a similar algorithm for subtraction, which (again) I
 can't prove but it seems to work.
α = a + am - b
β = a - b - bm
χ = α ^ β
μ = χ | α | β
then A - B = ((a - b) & ~μ; μ)
Again we're effectively finding the max. and min. values, and XORing
 them to find unknown carries.

Bitwise operations are easy, of course;
/* By assumption, a & am == b & bm == 0 */
A & B = (a & b; (a | am) & (b | bm) & ~(a & b))
A | B = (a | b; (am | bm) & ~(a | b))
/* It bothers me that & and | aren't symmetric, but I can't fix it */
A ^ B = (a ^ b; am | bm)

as are shifts by a constant (just shift 0s into both number and mask).

Multiplication by a constant can be done by decomposing into shifts
 and adds; but it can also be done directly; here we find (a;am) * k.
π = a * k
γ = am * k
then A * k = (π; 0) + (0; γ), for which we use our addition algo.

Multiplication of two unknown values is a nightmare, as unknown bits
 can propagate all over the place.  We can do a shift-add
 decomposition where the adds for unknown bits have all the 1s in
 the addend replaced with xs.  A few experiments suggest that this
 works, regardless of the order of operands.  For instance
 110x * x01 comes out as either
    110x
+ xx0x
= xxxx0x
or
     x0x
   x01
+ x01
= xxxx0x
We can slightly optimise this by handling all the 1 bits in one go;
 that is, for (a;am) * (b;bm) we first find (a;am) * b using our
 multiplication-by-a-constant algo above, then for each bit in bm
 we find (a;am) * bit and force all its nonzero bits to unknown;
 finally we add all our components.

Don't even ask about division; that scrambles bits so hard that the
 only thing you can say for sure is that the leading 0s in the
 numerator stay 0 in the result.  The only exception is divisions
 by a constant which can be converted into a shift, or divisions
 of a constant by another constant; if the numerator has any xs and
 the denominator has more than one 1, everything to the right of the
 first x is totally unknown in general.

-Ed

  reply	other threads:[~2017-05-18 16:39 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-15 16:04 [PATCH v2 1/3] bpf: Use 1<<16 as ceiling for immediate alignment in verifier David Miller
2017-05-16 12:37 ` Edward Cree
2017-05-16 19:52   ` David Miller
2017-05-16 22:53   ` Alexei Starovoitov
2017-05-17 14:00     ` Edward Cree
2017-05-17 15:33       ` Edward Cree
2017-05-18  0:16         ` David Miller
2017-05-18  1:18           ` Alexei Starovoitov
2017-05-18 14:10           ` Edward Cree
2017-05-18  2:48         ` Alexei Starovoitov
2017-05-18 14:49           ` Edward Cree
2017-05-18 16:38             ` Edward Cree [this message]
2017-05-18 18:41               ` Edward Cree
2017-05-19  1:22               ` Alexei Starovoitov
2017-05-19 14:21                 ` Edward Cree
2017-05-19 14:55                   ` Alexei Starovoitov
2017-05-19 17:17                     ` Edward Cree
2017-05-19 20:00                       ` Alignment in BPF verifier Edward Cree
2017-05-19 20:39                         ` David Miller
2017-05-19 23:05                           ` Daniel Borkmann
2017-05-23 14:41                             ` Edward Cree
2017-05-23 17:43                               ` Edward Cree
2017-05-23 23:59                                 ` Alexei Starovoitov
2017-05-23 19:45                               ` Alexei Starovoitov
2017-05-23 21:27                                 ` Daniel Borkmann
2017-05-24 13:46                                   ` Edward Cree
2017-05-24 16:39                                     ` Alexei Starovoitov
2017-05-25 16:31                                   ` David Miller
2017-05-19 20:48                         ` David Miller
2017-05-19 20:41                       ` [PATCH v2 1/3] bpf: Use 1<<16 as ceiling for immediate alignment in verifier David Miller
2017-05-19 21:37                         ` Alexei Starovoitov
2017-05-19 23:16                           ` David Miller
2017-05-20  0:20                             ` Alexei Starovoitov
2017-05-17 16:13       ` David Miller
2017-05-17 17:00         ` Edward Cree
2017-05-17 17:25           ` David Miller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=739ef342-728c-de57-c2a2-03fa85b3a246@solarflare.com \
    --to=ecree@solarflare.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=ast@fb.com \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.