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 111E1C433E0 for ; Mon, 1 Feb 2021 23:34:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C7E8064EC6 for ; Mon, 1 Feb 2021 23:34:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230518AbhBAXej (ORCPT ); Mon, 1 Feb 2021 18:34:39 -0500 Received: from mail.kernel.org ([198.145.29.99]:39690 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229527AbhBAXef (ORCPT ); Mon, 1 Feb 2021 18:34:35 -0500 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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.