Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp1145989rwi; Wed, 19 Oct 2022 07:14:40 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6LBesg+w9LFs9x+sZCy6Bh4U/422HlKr19Dkh7UWSgUofAAB8+tTLlFcmJQQN7UapjLU/k X-Received: by 2002:a17:902:8f91:b0:184:d3ed:afd8 with SMTP id z17-20020a1709028f9100b00184d3edafd8mr8776316plo.15.1666188879658; Wed, 19 Oct 2022 07:14:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666188879; cv=none; d=google.com; s=arc-20160816; b=GQorg+g8Kr83amlGf1UhPvPZ0hpSPYDnGptxcttV/3edLxjCOA5PXz8waIEDyFdUY1 AADaJF+5RRM55UKq8Ql0JuLpYIaEqSPgud0NXtBwN+wQqplDzv/60Q+7JGLZ6YFUU7/x 00E1OoCbrXGqQktf1hQdofgSQGmDFKUd6z6vLhXMgv77g+zdgJJAjCiWqeSJxVGQTOv7 d+HEG3NCLC2RUOunK+CNebXlFgyDrL9WqTWiBv+2I/ZSzq9eX/48u+nuJikxaroImjLq KUu9Xix4GSXCZmE7E2ohrtEiYCTHVDrXKVPv438dPBoMYCkMFgGs0+46DIyiZxbzEimd expg== 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=sSToEqxSMFGzFPekxIKBGpDPnfCZY+D4QvLvCCVvu1nQvA1wh3WwBlyQLolr/TrMIA j/3aNNKkw8ahWLYfSggyVYbmWJCJdsltPULaBwufHtQKYhxlhVsu0fKvsoemMI+YEbsS 8JDRNmAyGBGgZ2ZDgJbOBlaBXt3SG9D8T+u+6Q0U0ryVxlXFa5zB7/n9bMRrxaeahpwB csw6fmfKD4JfYCTyj881SmyYW4gPf597lW79yIMq8VSVqcqH3XYPH/HGio9TeYP4u/a7 ii4u35Kz2as5cZIxCM6QR+PixAqPdCZaJoW7jT46LcqTWerumECzvbZ5P57lCd2b3n/3 TeWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Z6I+mDO5; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z10-20020a170902d54a00b001769f4909b4si18569820plf.356.2022.10.19.07.14.22; Wed, 19 Oct 2022 07:14:39 -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=Z6I+mDO5; 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 S233888AbiJSN67 (ORCPT + 99 others); Wed, 19 Oct 2022 09:58:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56920 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234248AbiJSNz3 (ORCPT ); Wed, 19 Oct 2022 09:55:29 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7CD98132DFC; Wed, 19 Oct 2022 06:38:06 -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 ams.source.kernel.org (Postfix) with ESMTPS id 649DEB82338; Wed, 19 Oct 2022 08:49:45 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CD20AC433D6; Wed, 19 Oct 2022 08:49:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666169384; bh=fOZI99R9xfcgoIm40CdSm7/zmpiNb38vJN2fwxj97xQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Z6I+mDO5PSD7DvnhvBuLwjUd+IkuOgj78a66Pu3aORz+Alga0SBBHXz4p9MZ7xSpa jxNCgLIjAeSf5VASxlyx/ia/jY3M8zQ6+itaT/IOeTTWvtx4JurKIIgDbrdkkWs7KL 3NU8VH6/URODpMPS+droH2WbfnN2gROJzRJ5Xmz0= 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 6.0 256/862] bpf: Use this_cpu_{inc|dec|inc_return} for bpf_task_storage_busy Date: Wed, 19 Oct 2022 10:25:43 +0200 Message-Id: <20221019083301.365324583@linuxfoundation.org> X-Mailer: git-send-email 2.38.0 In-Reply-To: <20221019083249.951566199@linuxfoundation.org> References: <20221019083249.951566199@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.4 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 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; } -- 2.35.1