Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp1260741rdg; Fri, 13 Oct 2023 16:01:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE32oGUpTNwWK3oRHyCbmHhGkDR7gUA3KpfmPHKIGunwJhIebZwrfi4Bow6+pA5qC9q1qCw X-Received: by 2002:a05:6358:279f:b0:139:d5d5:7a8f with SMTP id l31-20020a056358279f00b00139d5d57a8fmr30360875rwb.30.1697238093545; Fri, 13 Oct 2023 16:01:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697238093; cv=none; d=google.com; s=arc-20160816; b=clkKUZcYP7EFYm2Rmbv1jPlj9K8+8OxYxcFG61i9NxH3NLyEl+EWzA8M3lzHZ/rdZn I8VAvfpxHAwK/OQX4k2L7nCaglRh56Q5jE4Y/5zxQOKr0PVnTpVTSnjoapdyai7Dku/3 SCpwP0poyU02tLQxtcr7GEoFJWYkMEIRbSpB03F02DAt/STOq3QqfUwKJbD88cLYBqZo JkDjlOcDsLly1DXvwJzjP6v4qG7+jKejgV8Pj2R9hFfAux4jkWPCQhNBEQ1BmODEqU13 gkyNkbaWWs7gSQrtctbOrMxLAugLOLkKBp0iUxElJpCNR+lZQSwYGgvxqg2akqbzinI4 sBqQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=HJYHc3Q9vcnfsjHnxMerBLxymRQGQkYLJXajHdUYVtw=; fh=4TVqlnyuZuSHH6P2LpvinW6+YAaTBa1MrWZa2Q6twUk=; b=hQQnWZiiji+W4OE2xO7vfS6Wrml5oxzEXZuOpW+8frJgFUkblfT+v7sLKchwnI0hbO NuMNOpP++vfgw51AzjBXmffnvozD7P5avZwsGwaJ0l2hRHQYGjE5FP/eQxDkb+xtCZuv S6AT+kIosanLEP5PYVqqHp1jpN+zvqvfeGFbjDVFL8qZkf6tWUTb82mEFr7U7aciFsgw NMdVakLasi5jE7Rcst+ybv8uD0TABitmaZvTP6oyBTpXMOKSyLV+bIJrQRgFMxBG4+EQ SuMLVJUL+DPqHnwS9wiLK2nwu1WQ2E8//Ga1E3xrx0BAW2Q1EIVhBpzY7T/QZMYeH9OG e/7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=khOgFP81; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id fd17-20020a056a002e9100b006896e2f30f2si18696157pfb.365.2023.10.13.16.01.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Oct 2023 16:01:33 -0700 (PDT) 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=@kernel.org header.s=k20201202 header.b=khOgFP81; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 71C9E830D130; Fri, 13 Oct 2023 16:01:00 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231924AbjJMXAu (ORCPT + 99 others); Fri, 13 Oct 2023 19:00:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34510 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229830AbjJMXAt (ORCPT ); Fri, 13 Oct 2023 19:00:49 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 64C96B7 for ; Fri, 13 Oct 2023 16:00:47 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9DCD3C433C7; Fri, 13 Oct 2023 23:00:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1697238046; bh=8qM9BSYxF4si0oZK0+zWRYG3pweYYK3vSzWPG9FsH9Y=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=khOgFP816HxAaaJBFb9SGkC1jihPOk1bYRxg562/KgHri4N7/lK+1LGiW2kHsAThk gMBZIDfcFSDqO7i12sQH5uibP0qc4F721SRUSQqpoqbzo8lhOTdY8N3EC4TqTR8H1p PSBYfs/hwM54RDVmnyDt7vcN8971GsikBv/x1WUdB/tPfi6JBIwV7LyzOIKvFrkCre GAQFDP7ZWclQxwciGSXhmaL8uUhKe4uGzRkWM8vqEJDweudGRCh3IWB/Futq4tMW9n nJ8wYUrH5yUdjN67zTzuEYY9W11dvWp9+DOUeJ5/xUnz7cxQTiE8moewpu2IWKwLgo W7sZkk+MRDhSw== Received: by quaco.ghostprotocols.net (Postfix, from userid 1000) id B2AEA40016; Fri, 13 Oct 2023 20:00:43 -0300 (-03) Date: Fri, 13 Oct 2023 20:00:43 -0300 From: Arnaldo Carvalho de Melo To: Namhyung Kim Cc: Jiri Olsa , Ian Rogers , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Song Liu , Hao Luo , bpf@vger.kernel.org Subject: Re: [PATCH] perf lock contention: Clear lock addr after use Message-ID: References: <20230928235018.2136-1-namhyung@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230928235018.2136-1-namhyung@kernel.org> X-Url: http://acmel.wordpress.com X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS 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]); Fri, 13 Oct 2023 16:01:00 -0700 (PDT) Em Thu, Sep 28, 2023 at 04:50:18PM -0700, Namhyung Kim escreveu: > It checks the current lock to calculated the delta of contention time. > The address is saved in the tstamp map which is allocated at begining of > contention and released at end of contention. > > But it's possible for bpf_map_delete_elem() to fail. In that case, the How can it fail? You do: pelem = bpf_map_lookup_elem(&tstamp, &pid); if (!pelem || pelem->lock != ctx[0]) return 0; So it is there, why would the removal using the same key fail? The patch should work as-is, I'm just curious about what would make there removal of a map entry that was successfully looked up on the same contention_end prog to fail when being removed... - Arnaldo > element in the tstamp map kept for the current lock and it makes the > next contention for the same lock tracked incorrectly. Specificially > the next contention begin will see the existing element for the task and > it'd just return. Then the next contention end will see the element and > calculate the time using the timestamp for the previous begin. > > This can result in a large value for two small contentions happened from > time to time. Let's clear the lock address so that it can be updated > next time even if the bpf_map_delete_elem() failed. > > Signed-off-by: Namhyung Kim > --- > tools/perf/util/bpf_skel/lock_contention.bpf.c | 4 ++++ > 1 file changed, 4 insertions(+) > > diff --git a/tools/perf/util/bpf_skel/lock_contention.bpf.c b/tools/perf/util/bpf_skel/lock_contention.bpf.c > index 4900a5dfb4a4..b11179452e19 100644 > --- a/tools/perf/util/bpf_skel/lock_contention.bpf.c > +++ b/tools/perf/util/bpf_skel/lock_contention.bpf.c > @@ -389,6 +389,7 @@ int contention_end(u64 *ctx) > > duration = bpf_ktime_get_ns() - pelem->timestamp; > if ((__s64)duration < 0) { > + pelem->lock = 0; > bpf_map_delete_elem(&tstamp, &pid); > __sync_fetch_and_add(&time_fail, 1); > return 0; > @@ -422,6 +423,7 @@ int contention_end(u64 *ctx) > data = bpf_map_lookup_elem(&lock_stat, &key); > if (!data) { > if (data_map_full) { > + pelem->lock = 0; > bpf_map_delete_elem(&tstamp, &pid); > __sync_fetch_and_add(&data_fail, 1); > return 0; > @@ -445,6 +447,7 @@ int contention_end(u64 *ctx) > data_map_full = 1; > __sync_fetch_and_add(&data_fail, 1); > } > + pelem->lock = 0; > bpf_map_delete_elem(&tstamp, &pid); > return 0; > } > @@ -458,6 +461,7 @@ int contention_end(u64 *ctx) > if (data->min_time > duration) > data->min_time = duration; > > + pelem->lock = 0; > bpf_map_delete_elem(&tstamp, &pid); > return 0; > } > -- > 2.42.0.582.g8ccd20d70d-goog > -- - Arnaldo