Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp1278218rdb; Fri, 20 Oct 2023 13:47:57 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH7bmJxJjIJHcuxEQpzluVeAI7SyPILwTXXe9V5B1UpKnCzZIY0E8Xz85gb6hfQmSF/G85N X-Received: by 2002:a05:6a20:12d0:b0:17a:4890:fa31 with SMTP id v16-20020a056a2012d000b0017a4890fa31mr2709471pzg.56.1697834876809; Fri, 20 Oct 2023 13:47:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697834876; cv=none; d=google.com; s=arc-20160816; b=uRl2YwKF6kJHE04PYXK981yYZao+5pVjx+lsIJ6PxCc+XrW1tCy0Z+aHFjsLb7Myit HpHL620d/glNjmHwVbJxzL3X76Fei9HSrMePrIPIJg8yt7B4UYFk3YcTtvz8C21MZpZE g/0b+ayYSqWcSxW4NLw/ou5XLX/E3KdQ7ZbvNawY/rs5lcNb6Qwx/jSK1MO3U6PR+h5e pwA4vOxqLKfxCYu2rgxO4b/QXt1RkEB8LWK+D0MahfEx6lR6JzYWsKsmsF2W/bgodEcm ucbm05p2e1ZdX5pRiM+7+WfO601eaQPYX0RGBBClGL+JeA5SE2BpKe5ODUheIsaZ5yS2 KNkg== 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=s0ZIMRigGTwdVjAgf14DvEXgC3201/iiyAMjiEO7lCw=; fh=Eu3OfQ0ohnhl6u2myNPCo9midvMFa9y2PC9Dj+pizlY=; b=wZmJz0gsWoYYJUD+1yte2I9isHEgLjkPagKcVYhb7LwTZHGORt4azEy7QFzmnTjBYx f0bPCrE/Z+5wttzp2ZHw0waCGo9MeOHGKfF7C7ItkGeuxvPgPl6USS9+Igu4qPjpdPSR tOmPDR0p1CfwQUfwFgJsTCI7HbPLGz3JF9aFmtFP8a3VX6RhB5M9u6KIFd02KL33+EqK DgzAGHJiMe7MGQ2lV8QCk1YBnNY0ifDQ7UZryDWn2L45iZKo139xhyhEShxRCyZ/iU7E ILNgAVqnoi5eKwDXCvf0tA2/buyfGQoP1C3kHC5XAZ+g3wwwho6S4YH34DwKciHBMYFw i1+w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=YZbhJEo4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id r22-20020aa79636000000b00690c23d353asi2512273pfg.217.2023.10.20.13.47.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Oct 2023 13:47:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=YZbhJEo4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (Postfix) with ESMTP id 8A120830C324; Fri, 20 Oct 2023 13:47:49 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230408AbjJTUrr (ORCPT + 99 others); Fri, 20 Oct 2023 16:47:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49710 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230348AbjJTUrq (ORCPT ); Fri, 20 Oct 2023 16:47:46 -0400 Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 11CFCE8; Fri, 20 Oct 2023 13:47:45 -0700 (PDT) Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-6ba54c3ed97so1179124b3a.2; Fri, 20 Oct 2023 13:47:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697834864; x=1698439664; darn=vger.kernel.org; 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=s0ZIMRigGTwdVjAgf14DvEXgC3201/iiyAMjiEO7lCw=; b=YZbhJEo4UOjprrvjrvFewOSic0H1Fdd2aWy9RxOXWOyVWMm04JY6DG4ic6DUoK+POC pKveaFOEcJsIVoq4Pi1pl8Vj8SDGXrfiS7zWN2xQTH/Kg2vC67oMNzTfEzpSLkWIWrFR 5qIWBMOLTdhfDKSBy14laUy8ArK/n8vLhwg+lyRGZSrmVqTA074PJfJ0dFtPXDKhoedV ee6batgpo9oTJ0z9fWkqxneeYxjJthOdMCUF0Aei6x8ID/ZpmhMtxrL47kSxpAz/IHK7 cKmHa4jE4rHAZHfwhlq0iow/5vPkLZY5KhVHTEVy7XfGpzIA26XuYYe0GwBX5CY7/G1y v7Rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697834864; x=1698439664; 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=s0ZIMRigGTwdVjAgf14DvEXgC3201/iiyAMjiEO7lCw=; b=tW8GXgNNB0cmEV/D2NhWfCv4TWA2b8VK5aWXm71HVIE5i8AHjy8TPL2YPybWp+GAGq +HvcrER7wt5KC0Tfu0Z3kv8rxjvPP8IkMoI7pC77SPiqwG33b68AWylEMKYih1pvP0e2 t2ErIxYjHa3K5GqnSe0Wjv/1wNamYwh2FV1YWc1FhtDZVh2Xx+qsXYIi371KFd3dEHL+ IsQWop61xUMpiHgTlyPZzAqxuZV7fOGM7CElIyfoCigvHUB0rIZEeDgsdbE16Oqw7Ww0 LrEkji1QxBLChBfb7Nw9UlxehXGYKtqI5sJkTJQXJm22eBnNUw4AhLjR/kB5OEb0GZ9e lAwg== X-Gm-Message-State: AOJu0YzYoqzUk/5esNk0quk+nzY5HFTFjtIPtWPMqpkpdN4d5iPRrxZj VOhytdTCxWbeQ/2VjyKHzd4= X-Received: by 2002:a05:6a00:853:b0:6be:b79:eca with SMTP id q19-20020a056a00085300b006be0b790ecamr3278962pfk.32.1697834864411; Fri, 20 Oct 2023 13:47:44 -0700 (PDT) Received: from bangji.hsd1.ca.comcast.net ([2601:647:6780:42e0:17e0:7ea9:fbc6:4c7d]) by smtp.gmail.com with ESMTPSA id r25-20020aa79639000000b00694f14a784bsm1971183pfg.52.2023.10.20.13.47.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Oct 2023 13:47:44 -0700 (PDT) Sender: Namhyung Kim From: Namhyung Kim To: Arnaldo Carvalho de Melo , Jiri Olsa Cc: Ian Rogers , Adrian Hunter , Peter Zijlstra , Ingo Molnar , LKML , linux-perf-users@vger.kernel.org, Song Liu , Hao Luo , bpf@vger.kernel.org Subject: [PATCH v3 2/3] perf lock contention: Check race in tstamp elem creation Date: Fri, 20 Oct 2023 13:47:40 -0700 Message-ID: <20231020204741.1869520-2-namhyung@kernel.org> X-Mailer: git-send-email 2.42.0.655.g421f12c284-goog In-Reply-To: <20231020204741.1869520-1-namhyung@kernel.org> References: <20231020204741.1869520-1-namhyung@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.5 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_BLOCKED,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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Fri, 20 Oct 2023 13:47:49 -0700 (PDT) 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 --- 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 == NULL) { struct tstamp_data zero = {}; - 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 = bpf_map_lookup_elem(&tstamp, &pid); if (pelem == NULL) { __sync_fetch_and_add(&task_fail, 1); -- 2.42.0.655.g421f12c284-goog