Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756381Ab0FIRed (ORCPT ); Wed, 9 Jun 2010 13:34:33 -0400 Received: from THUNK.ORG ([69.25.196.29]:48551 "EHLO thunker.thunk.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752734Ab0FIRec (ORCPT ); Wed, 9 Jun 2010 13:34:32 -0400 Date: Wed, 9 Jun 2010 13:34:24 -0400 From: tytso@mit.edu To: Linus Torvalds Cc: Ingo Molnar , Salman , Andrew Morton , peterz@infradead.org, akpm@inux-foundation.org, Linux Kernel Mailing List , tytso@google.com, Thomas Gleixner Subject: Re: [PATCH] Fix a race in pid generation that causes pids to be reused immediately. Message-ID: <20100609173424.GF6162@thunk.org> Mail-Followup-To: tytso@mit.edu, Linus Torvalds , Ingo Molnar , Salman , Andrew Morton , peterz@infradead.org, akpm@inux-foundation.org, Linux Kernel Mailing List , tytso@google.com, Thomas Gleixner References: <20100609062438.29081.91635.stgit@bumblebee1.mtv.corp.google.com> <20100609094850.GA23292@elte.hu> <20100609155041.GD6162@thunk.org> <20100609171015.GE6162@thunk.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.20 (2009-06-14) X-SA-Exim-Connect-IP: X-SA-Exim-Mail-From: tytso@thunk.org X-SA-Exim-Scanned: No (on thunker.thunk.org); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1547 Lines: 41 On Wed, Jun 09, 2010 at 10:25:50AM -0700, Linus Torvalds wrote: > > > On Wed, 9 Jun 2010, Linus Torvalds wrote: > > > > Otherwise you have three threads, two of which pick the same pid (because > > the test-and-set isn't atomic), and a third of which picks a new one. > > In fact, I don't think you need three threads at all. It's perfectly ok to > just have two threads, and they'd both end up picking the same 'pid' > without the atomicity guarantees of that 'test_and_set()' bitmap access. > > And they'd both be perfectly fine setting last_pid to that (shared) pid if > I read that cmpxchg loop right. No? Well, I was thinking about something like this: while (1) { last = pid_ns->last_pid; pid = last + 1; if (pid >= pid_max) pid = RESERVED_PIDS; if (cmpxchg(&pid_ns->last_pid, last, pid) == last) return pid; } Which I don't think is racy, unless I'm missing something. Both might end up picking the same pid, but only one will successfully set last_pid, and the other will just loop and try again. There appears to be some interesting uses of the bitmap by find_ge_pid() and next_pidmap() that I haven't completely grokked yet, especially as to why they're needed, though. Assuming they are needed, we might end up needing the bitmap after all, though. - Ted -- 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/