From patchwork Tue Dec 13 23:45:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nico Pache X-Patchwork-Id: 13072584 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 461B1C4332F for ; Tue, 13 Dec 2022 23:45:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 92AAF8E0003; Tue, 13 Dec 2022 18:45:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8DAB08E0002; Tue, 13 Dec 2022 18:45:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7A2398E0003; Tue, 13 Dec 2022 18:45:17 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 680BA8E0002 for ; Tue, 13 Dec 2022 18:45:17 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 2CFD0C0363 for ; Tue, 13 Dec 2022 23:45:17 +0000 (UTC) X-FDA: 80238916674.16.FDB00C9 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf12.hostedemail.com (Postfix) with ESMTP id B65AA40009 for ; Tue, 13 Dec 2022 23:45:15 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=KVgIBpyW; spf=pass (imf12.hostedemail.com: domain of npache@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=npache@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1670975115; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=DVhb72kd9R99mwbuxV0gpxyvFBS7R/Jb7x2Bq+94b50=; b=PZBsbBQzusiP/Qp8J/pQwmfaDTEwfdeu8RvXjb1K5yqiNDxbqFoBXtQ6EX8Ia+Kn7Jooil ho+DjYIDc4DC3D6LqYdDgjGom2xhUNntW6sxZ5KQ+CDF5ziWK+4Vh9Uwa4Tum0eEqM4vts irIgGXkZl/cePxFSd4jap7K2IUSdxVo= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=KVgIBpyW; spf=pass (imf12.hostedemail.com: domain of npache@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=npache@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1670975115; a=rsa-sha256; cv=none; b=zwilypQFcP+ISFiet2s82I6uw9XcQf8eECXXKk+7Y6JjF+FflNAfxMtXPzGgCrjEcbXK+s YNfjiat90A2bYCI8/Sjkm0xN95oTuBQBvtwt2zNM6vRFswPpEa7T6sw17AmrCEsrHkg7tL fhJAjGAVIi26AWZpFW4zMfApB44mcDo= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670975114; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=DVhb72kd9R99mwbuxV0gpxyvFBS7R/Jb7x2Bq+94b50=; b=KVgIBpyW/N7dY5+jCyEoDwAQauRn0UB1LsB942zONtwrg/rzDShPOa6a9omkTPpCvzZhJi BK2ewDDXDyu5JOzz31xL65XRJaZ3SVvj6KuIkqU9EG11O7IFasdZaE+TWo3zz++2PO6yNP nlH8xas1rZnXBcJbU0J9KcuFLTsjn5w= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-487-FE8nIjigNqSvYgEBCMkZsw-1; Tue, 13 Dec 2022 18:45:13 -0500 X-MC-Unique: FE8nIjigNqSvYgEBCMkZsw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BCAA1803533; Tue, 13 Dec 2022 23:45:12 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.22.32.182]) by smtp.corp.redhat.com (Postfix) with ESMTP id 37F894085720; Tue, 13 Dec 2022 23:45:12 +0000 (UTC) From: Nico Pache To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: muchun.song@linux.dev, mike.kravetz@oracle.com, akpm@linux-foundation.org, willy@infradead.org, gerald.schaefer@linux.ibm.com Subject: [RFC V2] mm: add the zero case to page[1].compound_nr in set_compound_order Date: Tue, 13 Dec 2022 16:45:05 -0700 Message-Id: <20221213234505.173468-1-npache@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: B65AA40009 X-Stat-Signature: 7ub7u14qwwx8c3urobcru67zhc4gdujq X-HE-Tag: 1670975115-306968 X-HE-Meta: U2FsdGVkX19hgbZULP7OnHTO1wnxFClEQMIv5+jhubPT4wz+QIu2cOYAeOL7vbf9dc2lqAmGrhjCjusGGwFYuBWPANb4rJp4m6zOvXV5y3ARoqmoA0SKj6MtLCvt+YXQvyMuywaiiP7SKcYnt+wqCAZOHvGjl5cxCGTMS1eU+8LUc9y0GotrsL/aOgQK6j9IlWY1aMnNVmd+uqqLmS525x06AsD5dfSDdWyXQgOGynd5sHXXZB2S/lqBDkxoSqXWUCYo9hHcqh6ADsUZcaInQDVZ1okp0R1cchtfuoENapk6O7ORNLRHdYuYXObK2c0ENGC/OJ6VPo93f17xEB2lwhlysw4ubUNUMFCaP3qoBTaya2M6AXwFDZFdy6xOTJVNz+uhlFYVuDnEWsnDTwEWqJKnxmHrsu5B3KBF5qMwsAkO+xAh2oosW1qJ08Zim/QQxAY2asOs4SY30V+EvLaH/vzeDOzjY7qhqa4x2nXXCZHkMJG+XdCxYWwuP3HgfCpgzYx61SAmWIf8Bg/SBPZBxx58t2oX5aCYSsCXo4kCeX5OghN02fFfDQH3TAstnBbG8FFyq2uUuoXvbwCT6nuWYEyaa3bxAgtnULJfDJTN6F5eN+iwpt1gA6M4ioo3rfz03MAazCghmZaaLtoUNX0+YXMID5niDUtgwYiHQaE/BNMdurhHPQBE4XHyXanOQ0sWTeC2q7zxc2JshU2N4p1/KVr9HObPyny48ESA246pmsDzJa8vpa3rWbGWoIrRiZ27/W0mT6NWp7mYS8yyPQmuXtDBZ8JcAM44eGOoGVt9/E4RyQ+PBEGEPytwXPSnafxtZTnWuEMmA66Yhnzm1XYCbKKTVcpoYv9aVg2FKxKaTGz26ODsCLpheV80q0krDddxKoE2uAr8Q2F1zdOEAl11pBYZUXavhLgtDmCpJSkAQazR/0GfUwfW0VxFeqY5UM3vuj+1JkuUP9LjEGJQVka Kog== 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: 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 Signed-off-by: Nico Pache --- include/linux/mm.h | 6 +++++- mm/hugetlb.c | 6 ------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 6a05a3bc0a28..9510f6294706 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -938,9 +938,13 @@ static inline int head_compound_pincount(struct page *head) static inline void set_compound_order(struct page *page, unsigned int order) { + unsigned long shift = (1U << order); 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; #endif } diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 3d9f4abec17c..706dec43a6a2 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -1344,9 +1344,6 @@ static void __destroy_compound_gigantic_page(struct page *page, } set_compound_order(page, 0); -#ifdef CONFIG_64BIT - page[1].compound_nr = 0; -#endif __ClearPageHead(page); } @@ -1865,9 +1862,6 @@ static bool __prep_compound_gigantic_page(struct page *page, unsigned int order, __ClearPageReserved(p); } set_compound_order(page, 0); -#ifdef CONFIG_64BIT - page[1].compound_nr = 0; -#endif __ClearPageHead(page); return false; }