From: Andrew Morton <akpm@osdl.org>
To: Philippe Elie <phil.el@wanadoo.fr>
Cc: linux-kernel@vger.kernel.org, levon@movementarian.org
Subject: Re: [PATCH] oprofile per-cpu buffer overrun
Date: Sun, 25 Jan 2004 20:07:01 -0800 [thread overview]
Message-ID: <20040125200701.3c7b769a.akpm@osdl.org> (raw)
In-Reply-To: <20040126023715.GA3166@zaniah>
Philippe Elie <phil.el@wanadoo.fr> wrote:
>
> Hi Andrew,
>
> In a ring buffer controlled by a read and write positions we
> can't use buffer_size but only buffer_size - 1 entry,
you can, actually.
> the last
> free entry act as a guard to avoid write pos overrun. This bug
> was hidden because the writer, oprofile_add_sample(), request
> one more entry than really needed.
>
>...
> diff -u -p -r1.9 cpu_buffer.c
> --- drivers/oprofile/cpu_buffer.c 26 May 2003 04:42:54 -0000 1.9
> +++ drivers/oprofile/cpu_buffer.c 24 Jan 2004 21:07:03 -0000
> @@ -86,9 +86,9 @@ static unsigned long nr_available_slots(
> unsigned long tail = b->tail_pos;
>
> if (tail > head)
> - return tail - head;
> + return (tail - head) - 1;
>
> - return tail + (b->buffer_size - head);
> + return tail + (b->buffer_size - head) - 1;
> }
When implementing a circular buffer it is better to not constrain the head
and tail indices - just let them grow and wrap without bound. You only need
to bring them in-bounds when you actually use them to index the buffer.
This way,
- head-tail is always the amount of used space, no need to futz around
handling the case where one has wrapped and the other hasn't.
- you get to use all of the buffer, because the cases head-tail == 0
(empty) and head-tail == bufsize (full) are now distinguishable.
It helps if the buffer size is a power of two, of course, but integer
modulus is pretty quick.
All the net drivers and the printk log buffer implement their ring buffers
in this way; it works nicely.
next prev parent reply other threads:[~2004-01-26 4:06 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-01-26 2:37 [PATCH] oprofile per-cpu buffer overrun Philippe Elie
2004-01-26 4:07 ` Andrew Morton [this message]
2004-01-26 5:56 ` Anton Blanchard
2004-01-26 10:32 ` John Levon
2004-01-26 11:07 ` Nick Piggin
2004-01-26 15:52 ` Philippe Elie
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=20040125200701.3c7b769a.akpm@osdl.org \
--to=akpm@osdl.org \
--cc=levon@movementarian.org \
--cc=linux-kernel@vger.kernel.org \
--cc=phil.el@wanadoo.fr \
/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.