Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp578325imu; Thu, 22 Nov 2018 02:08:11 -0800 (PST) X-Google-Smtp-Source: AFSGD/U2CPcX/Q/+4QNhPJZFjoHm77+MO5X/0zBQMqJuPSVinEPr7Kn+3K7p9kpEosOLVzaXxG4y X-Received: by 2002:a62:2c81:: with SMTP id s123mr7271423pfs.174.1542881291585; Thu, 22 Nov 2018 02:08:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542881291; cv=none; d=google.com; s=arc-20160816; b=tFY1wUjvaqREUQuLByArYk+u/Io+PFnRd5ABmCI26rXMUwMJrSntwt13JTowSWhxar S1kYDDCF+XNwwkvXejDMzsXlotvc5Xfmzfd5fqAYrV1L3oVOFvoipnDmcQ4vwvQITAKT KPWzo8ErcZNrxlmDXpU4rUq9UM7omBikbhJpIIXdaK5beEONV5NQMzTTxW1BjboFdqYc rRqvTl3HQLh+nYkWOROMvSQYgunPxHRPXkJnCvHx3xc83rxk+LH0Tkqr6GQhvdQI4g4Q hgmLg1gWvxj/WYV7c96D38zua0aV8SLN91+WJJ4TvcCdgUYnETFo9VfT7HuCYrK0TCOy vpTw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date; bh=r9CbbuPBDUILglbbzH4TozFCH3l9vE9Pj8mvnEQFb/0=; b=fxgdyre0CtRWV2vc4rT2Uxyl2CaUf9hKdsT0N6jg4SxgkzsaRDGI1OuWKALReSktMV I/YjS3u4FEwki5ZKjjbEfGPHF0QGqwGSZpUQ/Em/C7GvYiiNN3Ei4yiluMXLfFCfIasb BNKo7TezkCLY9K78myf6AuGfiCszQ3il21smSUTvq5IbazLyqBqTdxxC3rsQlhfXl5/b YAkVG8xvtX9/7FV6J8WJU2/ZK9Cw4eCbHTEbX8cW8hU94vSXLRLfbQvyIA8a3rg61RX0 IeNfzghG5FkoS6jfw64llkjOoX4HN5yaVRCaY3RywLT0XDaEZVcDGH8bMau6ZHZeYoGT XuQw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h3si27023587pgl.468.2018.11.22.02.07.57; Thu, 22 Nov 2018 02:08:11 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390319AbeKVJ1M (ORCPT + 99 others); Thu, 22 Nov 2018 04:27:12 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:38560 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731554AbeKVJ1M (ORCPT ); Thu, 22 Nov 2018 04:27:12 -0500 Received: from localhost.localdomain (c-24-6-170-16.hsd1.ca.comcast.net [24.6.170.16]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 66527B5F; Wed, 21 Nov 2018 22:50:44 +0000 (UTC) Date: Wed, 21 Nov 2018 14:50:43 -0800 From: Andrew Morton To: Daniel Colascione Cc: linux-kernel , Linux API , Tim Murray , Primiano Tucci , Joel Fernandes , Jonathan Corbet , Mike Rapoport , Vlastimil Babka , Roman Gushchin , Prashant Dhamdhere , "Dennis Zhou (Facebook)" , "Eric W. Biederman" , rostedt@goodmis.org, tglx@linutronix.de, mingo@kernel.org, linux@dominikbrodowski.net, jpoimboe@redhat.com, ard.biesheuvel@linaro.org, Michal Hocko , sfr@canb.auug.org.au, ktsanaktsidis@zendesk.com, David Howells , "open list:DOCUMENTATION" Subject: Re: [PATCH v2] Add /proc/pid_gen Message-Id: <20181121145043.fa029f4f91afddc2a10bb81e@linux-foundation.org> In-Reply-To: References: <20181121201452.77173-1-dancol@google.com> <20181121205428.165205-1-dancol@google.com> <20181121141220.0e533c1dcb4792480efbf3ff@linux-foundation.org> X-Mailer: Sylpheed 3.5.1 (GTK+ 2.24.31; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 21 Nov 2018 14:40:28 -0800 Daniel Colascione wrote: > On Wed, Nov 21, 2018 at 2:12 PM Andrew Morton wrote: > > > > On Wed, 21 Nov 2018 12:54:20 -0800 Daniel Colascione wrote: > > > > > Trace analysis code needs a coherent picture of the set of processes > > > and threads running on a system. While it's possible to enumerate all > > > tasks via /proc, this enumeration is not atomic. If PID numbering > > > rolls over during snapshot collection, the resulting snapshot of the > > > process and thread state of the system may be incoherent, confusing > > > trace analysis tools. The fundamental problem is that if a PID is > > > reused during a userspace scan of /proc, it's impossible to tell, in > > > post-processing, whether a fact that the userspace /proc scanner > > > reports regarding a given PID refers to the old or new task named by > > > that PID, as the scan of that PID may or may not have occurred before > > > the PID reuse, and there's no way to "stamp" a fact read from the > > > kernel with a trace timestamp. > > > > > > This change adds a per-pid-namespace 64-bit generation number, > > > incremented on PID rollover, and exposes it via a new proc file > > > /proc/pid_gen. By examining this file before and after /proc > > > enumeration, user code can detect the potential reuse of a PID and > > > restart the task enumeration process, repeating until it gets a > > > coherent snapshot. > > > > > > PID rollover ought to be rare, so in practice, scan repetitions will > > > be rare. > > > > In general, tracing is a rather specialized thing. Why is this very > > occasional confusion a sufficiently serious problem to warrant addition > > of this code? > > I wouldn't call tracing a specialized thing: it's important enough to > justify its own summit and a whole ecosystem of trace collection and > analysis tools. We use it in every day in Android. It's tremendously > helpful for understanding system behavior, especially in cases where > multiple components interact in ways that we can't readily predict or > replicate. Reliability and precision in this area are essential: > retrospective analysis of difficult-to-reproduce problems involves > puzzling over trace files and testing hypothesis, and when the trace > system itself is occasionally unreliable, the set of hypothesis to > consider grows. I've tried to keep the amount of kernel infrastructure > needed to support this precision and reliability to a minimum, pushing > most of the complexity to userspace. But we do need, from the kernel, > reliable process disambiguation. > > Besides: things like checkpoint and restart are also non-core > features, but the kernel has plenty of infrastructure to support them. > We're talking about a very lightweight feature in this thread. I'm still not understanding the seriousness of the problem. Presumably you've hit problems in real-life which were serious and frequent enough to justify getting down and writing the code. Please share some sob stories with us! > > Which userspace tools will be using pid_gen? Are the developers of > > those tools signed up to use pid_gen? > > I'll be changing Android tracing tools to capture process snapshots > using pid_gen, using the algorithm in the commit message. Which other tools could use this and what was the feedback from their developers? Those people are the intended audience and the best-positioned reviewers so let's hear from them? > > > +u64 read_pid_generation(struct pid_namespace *ns) > > > +{ > > > + u64 generation; > > > + > > > + > > > + spin_lock_irq(&pidmap_lock); > > > + generation = ns->generation; > > > + spin_unlock_irq(&pidmap_lock); > > > + return generation; > > > +} > > > > What is the spinlocking in here for? afaict the only purpose it serves > > is to make the 64-bit read atomic, so it isn't needed on 32-bit? > > ITYM the spinlock is necessary *only* on 32-bit, since 64-bit > architectures have atomic 64-bit reads, and 64-bit reads on 32-bit > architectures can tear. This function isn't a particularly hot path, > so I thought consistency across architectures would be more valuable > than avoiding the lock on some systems. OK.