Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp154933rwd; Mon, 12 Jun 2023 11:25:51 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5ujkkr1YaUI6g+txYB5sV8mUXi47cXTztbB7QTZWiSWaI8GsMaOnQ/fHKK+VH+i4weoVj1 X-Received: by 2002:a05:6a20:394c:b0:11c:d2af:70a6 with SMTP id r12-20020a056a20394c00b0011cd2af70a6mr1376745pzg.28.1686594351454; Mon, 12 Jun 2023 11:25:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686594351; cv=none; d=google.com; s=arc-20160816; b=qLo4XFtBd9kVGVJNkQ0+C913ZURBEIOUEDqwE63byXrYokb4EqAAansK5xio1I5X48 34+syEcmieROvSNe37I9rOqwD9NshO+7Iwt2Cnqm8oDTLO8Mnb2PaSRyTKfK3Crn+uYv PsJfnJdaiCMx4drNYmK1017oAbGiZKjoRz8vTa5QwF8EAnobkyUd/gmwcM2HgvjUY1XU FMphZ9MP9u3BQUlj5KCBduS+u0uP/yXhrW8nmo2DPgSjRT5EJNsgwyDLLfcCOpIKXpEB 8lSU6SlY9gOFfBQZcTznDo+syvhSHlNHy4VgcZe6MVaofTPH8UWuWsGcIu0Z5Nv6vAsy IB9Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=ILFhlbqLVYCkvMkd83ZOGAmp4d8jQSiKbMxbQnZD5rA=; b=zBzx44l6XUGAkX83A7gBiqAXdQdLwU2cKZ1/GwaUwbOd717YOD1oKREbGF8KK+UGdt t02H+i+bNpB9Hr+ZPDhZ9rlgM4HdQLDLQVYSzeH7CKY59K+r+MQRmYKqFRv9Lq1xawf9 rc3B8k15G7uUI/Uq5wCz21HDC2uQTPVtdCeNJtRcVqpZfyvLbKo2ib5A3dWtsZL25HMj HqGJSOS19Fyh9MHFMXz/gA8ZhcE+Pg4sXg84437ucN5W8k2V/+OBbwl4KuWhQrZJmkWe U/91LRFg+5vaNJhtfdkWoYkLmtp5nbDoXMd6bM7fD9ADcDKVgSEUZW142Fp7KNzXpsaU DcZg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=tdMkRizy; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o21-20020a637e55000000b005481915c0c6si6914599pgn.31.2023.06.12.11.25.38; Mon, 12 Jun 2023 11:25:51 -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=@google.com header.s=20221208 header.b=tdMkRizy; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231501AbjFLR76 (ORCPT + 99 others); Mon, 12 Jun 2023 13:59:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59316 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236989AbjFLR7v (ORCPT ); Mon, 12 Jun 2023 13:59:51 -0400 Received: from mail-qt1-x833.google.com (mail-qt1-x833.google.com [IPv6:2607:f8b0:4864:20::833]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B6665E51 for ; Mon, 12 Jun 2023 10:59:46 -0700 (PDT) Received: by mail-qt1-x833.google.com with SMTP id d75a77b69052e-3f9a81da5d7so31641cf.0 for ; Mon, 12 Jun 2023 10:59:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686592786; x=1689184786; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=ILFhlbqLVYCkvMkd83ZOGAmp4d8jQSiKbMxbQnZD5rA=; b=tdMkRizyfMWaXZvmVYCuSYSVv7+zDgFuO1ZU7MleR5M8JC17ke+8ydQweTOJ1zbPmU 1NRV3R/B5c1A5vEpe019yI1Hc+7oFSldlRgyqJpd3F132cvciHFG2l8UwYgKZYuJKvlx YLoDCZYWVSNgVtxwzCDfG7x743wTlnoQxEIi5KpiXCsracw//U6gR41Na4ea5ELqOfOX NQVm64FoeOuhMYYT++BMj00g7bMr9GpiTxoVEEi6z/EOuKMc1ccTRoQl8teYzYGVoaos bgA7yvaPTLi+lSenAmx8lDOEaX2NQHRA9kavD5yhgIVPwt6eAfhH0BMg3KrcxX+0gD2E +2cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686592786; x=1689184786; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ILFhlbqLVYCkvMkd83ZOGAmp4d8jQSiKbMxbQnZD5rA=; b=F35yEBwrQ/3li0hIwuirx8gZmK4K/LF0Zyp9XkvSAGL9Bazz6LXdxge1W9mENK/Gqp vPVUTv+IAnjchJuf5p8+e3r4c9IU6EMh8diqjVq9GkoUFIFsIGEOLKq0J0maz3ddttmZ ybD55nwlZVtwK+yJ0rw3vPSsIVz0nbtLyogVJZf0POeClwqLCiM7tsjQRKErnTM8fgHP tSgyqOm6E3IxH4bDPOrctK8602tIcf/zdQaCCaJEA6/8NxOh5lSN4ZAT8YA0x6Tes0Z9 sEHYY7IpWySWvI06JMUOrz0+3Ap7pE9N0oZ4Nsf2SvvJTRRDIkw0zopWsVgU5eHegetR qvbQ== X-Gm-Message-State: AC+VfDzQYJWpJqVgcLKi7jyUjj3IuCiM8TEVnCfgJbIu6Jfv56jYiOre tnRmlXrQk/28ZveTpfON/kAYbO7COwzXVmFEGU3qJI7kTpkM0Wo9cpDbjQ== X-Received: by 2002:a05:622a:1187:b0:3f9:f877:1129 with SMTP id m7-20020a05622a118700b003f9f8771129mr220710qtk.29.1686592785710; Mon, 12 Jun 2023 10:59:45 -0700 (PDT) MIME-Version: 1.0 References: <20230612150424.198914-1-james.clark@arm.com> In-Reply-To: From: Ian Rogers Date: Mon, 12 Jun 2023 10:59:34 -0700 Message-ID: Subject: Re: [PATCH] perf map: Fix refcount errors on Arm with -DREFCNT_CHECKING=1 To: Arnaldo Carvalho de Melo Cc: James Clark , linux-perf-users@vger.kernel.org, Peter Zijlstra , Ingo Molnar , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Adrian Hunter , linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham 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 On Mon, Jun 12, 2023 at 10:40=E2=80=AFAM Arnaldo Carvalho de Melo wrote: > > Em Mon, Jun 12, 2023 at 02:29:42PM -0300, Arnaldo Carvalho de Melo escrev= eu: > > Em Mon, Jun 12, 2023 at 09:32:30AM -0700, Ian Rogers escreveu: > > > On Mon, Jun 12, 2023 at 8:05=E2=80=AFAM James Clark wrote: > > > > > > > > When quitting after running a perf report, the refcount checker fin= ds > > > > some double frees. The issue is that map__put() is called on a func= tion > > > > argument so it removes the refcount wrapper that someone else was u= sing. > > > > > > > > Fix it by only calling map__put() on a reference that is owned by t= his > > > > function. > > > > > > > > Signed-off-by: James Clark > > > > > > Acked-by: Ian Rogers > > > > > > > --- > > > > tools/perf/util/symbol-elf.c | 9 +++++---- > > > > tools/perf/util/symbol.c | 9 +++++---- > > > > 2 files changed, 10 insertions(+), 8 deletions(-) > > > > > > > > diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-= elf.c > > > > index 63882a4db5c7..ec0d7810bbb0 100644 > > > > --- a/tools/perf/util/symbol-elf.c > > > > +++ b/tools/perf/util/symbol-elf.c > > > > @@ -1365,6 +1365,7 @@ static int dso__process_kernel_symbol(struct = dso *dso, struct map *map, > > > > struct dso *curr_dso =3D *curr_dsop; > > > > struct map *curr_map; > > > > char dso_name[PATH_MAX]; > > > > + struct map *map_ref; > > > > > > nit: can we narrow the scope of this by moving it to the scope where = it is used. > > > > Which is what you did in a patch I already processed, its only in > > tmp.perf-tools-next as I was going thru the other patches, but this one > > is there already. > > > > I'm checking the tools/perf/util/symbol.c part. > > I narrowed the scope and removed the symbol-elf.c part, end result: > > From 6fd34445b8c94aa7f519fb0b1ed45c7ef9f6cc4e Mon Sep 17 00:00:00 2001 > From: James Clark > Date: Mon, 12 Jun 2023 16:04:24 +0100 > Subject: [PATCH 1/1] perf map: Fix double 'struct map' reference free fou= nd > with -DREFCNT_CHECKING=3D1 > > When quitting after running a 'perf report', the refcount checker finds > some double frees. The issue is that map__put() is called on a function > argument so it removes the refcount wrapper that someone else was using. > > Fix it by only calling map__put() on a reference that is owned by this > function. > > Committer notes: > > Narrowed the map_ref scope as suggested by Ian, removed the symbol-elf > part as it was already fixed by another patch, from Ian. > > Signed-off-by: James Clark > Acked-by: Ian Rogers > Cc: Adrian Hunter > Cc: Alexander Shishkin > Cc: Ingo Molnar > Cc: Jiri Olsa > Cc: Mark Rutland > Cc: Namhyung Kim > Cc: Peter Zijlstra > Link: https://lore.kernel.org/r/20230612150424.198914-1-james.clark@arm.c= om > Signed-off-by: Arnaldo Carvalho de Melo Thanks Arnaldo! I think we should be able to automate finding these issues with the warn_unused_result function attribute: ``` diff --git a/tools/perf/util/map.h b/tools/perf/util/map.h index 66a87b3d9965..2c77c28ff000 100644 --- a/tools/perf/util/map.h +++ b/tools/perf/util/map.h @@ -172,7 +172,7 @@ struct map *map__new2(u64 start, struct dso *dso); void map__delete(struct map *map); struct map *map__clone(struct map *map); -static inline struct map *map__get(struct map *map) +__attribute__ ((warn_unused_result)) static inline struct map *map__get(struct map *map) { struct map *result; diff --git a/tools/perf/util/maps.h b/tools/perf/util/maps.h index 83144e0645ed..5b74465316dd 100644 --- a/tools/perf/util/maps.h +++ b/tools/perf/util/maps.h @@ -60,7 +60,7 @@ struct maps *maps__new(struct machine *machine); bool maps__empty(struct maps *maps); int maps__clone(struct thread *thread, struct maps *parent); -struct maps *maps__get(struct maps *maps); +struct maps *maps__get(struct maps *maps) __attribute__ ((warn_unused_resu= lt)); void maps__put(struct maps *maps); static inline void __maps__zput(struct maps **map) diff --git a/tools/perf/util/namespaces.h b/tools/perf/util/namespaces.h index 8c0731c6cbb7..04e1878b9551 100644 --- a/tools/perf/util/namespaces.h +++ b/tools/perf/util/namespaces.h @@ -50,7 +50,7 @@ int nsinfo__init(struct nsinfo *nsi); struct nsinfo *nsinfo__new(pid_t pid); struct nsinfo *nsinfo__copy(const struct nsinfo *nsi); -struct nsinfo *nsinfo__get(struct nsinfo *nsi); +struct nsinfo *nsinfo__get(struct nsinfo *nsi) __attribute__ ((warn_unused_result)); void nsinfo__put(struct nsinfo *nsi); bool nsinfo__need_setns(const struct nsinfo *nsi); diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h index 9068a21ce0fa..c6228252b093 100644 --- a/tools/perf/util/thread.h +++ b/tools/perf/util/thread.h @@ -71,7 +71,7 @@ struct thread *thread__new(pid_t pid, pid_t tid); irogers@irogers-glaptop0:~/kernel.org$ git diff diff --git a/tools/perf/util/map.h b/tools/perf/util/map.h index 66a87b3d9965..2c77c28ff000 100644 --- a/tools/perf/util/map.h +++ b/tools/perf/util/map.h @@ -172,7 +172,7 @@ struct map *map__new2(u64 start, struct dso *dso); void map__delete(struct map *map); struct map *map__clone(struct map *map); -static inline struct map *map__get(struct map *map) +__attribute__ ((warn_unused_result)) static inline struct map *map__get(struct map *map) { struct map *result; diff --git a/tools/perf/util/maps.h b/tools/perf/util/maps.h index 83144e0645ed..5b74465316dd 100644 --- a/tools/perf/util/maps.h +++ b/tools/perf/util/maps.h @@ -60,7 +60,7 @@ struct maps *maps__new(struct machine *machine); bool maps__empty(struct maps *maps); int maps__clone(struct thread *thread, struct maps *parent); -struct maps *maps__get(struct maps *maps); +struct maps *maps__get(struct maps *maps) __attribute__ ((warn_unused_resu= lt)); void maps__put(struct maps *maps); static inline void __maps__zput(struct maps **map) diff --git a/tools/perf/util/namespaces.h b/tools/perf/util/namespaces.h index 8c0731c6cbb7..04e1878b9551 100644 --- a/tools/perf/util/namespaces.h +++ b/tools/perf/util/namespaces.h @@ -50,7 +50,7 @@ int nsinfo__init(struct nsinfo *nsi); struct nsinfo *nsinfo__new(pid_t pid); struct nsinfo *nsinfo__copy(const struct nsinfo *nsi); -struct nsinfo *nsinfo__get(struct nsinfo *nsi); +struct nsinfo *nsinfo__get(struct nsinfo *nsi) __attribute__ ((warn_unused_result)); void nsinfo__put(struct nsinfo *nsi); bool nsinfo__need_setns(const struct nsinfo *nsi); diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h index 9068a21ce0fa..c6228252b093 100644 --- a/tools/perf/util/thread.h +++ b/tools/perf/util/thread.h @@ -71,7 +71,7 @@ struct thread *thread__new(pid_t pid, pid_t tid); int thread__init_maps(struct thread *thread, struct machine *machine); void thread__delete(struct thread *thread); -struct thread *thread__get(struct thread *thread); +struct thread *thread__get(struct thread *thread) __attribute__ ((warn_unused_result)); void thread__put(struct thread *thread); static inline void __thread__zput(struct thread **thread) ``` This shows the problem like: ``` util/symbol.c: In function =E2=80=98dso__load_kcore=E2=80=99: util/symbol.c:1467:25: error: ignoring return value of =E2=80=98map__get=E2= =80=99 declared with attribute =E2=80=98warn_unused_result=E2=80=99 [-Werror=3Dunu= sed-result] 1467 | map__get(map); | ``` I double checked and the symbol.c issue was the only one in my build environment. Using warn_unused_result should be done via compiler.h which is a bit more than the patch above. Thanks, Ian > --- > tools/perf/util/symbol.c | 10 ++++++---- > 1 file changed, 6 insertions(+), 4 deletions(-) > > diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c > index 6b9c55784b56a4be..d275d3bef7d54a40 100644 > --- a/tools/perf/util/symbol.c > +++ b/tools/perf/util/symbol.c > @@ -1458,16 +1458,18 @@ static int dso__load_kcore(struct dso *dso, struc= t map *map, > list_del_init(&new_node->node); > > if (RC_CHK_ACCESS(new_map) =3D=3D RC_CHK_ACCESS(replaceme= nt_map)) { > + struct map *map_ref; > + > map__set_start(map, map__start(new_map)); > map__set_end(map, map__end(new_map)); > map__set_pgoff(map, map__pgoff(new_map)); > map__set_map_ip(map, map__map_ip_ptr(new_map)); > map__set_unmap_ip(map, map__unmap_ip_ptr(new_map)= ); > /* Ensure maps are correctly ordered */ > - map__get(map); > - maps__remove(kmaps, map); > - err =3D maps__insert(kmaps, map); > - map__put(map); > + map_ref =3D map__get(map); > + maps__remove(kmaps, map_ref); > + err =3D maps__insert(kmaps, map_ref); > + map__put(map_ref); > map__put(new_map); > if (err) > goto out_err; > -- > 2.37.1 >