Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp2311254rda; Tue, 24 Oct 2023 21:14:13 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGAOo7e+kUeFmMMmkpqW694n/S1Hn1u4LDQpUF137QWNvY+51ESm31ucYAoBFdRwq62h57g X-Received: by 2002:a25:cb17:0:b0:d9b:90d2:aadf with SMTP id b23-20020a25cb17000000b00d9b90d2aadfmr16505873ybg.49.1698207252822; Tue, 24 Oct 2023 21:14:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698207252; cv=none; d=google.com; s=arc-20160816; b=DS9dpYP3RyY3IJtoFWuYv5aO9J9XJHXl7ZlTHJhNBdIYb20GCGjVyMOxNOangxqUcq fo8H5n6r8eV33+hgVvnIMiXOotJUdWd/UheDWhSrgr3/B7KHnST4Ay3Dv3GYS6u9MYsY 4U8wGRsJ6cJEl4P+L1Oeuoix9jiQwwu+w4PQNVYImKxSEZxLB/EvDYUWxICfUT0F3vV1 IRO+3thWIqgtCocrtQXsWCaixj8NnMVxR7V57UlZEaHflepnta8FfXmDX/9AFV4+388D ThM6BQMHH5tCHX2Aoz0IZa9A+lpEPC35q/vLyGgQPyhSOuwTmJ9nMqDV5yH5iZtSZf1s HW1A== 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=MRrGdq6DFD2VOzqL31MoSC4FSnbuEPDJ3WtOuo0gpgA=; fh=vXy/zKLYS8CIT5S93PT42H9kmQ6ohgipR/F8r41ABBw=; b=yWAFjh5P50+1RFqZkBDg2/6CRKVqy3nZehzlQeKyDh9PC686di8QMzjkEjg1yVd4AN CmN8ibIHsT20ErA3kZpiFD8zSEvuRnY5nPKNJ3U8eBqwbFMxjBgVMfq6WK6cKaV3aGg/ 9V8OsGEFyP1r2wX8cXO5jikCw5UOk7ryE8QROcSUEGViU0EsKx3llUyMDwOAu+1AiJf9 JWMBDPtDWDIISRkLzzoSVi+QOUWIL/T/bq3gPSFcc6OD90IRYGabRqknJUMBn/wOTqs/ xaJRhIDoYO3nWmPS2V9sHh57ussuHA6b8H0iajfH1FGm6u596mr7YS+YrHktbA3o3nPf rSEw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="G/3QN55J"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id p135-20020a25428d000000b00d9a63d1d310si10069287yba.109.2023.10.24.21.14.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 21:14:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="G/3QN55J"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 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 agentk.vger.email (Postfix) with ESMTP id 21C51805E44D; Tue, 24 Oct 2023 21:14:10 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232562AbjJYEOF (ORCPT + 99 others); Wed, 25 Oct 2023 00:14:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48704 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230093AbjJYEOD (ORCPT ); Wed, 25 Oct 2023 00:14:03 -0400 Received: from mail-lf1-x133.google.com (mail-lf1-x133.google.com [IPv6:2a00:1450:4864:20::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 06FA2122 for ; Tue, 24 Oct 2023 21:14:01 -0700 (PDT) Received: by mail-lf1-x133.google.com with SMTP id 2adb3069b0e04-507a5edc2ebso1143e87.1 for ; Tue, 24 Oct 2023 21:14:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698207239; x=1698812039; darn=vger.kernel.org; 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=MRrGdq6DFD2VOzqL31MoSC4FSnbuEPDJ3WtOuo0gpgA=; b=G/3QN55J/sCS0J/SfGcnPJ8kUmqO1BxPImMnbO/CxkKpmC4g8Wip4k7ozpr9UFyFXG KWi+EmbI/v2GFbfIzNMwJtsh2Du6XqPiAZyTBx+O9GPkh8QsMVNiIExvfvIBj/M1D4HO Cva3Af2meEXpOZ1xUIc2SzXto+sjyz5Vh3d/jlfvzmqLgeJxeU3cBfwqZlieLMzOrnB1 ec9Sey1XhOcYPQKNKSj7CACiqh+HC2LBrtf0c7UPwTWkG06kEQGi3hJB/gs/77XRd8sw QXIRdh/TLZf1wZ0zmNGahBBVWbAL5RTUOrbPjLynQ6lO+MHv+srdet+spYjaSxQxcTdz 0scQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698207239; x=1698812039; 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=MRrGdq6DFD2VOzqL31MoSC4FSnbuEPDJ3WtOuo0gpgA=; b=xEqIIwg3Gqn6+gDprCQF1ogCIxE5l/REEoirVHAKfcgVVKp8rApJEEd7ntyv+VSAFc cp8Xa48ffuRH2U+yJiXTrj8I+Si61o9oytU2jwSVPeUtkYE2hlnk7kBLrutVEw9zV/Ck QOuRj/lsyJSBfUMhWkq8FpcqPZ9H0RfUBW0c3mxKobGC57YlLDmGZg1a7GrnRlqrMq5H +a7Tt/WzMGEefh4tqzbsj336Al8cDmUDr2YCj41TueL+l8Iu+jbR2EoIvQUg+2QjWH6z LU5YHVkBzQwUJ5+AqquMWMwy8vkQCsJEs7Xq85kkKgOkn4Re/AvL9ecxXGtLBqrs4Emy 8T8g== X-Gm-Message-State: AOJu0YwKekrA0QF856pELtzL8vYqMccm7HKYHNjkz8Mx8cOe6xI5RXbU O4AOzoshO5JMvO/519w+U3W4iap8UjHZPN339Yli4g== X-Received: by 2002:a05:6512:360e:b0:501:b029:1a47 with SMTP id f14-20020a056512360e00b00501b0291a47mr17427lfs.1.1698207238852; Tue, 24 Oct 2023 21:13:58 -0700 (PDT) MIME-Version: 1.0 References: <20231020204741.1869520-1-namhyung@kernel.org> <20231020204741.1869520-2-namhyung@kernel.org> In-Reply-To: <20231020204741.1869520-2-namhyung@kernel.org> From: Ian Rogers Date: Tue, 24 Oct 2023 21:13:47 -0700 Message-ID: Subject: Re: [PATCH v3 2/3] perf lock contention: Check race in tstamp elem creation To: Namhyung Kim Cc: Arnaldo Carvalho de Melo , Jiri Olsa , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Song Liu , Hao Luo , bpf@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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,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 agentk.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 (agentk.vger.email [0.0.0.0]); Tue, 24 Oct 2023 21:14:10 -0700 (PDT) On Fri, Oct 20, 2023 at 1:47=E2=80=AFPM Namhyung Kim = wrote: > > When pelem is NULL, it'd create a new entry with zero data. But it > might be preempted by IRQ/NMI just before calling bpf_map_update_elem() > then there's a chance to call it twice for the same pid. So it'd be > better to use BPF_NOEXIST flag and check the return value to prevent > the race. > > Signed-off-by: Namhyung Kim Acked-by: Ian Rogers Thanks, Ian > --- > tools/perf/util/bpf_skel/lock_contention.bpf.c | 6 +++++- > 1 file changed, 5 insertions(+), 1 deletion(-) > > diff --git a/tools/perf/util/bpf_skel/lock_contention.bpf.c b/tools/perf/= util/bpf_skel/lock_contention.bpf.c > index b11179452e19..69d31fd77cd0 100644 > --- a/tools/perf/util/bpf_skel/lock_contention.bpf.c > +++ b/tools/perf/util/bpf_skel/lock_contention.bpf.c > @@ -328,7 +328,11 @@ int contention_begin(u64 *ctx) > if (pelem =3D=3D NULL) { > struct tstamp_data zero =3D {}; > > - bpf_map_update_elem(&tstamp, &pid, &zero, BPF_ANY); > + if (bpf_map_update_elem(&tstamp, &pid, &zero, BPF_NOEXIST= ) < 0) { > + __sync_fetch_and_add(&task_fail, 1); > + return 0; > + } > + > pelem =3D bpf_map_lookup_elem(&tstamp, &pid); > if (pelem =3D=3D NULL) { > __sync_fetch_and_add(&task_fail, 1); > -- > 2.42.0.655.g421f12c284-goog >