Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1316958rwb; Tue, 29 Nov 2022 11:49:19 -0800 (PST) X-Google-Smtp-Source: AA0mqf4zLMdxGt2Olm+ao1gGfb9BNOWlcEbDirAkJTK7X35kTioZ6aMQKJmUztNxtRakhw97/FHe X-Received: by 2002:a17:90b:711:b0:210:9858:2b2c with SMTP id s17-20020a17090b071100b0021098582b2cmr62292387pjz.191.1669751359423; Tue, 29 Nov 2022 11:49:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669751359; cv=none; d=google.com; s=arc-20160816; b=W3q4/XzKm11erkAnnwHTbIkF0FsWkr04XF9p1Re09xeX+nZh0J7SOfFQJmw9gPbOPq m9ngYq9C1qKwIZzTYL11+JBu14Lv0guHDrmVkoijpPgJ3p44do6UxBIlJOlFLiQChalJ kVPTywO+CVECbrZo1he0+pm+1vx7w4rJecy5/2AXzhxaHf2ldnc1SwzAmABslQYefOZ6 /9lQqxj+dKrczE2cEJm/YgUwTKrsp5xCXh3g60YkXQ6qGc3viaoz/QeH82O49TxJZ3+L QdiK/1hXvQH35Q84jTAZC/VuWsMUiHt3QNSwtIkvfNr0ybkXEpryfp6prRDIIeDTj7Qz jEwg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=1bmuvgUk4OTyzS0cP/IZYe3bJ8nPbcieDdEYGxb93AQ=; b=ei4euHUIY0EZZRwTkAqUFGYd7w84XZYsQ0If5aYpQeNyr621GnvWHxy8nWZ3qEneIv 1oDKPovHpuMVb3266ojtc6tkhPpPtQFvnrXnFohoQugd6vPBqjwh7hROzoSNluWGg8CA panif6CR3tgv3BcVbh3b8UeFIfif3Wv5z8GOmcfUlT2HudBDcRz8vYAiBSgInUPWDMUW R1aivDpXtMePnhI4ibxLOMrdXPdOS10iijA+LI5LT7j0ffybbVVNY1iAswdZUDCMj3YS Vf9spgNB1U0nkt06kvLFRpv0EthTULw0zQYsBbyEx4FzP/7KbRkGyQygKXl4U/2YlQoO lhXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=ntXkEtvh; 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 o9-20020a170902d4c900b00186a16c000dsi16764551plg.313.2022.11.29.11.49.08; Tue, 29 Nov 2022 11:49:19 -0800 (PST) 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=20210112 header.b=ntXkEtvh; 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 S237066AbiK2Tj0 (ORCPT + 85 others); Tue, 29 Nov 2022 14:39:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51586 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237069AbiK2TiM (ORCPT ); Tue, 29 Nov 2022 14:38:12 -0500 Received: from mail-yw1-x112d.google.com (mail-yw1-x112d.google.com [IPv6:2607:f8b0:4864:20::112d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 802086E553 for ; Tue, 29 Nov 2022 11:36:35 -0800 (PST) Received: by mail-yw1-x112d.google.com with SMTP id 00721157ae682-3691e040abaso149484727b3.9 for ; Tue, 29 Nov 2022 11:36:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=1bmuvgUk4OTyzS0cP/IZYe3bJ8nPbcieDdEYGxb93AQ=; b=ntXkEtvhVc/YnzEWpIUFZAj9rJeK2awgv14ux5liOk6ARFQ7QDmkY9WvWMn6EqOFU2 n0pA2iVfSHzd+Pt8sMCAWm0CK60nTMK4dvyiY3Poqo2zPHY5NUEYZa1aSGTCWnh4Ym/6 FI1Te3VLkqFV+1i2lu4M5DO0yWJBf5cq8aVFdwOJAFQ1nSnBIKa0RJYKkq24GZBrTmSk yg4RMe0/JV7RQAWFzsZR5jn7ePqPZQ1dqN2W0hi60e9o8c8NItwV6vTujonY0uxJhbCT cnopifUGk1l7GXsYdCiKrI8P9J0VYSn8g3mZvmOviAWx1ux5LIcU8jKeqjk5RhCWwhe0 ByyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=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=1bmuvgUk4OTyzS0cP/IZYe3bJ8nPbcieDdEYGxb93AQ=; b=vf6dI1alzEyK9xXPX68tX6afhjPYAUSsmvbxA/1lKIoPgCav9GbLXIIMGQ1yaf81Bn XSeX6gECYUHXIhU8UaygRCJFRRt6wr3nmePSMzgpm8xkImN8Zue50dP7n0+oIwm8smzT 8Lldri1XS+8kcj1uyEqEP0FuNNW6M/Xxy1j8msiUG2S/2APOX6tTrAwDeL7vooaswsQg vFcqLI9qvAedu0RsOZTD1FSxXHisECwbLO3Y7pQ9f0qcd7NH0+BgjWTYbaRwjwnVIMQ/ irxXJeApJfdyCeriwZFsmZzkSSWoGmmu4y6cIqh2+61WPwP0j6Eg/D1lCGsEkOaloKHU PuVg== X-Gm-Message-State: ANoB5pm8hGUm+PRdlu3KULdb+x8KWfDkn3BCOgDl62NbxpSMIIHOIvLO EK8snCCQq8xa0ote9jgrdv3KSKxNHGBq7FMLVpi5Xg== X-Received: by 2002:a0d:f0c5:0:b0:373:4bf9:626e with SMTP id z188-20020a0df0c5000000b003734bf9626emr37357556ywe.173.1669750594587; Tue, 29 Nov 2022 11:36:34 -0800 (PST) MIME-Version: 1.0 References: <41eda0ea-0ed4-1ffb-5520-06fda08e5d38@huawei.com> <07a7491e-f391-a9b2-047e-cab5f23decc5@huawei.com> <59fc54b7-c276-2918-6741-804634337881@huaweicloud.com> <541aa740-dcf3-35f5-9f9b-e411978eaa06@redhat.com> In-Reply-To: From: Hao Luo Date: Tue, 29 Nov 2022 11:36:23 -0800 Message-ID: Subject: Re: [net-next] bpf: avoid hashtab deadlock with try_lock To: Boqun Feng Cc: Waiman Long , Hou Tao , Tonghao Zhang , Peter Zijlstra , Ingo Molnar , Will Deacon , netdev@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Jiri Olsa , bpf , "houtao1@huawei.com" , LKML Content-Type: text/plain; charset="UTF-8" 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, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=unavailable 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 Tue, Nov 29, 2022 at 9:32 AM Boqun Feng wrote: > > Just to be clear, I meant to refactor htab_lock_bucket() into a try > lock pattern. Also after a second thought, the below suggestion doesn't > work. I think the proper way is to make htab_lock_bucket() as a > raw_spin_trylock_irqsave(). > > Regards, > Boqun > The potential deadlock happens when the lock is contended from the same cpu. When the lock is contended from a remote cpu, we would like the remote cpu to spin and wait, instead of giving up immediately. As this gives better throughput. So replacing the current raw_spin_lock_irqsave() with trylock sacrifices this performance gain. I suspect the source of the problem is the 'hash' that we used in htab_lock_bucket(). The 'hash' is derived from the 'key', I wonder whether we should use a hash derived from 'bucket' rather than from 'key'. For example, from the memory address of the 'bucket'. Because, different keys may fall into the same bucket, but yield different hashes. If the same bucket can never have two different 'hashes' here, the map_locked check should behave as intended. Also because ->map_locked is per-cpu, execution flows from two different cpus can both pass. Hao