Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp3704792ybv; Mon, 10 Feb 2020 05:02:53 -0800 (PST) X-Google-Smtp-Source: APXvYqwrDikFq/G9Xw3A5/rg0zJzEpvsXsCNgcURHrvOfqooy9s7DlZPLxDWQjOzYKGn/COHXuMB X-Received: by 2002:a05:6830:99:: with SMTP id a25mr1027923oto.130.1581339773249; Mon, 10 Feb 2020 05:02:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581339773; cv=none; d=google.com; s=arc-20160816; b=r3uSQIhwPFwtAnuJHikixFeaEZH6RsuRSJCC6XNp2f0HphAo1CbSESFTh5wTLcIP3n 9ftwyEFxOcKQDW5PUAsJ41iAVc5a83Q1YD1Ax0cORFC2RpSLuapPtpFIWlqcdJyWlD9K O8pc5KK6a4dnikjjQWj+7ZAP2ShOYBYLaO39M18qS0c/4mRI/Vw9UE8cyJ2j/70sFEdk g7IPXoEm/TRO/Wi4NMvk+vK0H03spOu/CeaTPh5F7Drsd7jfbp/+PO/FjJGP/vgU9mtd QvvqJrHnFmykeFmwk5wHr9Fu3ks0J3uPEd2UlfYwBh30kBDj4Wnu/kp1pzcAsVp5ZETL aVUQ== 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=kqqr+SdYbWoo3dGzTtgwbRbswflB6wOStk+v4Kzfwss=; b=AWrh7imIk7cy+S4WtuSnLovQCqqdGz9HFzQoyiy1BfJIf978pLB83vrmNdvsw6P1VX YdqTV/vQga8MsVVF0F3M8tIvWJ6G8GMQh7zC33cL/eEjD8Xve1wrKuwSdnstxTy7lWpS TkAA3yl4KNKEkhsQVIvPWHdhck4dQUj7YkeRTsgQ6h4XQr1k46w7bUNl9tasVA0WAuZ1 jxMypXeGE4i3KfPH3ukvd8NhM6O80yOCMBAN8aCrDQLkrp4ia4gRRrYLgW/I5G5YWcoQ rOFIfEeYBGHF+xcAXQGIRkoc10uHwOi/GTDsIzJ/jdM1gIrM3irEj6ivLgTMqjjJ3HqO Prdw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=kLXkkrcM; 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 i6si163292oth.182.2020.02.10.05.02.30; Mon, 10 Feb 2020 05:02:53 -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=kLXkkrcM; 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 S1730538AbgBJNB6 (ORCPT + 99 others); Mon, 10 Feb 2020 08:01:58 -0500 Received: from mail.kernel.org ([198.145.29.99]:40920 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729781AbgBJMkl (ORCPT ); Mon, 10 Feb 2020 07:40:41 -0500 Received: from localhost (unknown [209.37.97.194]) (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 72F8B2467A; Mon, 10 Feb 2020 12:40:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1581338440; bh=j+F8/QXxjYYhd5E9o7jGm+TxXK2oZB9TvZwAjvQnMTE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kLXkkrcMpM0EQuSKr4YqsIxFnDAoXvbUVEsEue9vBeffsX+56EsxmF1ETrrbAtC1M I0VxzTU1+QJIUpJcjgM2IIssAMTmLcoxs7kBTSQQu0lEi8+/eR4p4oqk1CaFmvCmxK WqUba8nvSRK4S0cadzqDZeHCSPwanzAkKWAUDbXw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andrii Nakryiko , Alexei Starovoitov Subject: [PATCH 5.5 160/367] libbpf: Dont attach perf_buffer to offline/missing CPUs Date: Mon, 10 Feb 2020 04:31:13 -0800 Message-Id: <20200210122439.600419382@linuxfoundation.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200210122423.695146547@linuxfoundation.org> References: <20200210122423.695146547@linuxfoundation.org> User-Agent: quilt/0.66 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 From: Andrii Nakryiko commit 783b8f01f5942a786998f5577bd9ff3992f22a1a upstream. It's quite common on some systems to have more CPUs enlisted as "possible", than there are (and could ever be) present/online CPUs. In such cases, perf_buffer creationg will fail due to inability to create perf event on missing CPU with error like this: libbpf: failed to open perf buffer event on cpu #16: No such device This patch fixes the logic of perf_buffer__new() to ignore CPUs that are missing or currently offline. In rare cases where user explicitly listed specific CPUs to connect to, behavior is unchanged: libbpf will try to open perf event buffer on specified CPU(s) anyways. Fixes: fb84b8224655 ("libbpf: add perf buffer API") Signed-off-by: Andrii Nakryiko Signed-off-by: Alexei Starovoitov Link: https://lore.kernel.org/bpf/20191212013609.1691168-1-andriin@fb.com Signed-off-by: Greg Kroah-Hartman --- tools/lib/bpf/libbpf.c | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -5945,7 +5945,7 @@ struct perf_buffer { size_t mmap_size; struct perf_cpu_buf **cpu_bufs; struct epoll_event *events; - int cpu_cnt; + int cpu_cnt; /* number of allocated CPU buffers */ int epoll_fd; /* perf event FD */ int map_fd; /* BPF_MAP_TYPE_PERF_EVENT_ARRAY BPF map FD */ }; @@ -6079,11 +6079,13 @@ perf_buffer__new_raw(int map_fd, size_t static struct perf_buffer *__perf_buffer__new(int map_fd, size_t page_cnt, struct perf_buffer_params *p) { + const char *online_cpus_file = "/sys/devices/system/cpu/online"; struct bpf_map_info map = {}; char msg[STRERR_BUFSIZE]; struct perf_buffer *pb; + bool *online = NULL; __u32 map_info_len; - int err, i; + int err, i, j, n; if (page_cnt & (page_cnt - 1)) { pr_warn("page count should be power of two, but is %zu\n", @@ -6152,20 +6154,32 @@ static struct perf_buffer *__perf_buffer goto error; } - for (i = 0; i < pb->cpu_cnt; i++) { + err = parse_cpu_mask_file(online_cpus_file, &online, &n); + if (err) { + pr_warn("failed to get online CPU mask: %d\n", err); + goto error; + } + + for (i = 0, j = 0; i < pb->cpu_cnt; i++) { struct perf_cpu_buf *cpu_buf; int cpu, map_key; cpu = p->cpu_cnt > 0 ? p->cpus[i] : i; map_key = p->cpu_cnt > 0 ? p->map_keys[i] : i; + /* in case user didn't explicitly requested particular CPUs to + * be attached to, skip offline/not present CPUs + */ + if (p->cpu_cnt <= 0 && (cpu >= n || !online[cpu])) + continue; + cpu_buf = perf_buffer__open_cpu_buf(pb, p->attr, cpu, map_key); if (IS_ERR(cpu_buf)) { err = PTR_ERR(cpu_buf); goto error; } - pb->cpu_bufs[i] = cpu_buf; + pb->cpu_bufs[j] = cpu_buf; err = bpf_map_update_elem(pb->map_fd, &map_key, &cpu_buf->fd, 0); @@ -6177,21 +6191,25 @@ static struct perf_buffer *__perf_buffer goto error; } - pb->events[i].events = EPOLLIN; - pb->events[i].data.ptr = cpu_buf; + pb->events[j].events = EPOLLIN; + pb->events[j].data.ptr = cpu_buf; if (epoll_ctl(pb->epoll_fd, EPOLL_CTL_ADD, cpu_buf->fd, - &pb->events[i]) < 0) { + &pb->events[j]) < 0) { err = -errno; pr_warn("failed to epoll_ctl cpu #%d perf FD %d: %s\n", cpu, cpu_buf->fd, libbpf_strerror_r(err, msg, sizeof(msg))); goto error; } + j++; } + pb->cpu_cnt = j; + free(online); return pb; error: + free(online); if (pb) perf_buffer__free(pb); return ERR_PTR(err);