From patchwork Tue Oct 18 00:25:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rik van Riel X-Patchwork-Id: 3857 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp1716157wrs; Mon, 17 Oct 2022 18:02:07 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5NttaQwO7X8Csb2M7I0uHTSDqXlZ/7COAeAfgqyrrhTpcg30CmtdTP2apMA89ZKkNwdkZE X-Received: by 2002:aa7:c144:0:b0:459:f897:793e with SMTP id r4-20020aa7c144000000b00459f897793emr361176edp.8.1666054927107; Mon, 17 Oct 2022 18:02:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666054927; cv=none; d=google.com; s=arc-20160816; b=KV7etuOao2l0MVxVPdpaEhm5Q8tzzFn4s1norYa8zWjzMJlR/3AvNquykNhAQZXnQG FclvTtqVNvlW82VPyrVQ25+khYZV2fR5rdaNnyCFSUX0RNIxL4yr+/TURkZ/Sjqk+L5y tuyxFeAGldyoLZxj9CYTktoPVrFno2uEC11Xkb/EN8t+Jub0p3e25x0NAylZtXbM9sWe CprWxs349g9nS6Y+W8tcVoCvlsScorzGCWUscsd8dO7fxwm9rDokeWuYjhg22N2xbOhx ywNXiLDGs2jDY1fFzS6kYj6zY5l+84TAXd+jbRkl8LQInNvwTqfF83Nty/kqZcrRUe8o LDBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:subject:cc:to:from:date; bh=vY9DzTal4pLD+ONd59GnhyzBaZdNj686DyyQp70t9uA=; b=AbjFRZ8q6Jg+i2Wo1N5LtLCybTMZCA3ZOT8EtKfjNq4TCo5Xvn7V4OpjxuuY8Ccbug KehK144zG7N114+2H68rI+BKUCD5U+vDkzrUQdyGHO7zHLnD74FiO63H+l133vLcbqeE HY35KBsdOmF5ph4Eyzdyt5HuDKzM5OeMdXkPUZP15MZh9+MW8Zoj8tDxzZrNi+IAkPll lN4vzAc0og7cBFp8e8K2SQUkm9urFCzqS3KC2x9H7hFHD6qxE275CFeGkOrVWQJVxM9O nhpMnLRcwKrTqgaFHeT/iDgSe8b9wtmRk6DKjGTw/GhbZhgmrgstF8LzRRbrd79jCOoH urmA== ARC-Authentication-Results: i=1; mx.google.com; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gb6-20020a170907960600b0078b96722000si12067866ejc.608.2022.10.17.18.01.39; Mon, 17 Oct 2022 18:02:07 -0700 (PDT) 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; 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 S230437AbiJRA7p (ORCPT + 99 others); Mon, 17 Oct 2022 20:59:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230387AbiJRA7n (ORCPT ); Mon, 17 Oct 2022 20:59:43 -0400 X-Greylist: delayed 1078 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Mon, 17 Oct 2022 17:59:42 PDT Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7335EFD3C for ; Mon, 17 Oct 2022 17:59:41 -0700 (PDT) Received: from [2603:3005:d05:2b00:6e0b:84ff:fee2:98bb] (helo=imladris.surriel.com) by shelob.surriel.com with esmtpsa (TLS1.2) tls TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1okaPu-0003yC-18; Mon, 17 Oct 2022 20:25:10 -0400 Date: Mon, 17 Oct 2022 20:25:05 -0400 From: Rik van Riel To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, stable@kernel.org, Naoya Horiguchi , Glen McCready , Mike Kravetz , Muchun Song , Andrew Morton , kernel-team@meta.com Subject: [PATCH] mm,hugetlb: take hugetlb_lock before decrementing h->resv_huge_pages Message-ID: <20221017202505.0e6a4fcd@imladris.surriel.com> X-Mailer: Claws Mail 4.1.0 (GTK 3.24.34; x86_64-redhat-linux-gnu) MIME-Version: 1.0 Sender: riel@shelob.surriel.com X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1746985211232917814?= X-GMAIL-MSGID: =?utf-8?q?1746985211232917814?= The h->*_huge_pages counters are protected by the hugetlb_lock, but alloc_huge_page has a corner case where it can decrement the counter outside of the lock. This could lead to a corrupted value of h->resv_huge_pages, which we have observed on our systems. Take the hugetlb_lock before decrementing h->resv_huge_pages to avoid a potential race. Fixes: a88c76954804 ("mm: hugetlb: fix hugepage memory leak caused by wrong reserve count") Cc: stable@kernel.org Cc: Naoya Horiguchi Cc: Glen McCready Cc: Mike Kravetz Cc: Muchun Song Cc: Andrew Morton Signed-off-by: Rik van Riel Reviewed-by: Mike Kravetz --- mm/hugetlb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index b586cdd75930..dede0337c07c 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -2924,11 +2924,11 @@ struct page *alloc_huge_page(struct vm_area_struct *vma, page = alloc_buddy_huge_page_with_mpol(h, vma, addr); if (!page) goto out_uncharge_cgroup; + spin_lock_irq(&hugetlb_lock); if (!avoid_reserve && vma_has_reserves(vma, gbl_chg)) { SetHPageRestoreReserve(page); h->resv_huge_pages--; } - spin_lock_irq(&hugetlb_lock); list_add(&page->lru, &h->hugepage_activelist); set_page_refcounted(page); /* Fall through */