Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1056115rwb; Tue, 29 Nov 2022 08:27:05 -0800 (PST) X-Google-Smtp-Source: AA0mqf6ny7/Ze2ZYJBL92Fvm6H+m5yxqUxGywdaiXCXxB9rNRdYwkWsdcS9Qf7f78t5dtIFhfrbF X-Received: by 2002:a17:902:f312:b0:189:6077:5598 with SMTP id c18-20020a170902f31200b0018960775598mr25080816ple.100.1669739225202; Tue, 29 Nov 2022 08:27:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669739225; cv=none; d=google.com; s=arc-20160816; b=iEZsmoeBzwoSs+Sk13HUB2QuikD+DJ7MymBKpagOHX5Nf2MqnbyWQdXyyi7DVDrNsI zw1OgLp+hVfv1aXmtWkbA09MsvH1Kx9bJcJS/kY/5wgExMMi+3nV102tyuqFxJInUrum 42KI7jWPYW7cQeoKcBtxbhv16pPKgSDZsX1o4pkbMxBF4U3DJyQbnq5fWeqiF/iqrq7N yVZK3gMp7jqr+RQPzhJehB/jhlCsLkeQ55hDZIXfisAkVzms2epqPD4nGuWH4ttQTmeR c3LKFLATuOwVV2mv+YZVAMGv9uMU582AcYERqf/9IndsJZ+CiM4Vmm4FD/gmXdDyZ2jI 2ZFQ== 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=MGSsOKZIaXvD2Db2EaTYBBqV/YNM2fgfWamu5PuCnRI=; b=CXpdB/+rkNtL3rFwa/BhO3h4ynrLerYzg0oG+HLOGwp/yu1n6HpdkJcF0StKHBuvOz TxnmzQHGWLCKhJdupqXWK1adgY8XdG1QDHyWgki3Repa5svIgDNPVNVBOhwpwGNHYRmm YjdRZgKYDSIWV+fk0DHpIfzR2KWavcFqpCBWxArvtR521xFfg687Os8FfWklAvxTWKum IKYB54LJtQnn6RoUnzB9Wpu3yJicDeu9rPH3Ywym08GwAxwszoFT0ucGHgF84nDRRx3F GeykG6vO/M6dXIhZAvieiiXYy3p7wtfaeqLqAghYZXTaAE0z8xDdQxsZsfDhKcb0uqMA X+Vw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="Y2ewe/Gn"; 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 z15-20020a63c04f000000b0043c1cb75c22si15091027pgi.333.2022.11.29.08.26.54; Tue, 29 Nov 2022 08:27:05 -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="Y2ewe/Gn"; 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 S232134AbiK2QHr (ORCPT + 85 others); Tue, 29 Nov 2022 11:07:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34470 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234997AbiK2QHn (ORCPT ); Tue, 29 Nov 2022 11:07:43 -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 81EB6178AB for ; Tue, 29 Nov 2022 08:06:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669738017; 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=MGSsOKZIaXvD2Db2EaTYBBqV/YNM2fgfWamu5PuCnRI=; b=Y2ewe/GnYmwhZJ9yFVdNxmiGAl/wlWoT3vsLyilCUTAHa+xpeI/Ttv05dChsHkrfrnEySv ZZ0v4D5vjw7nQhwPFRFcjKCRrr/Nte2jdm+4b4MTh/vyBJN1QRDZfnO8ZJjl2Tu9lWw0QA 5f5/rHkfhpDTR4iF7E3nTFQrWRDJTbE= 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-453-9TuuBb6BMwSaRGLNqSGnxg-1; Tue, 29 Nov 2022 11:06:55 -0500 X-MC-Unique: 9TuuBb6BMwSaRGLNqSGnxg-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5C42982DFCF; Tue, 29 Nov 2022 16:06:54 +0000 (UTC) Received: from [10.22.17.30] (unknown [10.22.17.30]) by smtp.corp.redhat.com (Postfix) with ESMTP id 57BB11121314; Tue, 29 Nov 2022 16:06:53 +0000 (UTC) Message-ID: <541aa740-dcf3-35f5-9f9b-e411978eaa06@redhat.com> Date: Tue, 29 Nov 2022 11:06:51 -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: Hou Tao , Boqun Feng , Tonghao Zhang , Peter Zijlstra , Ingo Molnar , Will Deacon Cc: 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 , Hao Luo , "houtao1@huawei.com" , LKML References: <20221121100521.56601-1-xiangxia.m.yue@gmail.com> <20221121100521.56601-2-xiangxia.m.yue@gmail.com> <7ed2f531-79a3-61fe-f1c2-b004b752c3f7@huawei.com> <9278cf3f-dfb6-78eb-8862-553545dac7ed@huawei.com> <41eda0ea-0ed4-1ffb-5520-06fda08e5d38@huawei.com> <07a7491e-f391-a9b2-047e-cab5f23decc5@huawei.com> <59fc54b7-c276-2918-6741-804634337881@huaweicloud.com> From: Waiman Long In-Reply-To: <59fc54b7-c276-2918-6741-804634337881@huaweicloud.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 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 07:45, Hou Tao wrote: > Hi, > > On 11/29/2022 2:06 PM, Tonghao Zhang wrote: >> On Tue, Nov 29, 2022 at 12:32 PM Hou Tao wrote: >>> Hi, >>> >>> On 11/29/2022 5:55 AM, Hao Luo wrote: >>>> On Sun, Nov 27, 2022 at 7:15 PM Tonghao Zhang wrote: >>>> Hi Tonghao, >>>> >>>> With a quick look at the htab_lock_bucket() and your problem >>>> statement, I agree with Hou Tao that using hash & >>>> min(HASHTAB_MAP_LOCK_MASK, n_bucket - 1) to index in map_locked seems >>>> to fix the potential deadlock. Can you actually send your changes as >>>> v2 so we can take a look and better help you? Also, can you explain >>>> your solution in your commit message? Right now, your commit message >>>> has only a problem statement and is not very clear. Please include >>>> more details on what you do to fix the issue. >>>> >>>> Hao >>> It would be better if the test case below can be rewritten as a bpf selftests. >>> Please see comments below on how to improve it and reproduce the deadlock. >>>>> Hi >>>>> only a warning from lockdep. >>> Thanks for your details instruction. I can reproduce the warning by using your >>> setup. I am not a lockdep expert, it seems that fixing such warning needs to set >>> different lockdep class to the different bucket. Because we use map_locked to >>> protect the acquisition of bucket lock, so I think we can define lock_class_key >>> array in bpf_htab (e.g., lockdep_key[HASHTAB_MAP_LOCK_COUNT]) and initialize the >>> bucket lock accordingly. > The proposed lockdep solution doesn't work. Still got lockdep warning after > that, so cc +locking expert +lkml.org for lockdep help. > > Hi lockdep experts, > > We are trying to fix the following lockdep warning from bpf subsystem: > > [   36.092222] ================================ > [   36.092230] WARNING: inconsistent lock state > [   36.092234] 6.1.0-rc5+ #81 Tainted: G            E > [   36.092236] -------------------------------- > [   36.092237] inconsistent {INITIAL USE} -> {IN-NMI} usage. > [   36.092238] perf/1515 [HC1[1]:SC0[0]:HE0:SE1] takes: > [   36.092242] ffff888341acd1a0 (&htab->lockdep_key){....}-{2:2}, at: > htab_lock_bucket+0x4d/0x58 > [   36.092253] {INITIAL USE} state was registered at: > [   36.092255]   mark_usage+0x1d/0x11d > [   36.092262]   __lock_acquire+0x3c9/0x6ed > [   36.092266]   lock_acquire+0x23d/0x29a > [   36.092270]   _raw_spin_lock_irqsave+0x43/0x7f > [   36.092274]   htab_lock_bucket+0x4d/0x58 > [   36.092276]   htab_map_delete_elem+0x82/0xfb > [   36.092278]   map_delete_elem+0x156/0x1ac > [   36.092282]   __sys_bpf+0x138/0xb71 > [   36.092285]   __do_sys_bpf+0xd/0x15 > [   36.092288]   do_syscall_64+0x6d/0x84 > [   36.092291]   entry_SYSCALL_64_after_hwframe+0x63/0xcd > [   36.092295] irq event stamp: 120346 > [   36.092296] hardirqs last  enabled at (120345): [] > _raw_spin_unlock_irq+0x24/0x39 > [   36.092299] hardirqs last disabled at (120346): [] > generic_exec_single+0x40/0xb9 > [   36.092303] softirqs last  enabled at (120268): [] > __do_softirq+0x347/0x387 > [   36.092307] softirqs last disabled at (120133): [] > __irq_exit_rcu+0x67/0xc6 > [   36.092311] > [   36.092311] other info that might help us debug this: > [   36.092312]  Possible unsafe locking scenario: > [   36.092312] > [   36.092313]        CPU0 > [   36.092313]        ---- > [   36.092314]   lock(&htab->lockdep_key); > [   36.092315]   > [   36.092316]     lock(&htab->lockdep_key); > [   36.092318] > [   36.092318]  *** DEADLOCK *** > [   36.092318] > [   36.092318] 3 locks held by perf/1515: > [   36.092320]  #0: ffff8881b9805cc0 (&cpuctx_mutex){+.+.}-{4:4}, at: > perf_event_ctx_lock_nested+0x8e/0xba > [   36.092327]  #1: ffff8881075ecc20 (&event->child_mutex){+.+.}-{4:4}, at: > perf_event_for_each_child+0x35/0x76 > [   36.092332]  #2: ffff8881b9805c20 (&cpuctx_lock){-.-.}-{2:2}, at: > perf_ctx_lock+0x12/0x27 > [   36.092339] > [   36.092339] stack backtrace: > [   36.092341] CPU: 0 PID: 1515 Comm: perf Tainted: G            E > 6.1.0-rc5+ #81 > [   36.092344] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS > rel-1.16.0-0-gd239552ce722-prebuilt.qemu.org 04/01/2014 > [   36.092349] Call Trace: > [   36.092351]  > [   36.092354]  dump_stack_lvl+0x57/0x81 > [   36.092359]  lock_acquire+0x1f4/0x29a > [   36.092363]  ? handle_pmi_common+0x13f/0x1f0 > [   36.092366]  ? htab_lock_bucket+0x4d/0x58 > [   36.092371]  _raw_spin_lock_irqsave+0x43/0x7f > [   36.092374]  ? htab_lock_bucket+0x4d/0x58 > [   36.092377]  htab_lock_bucket+0x4d/0x58 > [   36.092379]  htab_map_update_elem+0x11e/0x220 > [   36.092386]  bpf_prog_f3a535ca81a8128a_bpf_prog2+0x3e/0x42 > [   36.092392]  trace_call_bpf+0x177/0x215 > [   36.092398]  perf_trace_run_bpf_submit+0x52/0xaa > [   36.092403]  ? x86_pmu_stop+0x97/0x97 > [   36.092407]  perf_trace_nmi_handler+0xb7/0xe0 > [   36.092415]  nmi_handle+0x116/0x254 > [   36.092418]  ? x86_pmu_stop+0x97/0x97 > [   36.092423]  default_do_nmi+0x3d/0xf6 > [   36.092428]  exc_nmi+0xa1/0x109 > [   36.092432]  end_repeat_nmi+0x16/0x67 > [   36.092436] RIP: 0010:wrmsrl+0xd/0x1b So the lock is really taken in a NMI context. In general, we advise again using lock in a NMI context unless it is a lock that is used only in that context. Otherwise, deadlock is certainly a possibility as there is no way to mask off again NMI. Cheers, Longman