lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <YwaXvphVpy5A7fSs@yury-laptop>
Date:   Wed, 24 Aug 2022 14:27:26 -0700
From:   Yury Norov <yury.norov@...il.com>
To:     Andy Shevchenko <andy.shevchenko@...il.com>
Cc:     Linus Torvalds <torvalds@...ux-foundation.org>,
        Linux Kernel Mailing List <linux-kernel@...r.kernel.org>,
        Guenter Roeck <linux@...ck-us.net>,
        Dennis Zhou <dennis@...nel.org>,
        Russell King <linux@...linux.org.uk>,
        Catalin Marinas <catalin.marinas@....com>,
        Andy Shevchenko <andriy.shevchenko@...ux.intel.com>,
        Rasmus Villemoes <linux@...musvillemoes.dk>,
        Alexey Klimov <aklimov@...hat.com>,
        Kees Cook <keescook@...omium.org>,
        Andy Whitcroft <apw@...onical.com>
Subject: Re: [PATCH v2 3/3] lib/find_bit: optimize find_next_bit() functions

On Wed, Aug 24, 2022 at 08:56:02PM +0300, Andy Shevchenko wrote:
> On Wed, Aug 24, 2022 at 8:54 PM Andy Shevchenko
> <andy.shevchenko@...il.com> wrote:
> > On Wed, Aug 24, 2022 at 4:53 PM Yury Norov <yury.norov@...il.com> wrote:
> > > On Wed, Aug 24, 2022 at 12:19:05PM +0300, Andy Shevchenko wrote:
> > > > On Wed, Aug 24, 2022 at 4:56 AM Yury Norov <yury.norov@...il.com> wrote:
> 
> ...
> 
> > > > > +#define FIND_NEXT_BIT(EXPRESSION, size, start)                                 \
> > > > > +({                                                                             \
> > > > > +       unsigned long mask, idx, tmp, sz = (size), __start = (start);           \
> > > > > +                                                                               \
> > > > > +       if (unlikely(__start >= sz))                                            \
> > > > > +               goto out;                                                       \
> > > > > +                                                                               \
> > > > > +       mask = word_op(BITMAP_FIRST_WORD_MASK(__start));                        \
> > > > > +       idx = __start / BITS_PER_LONG;                                          \
> > > > > +                                                                               \
> > > > > +       for (tmp = (EXPRESSION) & mask; !tmp; tmp = (EXPRESSION)) {             \
> > > >
> > > > for (unsigned long tmp ...;
> > > > But hey, why not loop over idx (which probably should be named as
> > > > offset)
> > >
> > > Offset in structure, index in array, isn't?
> > >
> > > > as I proposed in the first patch? You will drop a lot of
> > > > divisions / multiplications, no?
> > >
> > > Those divisions and multiplications are optimized away, and
> > > what you suggested blows up the EXPRESSION.
> > >
> > > I tried like this:
> > >    mask = word_op(BITMAP_FIRST_WORD_MASK(__start));
> > >    idx = __start / BITS_PER_LONG;
> > >    tmp = (EXPRESSION);
> > >
> > >    while (1) {
> > >         if (tmp) {
> > >                sz = min(idx * BITS_PER_LONG + __ffs(word_op(tmp)), sz);
> > >                break;
> > >         }
> > >
> > >         if (++idx > sz)
> > >                 break;
> > >
> > >         tmp = (EXPRESSION);
> > >    }
> > >
> > > And it generated the same code, but looks less expressive to me.
> > > If you have some elegant approach in mind - can you please share
> > > it, and how the generated code looks?
> >
> > for (unsigned long idx = 0; idx < sz; idx++) {
> 
> Of source 0 should be changed to whatever start you have there.
> 
> >   unsigned long tmp;
> >
> >   tmp = (EXPRESSION);
> >   if (tmp) {
> >     ...
> >   }
> > }
> >
> > No?

No. For the first iteration, the tmp can't be calculated inside the loop
(my example above is wrong) because we need to clear first bits:

   mask = BITMAP_FIRST_WORD_MASK(__start);
   idx = __start / BITS_PER_LONG;
   tmp = (EXPRESSION) & mask;   // First fetch is here

   while (1) {
        if (tmp) {              // Evaluate here
               sz = min(idx * BITS_PER_LONG + __ffs(tmp), sz);
               break;
        }

        if (++idx > sz)         // Increment here
                break;

        tmp = (EXPRESSION);     // Other fetches here
   }

Trying to move iterator increment inside the for-loop, like you suggested
would break the sequence - common-case word fetch will happen before the
idx++.

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ