mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Ray Lee" <madrabbit@gmail.com>
To: "Andreas Mohr" <andi@rhlx01.fht-esslingen.de>
Cc: linux-kernel@vger.kernel.org
Subject: Re: ring buffer indices: way too much modulo (division!) fiddling
Date: Tue, 21 Mar 2006 11:51:14 -0800	[thread overview]
Message-ID: <2c0942db0603211151l5db29201p181aac20bb0cf523@mail.gmail.com> (raw)
In-Reply-To: <20060321182806.GA2691@rhlx01.fht-esslingen.de>

On 3/21/06, Andreas Mohr <andi@rhlx01.fht-esslingen.de> wrote:
> "Just One" shocking example (2.6.16 arch/i386/kernel/apm.c):
>
> static apm_event_t get_queued_event(struct apm_user *as)
> {
>         as->event_tail = (as->event_tail + 1) % APM_MAX_EVENTS;
>         return as->events[as->event_tail];
> }
[...]
>      7d7:       83 f9 14                cmp    $0x14,%ecx

APM_MAX_EVENTS appears to be 20, not a power of 2. GCC does the
obvious transform of modulo to an ANDL when the modulus is a sane
value. Rewrite the above as a four line function and run it through
gcc -S to see to difference bewteen APM_MAX_EVENTS=20 and =32.

> Any problems with such a change that I'm missing here?

<shrug> It's probably not a wide-spread problem. How many places in
the kernel use ringbuffers that aren't sized to a power of 2?

> ringbuf_advance_idx(my_idx_var, MY_BUF_SIZE)
> (any more generic name? It's not always about ring buffers...)

modulo_add? But it's probably unneccessary, unless this really is
widespread. Even then, just correcting those already in place would be
better than adding yet another abstraction of a simple operation to
the kernel.

> This kind of unnecessary modulo operation happens in lots of places (mostly
> network drivers).

Check to see if those are power of 2 sized buffers. gcc really will do
those correctly.

Ray

      reply	other threads:[~2006-03-21 19:51 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-03-21 18:28 Andreas Mohr
2006-03-21 19:51 ` Ray Lee [this message]

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=2c0942db0603211151l5db29201p181aac20bb0cf523@mail.gmail.com \
    --to=madrabbit@gmail.com \
    --cc=andi@rhlx01.fht-esslingen.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ray-gmail@madrabbit.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®