From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C24EAC433E0 for ; Mon, 1 Feb 2021 23:33:57 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6C42064ECC for ; Mon, 1 Feb 2021 23:33:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6C42064ECC Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=linux-foundation.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id EBD0F6B0006; Mon, 1 Feb 2021 18:33:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E6EA36B006E; Mon, 1 Feb 2021 18:33:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D84836B0070; Mon, 1 Feb 2021 18:33:56 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0191.hostedemail.com [216.40.44.191]) by kanga.kvack.org (Postfix) with ESMTP id C29636B0006 for ; Mon, 1 Feb 2021 18:33:56 -0500 (EST) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 8BA291EE6 for ; Mon, 1 Feb 2021 23:33:56 +0000 (UTC) X-FDA: 77771304072.20.spoon74_000fda7275c6 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin20.hostedemail.com (Postfix) with ESMTP id 6EF71180C07A3 for ; Mon, 1 Feb 2021 23:33:56 +0000 (UTC) X-HE-Tag: spoon74_000fda7275c6 X-Filterd-Recvd-Size: 3075 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf23.hostedemail.com (Postfix) with ESMTP for ; Mon, 1 Feb 2021 23:33:55 +0000 (UTC) Received: by mail.kernel.org (Postfix) with ESMTPSA id E5DA564D90; Mon, 1 Feb 2021 23:33:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1612222435; bh=IPcG23HxLRgvFstqvEFHiosYl3MWryDjBRJYYpKzO2Q=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=Ca/ruHFPXeRYJjJEHJZHSmK0v9XNR8c3pSbUAJN8BwFLo7ITTnwLCzWI8YASvX7Q1 l8mJndST3G86imADhoeSfUp3Ouvo7sePbOQgo/ZAPHsjMee8BhrlV8cGkrUd3MZIIm hhfIOUjfUMhIKtO+1zgXfiV6GFd3DJ2OfoGt9oFY= Date: Mon, 1 Feb 2021 15:33:54 -0800 From: Andrew Morton To: Miaohe Lin Cc: , Subject: Re: [PATCH] mm: simplify the VM_BUG_ON condition in pmdp_huge_clear_flush() Message-Id: <20210201153354.e640247cb5ab306e909322d0@linux-foundation.org> In-Reply-To: <20210201114319.34720-1-linmiaohe@huawei.com> References: <20210201114319.34720-1-linmiaohe@huawei.com> X-Mailer: Sylpheed 3.5.1 (GTK+ 2.24.31; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Mon, 1 Feb 2021 06:43:19 -0500 Miaohe Lin wrote: > The condition (A && !C && !D) || !A is equivalent to !A || (A && !C && !D) > and can be further simplified to !A || (!C && !D). > > .. > > --- a/mm/pgtable-generic.c > +++ b/mm/pgtable-generic.c > @@ -135,8 +135,8 @@ pmd_t pmdp_huge_clear_flush(struct vm_area_struct *vma, unsigned long address, > { > pmd_t pmd; > VM_BUG_ON(address & ~HPAGE_PMD_MASK); > - VM_BUG_ON((pmd_present(*pmdp) && !pmd_trans_huge(*pmdp) && > - !pmd_devmap(*pmdp)) || !pmd_present(*pmdp)); > + VM_BUG_ON(!pmd_present(*pmdp) || (!pmd_trans_huge(*pmdp) && > + !pmd_devmap(*pmdp))); > pmd = pmdp_huge_get_and_clear(vma->vm_mm, address, pmdp); > flush_pmd_tlb_range(vma, address, address + HPAGE_PMD_SIZE); > return pmd; True, and the resulting code is still readable enough. But a problem with such a complex expression is that the developer will have trouble figuring out why the BUG actually triggered. If we had a VM_BUG_ON_PMD() then we could print the pmd's value and permit diagnosis from that. But we don't have such a thing. So I suggest that it would be better to have VM_BUG_ON((pmd_present(*pmdp) && !pmd_trans_huge(*pmdp) && !pmd_devmap(*pmdp))); VM_BUG_ON(!pmd_present(*pmdp)); This way, the BUG()'s file-n-line output will tell us more about why the kernel went splat. I suppose maybe this could be optimized the same way, as VM_BUG_ON(!pmd_present(*pmdp)); /* Below assumes pmd_present() is true */ VM_BUG_ON(!pmd_trans_huge(*pmdp) && !pmd_devmap(*pmdp)); Which works because VM_BUG_ON is, depending up Kconfig, either a no-op or a noreturn-if-it-triggered. I'm not sure if I like this trick much though.