Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1436539rwb; Tue, 29 Nov 2022 13:37:50 -0800 (PST) X-Google-Smtp-Source: AA0mqf5hwCKqhif6L68aTn56Bci85KFN+lD+HEKtBGtGTtHF5n3MK9rAb1t/Ixzp4z27U0I3jmR5 X-Received: by 2002:a17:90b:3c91:b0:219:608a:75c with SMTP id pv17-20020a17090b3c9100b00219608a075cmr730730pjb.33.1669757869961; Tue, 29 Nov 2022 13:37:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669757869; cv=none; d=google.com; s=arc-20160816; b=jQF9ryZEAFv0+ZPAxzke6OE83adY4+ip7k/8GA+6qbpT5Up2nWwY7Tu72QBNTSSIIs ZZQN9/p9XSGFxh3/l3D00CfypbBQydzxXDnjUP4Wd9Lw0nrnjW4rW5g6+X0AQGYMHm2E I5NHDCELTMwgTMykKehnbB5lLHl7hPeFuU1YINTLRmgOVFXieeWCwubvGilka3y5t1JT d5rLghsZ/CGJ9ugOZVBxJ4l5Gb0nrJUmDrETofXjMS0wWWhgarxBNkwxHyuEQaa5Tkvk nr5bLl+k6LwMPOSzvuB7iW58PcnvqDD9fdNLxkitC85LoKNuplMVsagWmq/5sCpYCjmS p2bQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=d30QTxTORkW1N9eKwj6M+zoNiUdizDE1AT68blPInq4=; b=OAdaAmUDvVCwmV8ECVz7aCcdgsqol2h9FaW0Nt6xjdL4kA32zNocMqSaSNyjwOyrz/ fx6trawyGqcFKcbbMxbpfJmRfkH2nitNkJAaFCkPYOshuKXNslbCuXlA5hI6i8CDnp/p 4NTRKwsGhkHMjfwnMbVPLKLqisdeONl0QhxtRQxXgl8r3PKA93du0lBZKQPHw1DGWxpC wRl2RJMLAL3RQwIcPCJW1sBlOEPq+9Pmac1d8dFH0JSXuRIpEWbCzRASvA57xz4tI1S5 jiz/WwU+RKXLmO8ZhlSSYdHKURY8nKMXDvGh/3EEqA6O71cYzrFTjlviH0E4X/NgLEvV CCdQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=V3fXR2vs; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k16-20020a63f010000000b0047701022c7dsi16962081pgh.729.2022.11.29.13.37.38; Tue, 29 Nov 2022 13:37:49 -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=@redhat.com header.s=mimecast20190719 header.b=V3fXR2vs; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236874AbiK2VO6 (ORCPT + 84 others); Tue, 29 Nov 2022 16:14:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42642 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236771AbiK2VO4 (ORCPT ); Tue, 29 Nov 2022 16:14:56 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F37A42BB3E for ; Tue, 29 Nov 2022 13:13:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669756424; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=d30QTxTORkW1N9eKwj6M+zoNiUdizDE1AT68blPInq4=; b=V3fXR2vsmu6dP/sN09EHHruQE+B8tSK/reUc4EDY1oY+TK1gtdqZoWFWM/5+knuLD7EoXf hllBut8c1Pb3Kz/CbU+AnzbKkKfvcR1WY2sOudTfqXByTo+Mqmem8BaX4yTByZS8EMNgjo NKyQSC4Li/AhAh+hTwgOy22nTQ9pP+Q= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-517-aiGqA2bEOSuJ6sHhQ3xVHg-1; Tue, 29 Nov 2022 16:13:39 -0500 X-MC-Unique: aiGqA2bEOSuJ6sHhQ3xVHg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B4C94802314; Tue, 29 Nov 2022 21:13:38 +0000 (UTC) Received: from [10.22.17.30] (unknown [10.22.17.30]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5E4E640C83AA; Tue, 29 Nov 2022 21:13:37 +0000 (UTC) Message-ID: <51c23d0e-9f00-4433-f2e8-95113f0b2a9d@redhat.com> Date: Tue, 29 Nov 2022 16:13:35 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.0 Subject: Re: [net-next] bpf: avoid hashtab deadlock with try_lock Content-Language: en-US To: Hao Luo , Boqun Feng Cc: 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 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> From: Waiman Long In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Spam-Status: No, score=-2.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE 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 11/29/22 14:36, Hao Luo wrote: > 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. I would suggest that you add a in_nmi() check and if true use trylock to get the lock. You can continue to use raw_spin_lock_irqsave() in all other cases. Cheers, Longman