Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp3302293ybg; Sun, 20 Oct 2019 10:53:22 -0700 (PDT) X-Google-Smtp-Source: APXvYqznKXKroSwMW0TGmooE9uUQZRKvjzW4RQSYxYCtQM6AU6mJD/jaLkfsVWBTbhHjEqUv9HU2 X-Received: by 2002:aa7:d958:: with SMTP id l24mr3179779eds.234.1571594002488; Sun, 20 Oct 2019 10:53:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571594002; cv=none; d=google.com; s=arc-20160816; b=c1Y+gSht+JIXXg/FITz1B3O2mFGHCvhpBT6c4hM0gye4UDlBamxjnxaKKSeIAKac7/ 64E0H3pA78bzwwdoH9hZY8IGzPIGL2lgAHEAT/04/b9r9Os+BaUijLYUNLz2kQ80p/ax WaASJ1MH+XTBOfwPFRq793omxFOpL3nnQSrKbhfk4iivUdoUlNihCBqJtr/mTHcX2z6N p1Py9WCXAKQJJH6Jb2iGn8BcffxhqEzcDLyW2N8quTsJ0CTFwMRLN99h5EotEadnSyyX mwnu/qo6lgTO0H+2Nxzr8VnvT8gDiUfqfBdI4WHbsTAEobZb07pIQI8JYOd+j/Hd1bG9 tLrg== 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=WSFRF0UW8Mb2TMOWpL3QdsSyPzjWvZlmLJU9rKJ+Dro=; b=r1xQgLdOvC+6pkmUWvQaK+3zvd9qo1/Frjzci5QWhIwJ7h/hZjmoT7/7flbiFKzqhr XwVdsc+TLsHnlsx55KhOx208duVRWfMMtiuTY5vT6qAd/raHTvqlFhdZjxPmm5cK3M0K /kdkKEuI7m92GGcoy4Bpv3HQ//foAo9A5Pdy1bsNylVyLJvDEn8uO9GWWa647lQpb5Ie adF51r1qJl846UpCW5uNVw+fHWoHCjZ827CfgD9PNIBOCAKUiO3RfUQGF8tfm3jNIQcp hki2tuPyTSyC6i6lg9h/WgiCFIhq/JTUhMR/01wo5Asc4J0plFjkdpwDaSfdWOpKlvs4 Xu/g== 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 x50si3485871eda.155.2019.10.20.10.52.59; Sun, 20 Oct 2019 10:53:22 -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 S1726884AbfJTRwn (ORCPT + 99 others); Sun, 20 Oct 2019 13:52:43 -0400 Received: from mga09.intel.com ([134.134.136.24]:24807 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726740AbfJTRwc (ORCPT ); Sun, 20 Oct 2019 13:52:32 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 20 Oct 2019 10:52:30 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.67,320,1566889200"; d="scan'208";a="371971239" Received: from tassilo.jf.intel.com (HELO tassilo.localdomain) ([10.7.201.137]) by orsmga005.jf.intel.com with ESMTP; 20 Oct 2019 10:52:30 -0700 Received: by tassilo.localdomain (Postfix, from userid 1000) id 056033002A2; Sun, 20 Oct 2019 10:52:30 -0700 (PDT) From: Andi Kleen To: acme@kernel.org Cc: linux-kernel@vger.kernel.org, jolsa@kernel.org, eranian@google.com, kan.liang@linux.intel.com, peterz@infradead.org, Andi Kleen Subject: [PATCH v2 1/9] perf evsel: Always preserve errno while cleaning up perf_event_open failures Date: Sun, 20 Oct 2019 10:51:54 -0700 Message-Id: <20191020175202.32456-2-andi@firstfloor.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191020175202.32456-1-andi@firstfloor.org> References: <20191020175202.32456-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 abc7fda4a0fe..d831038b55f2 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -1574,7 +1574,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) || @@ -1727,8 +1727,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; @@ -1812,6 +1812,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)); @@ -1819,6 +1820,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