From patchwork Sat Jul 10 00:24:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Kravetz X-Patchwork-Id: 12368187 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=-16.8 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT 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 7D114C07E9B for ; Sat, 10 Jul 2021 00:25:17 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E8A1F613BE for ; Sat, 10 Jul 2021 00:25:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E8A1F613BE Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=oracle.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 96F936B0078; Fri, 9 Jul 2021 20:25:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7CC386B007B; Fri, 9 Jul 2021 20:25:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 55BBF8D0002; Fri, 9 Jul 2021 20:25:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0092.hostedemail.com [216.40.44.92]) by kanga.kvack.org (Postfix) with ESMTP id 18FD06B0073 for ; Fri, 9 Jul 2021 20:25:15 -0400 (EDT) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 4F4A929DE4 for ; Sat, 10 Jul 2021 00:25:14 +0000 (UTC) X-FDA: 78344783748.22.AE522D8 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by imf27.hostedemail.com (Postfix) with ESMTP id B6C62700009B for ; Sat, 10 Jul 2021 00:25:13 +0000 (UTC) Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 16A06d8L022576; Sat, 10 Jul 2021 00:24:58 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2020-01-29; bh=x4j+E1KMXH3Y+DvR6HKd8eoVUuq5iSCV+mgTRf/qcyA=; b=CZhPJUnsCGrQd34CoH7SYfOr8HD8rmB2UuGTbg6sbB+bJx9iNfUf0Vy19zFfT/wd7M+P /23KEXSpMkWLkqU96XDqN8vcPYPi58qwQrfRhVfwzIEcjFyoIBbC7Px5Z6z3yleu4ZZR mY4VCPmpv/650uaa1/Pg0Av1qfO9ZghBYhVj7Qwf889LpXBkbkjqRn6T5bzNnq7Ehgof 5E6pul4I46sxumiQmHQkPkDRYDLG96J4gz4L1eCtOUEIR8vE8JHpahlsO7KDkcNYlok+ xkEK1OYNPW69IFP1GuWtmF0h5i/ABwIsZa4GNcQubZkGfndltBCMNlvV+l3aLeLbi9HU vQ== Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by mx0b-00069f02.pphosted.com with ESMTP id 39pte5gjt4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 10 Jul 2021 00:24:58 +0000 Received: from pps.filterd (aserp3030.oracle.com [127.0.0.1]) by aserp3030.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 16A04e7u177862; Sat, 10 Jul 2021 00:24:57 GMT Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2176.outbound.protection.outlook.com [104.47.58.176]) by aserp3030.oracle.com with ESMTP id 39nbgawumw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 10 Jul 2021 00:24:57 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Vzpu7d+uTn/rmUbCE9Qie94MD56vtOJGLxfstX8fFumm8kJJS+04A74O6pKAmcS8pBaVbqQC7i0wZtMLkNwzaUOnZdVBBlfKsbPfgaVkcG0F1ecjhJptB22Q2WfKqnQM18YhVWXwOZFEsqgWhAohQrgUErgt/XN/5XJjKLD1qix+smA/clJrZDn3wh8nxMZ0a1YqRTTrxupLyPg/LbWOlGGWP+rIGzHp4pDxKKsMjqGUZL4QfSj77pVUlbwAmHmOXtM3CwKGvDfwFry8xHRDOq21JbyzD6s2Cadanc+6cbgSWcdjTikX2j4fwuZLFF5H3ugUrvpPCGN5nSqnwBZYyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=x4j+E1KMXH3Y+DvR6HKd8eoVUuq5iSCV+mgTRf/qcyA=; b=UZPl37C++HARTC+bc+gn1fCjEJ6gLDd3pLlJ6AHQVSi+c4IbNaPJyQMwZYFZRyp7Ne4LLjvLtKB9U5/1FkX/bidzl7CgMYVeDQQYYhBftGEoAAKUbh7aT8FSagUVzcsHyCkAbfo51OfdB5JPWlZj2XWkHG4WqgX2/3z9I/Z97Fkyty5anfSnmdFW1t0qWrWIxRbyOhYckSnK5kNRhGZpCtGn+WzhaxMshOmS9BJA0cIAS8xdEm0BaIqehWtS/emlxOYmWiGCQInNdpsBuMMoSftuRImJIuq3BR965IUWXhKvYFv82k8NOTHo5MRRIeuEe8/iUmojax6zN3TxOStVyQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=x4j+E1KMXH3Y+DvR6HKd8eoVUuq5iSCV+mgTRf/qcyA=; b=KBfQRJ2iq/IOx31NO3Fgxvxq+ODUu5UzN/qxks8YkOdT5F/6NqQCUHKKDJkNGicBZfTbLJCxJVsStPxZ+ZC7QJmqV0zPor0t5iKEDMM+Amk0DpD5kR2/gxP4oasS+tLurD0Q2HtQcFxl72LkikBUOLHGoJmpw/njFhzYWedmksQ= Received: from BY5PR10MB4196.namprd10.prod.outlook.com (2603:10b6:a03:20d::23) by SJ0PR10MB5439.namprd10.prod.outlook.com (2603:10b6:a03:303::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.20; Sat, 10 Jul 2021 00:24:55 +0000 Received: from BY5PR10MB4196.namprd10.prod.outlook.com ([fe80::8d2a:558d:ab4a:9c2a]) by BY5PR10MB4196.namprd10.prod.outlook.com ([fe80::8d2a:558d:ab4a:9c2a%5]) with mapi id 15.20.4308.023; Sat, 10 Jul 2021 00:24:55 +0000 From: Mike Kravetz To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Muchun Song , Michal Hocko , Oscar Salvador , David Hildenbrand , Matthew Wilcox , Naoya Horiguchi , Mina Almasry , Andrew Morton , Mike Kravetz Subject: [PATCH 1/3] hugetlb: simplify prep_compound_gigantic_page ref count racing code Date: Fri, 9 Jul 2021 17:24:39 -0700 Message-Id: <20210710002441.167759-2-mike.kravetz@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210710002441.167759-1-mike.kravetz@oracle.com> References: <20210710002441.167759-1-mike.kravetz@oracle.com> X-ClientProxiedBy: MWHPR14CA0041.namprd14.prod.outlook.com (2603:10b6:300:12b::27) To BY5PR10MB4196.namprd10.prod.outlook.com (2603:10b6:a03:20d::23) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from monkey.oracle.com (50.38.35.18) by MWHPR14CA0041.namprd14.prod.outlook.com (2603:10b6:300:12b::27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Sat, 10 Jul 2021 00:24:54 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 9d6d61c3-57c7-4532-fb49-08d943392476 X-MS-TrafficTypeDiagnostic: SJ0PR10MB5439: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8882; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: TgBuUmxsdVOwouriDYvKXMX3R41V35CZZ3vbKLJLj1BFpK2tkO2cyqnRaKENzfwFRAO/ct9eqQzosBbU8y/OFeequ7HeJ0tKHPvDR5GIyV8zcweMbnbifUx1luXyWZQSICJQlTSoguVPbOC4gOVOKs++CaxSSWUwErg9OzlYoRXmW6qSdL7C5Fw4msJzzT8y/hZ1HuFt0LChjF98zYct/jbHcNIdf86pVDVCKc+Jaq37Xh7meLfclZ9rBEPsbGGaWPIGKjBpslA6+ZJs1WbAW+aSDGBzn3SdbmqwEWzshP6/9e9CFw5RWMogStme8J46K8CuxmfDq3FKfWBybKBwqdnvrPNw8c5pvBBxTXrOmWncZOErYNWlrfeK53IBKqovXhPBWO674ZTfdb7TFR+5zJV3Qgk0qmCgA+kT+WeeCa5nnABVD/A8Y+YDuRkmo63U/Xolb/Llkww144asIgDroQVdrrVvb2k/WPLjLJKmVinDfjlMeGzYXiO2lj/OSn3Mg2VkkghkVgj1WVxSuLgb1BQ+v1U6u9rSTUVRIOQeDT3TPpo7dWG9WTDwwkkHhFm/gtIJ8JyKDBSQA3NI047u+C07YftEQCWc7Gw1KBPi4znS8cxAQNr/XffKIylAJEalmAUjaoo97YPY2OvFRetUfQnEnc9Z0UTOGLWqQbVhXDfPEbdtrfwtkzFs8MJp+Jz2PY4xTVdzsNyU0Wys6fdLdQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BY5PR10MB4196.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(136003)(39860400002)(376002)(396003)(366004)(346002)(2906002)(1076003)(52116002)(38100700002)(66946007)(38350700002)(186003)(86362001)(7696005)(83380400001)(36756003)(107886003)(8936002)(26005)(66556008)(66476007)(6666004)(54906003)(478600001)(8676002)(5660300002)(7416002)(6486002)(316002)(44832011)(956004)(2616005)(4326008);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: rWCyU1gGk9jszRCj0q5C3pitJK7+aXUFW4CVbaxkPtGKabDIoB+JaxTxKtIhGNdiIiTfd5nh7vov0I9QJIj+Iab8VyQEG0Vr5BaXfzxoE8NblY/8Yi9m+c5cfOyZnQjUX+cZkMAClz7u7XWjjP5B4Rp0F38PKBwJoB9jerjLf8GgW2AHLa8Dm3dLNQoP9m0L7iM3msw9pMkk7LmlAhlAFv/sxK4O9ZrlYQQ4Rzvc0xVvDXz7nKWbCMi5hCO5eFzilepWKI7GAwA/Pir49VHRkt4BlPycg8FU4a5G6QL2VnOhjv/3voww5jPrex3zodJAyT4IgmtcYiXHOVRmr/yFE+XeUbACfxXXYjxR0dp+XjriAFJzuFfHRNMW1BygXQt7eLG4l97EZvMi7r7IFS67HKtlL49oJQl/FoXBD/A1CzzC269sAvBDnTd1YHBCVgWfoUfkhR0dRA/CHowlVmE9ZFqSF1S7j0xcrZvr+LZp+tWmJPR9Fh1OEBvn2CZrbYRfl7wZDA2h/c4W4cBKHDQIUli6vUcHPM+M5d3VQYKeYBcwjVgfCvj1RX0lBNlr29q/jTUejoAEH5U941kTw370xAu6niSOHnjzec+NDNvjioU5+FreUqb+BfWD7Y/xgIcYiepGAtfoL3tOuN0Ij5arQ/WyPZm1tsWK1KyGihLpPdK6vnLdHzbClyEYgwOXso+7Me++fAQXcMYBhCe7RZMC1RF9mo77CVL/eg8T/E7KnprvNs864pDbCktd8ziYMuB2M188UP630Jrxw2mJ0lMxIwtmLqWTgynkMypJ1SQWXVYb/+fkSz7UZKKYFw6HcRPL4oZKM8vEolyLO/J21ZqTpc6KjCrL3f1feOP7jtHqojk7z+GY4iRP2aKtjHqSHsU5A+ldIlVTvo8epPT3CbHS4mZHeefMW7TWmW3RDxfq0D65G94TuRsSrQZ4nFXSrX/xXjGgqt8oTxpUptEDPn1vZLIt9t9sEgbd3JSoYZF7GkBvglPk8bdFmflvB+ZXb3dVAKXsuXArRkP22CyJ64mx6oJw0XqBM0xVO8sYcSNrmFcLbzocv587m7PD7xE4DJ/LoCdwQeoI0x3ZUyEDPcZ5or6eG1WyiELvnYjCBcA2nmw6MRIRdCpvMmTBTG/1VJ48mkoE1YJD7gyEq+gUfqHK96hSDVoiCD76wFeKi+HxfXepIvaH+paoOFCgiGf5wtxDDlEu+pw/R7RWhFxEO7GESok0Cf+OIPhBWdzTR5UBr4K5D64XzpBAgxyLEkR3mSPV2iE3Sl+5bBb+ytJKNMH4JR2y/De5oky74D5/8FXD/3bsM3SoTvBtMp+CWq2/mUXL X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9d6d61c3-57c7-4532-fb49-08d943392476 X-MS-Exchange-CrossTenant-AuthSource: BY5PR10MB4196.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Jul 2021 00:24:55.5740 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: tQHwZUb/IYYiSbffB0nQ2A+z8+OXVtBguMN2C0B7qGovfaY6ftWV2FvfZyGm+sifJ05vXBXqSUlBG2TGr/5ORg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR10MB5439 X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=10040 signatures=668682 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxlogscore=999 phishscore=0 adultscore=0 suspectscore=0 spamscore=0 malwarescore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2104190000 definitions=main-2107090126 X-Proofpoint-GUID: iThV51OF7WzHEMe3UbAYaxWLlxEoALJa X-Proofpoint-ORIG-GUID: iThV51OF7WzHEMe3UbAYaxWLlxEoALJa Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2020-01-29 header.b=CZhPJUns; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=KBfQRJ2i; spf=none (imf27.hostedemail.com: domain of mike.kravetz@oracle.com has no SPF policy when checking 205.220.165.32) smtp.mailfrom=mike.kravetz@oracle.com; dmarc=pass (policy=none) header.from=oracle.com X-Rspamd-Server: rspam02 X-Stat-Signature: krp8kwbbaxnt4fwazg73i5r3umwugf5d X-Rspamd-Queue-Id: B6C62700009B X-HE-Tag: 1625876713-527561 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: Code in prep_compound_gigantic_page waits for a rcu grace period if it notices a temporarily inflated ref count on a tail page. This was due to the identified potential race with speculative page cache references which could only last for a rcu grace period. This is overly complicated as this situation is VERY unlikely to ever happen. Instead, just quickly return an error. Also, only print a warning in prep_compound_gigantic_page instead of multiple callers. Signed-off-by: Mike Kravetz --- mm/hugetlb.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 924553aa8f78..e59ebba63da7 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -1657,16 +1657,12 @@ static bool prep_compound_gigantic_page(struct page *page, unsigned int order) * cache adding could take a ref on a 'to be' tail page. * We need to respect any increased ref count, and only set * the ref count to zero if count is currently 1. If count - * is not 1, we call synchronize_rcu in the hope that a rcu - * grace period will cause ref count to drop and then retry. - * If count is still inflated on retry we return an error and - * must discard the pages. + * is not 1, we return an error and caller must discard the + * pages. */ if (!page_ref_freeze(p, 1)) { - pr_info("HugeTLB unexpected inflated ref count on freshly allocated page\n"); - synchronize_rcu(); - if (!page_ref_freeze(p, 1)) - goto out_error; + pr_warn("HugeTLB page can not be used due to unexpected inflated ref count\n"); + goto out_error; } set_page_count(p, 0); set_compound_head(p, page); @@ -1830,7 +1826,6 @@ static struct page *alloc_fresh_huge_page(struct hstate *h, retry = true; goto retry; } - pr_warn("HugeTLB page can not be used due to unexpected inflated ref count\n"); return NULL; } } @@ -2828,8 +2823,8 @@ static void __init gather_bootmem_prealloc(void) prep_new_huge_page(h, page, page_to_nid(page)); put_page(page); /* add to the hugepage allocator */ } else { + /* VERY unlikely inflated ref count on a tail page */ free_gigantic_page(page, huge_page_order(h)); - pr_warn("HugeTLB page can not be used due to unexpected inflated ref count\n"); } /* From patchwork Sat Jul 10 00:24:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Kravetz X-Patchwork-Id: 12368189 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=-16.8 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT 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 BB3CCC07E99 for ; Sat, 10 Jul 2021 00:25:19 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4071A613BE for ; Sat, 10 Jul 2021 00:25:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4071A613BE Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=oracle.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 9C9CD6B007B; Fri, 9 Jul 2021 20:25:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 92CA58D0002; Fri, 9 Jul 2021 20:25:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 70A528D0001; Fri, 9 Jul 2021 20:25:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0150.hostedemail.com [216.40.44.150]) by kanga.kvack.org (Postfix) with ESMTP id 3E3916B007B for ; Fri, 9 Jul 2021 20:25:17 -0400 (EDT) Received: from smtpin01.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 7CE721828EE04 for ; Sat, 10 Jul 2021 00:25:16 +0000 (UTC) X-FDA: 78344783832.01.1CE9C02 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf30.hostedemail.com (Postfix) with ESMTP id 0C1E4E001806 for ; Sat, 10 Jul 2021 00:25:15 +0000 (UTC) Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 16A07Hsg018591; Sat, 10 Jul 2021 00:25:01 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2020-01-29; bh=uxw/Q9cnbmd2qoAVkRUIf9tKuSpi17BKl8fq8VMdxnk=; b=wz4NMyf5vtS2aPJgWAav0OCyB3Nu5/OypNsG/1+3sl3hP1DGBEn+8pULJsNAk/pZl+Vx GoX3rW/JA0BIv4KQ2MMxcoloFHL2cGYTH/cDLcx8gJziNheP7dDZEulcZNvzucu/KbrG Cnzj4bYvbbf6q5sLFXeawalYSopzcoJBHBJQ8bunvPxAOG+mHqLzv2eXHbd5/LdCMwZC xXo24E3tnraU4ZUQ0USYkIBSe22cfFyWCCTIxz7nEaHvkusMoFxdQ+CBYMitqfeadDso DCZRYXHVmBUO24QnFgPBJcZuvqA1t70cKGl+Oh8yMmsJ/17diU07mIOfS+1FoyMAFksm Zw== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by mx0b-00069f02.pphosted.com with ESMTP id 39pjkaspe4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 10 Jul 2021 00:25:01 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 16A06XUB021657; Sat, 10 Jul 2021 00:25:00 GMT Received: from nam02-sn1-obe.outbound.protection.outlook.com (mail-sn1anam02lp2048.outbound.protection.outlook.com [104.47.57.48]) by aserp3020.oracle.com with ESMTP id 39jfqkceyr-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 10 Jul 2021 00:25:00 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Pe255bLE0riGpU4a/mnvkuPc0YyLgxGf8Lgn0qLFrTzk/IIwHBTC47B3vFBbFQuIN0r9kS0fyzETpFV7KTJ/vDnEbRPRG/1emG9TozHqJO/4XBBDBhm94edVUpS4JbZW6g4eTt0Jk7i4b/32TjcV/2OOfGHpQvZLL9g468GTUXUl7wqd20O8fksqI7CviVC70mYdMYirBa730/pZ65I5oVJvT2HYDo8jxwS639fTSFY4GyLa055roJRbwYLzhqU3pTkUW1dyPesI96P8bu1vNKkAcheY8LOGnogp6PifOfymz4TNQygCqIEKJ0nsZ5mp/FR3IoJqnLf1w8dQ96lDJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=uxw/Q9cnbmd2qoAVkRUIf9tKuSpi17BKl8fq8VMdxnk=; b=frSur7XfQRv+HxGcw0EU/RZzN89qF3HkGZpk6s+jhsrubdsmeEbEjgiMECuxxTaP55D34yxgFnUiYdbfz4dbluX5YXe2Rx1WIuHTV0hexFG7umL2HHNX2dtO1/V26KnS/Fi9K/MOMT1T5T0JG/lC95/vyP2C0MnGE6lAZbVylx8KD1VIIYR5wnfAZENqpksuWyvmRAH72OHZkUMdLgr7mOUUeq8AFB0ZhXLI86IQeD67l+f2wxgAr0sKzivBFb0OgzJ8dv1Owp6d8p9aHQV3bAgD9tjqOG5afp86sQIfmU4AnrOhfKcQ0e2CXvpmBXmYcoSc2/L8Q6dJ2hvbIFNQlg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=uxw/Q9cnbmd2qoAVkRUIf9tKuSpi17BKl8fq8VMdxnk=; b=Rkjlymz4M9XlF0vL5jZoyQwt8gjVQnXNjsK8s4CPos3nY+Np+Z30GqJE3CHHO5xwjl9JiwaoaT2qx8++aR1eEnVBQ3ZT5sHEqYJOLilH45WemhnkrsB97p3aEw5FxMWan7nmUXvuHlctXb5NIhHhu3wnqUkdAir1Y1D9BhpPwWw= Received: from BY5PR10MB4196.namprd10.prod.outlook.com (2603:10b6:a03:20d::23) by BY5PR10MB3810.namprd10.prod.outlook.com (2603:10b6:a03:1fb::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.21; Sat, 10 Jul 2021 00:24:57 +0000 Received: from BY5PR10MB4196.namprd10.prod.outlook.com ([fe80::8d2a:558d:ab4a:9c2a]) by BY5PR10MB4196.namprd10.prod.outlook.com ([fe80::8d2a:558d:ab4a:9c2a%5]) with mapi id 15.20.4308.023; Sat, 10 Jul 2021 00:24:57 +0000 From: Mike Kravetz To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Muchun Song , Michal Hocko , Oscar Salvador , David Hildenbrand , Matthew Wilcox , Naoya Horiguchi , Mina Almasry , Andrew Morton , Mike Kravetz Subject: [PATCH 2/3] hugetlb: drop ref count earlier after page allocation Date: Fri, 9 Jul 2021 17:24:40 -0700 Message-Id: <20210710002441.167759-3-mike.kravetz@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210710002441.167759-1-mike.kravetz@oracle.com> References: <20210710002441.167759-1-mike.kravetz@oracle.com> X-ClientProxiedBy: MWHPR14CA0041.namprd14.prod.outlook.com (2603:10b6:300:12b::27) To BY5PR10MB4196.namprd10.prod.outlook.com (2603:10b6:a03:20d::23) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from monkey.oracle.com (50.38.35.18) by MWHPR14CA0041.namprd14.prod.outlook.com (2603:10b6:300:12b::27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Sat, 10 Jul 2021 00:24:56 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 754ecdb0-e7f6-4d5e-d5d5-08d94339259f X-MS-TrafficTypeDiagnostic: BY5PR10MB3810: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:10000; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 36QThsPdDBUf3x4m493fJwiX7mqFs2jbGw99llQd4Depa+cWRd+TwocfRrIaGH0k7jKbs49fEXoSyv4vGWFywQUFv1rvE13lJMnZd0BL1wMKlibk3rMOubNqAX8MEvxUCdDKTPLuH2NsrHUebJZQG644FqzkkywR3iEMBTYQpLzKeovnzG/9XgoTadS1cgP0224pbmA8aJ30oyAIMaHoX58rtz0lVWiV+BTFH6ndc2MYffr8JDW6dtDrQD88yKuKILpUFVODxPE4EnrxByIyUNMhbFhsV2ZJSx6Vit316y0n/4ixdo/z6ptCAmIeqg6QbhGDS8S9ptSsau/1Pi8NIQi4TnaG91ozEc0xGMGge6eOU1HWrdoaIX2V4HvGuxaeN6e99pOvU/c6vRbWq0EeIPI1JRmP8krS+NFemA2f5vb4UJlufqLuz3dNGNy+N0I2VqgOeBaaFLhFTvaqt2MxSSF+Z84g5j2EyHCZUeunfYNJZufYtPZmI2+cmT8ynsimErOQlY9isgyYM2tr07xzkwErgvvcNSV7lVKr3z+gbgSAEmoKBWyxp4h7VeLluEt+ZMaPhYKDnZx+A4/lKreQ8jJ5oP1MfuN675jPNe0wFg+WUQPZPBSkdZugmuZ3847mNRz9Fe/k9hK4opSSCZA8m9Qq7u1J5llmghrLcdnCX6/dDZ6rLvxbXdsWkYkPCdtK/HGaZFxbUlwkef8gPJik0bixYOeAWNUy5Nr3QfugbzSb8s7x2JnXGC+CcWxMf7tV/wG0aQStgT+Fzh6bc/YtD+hyxrg/nqOKhtfnCotfOeE= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BY5PR10MB4196.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(136003)(376002)(396003)(39860400002)(346002)(366004)(66476007)(2616005)(8676002)(107886003)(66946007)(186003)(38350700002)(316002)(86362001)(38100700002)(8936002)(956004)(66556008)(478600001)(36756003)(26005)(44832011)(4326008)(1076003)(52116002)(83380400001)(54906003)(966005)(2906002)(6486002)(6666004)(7696005)(5660300002)(7416002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: HD9kKKfN3BlvPjowPzyiczo0cYUdR2u8V/Jf7RuFQjUwpdHJhO+lKUOUfd6IjGuZxv76Vc9XSo2z2lt9vwmygj3zId2LiTKTJmX1bwz6B3ESCWxQFKoDIZ9ELxgfbdDtju+GJS32kx8VnBjGwf7O9n7iDmV8VkOJqHUF9rqBh4PSmTjhdUrDyccwxsPO17fQFD/SLDAfXoaVrst2QsVtg+qqTb9XuLN+LX0rTQ+2BfGRuqTzASSDXk8cRZfxSfiVllMk7eQLJQYwTkbEm9gu6hQwMXILUrhwozOuURY6bKmn6RXIdAME5N16C89FmIaTUcDx4wtusIWEIefMY36DbzvvMKvtpboUG4D/ioGh0v5Yqu6K++EvQ6C9R9GRwpJSnr5j6DJ4OFrFJqM3rLSagVQeB/Z/RwO74+aIKXoXgtgPN0k+Kvc6r8R1oowBt6xjrfAH8KdQMDQ1hiWWVjZU9OKqK1ZZLcV9eZW8KGff787qDoqCXFtSR8whhoRmtgiRQPdPQ5zrfF/Wo/mAa6/VrIV/MUNFIy5AdAYVXjHsFMNEiijXrztjLQA/qgjrj1I0ZynRH+W4Rr03g82RIxSZF8pB4sdX6KAJCeIq0lWizxONejjT3rVcXigi3VThzmUpRGVmEDE1nU1zVlH9PzMGUG+xHAUqRYe7mQ16fTfN5PttTwVWDZ4jG7K/+xxN6hddjwKJAg+z5in9ecLx6O+4hhqzZpGI55KQiWr863UYVjQYhD26K8e5ZWSynSDEvRdn9ijszgo8XJGn0Ot0JxzfBHFJFTwTfwfmNf6yhNqqRNMlV8j6QNdID07ssvdgPQV3dNAjTMv1KAlhFgnBNmR9kDyELvuAd3uIierWFXdnzCkoERtM+S8Y9s2Z5evROnHWANtUQWK4O/bjdxqIeNMg9GkOjxMkln2+8M8HcHwRaqGupLM4AdXfDIL2415x4miy2I0UidKrGCDQgaMwSqRe+7lcwVeqWb5CyKo5N/h5e8k55aPjnIV+Su4+XXbFBeci7kVnEsyH57ujmiPARDdjUC7J8b0uuHbN9uAadm1oqQ38nMNwk2gYt/XCTnzEIkxcXoilHaUP44QSv88LCN+WFKgkzg9MsMXMOkDEbsj4Y7XCwJuGC94hWE1Ni60LvyXA31a0Q981wT0ylT+lVhks9ppfa+NHa9sPi9DhwmtoRtLxc6jaBbqiGEtVyIE9HmaHWtduuZfEh3nIdr3KScDkIS6D6g8sHSPYijMeDEkNAgpNkX1MoiouRXZjAqtSRH5U2wUIf1k7E/T/L3+CzIkbT1VdcvWDGWAWB+570jjfTCNNvaE0zAxXQwBekFstqXcS X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 754ecdb0-e7f6-4d5e-d5d5-08d94339259f X-MS-Exchange-CrossTenant-AuthSource: BY5PR10MB4196.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Jul 2021 00:24:57.7594 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: W2t9iwkgbqy0+jgIV6GW+GyNDmU3XoS2vGNp8eXFv6Soc5v2gPpFW7ejXirqyAtON7q/SIbyrPFAaj+zezHqBQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR10MB3810 X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=10040 signatures=668682 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 spamscore=0 phishscore=0 mlxscore=0 bulkscore=0 malwarescore=0 adultscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2104190000 definitions=main-2107090126 X-Proofpoint-GUID: s_aFSw7GY4DmLA5NfZcTqAdxPdbJ1tKP X-Proofpoint-ORIG-GUID: s_aFSw7GY4DmLA5NfZcTqAdxPdbJ1tKP Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2020-01-29 header.b=wz4NMyf5; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=Rkjlymz4; spf=none (imf30.hostedemail.com: domain of mike.kravetz@oracle.com has no SPF policy when checking 205.220.177.32) smtp.mailfrom=mike.kravetz@oracle.com; dmarc=pass (policy=none) header.from=oracle.com X-Rspamd-Server: rspam05 X-Stat-Signature: 1ob3nwee8qpi113f4asbydm9wgrww8tz X-Rspamd-Queue-Id: 0C1E4E001806 X-HE-Tag: 1625876715-783032 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: When discussing the possibility of inflated page ref counts, Muuchun Song pointed out this potential issue [1]. It is true that any code could potentially take a reference on a compound page after allocation and before it is converted to and put into use as a hugetlb page. Specifically, this could be done by any users of get_page_unless_zero. There are three areas of concern within hugetlb code. 1) When adding pages to the pool. In this case, new pages are allocated added to the pool by calling put_page to invoke the hugetlb destructor (free_huge_page). If there is an inflated ref count on the page, it will not be immediately added to the free list. It will only be added to the free list when the temporary ref count is dropped. This is deemed acceptable and will not be addressed. 2) A page is allocated for immediate use normally as a surplus page or migration target. In this case, the user of the page will also hold a reference. There is no issue as this is just like normal page ref counting. 3) A page is allocated and MUST be added to the free list to satisfy a reservation. One such example is gather_surplus_pages as pointed out by Muchun in [1]. More specifically, this case covers callers of enqueue_huge_page where the page reference count must be zero. This patch covers this third case. Three routines call enqueue_huge_page when the page reference count could potentially be inflated. They are: gather_surplus_pages, alloc_and_dissolve_huge_page and add_hugetlb_page. add_hugetlb_page is called on error paths when a huge page can not be freed due to the inability to allocate vmemmap pages. In this case, the temporairly inflated ref count is not an issue. When the ref is dropped the appropriate action will be taken. Instead of VM_BUG_ON if the ref count does not drop to zero, simply return. In gather_surplus_pages and alloc_and_dissolve_huge_page the caller expects a page (or pages) to be put on the free lists. In this case we must ensure there are no temporary ref counts. We do this by calling put_page_testzero() earlier and not using pages without a zero ref count. The temporary page flag (HPageTemporary) is used in such cases so that as soon as the inflated ref count is dropped the page will be freed. [1] https://lore.kernel.org/linux-mm/CAMZfGtVMn3daKrJwZMaVOGOaJU+B4dS--x_oPmGQMD=c=QNGEg@mail.gmail.com/ Signed-off-by: Mike Kravetz --- mm/hugetlb.c | 100 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 78 insertions(+), 22 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index e59ebba63da7..3132c7395743 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -1072,6 +1072,8 @@ static void enqueue_huge_page(struct hstate *h, struct page *page) int nid = page_to_nid(page); lockdep_assert_held(&hugetlb_lock); + VM_BUG_ON_PAGE(page_count(page), page); + list_move(&page->lru, &h->hugepage_freelists[nid]); h->free_huge_pages++; h->free_huge_pages_node[nid]++; @@ -1399,11 +1401,20 @@ static void add_hugetlb_page(struct hstate *h, struct page *page, SetHPageVmemmapOptimized(page); /* - * This page is now managed by the hugetlb allocator and has - * no users -- drop the last reference. + * This page is about to be managed by the hugetlb allocator and + * should have no users. Drop our reference, and check for others + * just in case. */ zeroed = put_page_testzero(page); - VM_BUG_ON_PAGE(!zeroed, page); + if (!zeroed) + /* + * It is VERY unlikely soneone else has taken a ref on + * the page. In this case, we simply return as the + * hugetlb destructor (free_huge_page) will be called + * when this other ref is dropped. + */ + return; + arch_clear_hugepage_flags(page); enqueue_huge_page(h, page); } @@ -2015,9 +2026,10 @@ int dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn) * Allocates a fresh surplus page from the page allocator. */ static struct page *alloc_surplus_huge_page(struct hstate *h, gfp_t gfp_mask, - int nid, nodemask_t *nmask) + int nid, nodemask_t *nmask, bool zero_ref) { struct page *page = NULL; + bool retry = false; if (hstate_is_gigantic(h)) return NULL; @@ -2027,6 +2039,7 @@ static struct page *alloc_surplus_huge_page(struct hstate *h, gfp_t gfp_mask, goto out_unlock; spin_unlock_irq(&hugetlb_lock); +retry: page = alloc_fresh_huge_page(h, gfp_mask, nid, nmask, NULL); if (!page) return NULL; @@ -2044,11 +2057,35 @@ static struct page *alloc_surplus_huge_page(struct hstate *h, gfp_t gfp_mask, spin_unlock_irq(&hugetlb_lock); put_page(page); return NULL; - } else { - h->surplus_huge_pages++; - h->surplus_huge_pages_node[page_to_nid(page)]++; } + if (zero_ref) { + /* + * Caller requires a page with zero ref count. + * We will drop ref count here. If someone else is holding + * a ref, the page will be freed when they drop it. Abuse + * temporary page flag to accomplish this. + */ + SetHPageTemporary(page); + if (!put_page_testzero(page)) { + /* + * Unexpected inflated ref count on freshly allocated + * huge. Retry once. + */ + pr_info("HugeTLB unexpected inflated ref count on freshly allocated page\n"); + spin_unlock_irq(&hugetlb_lock); + if (retry) + return NULL; + + retry = true; + goto retry; + } + ClearHPageTemporary(page); + } + + h->surplus_huge_pages++; + h->surplus_huge_pages_node[page_to_nid(page)]++; + out_unlock: spin_unlock_irq(&hugetlb_lock); @@ -2090,7 +2127,7 @@ struct page *alloc_buddy_huge_page_with_mpol(struct hstate *h, nodemask_t *nodemask; nid = huge_node(vma, addr, gfp_mask, &mpol, &nodemask); - page = alloc_surplus_huge_page(h, gfp_mask, nid, nodemask); + page = alloc_surplus_huge_page(h, gfp_mask, nid, nodemask, false); mpol_cond_put(mpol); return page; @@ -2162,7 +2199,7 @@ static int gather_surplus_pages(struct hstate *h, long delta) spin_unlock_irq(&hugetlb_lock); for (i = 0; i < needed; i++) { page = alloc_surplus_huge_page(h, htlb_alloc_mask(h), - NUMA_NO_NODE, NULL); + NUMA_NO_NODE, NULL, true); if (!page) { alloc_ok = false; break; @@ -2203,24 +2240,20 @@ static int gather_surplus_pages(struct hstate *h, long delta) /* Free the needed pages to the hugetlb pool */ list_for_each_entry_safe(page, tmp, &surplus_list, lru) { - int zeroed; - if ((--needed) < 0) break; - /* - * This page is now managed by the hugetlb allocator and has - * no users -- drop the buddy allocator's reference. - */ - zeroed = put_page_testzero(page); - VM_BUG_ON_PAGE(!zeroed, page); + /* Add the page to the hugetlb allocator */ enqueue_huge_page(h, page); } free: spin_unlock_irq(&hugetlb_lock); - /* Free unnecessary surplus pages to the buddy allocator */ + /* + * Free unnecessary surplus pages to the buddy allocator. + * Pages have no ref count, call free_huge_page directly. + */ list_for_each_entry_safe(page, tmp, &surplus_list, lru) - put_page(page); + free_huge_page(page); spin_lock_irq(&hugetlb_lock); return ret; @@ -2529,6 +2562,7 @@ static int alloc_and_dissolve_huge_page(struct hstate *h, struct page *old_page, { gfp_t gfp_mask = htlb_alloc_mask(h) | __GFP_THISNODE; int nid = page_to_nid(old_page); + bool alloc_retry = false; struct page *new_page; int ret = 0; @@ -2539,9 +2573,30 @@ static int alloc_and_dissolve_huge_page(struct hstate *h, struct page *old_page, * the pool. This simplifies and let us do most of the processing * under the lock. */ +alloc_retry: new_page = alloc_buddy_huge_page(h, gfp_mask, nid, NULL, NULL); if (!new_page) return -ENOMEM; + /* + * If all goes well, this page will be directly added to the free + * list in the pool. For this the ref count needs to be zero. + * Attempt to drop now, and retry once if needed. It is VERY + * unlikely there is another ref on the page. + * + * If someone else has a reference to the page, it will be freed + * when they drop their ref. Abuse temporary page flag to accomplish + * this. Retry once if there is an inflated ref count. + */ + SetHPageTemporary(new_page); + if (!put_page_testzero(new_page)) { + if (alloc_retry) + return -EBUSY; + + alloc_retry = true; + goto alloc_retry; + } + ClearHPageTemporary(new_page); + __prep_new_huge_page(h, new_page); retry: @@ -2581,11 +2636,10 @@ static int alloc_and_dissolve_huge_page(struct hstate *h, struct page *old_page, remove_hugetlb_page(h, old_page, false); /* - * Reference count trick is needed because allocator gives us - * referenced page but the pool requires pages with 0 refcount. + * Ref count on new page is already zero as it was dropped + * earlier. It can be directly added to the pool free list. */ __prep_account_new_huge_page(h, nid); - page_ref_dec(new_page); enqueue_huge_page(h, new_page); /* @@ -2599,6 +2653,8 @@ static int alloc_and_dissolve_huge_page(struct hstate *h, struct page *old_page, free_new: spin_unlock_irq(&hugetlb_lock); + /* Page has a zero ref count, but needs a ref to be freed */ + set_page_refcounted(new_page); update_and_free_page(h, new_page, false); return ret; From patchwork Sat Jul 10 00:24:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Kravetz X-Patchwork-Id: 12368311 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=-16.8 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=unavailable 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 D6C39C07E99 for ; Sat, 10 Jul 2021 02:11:36 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 52634613B5 for ; Sat, 10 Jul 2021 02:11:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 52634613B5 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=oracle.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 2FC126B0073; Fri, 9 Jul 2021 22:11:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2A92D6B0078; Fri, 9 Jul 2021 22:11:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0D5918D0001; Fri, 9 Jul 2021 22:11:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0220.hostedemail.com [216.40.44.220]) by kanga.kvack.org (Postfix) with ESMTP id D43F96B0073 for ; Fri, 9 Jul 2021 22:11:35 -0400 (EDT) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 104F218481CA6 for ; Sat, 10 Jul 2021 02:11:35 +0000 (UTC) X-FDA: 78345051750.20.8691BB6 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf01.hostedemail.com (Postfix) with ESMTP id 9A6D3500B9DB for ; Sat, 10 Jul 2021 02:11:34 +0000 (UTC) Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 16A06iVm016569; Sat, 10 Jul 2021 00:25:02 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2020-01-29; bh=mv+YLHT5ozjyssslwLvKDVloPcrXlhS+X96HUt0ai9I=; b=stYkJR9MUvqcbzqeVbK749Jg9S73B0z7Rr5C1FKfNkQ7LQNXPELN/fZRZDalCFo2qWhl KMLLjDhpK1Ar7n+dANnRfrMC8gwsqxBhPdCGDoPl4Vxl5S4DJ/yqTf6o4/7R9rJPsCjm kjOXm62eoGV+4cGVevWMaRTGXNuu6c2djNNkKdsDmSWFIbIcvUkHGOYQqkrBoqbOaz2Z rFBtLTGaUVHBcrV4DJex0PdAu6b2dFPU8fKwxR0tfl0lF4GdnHXiEYQXK90sfCEqY7LU Ni6eVUX14qQerI2QxiGTjPCa1OwBc2tMC/4D0g9M3Im1HeriPJ/5kjJ+Ab5QZejE9tQ8 ow== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by mx0b-00069f02.pphosted.com with ESMTP id 39npbymemu-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 10 Jul 2021 00:25:02 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 16A06XUC021657; Sat, 10 Jul 2021 00:25:01 GMT Received: from nam02-sn1-obe.outbound.protection.outlook.com (mail-sn1anam02lp2048.outbound.protection.outlook.com [104.47.57.48]) by aserp3020.oracle.com with ESMTP id 39jfqkceyr-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 10 Jul 2021 00:25:01 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UwRs2WpZToDnPQKfszZuJKkUaCogEi5w3VoodvsYDalV08RABPu5TnFr0WA7GqoAz1bAI/n9fg/w0+jqTUV2uTW/71weOQQO36jZmS7C1DUA3jEhEyLqT842WEI8wWJjKx7Di/i5scOq7EGsNB3N8bL/OUa5lcNx4cz5Tb/6VPg0m8IQT1PoQx4liZCVA/KD7omMryRa5Bqt9l4OGrQJ4gtsry4ZKgxFyt3KrhAB6iX+h/hM1dVCyQ8IkoqK5YGVnD5pc0U7x5GUWuS0j/6qzd871MYUVbHEzRBDzc2hwwzTqVdjeJMLyTXObq8yMUc8/YaoOfYHIb2NaJGQxOL74Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=mv+YLHT5ozjyssslwLvKDVloPcrXlhS+X96HUt0ai9I=; b=T5KT6viuVyOCiEqyyoCoDxXXOvWxLM4EeGlLIERq7HmwpWuZEs102WUW68cmjjm4P7dtKJlevNSwLEuAfjZqYoLT7dmZHPvh3eh8+gx5ktp2VoYRIUdl+ojb4NFHbS2kxKqacJkSDhO9uGtLlkeq0O2LrXMF11Gn0o5CvsBDcSpDkvFY+KxpAd6aJ+WuyyXHnxuo3pJy+wJlgKVcrEer8kuWXOkIfT1EKb6Jsx/gOrC6v9F2I2pJW7f+awXmhWny70NIFCNKz51csxU1AWr8fUKq+1vcSqSwkHaACs728YhNUvK5fO2hUyYxjzXkrek1g4zqdu+6bXyRA0ygQlh96A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=mv+YLHT5ozjyssslwLvKDVloPcrXlhS+X96HUt0ai9I=; b=W9NJ2Cho01gOw7/urhHvfadCeQPn0buIZPW0JjTL6lCjcahLYntSAnvQXfg5kiz6ZXOoCcpYcepbg39T32Z6XjSH2ikQxdW65Mf9CKH4rDUdXH5NOKqH5pm+lb5TVz8Wmic2NBrcmTVT5PNd1wrduLgfki1DWQSngLfo39fFmzQ= Received: from BY5PR10MB4196.namprd10.prod.outlook.com (2603:10b6:a03:20d::23) by BY5PR10MB3810.namprd10.prod.outlook.com (2603:10b6:a03:1fb::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.21; Sat, 10 Jul 2021 00:24:59 +0000 Received: from BY5PR10MB4196.namprd10.prod.outlook.com ([fe80::8d2a:558d:ab4a:9c2a]) by BY5PR10MB4196.namprd10.prod.outlook.com ([fe80::8d2a:558d:ab4a:9c2a%5]) with mapi id 15.20.4308.023; Sat, 10 Jul 2021 00:24:59 +0000 From: Mike Kravetz To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Muchun Song , Michal Hocko , Oscar Salvador , David Hildenbrand , Matthew Wilcox , Naoya Horiguchi , Mina Almasry , Andrew Morton , Mike Kravetz Subject: [PATCH 3/3] hugetlb: before freeing hugetlb page set dtor to appropriate value Date: Fri, 9 Jul 2021 17:24:41 -0700 Message-Id: <20210710002441.167759-4-mike.kravetz@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210710002441.167759-1-mike.kravetz@oracle.com> References: <20210710002441.167759-1-mike.kravetz@oracle.com> X-ClientProxiedBy: MWHPR14CA0041.namprd14.prod.outlook.com (2603:10b6:300:12b::27) To BY5PR10MB4196.namprd10.prod.outlook.com (2603:10b6:a03:20d::23) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from monkey.oracle.com (50.38.35.18) by MWHPR14CA0041.namprd14.prod.outlook.com (2603:10b6:300:12b::27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Sat, 10 Jul 2021 00:24:58 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 34889300-85f9-4032-241e-08d9433926b1 X-MS-TrafficTypeDiagnostic: BY5PR10MB3810: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8273; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 3alEj0/KMSIAShI1sDH67r7H5GrAlm1IVfeh6tCFGFveC6SVh2SnbLlRzJuNttnk5zeiwl/7KG0k/29AINHpqlMcFdbUKPiXmwBmBY8N3Gc/zlLUv6EG/y/yFKa2q/w5M+0nnWfZYt7h2WEp+wstUxcucHJnMqaHpD0mAAf1FIhIbZqj0w87ymkFl2vBRLNYe73koNbzl5BzEvHGddBoFcIt4A+InjZp/eNcOVNqiZrgADnvEeNeqlNRLXRvAKPlXuNTtpVyD5KpQPsEAHczeWegLH4d0HC5XcnUBPGeKQcXJlLnhXtOalYPM5CjOiCF3UiGYV3u7Url4cBQPLYUVLd+mNxrDANMxfXrF0wOBNX3QqrCRNwFfgLwC3t43jcmKzMl+31apnnfQasIEc+nkn+ofUEGghFWNiiIAFxtKXTgcyNJ5hbtBHzRH4wK9aQvY2OrSzwVK3TmF48RJethXdYtHCQOJBjEwJd0uFLz9Hh1SrnH1Cki6M1fKMY+sGlrqiQPenDJXRdTE43bnNAVWqqxI6Qp4jx/fC5BJw5ebwiH4PFRIzJ1poLWp/LmChvB7Y6xv+vhq6NOBLfDiX/Y96LIMCZzg00khp2FmU3/FCtnMMQw1kitCxc1vk0UFUEoLLCF7NKks9lqAyqTEgr8QZboXnSeeUNeIz8dTx+lJ5lyoITWEHNBxz2Sa024ZW55 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BY5PR10MB4196.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(136003)(376002)(396003)(39860400002)(346002)(366004)(66476007)(2616005)(8676002)(107886003)(66946007)(186003)(38350700002)(316002)(86362001)(38100700002)(8936002)(956004)(66556008)(478600001)(36756003)(26005)(44832011)(4326008)(1076003)(52116002)(83380400001)(54906003)(2906002)(6486002)(6666004)(7696005)(5660300002)(7416002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 215ozdSJsLflD6kitJsr9o2cPa79VosgtsPbzKcw6WtbXn3mIhLkE+WJt4a34pZC9T9R45w2Okq7To8v2j0Crb097NWcGEU3/yEtPdY8TO0m1aom7XipkHZZ5GMqhA84xYscwHXZemi0sIjyvTKimA7QWwvtO5hs0UdKWM//KfQTqiWorupImp80ZY8sdoi6d/s+mF7TXG4rHJCFfMwOC/i/yqq1e8cfzHoNU6LkA+MBP7hHDBlBNcnxV3frHWRRQJhUuKoFYe4bTzt5U2gWUjd/M6n6bKYjTff7DPWb1Gkr3/D4fDKT+u7LY7IJ0VQVJa5f5uMZydRumRH2sy+Rj27pEDKACzxOx5mB6YqBqjAVOi0h8ihQL5Tx4bMEZNuO2MbIwhQcPwBk589/3FACG56Pvi9G8+G8RTmfWK75kZkV6EAYoSBblzvwc7hZzjgrpFz8v8a15OkDSSav5yepuezpZuENzBQy9iA7C3MQJxE6/EmBXjEhdtBx66VkFpZlteyDXP7vETAi9hsvVjcJgGaOSkw6fOzYPU+EgX6bMPLntvSMbHgBGbPFczgaKJHBwPRBvpUq8R6BZQdzEerjuydRYjN/LhrkNoPqJWhPV2ZYCmW0RjjqQKeBOJ7RCbr3gSx2S7mNeJhdsd2HXyf3GAcawWtN0CDYnEyy/1tplR8UWeFGo3/GPnsj4GaGMYPZrLTXmJzmuvcUk5hMJeRcEMuKSAr2N7smdCGoZv4JPURKDH0pLIcAgKiPl9ezHqxa4ixCvwPhB2ywkPoXxVpQrg+wnceFPQGtRC+K6ZuQ2T7OBGbFU3E4T8ewI2mNYCsGEdHadocYo3m4c49bJnnNebb/jyrbzb3RXgNZxKWPf/E+aY1ccmmbHOs+leY6Jp9insNppLx2bNb5rbYEU0c0XhE9Gcfism0Rx8DZaFljY1cnspAIyuTtG9uiYSg/cCChnuoIM7bHMExIjmsCeFwg+CeytJ5wF9tXlSc6PdaHWH7YJHaaBRRIOSwg5i1v0Ds8J7R+8tKxUuPKXvi2KOVrJ8gMgVpgBUaV0kFwCO2hAzR5NTJIzaUG3rrdDh8tMWGn7z1CNr18D+c70FjMbJih2VQ/CHiTfefmTk8nGbuddETo/0KfRD17skT98Nii+78GdS4SlZl7V4Tzedqpg05kd2t/PiGIaui500RyIvBb9OL46Rpubs/iEGCjOmspFKHX+eoP8zjd70+TZCvWQu1cv+XzO73cd8LuiHypzZUni8Q2Ns5qqTIbXEL7GWPbigVEn35B9Xuxaj3fb3ACkDXd99KUjZcd1P40xUsyQBMUE4JV6iraMKvBbx0/BtQ/wTBg X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 34889300-85f9-4032-241e-08d9433926b1 X-MS-Exchange-CrossTenant-AuthSource: BY5PR10MB4196.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Jul 2021 00:24:59.3763 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: MkEPkarli7bDVDXeGVe1EsLYKKsTuYEGI1uN4kn6mwbKsKcCilZIpmMBpR4Psw2IgOq4qSxGDCQiPe7vqDM83w== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR10MB3810 X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=10040 signatures=668682 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 spamscore=0 phishscore=0 mlxscore=0 bulkscore=0 malwarescore=0 adultscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2104190000 definitions=main-2107090126 X-Proofpoint-GUID: smOJxBCrx1LXFx-QAxHclgtU-gkdQ3cW X-Proofpoint-ORIG-GUID: smOJxBCrx1LXFx-QAxHclgtU-gkdQ3cW X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 9A6D3500B9DB X-Stat-Signature: e9fquidoudyimz35e13kowwin3ctcg8q Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2020-01-29 header.b=stYkJR9M; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=W9NJ2Cho; dmarc=pass (policy=none) header.from=oracle.com; spf=none (imf01.hostedemail.com: domain of mike.kravetz@oracle.com has no SPF policy when checking 205.220.177.32) smtp.mailfrom=mike.kravetz@oracle.com X-HE-Tag: 1625883094-243005 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: When removing a hugetlb page from the pool the ref count is set to one (as the free page has no ref count) and compound page destructor is set to NULL_COMPOUND_DTOR. Since a subsequent call to free the hugetlb page will call __free_pages for non-gigantic pages and free_gigantic_page for gigantic pages the destructor is not used. However, consider the following race with code taking a speculative reference on the page: Thread 0 Thread 1 -------- -------- remove_hugetlb_page set_page_refcounted(page); set_compound_page_dtor(page, NULL_COMPOUND_DTOR); get_page_unless_zero(page) __update_and_free_page __free_pages(page, huge_page_order(h)); /* Note that __free_pages() will simply drop the reference to the page. */ put_page(page) __put_compound_page() destroy_compound_page NULL_COMPOUND_DTOR BUG: kernel NULL pointer dereference, address: 0000000000000000 To address this race, set the dtor to the normal compound page dtor for non-gigantic pages. The dtor for gigantic pages does not matter as gigantic pages are changed from a compound page to 'just a group of pages' before freeing. Hence, the destructor is not used. Signed-off-by: Mike Kravetz Reviewed-by: Muchun Song --- mm/hugetlb.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 3132c7395743..fa8ec2072949 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -1370,8 +1370,28 @@ static void remove_hugetlb_page(struct hstate *h, struct page *page, h->surplus_huge_pages_node[nid]--; } + /* + * Very subtle + * + * For non-gigantic pages set the destructor to the normal compound + * page dtor. This is needed in case someone takes an additional + * temporary ref to the page, and freeing is delayed until they drop + * their reference. + * + * For gigantic pages set the destructor to the null dtor. This + * destructor will never be called. Before freeing the gigantic + * page destroy_compound_gigantic_page will turn the compound page + * into a simple group of pages. After this the destructor does not + * apply. + * + * This handles the case where more than one ref is held when and + * after update_and_free_page is called. + */ set_page_refcounted(page); - set_compound_page_dtor(page, NULL_COMPOUND_DTOR); + if (hstate_is_gigantic(h)) + set_compound_page_dtor(page, NULL_COMPOUND_DTOR); + else + set_compound_page_dtor(page, COMPOUND_PAGE_DTOR); h->nr_huge_pages--; h->nr_huge_pages_node[nid]--;