Received: by 2002:a89:2c3:0:b0:1ed:23cc:44d1 with SMTP id d3csp1156573lqs; Wed, 6 Mar 2024 07:52:43 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVZfz2jvdxiBoZXhiG7wf9EUOYy65f5fZTCCwlwAvhCDKYjvMZXXSUIUxhGQUO0weJc8XlOCJ0548fVwqlJImewUK+k0eO8Es3pa/jcPg== X-Google-Smtp-Source: AGHT+IELfHQEMg8vU+j9rUbwVBkj2nT2h5AobkR9FKxOshMPGixncQvapwgyX+Y2sr/Xlbxw6KfM X-Received: by 2002:a05:620a:2990:b0:788:43d1:d33a with SMTP id r16-20020a05620a299000b0078843d1d33amr610429qkp.29.1709740363482; Wed, 06 Mar 2024 07:52:43 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709740363; cv=pass; d=google.com; s=arc-20160816; b=D/uq9KfzPgcwxSE8tbnIgl65STT1/wbIEA46f35u0VE2M0DmjVG/m3+LuJx9yyrl9U QnCFf/TrxVa26wbGjLE6lYFodwOVQ9/lFWHknP7jFWlcuIDg8Rvf2xUMFF9vYUx9uSRJ x3Jg81VO0+3JTnWtLJjsOKrxdlOYfN9bpV9upctL4ckjdb4rqskO+2duLNvmqCGhN0EY X4/3lCVSpBoqDYIp9LqohzGszUMk39Immuk5o3h/Mo8DfBvibhBxDK6dcvj7SO3YfZxw O7RHSlTbY8QC5aPX3ZmZve2XeD9bq3gU6+pXcautD9MKh7hZ9jwMTydjYPglwGNw7/Uh 8ptA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :subject:cc:to:from:date; bh=wacGfOo2010VaHWQ15vn6fY1u01apQ5am33kmd/xfOI=; fh=XDiHU2tTI50wx3CadW2Nly28ON6Xzr52Af0DK/cpgTc=; b=sZuHr2uPmbHd2WxVcZZBxqGyMw5x26vrYnm1IUpHcVF4BkMrmgNSZgy59ebqzIJvVk fP0fH1yjNtPjurK9cIJWWfIWBDu7XOGzkQ232sey4rBUud70oKzx3VPShFJmN5kSDT2q 8NUtzZx0G0MYnAkfx/sH2NFQc65dUgqHvOx/QGwK1nrYZhaPRMa8sqmvZ/YT49GDLBYd MxOTgAVYjFEz7A9yQX/hpoWW4CrCb9vzPqWhH4gLj9FPY5ZFZggq+pPefc+drHBrRFdV iWT8KWmNADyglrzeq7YaXLibs2wbOHUfXq5dM4Phso3KwvGlmYP467QFezGRsIawm/a8 XI9g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-94197-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-94197-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id w8-20020a05620a148800b007882eef5651si5607129qkj.63.2024.03.06.07.52.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 07:52:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-94197-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel+bounces-94197-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-94197-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 39DEE1C20CAE for ; Wed, 6 Mar 2024 15:52:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7EFC913541A; Wed, 6 Mar 2024 15:52:30 +0000 (UTC) Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 09E97135A72 for ; Wed, 6 Mar 2024 15:52:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709740350; cv=none; b=eeoI7xwEgnSKcO1zPHuIs3OnKwWC2O47xT+e3eaoXEN0MfiCoHosOTwAg/buCL1Od6AMrj6GVr19yf3EQwLRjRUG44YJf/MEOn9VhduQJXkaHLjmpEdIH4733U4GNCiG73XQBeStjGvTW5YxDZKA+yKQ01z+WQQP83g0W0HyWgQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709740350; c=relaxed/simple; bh=CWhU91WDJ+IUTp7tX3uVK1SsCEBfhsgXGPayXUKkAtw=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=NmEGREe3Qcx3B07uYQwGywsiPaO2oRfzm5oeLEVRZZ93kXEmh6Qbd8aEad8JkgM2EAB2IbYHmLAJBrE5ybeF3yYWCLzSMbVI0JvRtG160SKHqGpYcOusWjwV5LVfR8CFnLzGFirijsViq6R+/YIibnxwdnMzkD9bO27kYd/ODnA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7B865C433C7; Wed, 6 Mar 2024 15:52:28 +0000 (UTC) Date: Wed, 6 Mar 2024 10:54:20 -0500 From: Steven Rostedt To: Sam Sun Cc: linux-kernel@vger.kernel.org, syzkaller@googlegroups.com, xrivendell7@gmail.com, ardb@kernel.org, jbaron@akamai.com, jpoimboe@kernel.org, peterz@infradead.org, linux-mm@kvack.org, akpm@linux-foundation.org Subject: Re: [Bug] WARNING in static_key_disable_cpuslocked Message-ID: <20240306105420.6a6bea2c@gandalf.local.home> In-Reply-To: References: X-Mailer: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Tue, 5 Mar 2024 15:54:24 +0800 Sam Sun wrote: > We analyzed the cause of this bug. It seems that in function > static_key_disable_cpuslocked(), there is a small racing window > between two atomic_read(&key->enabled) in line 228 & 229, triggering > the WARN_ON_ONCE macro. This might cause function returned without > actually disabling the static_key "key". I am not sure if there is any > other potential threat here. > > I searched on web and found that there was a similar bug reported by > syzbot several years ago > (https://groups.google.com/g/syzkaller-bugs/c/_W3lgRCwlb4/m/TqzyQcPpAQAJ). > At that time the fix was in the net instead of kernel/jump_label.c. So > I checked the call stack and cc this email to memory management > maintainers. Hope there is no confusion. > > If you have any questions, please contact us. > Reported by: Yue Sun > Reported by: xingwei lee Thanks for the report. I wonder if it simply needs to add the tests in the locking? Like the patch below. Because I could see: CPU 0 CPU 1 ----- ----- key->enabled = 0 static_key_enable_cpus_locked() jump_label_lock(); static_key_disable_cpus_locked() if (key->enabled != 1) { key->enabled = 1; WARN_ON(key->enabled != 0) Now I guess the question is, why is something trying to disable something that is not enabled? Is the above scenario OK? Or should the users of static_key also prevent this? -- Steve diff --git a/kernel/jump_label.c b/kernel/jump_label.c index d9c822bbffb8..f154caf2a62b 100644 --- a/kernel/jump_label.c +++ b/kernel/jump_label.c @@ -194,12 +194,12 @@ void static_key_enable_cpuslocked(struct static_key *key) STATIC_KEY_CHECK_USE(key); lockdep_assert_cpus_held(); + jump_label_lock(); if (atomic_read(&key->enabled) > 0) { WARN_ON_ONCE(atomic_read(&key->enabled) != 1); - return; + goto unlock; } - jump_label_lock(); if (atomic_read(&key->enabled) == 0) { atomic_set(&key->enabled, -1); jump_label_update(key); @@ -208,6 +208,7 @@ void static_key_enable_cpuslocked(struct static_key *key) */ atomic_set_release(&key->enabled, 1); } +unlock: jump_label_unlock(); } EXPORT_SYMBOL_GPL(static_key_enable_cpuslocked); @@ -225,14 +226,15 @@ void static_key_disable_cpuslocked(struct static_key *key) STATIC_KEY_CHECK_USE(key); lockdep_assert_cpus_held(); + jump_label_lock(); if (atomic_read(&key->enabled) != 1) { WARN_ON_ONCE(atomic_read(&key->enabled) != 0); - return; + goto unlock; } - jump_label_lock(); if (atomic_cmpxchg(&key->enabled, 1, 0)) jump_label_update(key); +unlock: jump_label_unlock(); } EXPORT_SYMBOL_GPL(static_key_disable_cpuslocked);