Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp153262pxy; Tue, 20 Apr 2021 23:04:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxVcoEtdc4N5lEMF/0J5PrTAepZm2UTc0SyFBuvoK+dgt67tDVVqbW/hDt6jx6f9UDUUGl8 X-Received: by 2002:a17:906:53cd:: with SMTP id p13mr30932931ejo.379.1618985093205; Tue, 20 Apr 2021 23:04:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618985093; cv=none; d=google.com; s=arc-20160816; b=HfO4Wy7ighvXhm+J4G3ixilD3Nc9x+5kP9b/IjTPUtWlshIh5/ytZZjlqyzCQbSJJU QwkQin+AYJzPX7W0hzcmTfvxjhSbQO61XL8Ai3+cHq38bGuWb4z1tTZe2ydCxbZNz6P+ Ay3MEg0dRl9nrrQ9yJFUTB6d389hrqpUnv+unsjIqHKr6LZd3hmIrjtV10N+vFHpRJWY h5F9ZOHnCHpauHpBD+/d/mrr20e+AxRGGa5GP8g3xFgSaJBEc8s6EXVmb8mp9yA4z4fJ phSJaeUnMER/5vhuSC9Etlujn+Z9MEh035HSEqlaTqKR+kFNcOxT4jCivgkO0m3orrOX F2MA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=HyY9cW4ZspnsIypCtHXf1FMM7FMaR7EgF6f9kJkBvd4=; b=TC3s/xtwTK0HCRuYgxsnXzF21gNXbU0BPfoq3O7azsUXT9ASYxkl9qy6yqklRpjUvY l5N4C4WKq/dCTJEm3wmRIOutZ+jrq3We1QJXt1NQKp10fI3cxQ27oEbX1vDjZK6HojaD HdCf1nn2L1lgesQVMRLU3RqVMLrpPqAf+qTOp0mjQlP873x9UcZ3VGxx/EW4BzI5pBjR MpyETIT4t4VHTwLyZpk6CfOI+H/wsNLbhnDh5FJhmTYaCymvqnNaxtnMrX6kXjUa4euD KLBJq0/tDSxTtJTguL5qph4fzkyQ3nonIDnwwOQiP/DpJ3Rrcd3cVb8rVnI1TKlayb9i aPow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=YRXCkaNt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v7si1147349edb.231.2021.04.20.23.04.30; Tue, 20 Apr 2021 23:04:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance-com.20150623.gappssmtp.com header.s=20150623 header.b=YRXCkaNt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235395AbhDUGEE (ORCPT + 99 others); Wed, 21 Apr 2021 02:04:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58298 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235384AbhDUGEA (ORCPT ); Wed, 21 Apr 2021 02:04:00 -0400 Received: from mail-pf1-x42b.google.com (mail-pf1-x42b.google.com [IPv6:2607:f8b0:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1AEB0C06174A for ; Tue, 20 Apr 2021 23:03:26 -0700 (PDT) Received: by mail-pf1-x42b.google.com with SMTP id m11so27683559pfc.11 for ; Tue, 20 Apr 2021 23:03:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=HyY9cW4ZspnsIypCtHXf1FMM7FMaR7EgF6f9kJkBvd4=; b=YRXCkaNtkRpZGmmh+k7lz8wyKQRehKcuvHjGnHVKKiyMgJXE3BYneLYkVxsra+0ajv rZynHqCBjPgkUZE4eSmnrutWc/XhOC3SUeMULOpzKsmVijpI2qoPoQ9Fr8ID30VYBZ65 /1wjPDlT8JdNCQp2IOfmPLWFhVTeOCt5YkeCO56BnzzeUScwD1kNmMNcpqlK6f5LKRWV O7JOrwbkNK/6aGNYxwAFl94in+ecZzq9PI/HFAn6CMJw+UDSpyS2WzYbOizxhVFm9bvh TI23lLfYJNlUyFlzbZvZdFCGnTHv3u5RTStJnl9T2+y+ncfM4GQ5YFOcrD8uhXl1oC3F MpjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=HyY9cW4ZspnsIypCtHXf1FMM7FMaR7EgF6f9kJkBvd4=; b=UrMj7C6XtGSTo6uHYdRyOlDW5Fg2sh26KOfwE2vxJSeEoAvnOd+LPbxgHywyRA2D56 TKtkovKP7vb2cLRXOwy2xbAxGjT1x/brMowe49cuoQImHCt0Yxei1jkY5P0ta4cp+oon tTgNJa5o8VHpCof5SY4hw/WVlV2h1LaV9I42CFgy5l+ovIzytAojd5dnpPFkozsMECXR uHgPMPf2/epyj7Nw5eOqd6YkpospBd3SbJkXXzKvYI6fuQ/eVTOyi2QdD5e2L/G+w6f4 YlEBTCBFyqjdc38bSjSlNHIBSnPV/MNi9bJw/eik95SnDbOOcmhY72acKivVoWG0Yw9V IXKg== X-Gm-Message-State: AOAM533R4HzkKKa7Ahm6eAeVNho5Sqm8xMedrrarhKe0mV48fNmnXrbz PMXyCflapgEbSZwyhWlBMZ1ikA== X-Received: by 2002:a63:3d48:: with SMTP id k69mr20037675pga.239.1618985005695; Tue, 20 Apr 2021 23:03:25 -0700 (PDT) Received: from localhost.localdomain ([139.177.225.255]) by smtp.gmail.com with ESMTPSA id nh24sm887161pjb.38.2021.04.20.23.03.22 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 20 Apr 2021 23:03:25 -0700 (PDT) From: Muchun Song To: mike.kravetz@oracle.com, akpm@linux-foundation.org, mhocko@suse.com, osalvador@suse.de Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song Subject: [PATCH] mm: hugetlb: fix a race between memory-failure/soft_offline and gather_surplus_pages Date: Wed, 21 Apr 2021 14:02:59 +0800 Message-Id: <20210421060259.67554-1-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The possible bad scenario: CPU0: CPU1: gather_surplus_pages() page = alloc_surplus_huge_page() memory_failure_hugetlb() get_hwpoison_page(page) __get_hwpoison_page(page) get_page_unless_zero(page) zero = put_page_testzero(page) VM_BUG_ON_PAGE(!zero, page) enqueue_huge_page(h, page) put_page(page) The refcount can possibly be increased by memory-failure or soft_offline handlers, we can trigger VM_BUG_ON_PAGE and wrongly add the page to the hugetlb pool list. Signed-off-by: Muchun Song --- mm/hugetlb.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 3476aa06da70..6c96332db34b 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -2145,17 +2145,14 @@ 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. + * The refcount can possibly be increased by memory-failure or + * soft_offline handlers. */ - zeroed = put_page_testzero(page); - VM_BUG_ON_PAGE(!zeroed, page); - enqueue_huge_page(h, page); + if (likely(put_page_testzero(page))) + enqueue_huge_page(h, page); } free: spin_unlock_irq(&hugetlb_lock); -- 2.11.0