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]
Date:   Wed, 4 Dec 2019 13:54:50 +0100
From:   Petr Mladek <pmladek@...e.com>
To:     John Ogness <john.ogness@...utronix.de>
Cc:     linux-kernel@...r.kernel.org,
        Peter Zijlstra <peterz@...radead.org>,
        Sergey Senozhatsky <sergey.senozhatsky.work@...il.com>,
        Steven Rostedt <rostedt@...dmis.org>,
        Linus Torvalds <torvalds@...ux-foundation.org>,
        Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
        Andrea Parri <andrea.parri@...rulasolutions.com>,
        Thomas Gleixner <tglx@...utronix.de>,
        Sergey Senozhatsky <sergey.senozhatsky@...il.com>,
        Brendan Higgins <brendanhiggins@...gle.com>,
        kexec@...ts.infradead.org
Subject: Re: [RFC PATCH v5 2/3] printk-rb: new printk ringbuffer
 implementation (reader)

On Tue 2019-12-03 14:46:07, John Ogness wrote:
> On 2019-12-03, Petr Mladek <pmladek@...e.com> wrote:
> >> Add the reader implementation for the new ringbuffer.
> >> 
> >> Signed-off-by: John Ogness <john.ogness@...utronix.de>
> >> ---
> >>  kernel/printk/printk_ringbuffer.c | 234 ++++++++++++++++++++++++++++++
> >>  kernel/printk/printk_ringbuffer.h |  12 +-
> >>  2 files changed, 245 insertions(+), 1 deletion(-)
> >> 
> >> diff --git a/kernel/printk/printk_ringbuffer.c b/kernel/printk/printk_ringbuffer.c
> >> index 09c32e52fd40..f85762713583 100644
> >> --- a/kernel/printk/printk_ringbuffer.c
> >> +++ b/kernel/printk/printk_ringbuffer.c
> >> @@ -674,3 +674,237 @@ void prb_commit(struct prb_reserved_entry *e)
> >>  	local_irq_restore(e->irqflags);
> >>  }
> >>  EXPORT_SYMBOL(prb_commit);
> >> +
> >> +/*
> >> + * Given @blk_lpos, return a pointer to the raw data from the data block
> >> + * and calculate the size of the data part. A NULL pointer is returned
> >> + * if @blk_lpos specifies values that could never be legal.
> >> + *
> >> + * This function (used by readers) performs strict validation on the lpos
> >> + * values to possibly detect bugs in the writer code. A WARN_ON_ONCE() is
> >> + * triggered if an internal error is detected.
> >> + */
> >> +static char *get_data(struct prb_data_ring *data_ring,
> >> +		      struct prb_data_blk_lpos *blk_lpos,
> >> +		      unsigned long *data_size)
> >> +{
> >> +	struct prb_data_block *db;
> >> +
> >> +	if (blk_lpos->begin == INVALID_LPOS &&
> >> +	    blk_lpos->next == INVALID_LPOS) {
> >> +		/* descriptor without a data block */
> >> +		return NULL;
> >> +	} else if (DATA_WRAPS(data_ring, blk_lpos->begin) ==
> >> +		   DATA_WRAPS(data_ring, blk_lpos->next)) {
> >> +		/* regular data block */
> >> +		if (WARN_ON_ONCE(blk_lpos->next <= blk_lpos->begin))
> >> +			return NULL;
> >> +		db = to_block(data_ring, blk_lpos->begin);
> >> +		*data_size = blk_lpos->next - blk_lpos->begin;
> >> +
> >> +	} else if ((DATA_WRAPS(data_ring, blk_lpos->begin) + 1 ==
> >> +		    DATA_WRAPS(data_ring, blk_lpos->next)) ||
> >> +		   ((DATA_WRAPS(data_ring, blk_lpos->begin) ==
> >> +		     DATA_WRAPS(data_ring, -1UL)) &&
> >> +		    (DATA_WRAPS(data_ring, blk_lpos->next) == 0))) {
> >
> > I am a bit confused. I would expect that (-1UL + 1) = 0. So the second
> > condition after || looks just like a special variant of the first
> > valid condition.
> >
> > Or do I miss anything? Is there a problems with type casting?
> 
> Sorry, this code deserves a comment.
> 
> Here we are only comparing the number of wraps. For a wrapping data
> block, @begin will be 1 wrap less than @next. The first part of the
> check is checking the typical case, making sure that:
> 
>    1 + WRAPS(@begin) == WRAPS(@next)
> 
> There is also the case when the lpos overflows. In that case the number
> of wraps starts over at zero (without having overflowed). (Note: The
> lpos overflows, _not_ the number of wraps. This is why the first check
> is not enough.) In this case, the number of wraps of the highest
> possible lpos value (-1UL) should be the same as the number of wraps of
> @begin. And the number of wraps of @next should be 0. The simplified
> pseudo-code check is:
> 
>    WRAPS(@begin) == WRAPS(-1UL)
>       &&
>    WRAPS(@next) == 0

Got it. I knew that it must have been something like this but I did
not see it.

I wonder if the following might be easier to understand even for
people like me ;-)

	} else if (DATA_WRAPS(data_ring, blk_lpos->begin + DATA_SIZE(data_ring)) ==
		    DATA_WRAPS(data_ring, blk_lpos->next)) {

Best Regards,
Petr

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ