Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1990595imu; Fri, 14 Dec 2018 04:10:05 -0800 (PST) X-Google-Smtp-Source: AFSGD/Xt/Y5qM2P/ppNlWdA1Ato/Cc106vHjJXvZIvZW2XamAeG7pywQnASliEkqvA0UfJDrroVa X-Received: by 2002:a62:509b:: with SMTP id g27mr2646792pfj.48.1544789405222; Fri, 14 Dec 2018 04:10:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544789405; cv=none; d=google.com; s=arc-20160816; b=yBU1h/EaRE4ZVNKldv0Nu0aL4NjNT1W9PdQAtdIeB9ppK+ceDh3UZvYn2NiNAfploI 4DpuSiEm7kcVj2E3/tO/6fLZJUBxPCezN9/MY9nNFRr9tBqlG34akEWBiMAtJu/xofD4 21tM7DfhPdmk+lZGRlnS8ffbqgPub2x9Ala0pw0aUgT35GaCwo5ydpXZuDEf5112q1c4 bpN/zdL3iLhFsxjvVdgSOUh+409mm7KKy8PbtOEmu/SVM9AwQsDZPVxHXrGw0WC4EXfA +m/JF+mXfQI9DkvANCdwKpbZSC9b4QFE1qUGiYcY2Jh6QQAqLdsZyE2ulwoh9dEjFI6D MSIg== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=LzWymiaWGuicxKyk0KqeTXBsIaGHhkuiKl0+h6Kk8IA=; b=gaGip9zS0lZqFFk3R53xhX5N/qPxWkJ5x1aflBHImiuJr/w97pKwTEhCJ6pT/yUS6H iNiBfT2ja5UVwzSgGRn7GSFBcWIYj/VknIeAZqJV9oSC61nvaXK9rPd+pcZnPWMxzNmM pp+hVPrRYiO2Gvxhhx6WTmST5Pio3RJOaTRZkC29EYc5830d5rKArKFkd2z8DN2l+EO1 0gbLOLNM5s44wlfEXSUVmMJ0nA7Q9V2NRIJH6+HOWFQV7H0dSFQy8jHJUpkCp6iLGnkN jZqNk+X0YBh3FScgaayQ7GcDE76YP1e3yAAwf6fSCpQLTKqXWojlJrkqMw4SJ9uX0a3M sntQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=hq3vwblw; 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 s5si3770382plr.211.2018.12.14.04.09.46; Fri, 14 Dec 2018 04:10:05 -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; dkim=pass header.i=@kernel.org header.s=default header.b=hq3vwblw; 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 S1731289AbeLNMIe (ORCPT + 99 others); Fri, 14 Dec 2018 07:08:34 -0500 Received: from mail.kernel.org ([198.145.29.99]:53968 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731280AbeLNMIb (ORCPT ); Fri, 14 Dec 2018 07:08:31 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id BB616214AE; Fri, 14 Dec 2018 12:08:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1544789310; bh=jRhRDEZfrcx30+167hz9xPCDphFrjTQq0o9tuViYok0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hq3vwblw8gCjHTmEc73Hs5SLCS87CShgy213gc6sqy3EojOlxwOe/aHHSHBvv2RHO U+8swCszZGcNCq2OpecukhTy+wrMIEKQB9po6q8fR5OzIpNFr4imJZPZZtTutU6Wj7 sZ9Uo7m2UUT2iDfAzulEO2mYHz84DoVNZODUXEGI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jiri Olsa , Alexander Shishkin , Krister Johansen , Namhyung Kim , Peter Zijlstra , Arnaldo Carvalho de Melo , Sasha Levin Subject: [PATCH 4.14 33/89] perf tools: Restore proper cwd on return from mnt namespace Date: Fri, 14 Dec 2018 12:59:46 +0100 Message-Id: <20181214115731.318917308@linuxfoundation.org> X-Mailer: git-send-email 2.20.0 In-Reply-To: <20181214115729.658859279@linuxfoundation.org> References: <20181214115729.658859279@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ [ Upstream commit b01c1f69c8660eaeab7d365cd570103c5c073a02 ] When reporting on 'record' server we try to retrieve/use the mnt namespace of the profiled tasks. We use following API with cookie to hold the return namespace, roughly: nsinfo__mountns_enter(struct nsinfo *nsi, struct nscookie *nc) setns(newns, 0); ... new ns related open.. ... nsinfo__mountns_exit(struct nscookie *nc) setns(nc->oldns) Once finished we setns to old namespace, which also sets the current working directory (cwd) to "/", trashing the cwd we had. This is mostly fine, because we use absolute paths almost everywhere, but it screws up 'perf diff': # perf diff failed to open perf.data: No such file or directory (try 'perf record' first) ... Adding the current working directory to be part of the cookie and restoring it in the nsinfo__mountns_exit call. Signed-off-by: Jiri Olsa Cc: Alexander Shishkin Cc: Krister Johansen Cc: Namhyung Kim Cc: Peter Zijlstra Fixes: 843ff37bb59e ("perf symbols: Find symbols in different mount namespace") Link: http://lkml.kernel.org/r/20181101170001.30019-1-jolsa@kernel.org [ No need to check for NULL args for free(), use zfree() for struct members ] Signed-off-by: Arnaldo Carvalho de Melo Signed-off-by: Sasha Levin --- tools/perf/util/namespaces.c | 17 +++++++++++++++-- tools/perf/util/namespaces.h | 1 + 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/namespaces.c b/tools/perf/util/namespaces.c index 1ef0049860a8..eadc7ddacbf6 100644 --- a/tools/perf/util/namespaces.c +++ b/tools/perf/util/namespaces.c @@ -17,6 +17,7 @@ #include #include #include +#include struct namespaces *namespaces__new(struct namespaces_event *event) { @@ -185,6 +186,7 @@ void nsinfo__mountns_enter(struct nsinfo *nsi, char curpath[PATH_MAX]; int oldns = -1; int newns = -1; + char *oldcwd = NULL; if (nc == NULL) return; @@ -198,9 +200,13 @@ void nsinfo__mountns_enter(struct nsinfo *nsi, if (snprintf(curpath, PATH_MAX, "/proc/self/ns/mnt") >= PATH_MAX) return; + oldcwd = get_current_dir_name(); + if (!oldcwd) + return; + oldns = open(curpath, O_RDONLY); if (oldns < 0) - return; + goto errout; newns = open(nsi->mntns_path, O_RDONLY); if (newns < 0) @@ -209,11 +215,13 @@ void nsinfo__mountns_enter(struct nsinfo *nsi, if (setns(newns, CLONE_NEWNS) < 0) goto errout; + nc->oldcwd = oldcwd; nc->oldns = oldns; nc->newns = newns; return; errout: + free(oldcwd); if (oldns > -1) close(oldns); if (newns > -1) @@ -222,11 +230,16 @@ void nsinfo__mountns_enter(struct nsinfo *nsi, void nsinfo__mountns_exit(struct nscookie *nc) { - if (nc == NULL || nc->oldns == -1 || nc->newns == -1) + if (nc == NULL || nc->oldns == -1 || nc->newns == -1 || !nc->oldcwd) return; setns(nc->oldns, CLONE_NEWNS); + if (nc->oldcwd) { + WARN_ON_ONCE(chdir(nc->oldcwd)); + zfree(&nc->oldcwd); + } + if (nc->oldns > -1) { close(nc->oldns); nc->oldns = -1; diff --git a/tools/perf/util/namespaces.h b/tools/perf/util/namespaces.h index 05d82601c9a6..23584a6dd048 100644 --- a/tools/perf/util/namespaces.h +++ b/tools/perf/util/namespaces.h @@ -36,6 +36,7 @@ struct nsinfo { struct nscookie { int oldns; int newns; + char *oldcwd; }; int nsinfo__init(struct nsinfo *nsi); -- 2.19.1