Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 15 Jun 2003 11:25:57 -0700 (PDT)
From:      Don Lewis <truckman@FreeBSD.org>
To:        iedowse@maths.tcd.ie
Cc:        freebsd-arch@FreeBSD.org
Subject:   Re: Message buffer and printf reentrancy patch
Message-ID:  <200306151826.h5FIPvM7046944@gw.catspoiler.org>
In-Reply-To: <200306151406.aa36218@salmon.maths.tcd.ie>

next in thread | previous in thread | raw e-mail | index | archive | help
On 15 Jun, Ian Dowse wrote:
> 
> Below is a patch that makes the implementation of the kernel message
> buffer mostly reentrant and more generic, and stops printf() ever
> calling directly into the tty code. This should fix panics that can
> occur via tputchar() when using xconsole, and generally make the
> use of printf() in the kernel a bit safer. Many of the ideas here
> were suggested by Bruce Evans.
> 
> A summary of the changes:
>  - Use atomic operations to update the message buffer pointers.
>  - Use a kind of sequence number for the pointers instead of just
>    the offset into the buffer, as this avoids the need for the read
>    code to touch the write pointer or the write code to touch the
>    read pointer.

> +#define MSGBUF_SEQNORM(mbp, seq) ((seq) % (mbp)->msg_seqmod + ((seq) < 0 ? \
> +    (mbp)->msg_seqmod : 0))
> +#define MSGBUF_SEQ_TO_POS(mbp, seq) ((int)((u_int)(seq) % \
> +    (u_int)(mbp)->msg_size))
> +#define MSGBUF_SEQSUB(mbp, seq1, seq2) (MSGBUF_SEQNORM(mbp, (seq1) - (seq2)))
> +

According to my copy of K&R, there is no guarantee that ((negative_int %
postive_int) <= 0) on all platforms, though this is generally true.

If the sequence numbers wrap, there will be a discontinuity in the
sequence of normalized sequence numbers unless msg_seqmod evenly divides
the full integer range, which would indicate that msg_seqmod needs to be
a power of two on the platforms of interest.

Integer division is fairly slow operation for most CPUs, so why not just
enforce the power of two constraint and just grab the bottom bits of the
sequence numbers using a bitwise logical operation to normalize?



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200306151826.h5FIPvM7046944>