From patchwork Fri Oct 28 18:01:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Namhyung Kim X-Patchwork-Id: 12509 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp972202wru; Fri, 28 Oct 2022 11:10:20 -0700 (PDT) X-Google-Smtp-Source: AMsMyM56H8YMQMQSMh75B8slxOJVGi1UbDQ1Atj0fR4/9lw9EhDmR180fqjfCOycBH+CpTEK8sZO X-Received: by 2002:a17:906:fd85:b0:77b:b538:6472 with SMTP id xa5-20020a170906fd8500b0077bb5386472mr588457ejb.48.1666980620712; Fri, 28 Oct 2022 11:10:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666980620; cv=none; d=google.com; s=arc-20160816; b=V+ojdpN0dihuRP3SCG1V/nVAD3xhR3zNfSA1SeDaPuXsl7XvKXMNES1ZVwbMg8yXCz pMQGeHtIT5/Agvsv1rF03UcsAzdJYfCwyx4+rlZTHJkIkK39IWyb5gsT/B1OiP9Pxv3k i1YdEk1ikXBvoZKhNaCAAwgaDwKKWr/w5TaMk3bBroY1PWJts7OayqdTu4E/6pc2BFBO xx4OOi4csWlz14SEbkIgMhUtfaSK1mdWhmmkoKvKSy7L8jYMSvHEP5735oXk7xr6INVr BLYj0NlDTK8JE9+sB6OmNFMTIWyfGXRNJDHkf+rgnczPOZLK0leT9oAJGArTigFouGqw oU5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature; bh=BDQ26YrilcoiUxmd9QdQ1Dk7XFU7csSgsYNB1op6NE8=; b=U4ByJw7Q87zVMJ40CN37lJgMR6ohZA8A6w8Ui9O1Rp7QIzU6rSWvm5ZrlglR0HtWdX ofpMLiMQ/om2EZSRWiOc/r7r37cXx4x3mdZVrJBAupiuTCOiW+KIjY3ex1GanJjBVmYj 2ieAtq5cfXM4FOodaGJHFWamzb+VGyE6R8/oEiiyXVh0CJu3jGKtVf1IQleTAdf80pJY FjcKpoCECeTtcstYUW1oB5IXoYeOEwVOQlNihX2rJW38knzSDy6t4ibuuDoHi23yYb+W 6qWYBQh3wt1UPUtnXVcLzi7mSzaLVPwLlRogyERg0zTB7BLod7ohhyja5ZmyMXquvwgV jFdw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=SQ3Z7rvG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o18-20020aa7d3d2000000b0045c0de4b0e9si4182635edr.170.2022.10.28.11.09.56; Fri, 28 Oct 2022 11:10:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=SQ3Z7rvG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230163AbiJ1SBz (ORCPT + 99 others); Fri, 28 Oct 2022 14:01:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229930AbiJ1SBj (ORCPT ); Fri, 28 Oct 2022 14:01:39 -0400 Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 936C020FB2B; Fri, 28 Oct 2022 11:01:38 -0700 (PDT) Received: by mail-pj1-x1033.google.com with SMTP id u8-20020a17090a5e4800b002106dcdd4a0so10552262pji.1; Fri, 28 Oct 2022 11:01:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=BDQ26YrilcoiUxmd9QdQ1Dk7XFU7csSgsYNB1op6NE8=; b=SQ3Z7rvGLUrN/t+Hbqt2wDeDYS+lmrdAuACOFCN3M0r2GzZxb/crepMgS5OP/2NbkF cShzFKOVPzUVMu/4Q3R7Bz/pcOtmgyOxVgdcU+PSkvvPXf/mHjwUInz7cX/LFupYbQsF 5fgXhixo3JpdAiOIoqLKejnqonJtnoS8NuHJ8Z16m3ZpXaaFDLjXee+tNGbpTWloi/tz qYP1zAB6XKeJ8lA1jd6b3EyIYqGIPUOyP5PtS0TonCxJDzbiJqq8cVzxqQ9WKHgzdm8/ tEyKfNfcJExD656B2UX8dM/cEOY4pkLpgCRU4vC4t7G3xWFgwSZFqCym2Q/LfDnemOqf szkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=BDQ26YrilcoiUxmd9QdQ1Dk7XFU7csSgsYNB1op6NE8=; b=ji/c5lZiXBorY7XHTCEpMCOUzcIOgIA5Ei1GLUbV46y5Jl2NnztKMVMe1XFviUCDOn BnAo0bWls/53Z3Mj6qZPXgr+Kku3QX0tEeezEn+QHgQ8TiKWSL0h4+ONtEwu1eDNiFEP OeHHQuFl9cVpkNG9n8lxu72Yw9ababNJThyIDzllbck2pzDrvR/g16kIW8kRlM9KcE/u J0dX72tcq7o7yoH3i3eZb4LGUMmWi+JTOIRmVvpI6jZST/GhcpmXfpw+tqLOmMZ7JQ7d sV4nKpoqrL+ry1+hLUgW74cl+xNnhmNrFdVEAiP5zOQnNKvYBHZadZ+NoTUEbuXCypLC 3NLg== X-Gm-Message-State: ACrzQf0mrUCsA/fs63fNQJ2Cgcz6jaBVgQcUGhAuk7340LcZE4W/tK/0 3TdmgA7Ur5m2Lw755i/9dwM= X-Received: by 2002:a17:902:ce0e:b0:17d:a730:3835 with SMTP id k14-20020a170902ce0e00b0017da7303835mr172589plg.131.1666980097967; Fri, 28 Oct 2022 11:01:37 -0700 (PDT) Received: from youngsil.svl.corp.google.com ([2620:15c:2d4:203:976f:f075:7c14:87a2]) by smtp.gmail.com with ESMTPSA id n3-20020a170903110300b00186b86ed450sm3400236plh.156.2022.10.28.11.01.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Oct 2022 11:01:36 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ingo Molnar , Peter Zijlstra , LKML , Ian Rogers , Adrian Hunter , linux-perf-users@vger.kernel.org Subject: [PATCH 3/4] perf lock contention: Avoid variable length arrays Date: Fri, 28 Oct 2022 11:01:27 -0700 Message-Id: <20221028180128.3311491-4-namhyung@kernel.org> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog In-Reply-To: <20221028180128.3311491-1-namhyung@kernel.org> References: <20221028180128.3311491-1-namhyung@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747955871418918610?= X-GMAIL-MSGID: =?utf-8?q?1747955871418918610?= The msan also warns about the use of VLA for stack_trace variable. We can dynamically allocate instead. While at it, simplify the error handle a bit (and fix bugs). Signed-off-by: Namhyung Kim --- tools/perf/util/bpf_lock_contention.c | 41 ++++++++++++++++++--------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/tools/perf/util/bpf_lock_contention.c b/tools/perf/util/bpf_lock_contention.c index 06466da792e4..0deec1178778 100644 --- a/tools/perf/util/bpf_lock_contention.c +++ b/tools/perf/util/bpf_lock_contention.c @@ -108,28 +108,36 @@ int lock_contention_stop(void) int lock_contention_read(struct lock_contention *con) { - int fd, stack; + int fd, stack, err = 0; s32 prev_key, key; struct lock_contention_data data = {}; - struct lock_stat *st; + struct lock_stat *st = NULL; struct machine *machine = con->machine; - u64 stack_trace[con->max_stack]; + u64 *stack_trace; + size_t stack_size = con->max_stack * sizeof(*stack_trace); fd = bpf_map__fd(skel->maps.lock_stat); stack = bpf_map__fd(skel->maps.stacks); con->lost = skel->bss->lost; + stack_trace = zalloc(stack_size); + if (stack_trace == NULL) + return -1; + prev_key = 0; while (!bpf_map_get_next_key(fd, &prev_key, &key)) { struct map *kmap; struct symbol *sym; int idx = 0; + /* to handle errors in the loop body */ + err = -1; + bpf_map_lookup_elem(fd, &key, &data); st = zalloc(sizeof(*st)); if (st == NULL) - return -1; + break; st->nr_contended = data.count; st->wait_time_total = data.total_time; @@ -163,25 +171,32 @@ int lock_contention_read(struct lock_contention *con) st->name = strdup(sym->name); if (ret < 0 || st->name == NULL) - return -1; + break; } else if (asprintf(&st->name, "%#lx", (unsigned long)st->addr) < 0) { - free(st); - return -1; + break; } if (verbose) { - st->callstack = memdup(stack_trace, sizeof(stack_trace)); - if (st->callstack == NULL) { - free(st); - return -1; - } + st->callstack = memdup(stack_trace, stack_size); + if (st->callstack == NULL) + break; } hlist_add_head(&st->hash_entry, con->result); prev_key = key; + + /* we're fine now, reset the values */ + st = NULL; + err = 0; } - return 0; + free(stack_trace); + if (st) { + free(st->name); + free(st); + } + + return err; } int lock_contention_finish(void)