From patchwork Sat Oct 22 07:21:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 7289 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4242:0:0:0:0:0 with SMTP id s2csp1092376wrr; Sat, 22 Oct 2022 00:53:57 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5Y0iVkJW9nxNvlf66DjClYjkcqCNjBZSK94QOKulIuSufKJ1quPrT4QUvNMJSh15uGJ23B X-Received: by 2002:a17:90b:254e:b0:20b:7e26:f0a0 with SMTP id nw14-20020a17090b254e00b0020b7e26f0a0mr60178362pjb.203.1666425226349; Sat, 22 Oct 2022 00:53:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666425226; cv=none; d=google.com; s=arc-20160816; b=v+8rVIqV/N3KRp9ZqPZ3PAXp/twTEjjSWfOI8zbfiWancpGUeD0lpPW29pvD40JSol S6HnYXiKJEP4fj/9v/Wy5lDUUYt53PDyPpYSzRL5EhlVdd2U7AJ8vOWgKwmIiBs/I754 pR7Xvqyu/mx6xMqyKRXTItJDJ7hlCgmRG3MRqDc+ZtSInPxWWmpo74qCSMKZ7GR+/bvE HaTy4LlQz4eyNXXaWom4ckhDukKQusbRgxTlnCTyU37mBON+DeiSKh9K9hfokFn2ny8c q3j0NjamqDGM2rrxScDslddQV7XWURR70eXbf6EufEcuQX9CQclHJ0AfnsdAmx93Idl8 1I/g== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=P8mjak5nTDJH0TaxJk3QWYe/p1OZ6iGjt0fYIhsGcuw=; b=fCj6noOtxgIQhBgXyvWXSVFyZbR69FYHx73clT9pbYuX0Rx3nhZHX1TZZTPMkVcXrb a6Ftegc+e0KKW5HqfFXe/NdvmTF8j7RbD5SWyzlNN9STdJdE00sYoBT6DCxjDacf9ppX 3UndtOQDDAjjcSVx9ZZ0YmSMDtuvgwbAkZ705kxio8ji7Va2zi0bY14aI8YGMJ3HoKe5 OG1v3EO12IK3uAltWqkId16TZTznYTX0DLTSpscMv8EyL82M4qDJtgBL3PO+me1D9YzS y2Sc1Abmmk1FCeEjR6wdRcAMHIHqblbE9JufKsGdB67mJYbszjBoPsm14QqjpJTu3IiJ qIuQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=XwfLxX3G; 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=linuxfoundation.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n8-20020a1709026a8800b0016be68156aesi24942243plk.173.2022.10.22.00.53.34; Sat, 22 Oct 2022 00:53:46 -0700 (PDT) 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=@linuxfoundation.org header.s=korg header.b=XwfLxX3G; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231485AbiJVHxV (ORCPT + 99 others); Sat, 22 Oct 2022 03:53:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54874 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231751AbiJVHu5 (ORCPT ); Sat, 22 Oct 2022 03:50:57 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 985038A6DE; Sat, 22 Oct 2022 00:46:31 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id BF71560B3D; Sat, 22 Oct 2022 07:44:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AE747C433D6; Sat, 22 Oct 2022 07:44:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666424641; bh=fOZI99R9xfcgoIm40CdSm7/zmpiNb38vJN2fwxj97xQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XwfLxX3GhSxhqhdMdHZPtGG4vawlKb4qhdSupzz2OtY/nsAFRV3O3SIkTnbyr5NO4 ZTy6r3DUiDN3dAW2GIWV6qcnZZouq6PGznF/MA6kx6myEQy2u69yh28P2QH61Q0EL1 4h7I+HpkxvamDm9nmX9WtFyN65z2PO2PVmJjKvA8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Hou Tao , Alexei Starovoitov , Martin KaFai Lau , Sasha Levin Subject: [PATCH 5.19 221/717] bpf: Use this_cpu_{inc|dec|inc_return} for bpf_task_storage_busy Date: Sat, 22 Oct 2022 09:21:40 +0200 Message-Id: <20221022072454.252150939@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221022072415.034382448@linuxfoundation.org> References: <20221022072415.034382448@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747373498377779421?= X-GMAIL-MSGID: =?utf-8?q?1747373498377779421?= From: Hou Tao [ Upstream commit 197827a05e13808c60f52632e9887eede63f1c16 ] Now migrate_disable() does not disable preemption and under some architectures (e.g. arm64) __this_cpu_{inc|dec|inc_return} are neither preemption-safe nor IRQ-safe, so for fully preemptible kernel concurrent lookups or updates on the same task local storage and on the same CPU may make bpf_task_storage_busy be imbalanced, and bpf_task_storage_trylock() on the specific cpu will always fail. Fixing it by using this_cpu_{inc|dec|inc_return} when manipulating bpf_task_storage_busy. Fixes: bc235cdb423a ("bpf: Prevent deadlock from recursive bpf_task_storage_[get|delete]") Signed-off-by: Hou Tao Acked-by: Alexei Starovoitov Link: https://lore.kernel.org/r/20220901061938.3789460-2-houtao@huaweicloud.com Signed-off-by: Martin KaFai Lau Signed-off-by: Sasha Levin --- kernel/bpf/bpf_local_storage.c | 4 ++-- kernel/bpf/bpf_task_storage.c | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/kernel/bpf/bpf_local_storage.c b/kernel/bpf/bpf_local_storage.c index 8ce40fd869f6..d13ffb00e981 100644 --- a/kernel/bpf/bpf_local_storage.c +++ b/kernel/bpf/bpf_local_storage.c @@ -555,11 +555,11 @@ void bpf_local_storage_map_free(struct bpf_local_storage_map *smap, struct bpf_local_storage_elem, map_node))) { if (busy_counter) { migrate_disable(); - __this_cpu_inc(*busy_counter); + this_cpu_inc(*busy_counter); } bpf_selem_unlink(selem, false); if (busy_counter) { - __this_cpu_dec(*busy_counter); + this_cpu_dec(*busy_counter); migrate_enable(); } cond_resched_rcu(); diff --git a/kernel/bpf/bpf_task_storage.c b/kernel/bpf/bpf_task_storage.c index e9014dc62682..6f290623347e 100644 --- a/kernel/bpf/bpf_task_storage.c +++ b/kernel/bpf/bpf_task_storage.c @@ -26,20 +26,20 @@ static DEFINE_PER_CPU(int, bpf_task_storage_busy); static void bpf_task_storage_lock(void) { migrate_disable(); - __this_cpu_inc(bpf_task_storage_busy); + this_cpu_inc(bpf_task_storage_busy); } static void bpf_task_storage_unlock(void) { - __this_cpu_dec(bpf_task_storage_busy); + this_cpu_dec(bpf_task_storage_busy); migrate_enable(); } static bool bpf_task_storage_trylock(void) { migrate_disable(); - if (unlikely(__this_cpu_inc_return(bpf_task_storage_busy) != 1)) { - __this_cpu_dec(bpf_task_storage_busy); + if (unlikely(this_cpu_inc_return(bpf_task_storage_busy) != 1)) { + this_cpu_dec(bpf_task_storage_busy); migrate_enable(); return false; }