Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758372Ab0FIVeJ (ORCPT ); Wed, 9 Jun 2010 17:34:09 -0400 Received: from casper.infradead.org ([85.118.1.10]:58340 "EHLO casper.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754736Ab0FIVeH convert rfc822-to-8bit (ORCPT ); Wed, 9 Jun 2010 17:34:07 -0400 Subject: Re: [PATCH] Fix a race in pid generation that causes pids to be reused immediately. From: Peter Zijlstra To: Linus Torvalds Cc: Salman , akpm@linux-foundation.org, mingo@elte.hu, linux-kernel@vger.kernel.org, tytso@google.com In-Reply-To: References: <20100609210014.7464.92234.stgit@bumblebee1.mtv.corp.google.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8BIT Date: Wed, 09 Jun 2010 23:33:27 +0200 Message-ID: <1276119207.1745.154.camel@laptop> Mime-Version: 1.0 X-Mailer: Evolution 2.28.3 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1744 Lines: 50 On Wed, 2010-06-09 at 14:21 -0700, Linus Torvalds wrote: > > On Wed, 9 Jun 2010, Salman wrote: > > +/* > > + * If we started walking pids at 'base', is 'a' seen before 'b'? > > + * > > + */ > > +static int pid_before(int base, int a, int b) > > +{ > > + int a_lt_b = (a < b); > > + int min_a_b = min(a, b); > > + int max_a_b = max(a, b); > > + > > + if ((base <= min_a_b) || (base >= max_a_b)) > > + return a_lt_b; > > + > > + return !a_lt_b; > > +} > > Ok, so that's a very confusing expression. I'm sure it gets the right > value, but it's not exactly straightforward, is it? > > Wouldn't it be nicer to write it out in a more straightforward way? > Something like > > /* a and b in order? base must not be between them */ > if (a <= b) > return (base <= a || base >= b); > /* b < a? We reach 'a' first iff base is between them */ > return base >= b && base <= a; > > would seem to be equivalent and easier to explain, no? > > And when you write it that way, it looks like the compiler should be able > to trivially CSE the five comparisons down to just three (notice how the > "base <= a" and "base >= b" comparisons are repeated. Which I'm sure some > super-optimizing compiler can do from your version too, but mine seems > more straightforward. > > But maybe I did that thing wrong, and I just confused myself. I have _not_ > checked the logic deeply, somebody else should definitely double-check me. Isn't: return a - base < b - base, the natural way to express this? -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/