From patchwork Thu Dec 7 01:16:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 174826 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp4487336vqy; Wed, 6 Dec 2023 17:17:56 -0800 (PST) X-Google-Smtp-Source: AGHT+IEtEZYatR4fh6AmF9EQAbtCPznx0LR0s4n4VThtgTDy9uH3CUst0F/ZIIHL6LlIrFfNS8Eo X-Received: by 2002:a05:6e02:8ed:b0:35d:6fb4:b2fb with SMTP id n13-20020a056e0208ed00b0035d6fb4b2fbmr1919479ilt.113.1701911876487; Wed, 06 Dec 2023 17:17:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701911876; cv=none; d=google.com; s=arc-20160816; b=ZUHYGU9I7SAVWSwtHmGxABrNk8SmwXVqOE+nFgqxVGoopJhPRX2QrG5zpYbr7LKaxO Cwnf/nHnXyevLfrEPc9IlMjQncnxZlxt+r6WMIb2ADnoUfyfkgcYXq/m7pwWS4BgdWOi BMrEdqD+4rYDBbkCaLeIo6RkiJFxuVC81E/VRFUVgiPWOSbkfJ+n6JJ/KETR3lz0RgcX hz73etMCR/7wOjtxw6muIJJIU3Ep14yRIDeqq6DQyxrSvkEzLWqYDswJDTGQyQnXvHwl N1YMmYW63Pi2gUNfBs8gmqNoDUyGiQfZ6rXs9iVLyyhzfLkBOs4UkYsejGW+r4g9h++U z3kQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=OZVma1lrp2oy91ELMTmGFGFg9/I25IFe6Z4TysewRjs=; fh=Io7x3OacpefNRRNKoYAzVE0aWTDwD7FRzyCdD1e3AN4=; b=BaTW3HfRu9Las/ax4aGXTKqcx7r+m/Ntx+27XmbQbR/1VYF4qwk3m3+G3Uti/0aRGE TgzEPSlgV/M3VA3kpOBsVfFuSorZvC1te4q36t4GcPT+heFlrSsYRgfMXyyAMw3LtVdg FCp9UiTblwap6BIvVm9IOmI0+9arjbnF5RTyirjJrCYdudSOSpPJ5gu94jDgXrQb+xYj Hz3RQLKvvs/QhN/rkKoGA63rI4Se5IsOLin9z7eakjHQvTAnDW36mEiKAAtkHQPjILb3 uIY4U3OdF/1JJ5Gt5b9337ixgEAOgEVSs3uIaJDylCYw0zRj+4hwfMXnmF+MJPJ42/9f p4gQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="x9Q/OiL4"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id s23-20020a632c17000000b005bdc61e1793si205192pgs.358.2023.12.06.17.17.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 17:17:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="x9Q/OiL4"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 49ED6802852A; Wed, 6 Dec 2023 17:17:51 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1442013AbjLGBRd (ORCPT + 99 others); Wed, 6 Dec 2023 20:17:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52170 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1441968AbjLGBR3 (ORCPT ); Wed, 6 Dec 2023 20:17:29 -0500 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5BB75D5E for ; Wed, 6 Dec 2023 17:17:35 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5d3a1f612b3so1802567b3.1 for ; Wed, 06 Dec 2023 17:17:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701911854; x=1702516654; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=OZVma1lrp2oy91ELMTmGFGFg9/I25IFe6Z4TysewRjs=; b=x9Q/OiL4uAbpgM1f8c21FZuKuVNpBnUW53hzBC/jHYn3n+fHEwnKWSzQZBrrna1VNh 9Vtgm65ZcUlOriYR1tMVO6YlylnQV3k9Es4NX6VfMP4keGxa0v4rvlMrxTUAmc2H56G4 7gpizSlS7fARuSHoxTCdjhGRTzyWkYlbIGkVZVNHSIIlXHO/sqNTq2mnAFPuUHpK3567 d5QnGwOMdcz8vUXKepe8+Gq2VF578yzSHqm1oPWnaTVlbCjYGEO1VcTsVzPCc/5GAk/R 5osUvRo4kOsdLOS8sHtGbsVkyAUzcBxvijC8wszI1mMClWZN4NEIxuE0WSVcBJaF4+QZ 08tg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701911854; x=1702516654; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=OZVma1lrp2oy91ELMTmGFGFg9/I25IFe6Z4TysewRjs=; b=b4iqkj+HYKMdcZmNWnEU8V+DWgHLm3x45AWmOTmUKewB95M8P6oY4jQn2nGv6EKUPQ DHOmfnwrulSKIyzJFWBCoa35cI6+NXYnPXYoX0VbqbdNq7t1lHKx0NRXwNVrMBfm6Qs5 QUWvvkjwDxkI8rxlq8fEdnpVwFokGvjAcKoqIb5xpkf1ZUrvlCDiOvVCLWlqucgNuslT aBDsSfOMHVkhmx0T0DaxkSJyuShyODEgTkAJW4EXuqsCf7rfp3MK8csRXqXbHWaL/zAo YKYFfdq14nMtUvyrWtphOU59lUH4zkp0mJBuQjRnq4qA4EF+moZon4mKwYUq7XtmGQWi rXCQ== X-Gm-Message-State: AOJu0YwAe45/hiYU2P6iDUKSx5DUduCo3hE2IczfV9gTGPHfDskQ2SHr DpwHd3oLwaccjZ7snAWkfchwNis5lM74 X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:35bf:293e:7696:34e9]) (user=irogers job=sendgmr) by 2002:a05:690c:2888:b0:5d3:cca8:1d59 with SMTP id ee8-20020a05690c288800b005d3cca81d59mr29912ywb.5.1701911854339; Wed, 06 Dec 2023 17:17:34 -0800 (PST) Date: Wed, 6 Dec 2023 17:16:37 -0800 In-Reply-To: <20231207011722.1220634-1-irogers@google.com> Message-Id: <20231207011722.1220634-4-irogers@google.com> Mime-Version: 1.0 References: <20231207011722.1220634-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog Subject: [PATCH v6 03/47] perf events x86: Use function to add missing lock From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Kajol Jain , Athira Rajeev , Huacai Chen , Masami Hiramatsu , Vincent Whitchurch , "Steinar H. Gunderson" , Liam Howlett , Miguel Ojeda , Colin Ian King , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , Ming Wang , James Clark , K Prateek Nayak , Sean Christopherson , Leo Yan , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Li Dong , Sandipan Das , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Guilherme Amadio X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Wed, 06 Dec 2023 17:17:51 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784583947748561680 X-GMAIL-MSGID: 1784583947748561680 Switch from loop macro maps__for_each_entry to maps__for_each_map function that takes a callback. The function holds the maps lock, which should be held during iteration. Signed-off-by: Ian Rogers --- tools/perf/arch/x86/util/event.c | 103 +++++++++++++++++-------------- 1 file changed, 58 insertions(+), 45 deletions(-) diff --git a/tools/perf/arch/x86/util/event.c b/tools/perf/arch/x86/util/event.c index 5741ffe47312..e65b7dbe27fb 100644 --- a/tools/perf/arch/x86/util/event.c +++ b/tools/perf/arch/x86/util/event.c @@ -14,66 +14,79 @@ #if defined(__x86_64__) -int perf_event__synthesize_extra_kmaps(struct perf_tool *tool, - perf_event__handler_t process, - struct machine *machine) +struct perf_event__synthesize_extra_kmaps_cb_args { + struct perf_tool *tool; + perf_event__handler_t process; + struct machine *machine; + union perf_event *event; +}; + +static int perf_event__synthesize_extra_kmaps_cb(struct map *map, void *data) { - int rc = 0; - struct map_rb_node *pos; - struct maps *kmaps = machine__kernel_maps(machine); - union perf_event *event = zalloc(sizeof(event->mmap) + - machine->id_hdr_size); + struct perf_event__synthesize_extra_kmaps_cb_args *args = data; + union perf_event *event = args->event; + struct kmap *kmap; + size_t size; - if (!event) { - pr_debug("Not enough memory synthesizing mmap event " - "for extra kernel maps\n"); - return -1; - } + if (!__map__is_extra_kernel_map(map)) + return 0; - maps__for_each_entry(kmaps, pos) { - struct kmap *kmap; - size_t size; - struct map *map = pos->map; + kmap = map__kmap(map); - if (!__map__is_extra_kernel_map(map)) - continue; + size = sizeof(event->mmap) - sizeof(event->mmap.filename) + + PERF_ALIGN(strlen(kmap->name) + 1, sizeof(u64)) + + args->machine->id_hdr_size; - kmap = map__kmap(map); + memset(event, 0, size); - size = sizeof(event->mmap) - sizeof(event->mmap.filename) + - PERF_ALIGN(strlen(kmap->name) + 1, sizeof(u64)) + - machine->id_hdr_size; + event->mmap.header.type = PERF_RECORD_MMAP; - memset(event, 0, size); + /* + * kernel uses 0 for user space maps, see kernel/perf_event.c + * __perf_event_mmap + */ + if (machine__is_host(args->machine)) + event->header.misc = PERF_RECORD_MISC_KERNEL; + else + event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL; - event->mmap.header.type = PERF_RECORD_MMAP; + event->mmap.header.size = size; - /* - * kernel uses 0 for user space maps, see kernel/perf_event.c - * __perf_event_mmap - */ - if (machine__is_host(machine)) - event->header.misc = PERF_RECORD_MISC_KERNEL; - else - event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL; + event->mmap.start = map__start(map); + event->mmap.len = map__size(map); + event->mmap.pgoff = map__pgoff(map); + event->mmap.pid = args->machine->pid; - event->mmap.header.size = size; + strlcpy(event->mmap.filename, kmap->name, PATH_MAX); - event->mmap.start = map__start(map); - event->mmap.len = map__size(map); - event->mmap.pgoff = map__pgoff(map); - event->mmap.pid = machine->pid; + if (perf_tool__process_synth_event(args->tool, event, args->machine, args->process) != 0) + return -1; - strlcpy(event->mmap.filename, kmap->name, PATH_MAX); + return 0; +} - if (perf_tool__process_synth_event(tool, event, machine, - process) != 0) { - rc = -1; - break; - } +int perf_event__synthesize_extra_kmaps(struct perf_tool *tool, + perf_event__handler_t process, + struct machine *machine) +{ + int rc; + struct maps *kmaps = machine__kernel_maps(machine); + struct perf_event__synthesize_extra_kmaps_cb_args args = { + .tool = tool, + .process = process, + .machine = machine, + .event = zalloc(sizeof(args.event->mmap) + machine->id_hdr_size), + }; + + if (!args.event) { + pr_debug("Not enough memory synthesizing mmap event " + "for extra kernel maps\n"); + return -1; } - free(event); + rc = maps__for_each_map(kmaps, perf_event__synthesize_extra_kmaps_cb, &args); + + free(args.event); return rc; }