Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp3222411ybg; Sun, 20 Oct 2019 09:14:31 -0700 (PDT) X-Google-Smtp-Source: APXvYqwdGCuYPgL+9NZA3bwh1bDanP9DtPE8/UL6c75sFRKqiT5IIKVjaQKQsOBV71YSjJ+uADxY X-Received: by 2002:a05:6402:149a:: with SMTP id e26mr20313595edv.123.1571588071011; Sun, 20 Oct 2019 09:14:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571588071; cv=none; d=google.com; s=arc-20160816; b=Rn6WbOSnxPPkos4eM2uZjkSeMRHaf7GjOIsYfIzn0AIKGvUsSm4L8hJVuu+a63eL1M 5ZQM95yt7tDTeMGWh0nIX1OTgadclp/U+lJRvH/voDXTjGuKMx6jDgaRYNvTmU++4xwO 2lWMuY0DK9XVIWdBiMS36uhgEB4atSvZq+WipClyrUzVB7p0IHxMuKfIZA+Crk25Qblt lvIyKbwJxC5TIclSxg41A25GpoY3PSli9nYXYp2ZuseGJay3KBz4dCFaHjqB60Oo6I+a wi0n7sDS6bvDVntLTu61erc7LCM+mDSJ59fe4aU+sqpXRpMq1tq/GthMdLgT4J0Aq5J5 qk6g== 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:date:subject:cc:to:from; bh=WSaKkaUj2WlazIlaem2WcsBgahPLS2e9hzFwgxWgbnE=; b=iow3yDBHteGdLq2BXkndQ1tmubeA+Bs+gLjopxMjLWfkB/cVzP1oU1olSbSfM3ggnw urunzq4YRV66xSY2LgJiA4aI1SHR1scHJU3nkX8dcmbVkck5LQNMlMlwcQb8DzRjQmmp 8f9nx4vMVB60Y8iKhdr75qZQ+VKbwbETMXXTk1eh9YgDuMQedWcuscOSnh3KEHjt8HOt 31suE7CMU7dlLhOyHkD558lH/ypy13FD1t7gnC4UZUlJVQN6jLrCpIFYPgbonijlg50C LPTM654UEKiKDIx0R7zxIA8RzWGncPRLI7fj8CIUZk4yiCbvHe8ZEH63PrfwW0TlyhVk 7SvQ== 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 m10si7513247eds.428.2019.10.20.09.14.08; Sun, 20 Oct 2019 09:14:31 -0700 (PDT) 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 S1726684AbfJTQOC (ORCPT + 99 others); Sun, 20 Oct 2019 12:14:02 -0400 Received: from mga17.intel.com ([192.55.52.151]:25889 "EHLO mga17.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726524AbfJTQN7 (ORCPT ); Sun, 20 Oct 2019 12:13:59 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga107.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 20 Oct 2019 09:13:58 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.67,320,1566889200"; d="scan'208";a="195909355" Received: from tassilo.jf.intel.com (HELO tassilo.localdomain) ([10.7.201.137]) by fmsmga008.fm.intel.com with ESMTP; 20 Oct 2019 09:13:58 -0700 Received: by tassilo.localdomain (Postfix, from userid 1000) id 265D53002A2; Sun, 20 Oct 2019 09:13:58 -0700 (PDT) From: Andi Kleen To: acme@kernel.org Cc: jolsa@kernel.org, eranian@google.com, kan.liang@linux.intel.com, peterz@infradead.org, linux-kernel@vger.kernel.org, Andi Kleen Subject: [PATCH v1 1/9] perf evsel: Always preserve errno while cleaning up perf_event_open failures Date: Sun, 20 Oct 2019 09:13:38 -0700 Message-Id: <20191020161346.18938-2-andi@firstfloor.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191020161346.18938-1-andi@firstfloor.org> References: <20191020161346.18938-1-andi@firstfloor.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andi Kleen In some cases when perf_event_open fails, it may do some closes to clean up. In special cases these closes can fail too, which overwrites the errno of the perf_event_open, which is then incorrectly reported. Save/restore errno around closes. Signed-off-by: Andi Kleen --- tools/perf/util/evsel.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 85825384f9e8..a646975a20f3 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -1740,7 +1740,7 @@ int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus, { int cpu, thread, nthreads; unsigned long flags = PERF_FLAG_FD_CLOEXEC; - int pid = -1, err; + int pid = -1, err, old_errno; enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE; if ((perf_missing_features.write_backward && evsel->core.attr.write_backward) || @@ -1893,8 +1893,8 @@ int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus, */ if (err == -EMFILE && set_rlimit < INCREASED_MAX) { struct rlimit l; - int old_errno = errno; + old_errno = errno; if (getrlimit(RLIMIT_NOFILE, &l) == 0) { if (set_rlimit == NO_CHANGE) l.rlim_cur = l.rlim_max; @@ -1978,6 +1978,7 @@ int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus, if (err) threads->err_thread = thread; + old_errno = errno; do { while (--thread >= 0) { close(FD(evsel, cpu, thread)); @@ -1985,6 +1986,7 @@ int evsel__open(struct evsel *evsel, struct perf_cpu_map *cpus, } thread = nthreads; } while (--cpu >= 0); + errno = old_errno; return err; } -- 2.21.0