Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1382391rwb; Wed, 14 Dec 2022 09:28:55 -0800 (PST) X-Google-Smtp-Source: AA0mqf55YmlhNQUxGjrNkEQ9/Ho/8Hu5IN7liwP33iRYtmaVqPiAMB9hVeqPyvwPP8zLXoU8UVnB X-Received: by 2002:a17:90a:d984:b0:219:6619:848a with SMTP id d4-20020a17090ad98400b002196619848amr26103298pjv.36.1671038935660; Wed, 14 Dec 2022 09:28:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671038935; cv=none; d=google.com; s=arc-20160816; b=yijH6d58HRgK7wO8jFx/BJOD2O7FeTr328a5YBr8UO7duStmjw77Ji186LiWg0ISGH i4H/LWc4nQ+bEToTYP1q5GVeySes1LMq0g6HymVXFtQp1lO1698MS4KYOzZeJtebFxv9 oEI0hR5b6t75cdk/qNYB4Xe13IqBC3ux4PWrdscuquIAYk4jBLbpTEWJkUhTSi+4X9hd aQt14EkDEDLrubo+NLUwNQfCthiNBIhOEJwt9fBQf4MFWo365zljKzScbJmGI0B8G3Zb crU98w2gzAdWAXrjKnXBuSigFVz9LOBOt2r5cuqu3Jf3qIq6CltERAOdXBQuiEWe+Fg4 tsMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=gOpGhv6UKz7VHR3jMFGZw2EISvCY2AuNGxNyyrkNXxA=; b=SCwbwrD95sUj9r1unASVKLpQgDVQiVvTvpkJEZrmiktrH9hjprhu0L0HftBxh0RQNb G7A461D6OYbJaG4ARvGKeDkgq3TCT3cG4yrTne3SvVE8cn+rKkFOjT6K2ePKwtQrSIwZ qvxWuaUdcXQ0dpzE+W1OS99LYWJlAqX+8iuBNScPQTsirKEb6QItMfiUwi6BYBsshmh/ 4WjOtub1BI2VtrqOVYzmz51f/sFpOEgU34TD6NBRGD2bRhHMJ8AWE5qi4Ix8XYDQsixk CXFzxQFBGGo6YszHt8o9ZSvRrM+Mr7Gdz/CCeKBjceJ1AUtUNnYZO1HxGV/YjREG4N2+ iPqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b="a0+L0Q0/"; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id oo9-20020a17090b1c8900b0020a66fc1398si2786795pjb.91.2022.12.14.09.28.46; Wed, 14 Dec 2022 09:28:55 -0800 (PST) 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=@infradead.org header.s=casper.20170209 header.b="a0+L0Q0/"; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237321AbiLNREP (ORCPT + 70 others); Wed, 14 Dec 2022 12:04:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40678 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229974AbiLNREN (ORCPT ); Wed, 14 Dec 2022 12:04:13 -0500 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4A2A12AD3 for ; Wed, 14 Dec 2022 09:04:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=gOpGhv6UKz7VHR3jMFGZw2EISvCY2AuNGxNyyrkNXxA=; b=a0+L0Q0/xdslYVEnG3wV0PLkbV w2MST8acNOJgFLkYs124lEDIZDn1yO+vxjDFs5Iv4K5iSkHIgcvNW+tPvYbJB8XNGVvUjK5Areghc duAsWRMQwVo4ObmiFjxgCBrQ2DRj+k+CXmK6XDAN/b+OnLaJoTVeWmRTkAhkMMgKYS5x4fLfkDqoq dLyxrp6e4/YQeisgHYNgge4iKFNMEMwNfX7yHVJ3ogsNt//Ep6P3voubw8H5O6DyIVaWFisBzUnRD RyO8VuSn00gmggOhzseEzT7BSFJSuv44tpGI6N6pCF2T/UJgYc945Xm7epcNg+ceOmzbh5G8Tn40i 6nZqWbnQ==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1p5VB1-00DR0S-U5; Wed, 14 Dec 2022 17:04:15 +0000 Date: Wed, 14 Dec 2022 17:04:15 +0000 From: Matthew Wilcox To: Nico Pache Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, muchun.song@linux.dev, mike.kravetz@oracle.com, akpm@linux-foundation.org, gerald.schaefer@linux.ibm.com Subject: Re: [RFC V2] mm: add the zero case to page[1].compound_nr in set_compound_order Message-ID: References: <20221213234505.173468-1-npache@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221213234505.173468-1-npache@redhat.com> X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE,URI_DOTEDU 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 On Tue, Dec 13, 2022 at 04:45:05PM -0700, Nico Pache wrote: > Since commit 1378a5ee451a ("mm: store compound_nr as well as > compound_order") the page[1].compound_nr must be explicitly set to 0 if > calling set_compound_order(page, 0). > > This can lead to bugs if the caller of set_compound_order(page, 0) forgets > to explicitly set compound_nr=0. An example of this is commit ba9c1201beaa > ("mm/hugetlb: clear compound_nr before freeing gigantic pages") > > Collapse these calls into the set_compound_order by utilizing branchless > bitmaths [1]. > > [1] https://graphics.stanford.edu/~seander/bithacks.html#ConditionalSetOrClearBitsWithoutBranching > > V2: slight changes to commit log and remove extra '//' in the comments We don't usually use // comments anywhere in the kernel other than the SPDX header. > static inline void set_compound_order(struct page *page, unsigned int order) > { > + unsigned long shift = (1U << order); Shift is a funny name for this variable. order is the shift. this is 'nr'. > page[1].compound_order = order; > #ifdef CONFIG_64BIT > - page[1].compound_nr = 1U << order; > + // Branchless conditional: > + // order > 0 --> compound_nr = shift > + // order == 0 --> compound_nr = 0 > + page[1].compound_nr = shift ^ (-order ^ shift) & shift; Can the compiler see through this? Before, the compiler sees: page[1].compound_order = 0; page[1].compound_nr = 1U << 0; ... page[1].compound_nr = 0; and it can eliminate the first store. Now the compiler sees: unsigned long shift = (1U << 0); page[1].compound_order = order; page[1].compound_nr = shift ^ (0 ^ shift) & shift; Does it do the maths at compile-time, knowing that order is 0 at this callsite and deducing that it can just store a 0? I think it might, since shift is constant-1, page[1].compound_nr = 1 ^ (0 ^ 1) & 1; -> page[1].compound_nr = 1 ^ 1 & 1; -> page[1].compound_nr = 0 & 1; -> page[1].compound_nr = 0; But you should run it through the compiler and check the assembly output for __destroy_compound_gigantic_page().