Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp223459pxy; Wed, 21 Apr 2021 01:10:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwYOhAT4Jvr2x4Gp3+wZu1obptnSeO4iZCE3gT1i8Qxr9eZeyliZBdLWbJBWe39UUYq0CNl X-Received: by 2002:a17:90b:1652:: with SMTP id il18mr10144563pjb.32.1618992605690; Wed, 21 Apr 2021 01:10:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618992605; cv=none; d=google.com; s=arc-20160816; b=zXbLnihg8b7WVxstf/hZfk6MxyehZDvcqv454aR02W4TwRORrkooNqq6uPdp7penmQ 3yYG3nnn5lugWLiXBu08DMMXncBS7nhcSp88bnN8qEH+DCoCoetUqSEl2AYn/StK6PES BhsAczjNDCDvoDJeBEXZh3lc7bmpjwxWbuf1i9MrB2Rk0kSmHLrSB8VtLH+0KjqP5LuC 4gsLDdw4LMMLD4feR02av1+5enaCsFc4yUgo0zgvY+7870JZCCh12xI1AOPfl5wM2nX3 aNP1UADR1GL1c6eU/tZmj/cpcbyO9IeAUz3wLRouDQlCGeZSJgcSFDNjA0BwX0qUWoPF 1YyA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=njgxBDKMFjECLNEdhGU4YY0FuQCqkpgNlJ1UWX90J0s=; b=zzC5ZcC7qISU77pga+9E7HTCdr3esDD6C2kCPgT+8cSdrA4otHCYF51Th4v7DtGpZ4 cOG19Bjtd4nJ2PF5sAyj6NJXljokIr6JVHG6NN1+MrcXaWnzLo3KYpbm6FX13L5woqkd BAnc6FLQ3sCWd/S2SGcULmWXT+Am/e41XDwTZl3ct5k0ft9EUSnYAknvj8gABmNh79Rw zpaPFIiSdUjv/22FAYriXhxdDhYKhqiHlGYS0qMsKB4uWFlF3kHB28dyAV2AF5vom3fG TTu9H7sJHhVkkPmimsTn1sh1aehLzrmrl0F2VpIqPqvQNLN/ezpMDf5d10ZOzcW0BK/E VmUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=gTKsJlyW; 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=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=suse.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id kk9si1940394pjb.125.2021.04.21.01.09.53; Wed, 21 Apr 2021 01:10:05 -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=@suse.com header.s=susede1 header.b=gTKsJlyW; 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=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=suse.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234139AbhDUIEM (ORCPT + 99 others); Wed, 21 Apr 2021 04:04:12 -0400 Received: from mx2.suse.de ([195.135.220.15]:51330 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230343AbhDUIEJ (ORCPT ); Wed, 21 Apr 2021 04:04:09 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1618992216; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=njgxBDKMFjECLNEdhGU4YY0FuQCqkpgNlJ1UWX90J0s=; b=gTKsJlyWkHqNt5j/tj2PeXgnPL90WCzffITUftA0RuVPTb0ghtpes69iLSnZ4ejd+ruHMa 9dhaoYmWu2yX1EA2bAtmm10GVa7rM4NVeF6NgDGvUGvhsq96EI5P8W8AravyPv289bnb9m fI/VDO6X0YwsIkI7rPH7J220cwODkOA= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id D3167B137; Wed, 21 Apr 2021 08:03:35 +0000 (UTC) Date: Wed, 21 Apr 2021 10:03:34 +0200 From: Michal Hocko To: Muchun Song Cc: mike.kravetz@oracle.com, akpm@linux-foundation.org, osalvador@suse.de, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Naoya Horiguchi Subject: Re: [PATCH] mm: hugetlb: fix a race between memory-failure/soft_offline and gather_surplus_pages Message-ID: References: <20210421060259.67554-1-songmuchun@bytedance.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210421060259.67554-1-songmuchun@bytedance.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [Cc Naoya] On Wed 21-04-21 14:02:59, Muchun Song wrote: > 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. The hwpoison side of this looks really suspicious to me. It shouldn't really touch the reference count of hugetlb pages without being very careful (and having hugetlb_lock held). What would happen if the reference count was increased after the page has been enqueed into the pool? This can just blow up later. > 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 > -- Michal Hocko SUSE Labs