From owner-freebsd-arch@FreeBSD.ORG Sun Jun 15 11:26:06 2003 Return-Path: Delivered-To: freebsd-arch@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 99AC437B401 for ; Sun, 15 Jun 2003 11:26:06 -0700 (PDT) Received: from gw.catspoiler.org (217-ip-163.nccn.net [209.79.217.163]) by mx1.FreeBSD.org (Postfix) with ESMTP id EAED743F75 for ; Sun, 15 Jun 2003 11:26:05 -0700 (PDT) (envelope-from truckman@FreeBSD.org) Received: from FreeBSD.org (mousie.catspoiler.org [192.168.101.2]) by gw.catspoiler.org (8.12.9/8.12.9) with ESMTP id h5FIPvM7046944; Sun, 15 Jun 2003 11:26:01 -0700 (PDT) (envelope-from truckman@FreeBSD.org) Message-Id: <200306151826.h5FIPvM7046944@gw.catspoiler.org> Date: Sun, 15 Jun 2003 11:25:57 -0700 (PDT) From: Don Lewis To: iedowse@maths.tcd.ie In-Reply-To: <200306151406.aa36218@salmon.maths.tcd.ie> MIME-Version: 1.0 Content-Type: TEXT/plain; charset=us-ascii cc: freebsd-arch@FreeBSD.org Subject: Re: Message buffer and printf reentrancy patch X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 15 Jun 2003 18:26:06 -0000 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?