From patchwork Tue Dec 13 23:39:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nico Pache X-Patchwork-Id: 13072581 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 2E26AC4332F for ; Tue, 13 Dec 2022 23:39:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 29EAC8E0003; Tue, 13 Dec 2022 18:39:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 24F278E0002; Tue, 13 Dec 2022 18:39:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 13F0A8E0003; Tue, 13 Dec 2022 18:39:47 -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 F12078E0002 for ; Tue, 13 Dec 2022 18:39:46 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id CAD9D4045A for ; Tue, 13 Dec 2022 23:39:46 +0000 (UTC) X-FDA: 80238902772.25.B4A8236 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf27.hostedemail.com (Postfix) with ESMTP id 18E884000E for ; Tue, 13 Dec 2022 23:39:43 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=GhanTi5X; spf=pass (imf27.hostedemail.com: domain of npache@redhat.com designates 170.10.133.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=1670974784; 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=q50OQjlYexkQlDEQoAk6I4jcWlBiIdpfALgNpYBxl74=; b=DDuaaj4C//55q1+WsZXLPPXEdG66swc6gJTNv2FjRmMNiVoViY5jjEmr38/D/Oq89srMU1 qtQnfcEhzutnd1LchFfvnwVgV8Hw1vbLO8NlfiFT5i9iIoMBH+jSeE/lMrIojQN8AXetrQ 99ZVs2sY7Y/YJeA3l70zwvfvK2GW5Es= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=GhanTi5X; spf=pass (imf27.hostedemail.com: domain of npache@redhat.com designates 170.10.133.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=1670974784; a=rsa-sha256; cv=none; b=F3/YKlBBP4XtqxOfzlWj5Mfy8AIoPbixBX2KzU404CLMEeKFwiVbseTZvIQsnpfN+A80vY EM8nBdoUcJZnflHEbqrCKQ/fvwnWL2M/3i1/uuNdohksibNhhI0XRFKS36BqzwYLPDAfBO bie2Q5TMswdEFb3DvEhxl9Lu2VoJwtY= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670974783; 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=q50OQjlYexkQlDEQoAk6I4jcWlBiIdpfALgNpYBxl74=; b=GhanTi5Xb38VgcL8wIPAP6WuhKwNwOtd5uxMzSRUu11pF+04xV9OGNauxCGl5cDJBtrUog Y85+j+iEJqNK0FPI5rizLx5Q1CJ69BBH4heHW/T0K5A+CEqWbEPTWwKqnbi5rCSE5LnlQI g3BzRhPaELhmxcT8dQWY09qJvy21Gcc= 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-580-mYMdRKRaNxWghhQnAOvOiA-1; Tue, 13 Dec 2022 18:39:41 -0500 X-MC-Unique: mYMdRKRaNxWghhQnAOvOiA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 66593101A54E; Tue, 13 Dec 2022 23:39:41 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.22.32.182]) by smtp.corp.redhat.com (Postfix) with ESMTP id C48E12026D68; Tue, 13 Dec 2022 23:39:40 +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] mm: add the zero case to page[1].compound_nr in set_compound_order Date: Tue, 13 Dec 2022 16:39:24 -0700 Message-Id: <20221213233924.170878-1-npache@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 18E884000E X-Rspam-User: X-Stat-Signature: bhbgz4yjg1jxhtyrimoduhkqjfg68q4x X-HE-Tag: 1670974783-495235 X-HE-Meta: U2FsdGVkX1/QCXIyFKJeqOIQs68lnBKlu05clNloaBC9QddDfyWEMtgiIbg6Qhs2W2ypPWVVeVUOjWwzI6mNd/J6QDjv5d9SeWdFY3lM4d3cXyvDZwGb+atUdl/b+uoeH1NYciznlhx5In7mQCnjb+FzjlH49K3jWEKm8y28wpYBMipBdYylfSLKY7+24OIPg3nlghFKm7E8y0tCt23UjSRyoszCnNKfI2ioeslPpZZ+hTQSeH3dEiG9Y4kheJNcucOeoG8AsN49PVDjw4k3cr8zIRoXGNQ2PQKFdi4XjblVTMovCB+itvGMJqy8NG3mYVDPYHfSpYbetwO2E8y3lAJzeYKlzPvH/h2PHH5wDCwc0ypwwV/WN4AClat1c+8zQjGcQtLUVaXNl9HmS6UnLYB/ZUnSELDBlbXt2lOaf6aTtRN/07NbXW4w5nm7WIJywPqkYnwWkG71jA702BWCMbiuVsrmlI569LC+LOXpEuII+MHTfsB2bJnj3nOMUz5ZYPGtq+YTczaNG9TP5IdCBI9r+TrWADMZvlitA2BMQvOxTd0EFA7CT6wq5SsglQc7Mzqwd9YZF+5MaoBjG8TUOt59NG62gIwqRIPkxHmDBBU0/d0FS59LkXC3g7jB9a+1NSRpwayEdQzts5tSV4Dng9Nw8NxfzkQjNeB9krzfPEXX+um5CeSoKv8LVHk0XC/rl5OOBdQGvA53yQGnKbX3RR9gC62gTESX7OjvSmVaVw35+vYEhVQM6p1vhqXtSuxdf3BCWfG3Or1RQaVBejELimFPwgw6Gj17yUwO6rV/r01unvkJdh5Ibx62V8/RFhr1kkI4QqC52fSas7yPNeVW7RblvpqnuJvKPz6htIX2jyr3/Oyuc6ra1ljxY/7+CV7AZxTxbzXX8JF6OLoNGPpwUrYbwwRk4luH/WJkdi9rVqdpx0ePUmV7thUMQj9rEtc/wB/hcndbCLy9IMYSDx2 XSA== 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 Signed-off-by: Nico Pache Author: 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..75b57c71781f 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; }