Received: by 10.192.165.156 with SMTP id m28csp51926imm; Tue, 10 Apr 2018 16:12:11 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+MzQ02tdIgKQZ3yl5vIMn+9wSk/en7OoD4kBwPHpLhYLOZg79hAM+FeAZJPIWCmPvvQHhj X-Received: by 2002:a17:902:758d:: with SMTP id j13-v6mr2467977pll.334.1523401931780; Tue, 10 Apr 2018 16:12:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523401931; cv=none; d=google.com; s=arc-20160816; b=W0QXgsAbuFmpXdiweKIAIVe3vbstmpvH4FcuckTa5qCQWRGVQwRgwTmI7kMs5VwpJ/ V+SBFEugBv04a1UH33ozNiT4jJP7PdIRYaIoEh8k7hIEgPi51dTsmjOxYMv7ednWFbYD aNGE262NJoFV63bhgIY4V8zPEaD9/SqLglm+aHOGxavqspUE4HP/HbN0BTL0gzxZaFN5 dCtyNLjxEtkrd42ctgzGYXHo94+Cz/J7yVgDOlfRg/rjnj2TEukbabej74tL0BQ1JdF7 rvnlzjDCQzjD8yVOcSwgWDXHT4NcrXb1UzVYVZT5Dh4dOeYCsfv0oEcadZ6TTZeG4xsA naMg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=rDwNVi5Nx2aFGsoOmQbRM34Iu1azgd6IfyfsDkIJ6vg=; b=kgwUiGUNLepYNVdy9wJnVyHqj512vhNhjkxW3J8NiPOJsaLPxPvPBEariCQm66UNnV 1YyDkmran9m2+/QlznlAHqeljeAQ8OCkXB0Kd6aRk2/FaSpUl5DX0WzIIODsk/pxoy7T sf9vNcarRO02yxUxzUtu8W9Y3enur7ynbV41ceKuP5zbwJmwA+wMRZH7OlvRB6NCUE8c wlaPAkWK0qWnBDSGbvOMGy4f6YZ2sRjt4fVknougvCBNKtwtwWyHVfsomxSrT5fsM7hP nJ5Z08cusMPHCKr0qAsl45j3b0HIew4H6uW1XRpWvnWtX0K4D9RfTUTXB3cep9gMSW+h XjTg== 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 l5si1787411pgs.605.2018.04.10.16.11.34; Tue, 10 Apr 2018 16:12:11 -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 S1756067AbeDJXDb (ORCPT + 99 others); Tue, 10 Apr 2018 19:03:31 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:41942 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753668AbeDJWe7 (ORCPT ); Tue, 10 Apr 2018 18:34:59 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 7BEBACDB; Tue, 10 Apr 2018 22:34:58 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Mengting Zhang , Arnaldo Carvalho de Melo , Jiri Olsa , Cheng Jian , Li Bin , Wang Nan , Sasha Levin Subject: [PATCH 4.14 011/138] perf evsel: Enable ignore_missing_thread for pid option Date: Wed, 11 Apr 2018 00:23:21 +0200 Message-Id: <20180410212903.460139042@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180410212902.121524696@linuxfoundation.org> References: <20180410212902.121524696@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 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. ------------------ From: Mengting Zhang [ Upstream commit ca8000684ec4e66f965e1f9547a3c6cb834154ca ] While monitoring a multithread process with pid option, perf sometimes may return sys_perf_event_open failure with 3(No such process) if any of the process's threads die before we open the event. However, we want perf continue monitoring the remaining threads and do not exit with error. Here, the patch enables perf_evsel::ignore_missing_thread for -p option to ignore complete failure if any of threads die before we open the event. But it may still return sys_perf_event_open failure with 22(Invalid) if we monitors several event groups. sys_perf_event_open: pid 28960 cpu 40 group_fd 118202 flags 0x8 sys_perf_event_open: pid 28961 cpu 40 group_fd 118203 flags 0x8 WARNING: Ignored open failure for pid 28962 sys_perf_event_open: pid 28962 cpu 40 group_fd [118203] flags 0x8 sys_perf_event_open failed, error -22 That is because when we ignore a missing thread, we change the thread_idx without dealing with its fds, FD(evsel, cpu, thread). Then get_group_fd() may return a wrong group_fd for the next thread and sys_perf_event_open() return with 22. sys_perf_event_open(){ ... if (group_fd != -1) perf_fget_light()//to get corresponding group_leader by group_fd ... if (group_leader) if (group_leader->ctx->task != ctx->task)//should on the same task goto err_context ... } This patch also fixes this bug by introducing perf_evsel__remove_fd() and update_fds to allow removing fds for the missing thread. Changes since v1: - Change group_fd__remove() into a more genetic way without changing code logic - Remove redundant condition Changes since v2: - Use a proper function name and add some comment. - Multiline comment style fixes. Committer testing: Before this patch the recently added 'perf stat --per-thread' for system wide counting would race while enumerating all threads using /proc: [root@jouet ~]# perf stat --per-thread failed to parse CPUs map: No such file or directory Usage: perf stat [] [] -C, --cpu list of cpus to monitor in system-wide -a, --all-cpus system-wide collection from all CPUs [root@jouet ~]# perf stat --per-thread failed to parse CPUs map: No such file or directory Usage: perf stat [] [] -C, --cpu list of cpus to monitor in system-wide -a, --all-cpus system-wide collection from all CPUs [root@jouet ~]# When, say, the kernel was being built, so lots of shortlived threads, after this patch this doesn't happen. Signed-off-by: Mengting Zhang Tested-by: Arnaldo Carvalho de Melo Acked-by: Jiri Olsa Cc: Cheng Jian Cc: Li Bin Cc: Wang Nan Link: http://lkml.kernel.org/r/1513148513-6974-1-git-send-email-zhangmengting@huawei.com [ Remove one use 'evlist' alias variable ] Signed-off-by: Arnaldo Carvalho de Melo Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- tools/perf/builtin-record.c | 4 +-- tools/perf/util/evsel.c | 47 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 4 deletions(-) --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -1856,8 +1856,8 @@ int cmd_record(int argc, const char **ar goto out; } - /* Enable ignoring missing threads when -u option is defined. */ - rec->opts.ignore_missing_thread = rec->opts.target.uid != UINT_MAX; + /* Enable ignoring missing threads when -u/-p option is defined. */ + rec->opts.ignore_missing_thread = rec->opts.target.uid != UINT_MAX || rec->opts.target.pid; err = -ENOMEM; if (perf_evlist__create_maps(rec->evlist, &rec->opts.target) < 0) --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -1591,10 +1591,46 @@ static int __open_attr__fprintf(FILE *fp return fprintf(fp, " %-32s %s\n", name, val); } +static void perf_evsel__remove_fd(struct perf_evsel *pos, + int nr_cpus, int nr_threads, + int thread_idx) +{ + for (int cpu = 0; cpu < nr_cpus; cpu++) + for (int thread = thread_idx; thread < nr_threads - 1; thread++) + FD(pos, cpu, thread) = FD(pos, cpu, thread + 1); +} + +static int update_fds(struct perf_evsel *evsel, + int nr_cpus, int cpu_idx, + int nr_threads, int thread_idx) +{ + struct perf_evsel *pos; + + if (cpu_idx >= nr_cpus || thread_idx >= nr_threads) + return -EINVAL; + + evlist__for_each_entry(evsel->evlist, pos) { + nr_cpus = pos != evsel ? nr_cpus : cpu_idx; + + perf_evsel__remove_fd(pos, nr_cpus, nr_threads, thread_idx); + + /* + * Since fds for next evsel has not been created, + * there is no need to iterate whole event list. + */ + if (pos == evsel) + break; + } + return 0; +} + static bool ignore_missing_thread(struct perf_evsel *evsel, + int nr_cpus, int cpu, struct thread_map *threads, int thread, int err) { + pid_t ignore_pid = thread_map__pid(threads, thread); + if (!evsel->ignore_missing_thread) return false; @@ -1610,11 +1646,18 @@ static bool ignore_missing_thread(struct if (threads->nr == 1) return false; + /* + * We should remove fd for missing_thread first + * because thread_map__remove() will decrease threads->nr. + */ + if (update_fds(evsel, nr_cpus, cpu, threads->nr, thread)) + return false; + if (thread_map__remove(threads, thread)) return false; pr_warning("WARNING: Ignored open failure for pid %d\n", - thread_map__pid(threads, thread)); + ignore_pid); return true; } @@ -1719,7 +1762,7 @@ retry_open: if (fd < 0) { err = -errno; - if (ignore_missing_thread(evsel, threads, thread, err)) { + if (ignore_missing_thread(evsel, cpus->nr, cpu, threads, thread, err)) { /* * We just removed 1 thread, so take a step * back on thread index and lower the upper