Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp791188rwb; Wed, 26 Jul 2023 03:00:33 -0700 (PDT) X-Google-Smtp-Source: APBJJlGhhkZfeL730zQxQ5C9WHeiUKw3DC4mnrEyXF97ypeeKRuMjLkZlMy1h8MswUYaDGmroDzM X-Received: by 2002:a17:902:c409:b0:1ac:63ac:10a7 with SMTP id k9-20020a170902c40900b001ac63ac10a7mr1372260plk.68.1690365633480; Wed, 26 Jul 2023 03:00:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690365633; cv=none; d=google.com; s=arc-20160816; b=q7AayUiJY99LBSH3heQqWmsjrB0/mk9hmlTBFNrqHR6nwdbhKcJU6tKJTohK6HOves C1H5/8GtED3fl50Ku/38jxa89PdKVxLWNNKVRFf8fWpNfWVnWU+74UjOR1tjUVVPj/iD v1k4oZhnm98Q7G132MwpJv95IX72paGISrjW9MCRDOymjzFCtvdF9rj+m53qqudWkANp 7u9v2iKDfCK0E5917iSDVtBHxTMZGYHiym8yaeqVan1yz3NxT4VMILQyURVj9vTpTSwE sjNRG907cTPIV1A1haDWrPvvrQhBbJDsc3IMkE44JQe2fql8gvZv+5BkA8PID1jcIg2n ZasQ== 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:dkim-signature:date; bh=McfcWIQnPPHbZphZYBIYnAn4Lt4MKunTWFsFjRbBvKo=; fh=sQE9x5wq0vKx1OqD3/Nv95/EYwFnTCtWhQapViTOuxU=; b=UPFoml9bA5JBjuyXcy4l2J/hNUAEjgFHdXqybMLWVoMDfcD7Pzrrm650d2g9G8EaD9 NNliS5r4KmYX8m+PK6Hbji2v2ZvtCFyM64YLc/aO2myK37HKatirOVXES1/4/6Uzk7zf iD6Le2KLdDdgsZYUOuulMxy4kxADGT5o+eRgJvVBC6YouXR85GVqC/AQ9TFmyDljBHOz Y/72hEAzR8dNesfQmQZTMXzHhxV/H3OlEy9yHxeP7xTizfNuwnna9wCVpUfHRX1LmCG+ B50MnoqW3k+veTaL4SbKtIQt2UxXNVfxghM3vp3ReKsK2VNDOndPUEC7HqlewHAaOBSi 7nfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=VPvLRZyn; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e14-20020a17090301ce00b001bbd0358ef7si527885plh.518.2023.07.26.03.00.19; Wed, 26 Jul 2023 03:00:33 -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; dkim=pass header.i=@linux.dev header.s=key1 header.b=VPvLRZyn; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230205AbjGZIzg (ORCPT + 99 others); Wed, 26 Jul 2023 04:55:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54558 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230194AbjGZIzO (ORCPT ); Wed, 26 Jul 2023 04:55:14 -0400 Received: from out-38.mta1.migadu.com (out-38.mta1.migadu.com [IPv6:2001:41d0:203:375::26]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 62DBA30EB for ; Wed, 26 Jul 2023 01:48:30 -0700 (PDT) Date: Wed, 26 Jul 2023 17:48:18 +0900 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1690361307; h=from:from:reply-to:subject:subject: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=McfcWIQnPPHbZphZYBIYnAn4Lt4MKunTWFsFjRbBvKo=; b=VPvLRZynAgP2muaMo3LsPtLZy2mhoXJLuhdHAtWI6xGA1KbScW/I/bmY75xBDkKv/0HAsw 5i4Zhfs+toFsZZvzs+e2qApek5r37BcJHL7nMO5kXIUV4mBtqMr21tmpxVHtq2CJw9wD5K +Z+B5fzao0764V0NTUf+6eRd76p17Ng= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Naoya Horiguchi To: Mike Kravetz Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Jiaqi Yan , Muchun Song , Miaohe Lin , Axel Rasmussen , James Houghton , Michal Hocko , Andrew Morton , stable@vger.kernel.org Subject: Re: [PATCH v2 1/2] hugetlb: Do not clear hugetlb dtor until allocating vmemmap Message-ID: <20230726084818.GA268581@ik1-406-35019.vs.sakura.ne.jp> References: <20230718004942.113174-1-mike.kravetz@oracle.com> <20230718004942.113174-2-mike.kravetz@oracle.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20230718004942.113174-2-mike.kravetz@oracle.com> X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=unavailable 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 On Mon, Jul 17, 2023 at 05:49:41PM -0700, Mike Kravetz wrote: > Freeing a hugetlb page and releasing base pages back to the underlying > allocator such as buddy or cma is performed in two steps: > - remove_hugetlb_folio() is called to remove the folio from hugetlb > lists, get a ref on the page and remove hugetlb destructor. This > all must be done under the hugetlb lock. After this call, the page > can be treated as a normal compound page or a collection of base > size pages. > - update_and_free_hugetlb_folio() is called to allocate vmemmap if > needed and the free routine of the underlying allocator is called > on the resulting page. We can not hold the hugetlb lock here. > > One issue with this scheme is that a memory error could occur between > these two steps. In this case, the memory error handling code treats > the old hugetlb page as a normal compound page or collection of base > pages. It will then try to SetPageHWPoison(page) on the page with an > error. If the page with error is a tail page without vmemmap, a write > error will occur when trying to set the flag. > > Address this issue by modifying remove_hugetlb_folio() and > update_and_free_hugetlb_folio() such that the hugetlb destructor is not > cleared until after allocating vmemmap. Since clearing the destructor > requires holding the hugetlb lock, the clearing is done in > remove_hugetlb_folio() if the vmemmap is present. This saves a > lock/unlock cycle. Otherwise, destructor is cleared in > update_and_free_hugetlb_folio() after allocating vmemmap. > > Note that this will leave hugetlb pages in a state where they are marked > free (by hugetlb specific page flag) and have a ref count. This is not > a normal state. The only code that would notice is the memory error > code, and it is set up to retry in such a case. > > A subsequent patch will create a routine to do bulk processing of > vmemmap allocation. This will eliminate a lock/unlock cycle for each > hugetlb page in the case where we are freeing a large number of pages. > > Fixes: ad2fa3717b74 ("mm: hugetlb: alloc the vmemmap pages associated with each HugeTLB page") > Cc: > Signed-off-by: Mike Kravetz I wrote a reproducer to cause the race condition between memory failure and shrinking free hugepage pool (with vmemmap optimization enabled). Then I observed that v6.5-rc2 kernel panicked with "BUG: unable to handle page fault for address...", and confirmed that the kernel with your patches do not reproduce it. Thank you for fixing this. Tested-by: Naoya Horiguchi