lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date:   Wed, 24 Nov 2021 14:45:59 -0800
From:   John Hubbard <jhubbard@...dia.com>
To:     Andrew Morton <akpm@...ux-foundation.org>,
        Matthew Wilcox <willy@...radead.org>
Cc:     cgel.zte@...il.com, linux-mm@...ck.org,
        linux-kernel@...r.kernel.org, chiminghao <chi.minghao@....com.cn>,
        Zeal Robot <zealci@....com.cn>
Subject: Re: [PATCH] mm: Use BUG_ON instead of if condition followed by BUG

On 11/24/21 14:27, Andrew Morton wrote:
> On Wed, 24 Nov 2021 13:23:42 +0000 Matthew Wilcox <willy@...radead.org> wrote:
> 
>> On Wed, Nov 24, 2021 at 03:08:49AM +0000, cgel.zte@...il.com wrote:
>>> From: chiminghao <chi.minghao@....com.cn>
>>>
>>> Fix the following coccinelle report:
>>> ./mm/memory_hotplug.c:2210:2-5:
>>> WARNING  Use BUG_ON instead of if condition followed by BUG.
>>
>> What coccinelle script is reporting this?
>>
>>> -	if (try_remove_memory(start, size))
>>> -		BUG();
>>> +	BUG_ON(try_remove_memory(start, size));
>>
>> I really, really, really do not like this.  For functions with
>> side-effects, this is bad style.  If it's a pure predicate, then
>> sure, but this is bad.
> 
> I don't like it either.  Yes, BUG() is special but it's such dangerous
> practice.  I'd vote to change coccinelle.
> 

Definitely! Or at least use a safer pattern/habit, with just a passive
variable in the BUG_ON() call, approximately like this:

diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
index 852041f6be41..48bd5ff341e7 100644
--- a/mm/memory_hotplug.c
+++ b/mm/memory_hotplug.c
@@ -2201,13 +2201,12 @@ static int __ref try_remove_memory(u64 start, u64 size)
*/
void __remove_memory(u64 start, u64 size)
{
-
+       int ret = try_remove_memory(start, size);
/*
* trigger BUG() if some memory is not offlined prior to calling this
* function
*/
-       if (try_remove_memory(start, size))
-               BUG();
+       BUG_ON(ret);
}

/*

...and by the way, while going to type that, I immediately stumbled upon
another pre-existing case of this sort of thing, in try_remove_memory(),
which does this:

static int __ref try_remove_memory(u64 start, u64 size)
{
	struct vmem_altmap mhp_altmap = {};
	struct vmem_altmap *altmap = NULL;
	unsigned long nr_vmemmap_pages;
	int rc = 0, nid = NUMA_NO_NODE;

	BUG_ON(check_hotplug_memory_range(start, size));

...


thanks,
-- 
John Hubbard
NVIDIA

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ