From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 03D74C001E0 for ; Thu, 20 Jul 2023 01:34:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7C33D2800A6; Wed, 19 Jul 2023 21:34:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7725C28004C; Wed, 19 Jul 2023 21:34:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 639962800A6; Wed, 19 Jul 2023 21:34:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 5592828004C for ; Wed, 19 Jul 2023 21:34:57 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 2AE0F804AD for ; Thu, 20 Jul 2023 01:34:56 +0000 (UTC) X-FDA: 81030271392.26.14A06D3 Received: from mail-yw1-f171.google.com (mail-yw1-f171.google.com [209.85.128.171]) by imf02.hostedemail.com (Postfix) with ESMTP id 48BFC80006 for ; Thu, 20 Jul 2023 01:34:54 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=7BpcQLW3; spf=pass (imf02.hostedemail.com: domain of jiaqiyan@google.com designates 209.85.128.171 as permitted sender) smtp.mailfrom=jiaqiyan@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1689816894; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=i/BV8QhI2Yg0HLi9NdTgzIisR0+G5gj8nV7Uz8iT+f4=; b=SNr9ye/G2AuiS/ZqNO0BRibKak65r2BbGKGy0in8L2TacrSP+rwYiO/2w+/ruNnUNn2qMf b49d98hJ/3Rl2op1RtKej52pjFW9VSLNbI/eCmOYilG3lcrYG8h5qExkI1/NqK6Y22Z5OS tAfjBmQygtsNGGYB1qcjfBPaiNcv/PE= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=7BpcQLW3; spf=pass (imf02.hostedemail.com: domain of jiaqiyan@google.com designates 209.85.128.171 as permitted sender) smtp.mailfrom=jiaqiyan@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1689816894; a=rsa-sha256; cv=none; b=7awnja2Ev+1WhoiPD54s4fPeRut6BR0mHY5PMnTqTkLcyo3MRHTlGUJNuQyoeRYCb9jFmd HhWAFTHEFBW7QRKOh/OrKWwTCdnLHuRgx0q1JBJS8KYCQyMG8RpKxfsFsCPwvdeZ81vbEC hC3x3TM6a4Umk8j8Cplx03TbY2MiKIs= Received: by mail-yw1-f171.google.com with SMTP id 00721157ae682-579dfae6855so3520797b3.1 for ; Wed, 19 Jul 2023 18:34:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689816893; x=1690421693; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=i/BV8QhI2Yg0HLi9NdTgzIisR0+G5gj8nV7Uz8iT+f4=; b=7BpcQLW36ZH5fze5m2CW64qezajREg2orxdNtunJAFZNK9Soj3VSEd1Cy1Hj0bWzOU lH8xkw9qf172dJ1u0mR/4h1zfYVblHRU87ZM6ysa5HHWfN22BVeyoXTTQpT/vNwOB3os 0sAsw0C4ZCJyMbtN9qvJxsNTXWgv44m7I721tokPL4Ja9OLcjgbjgPwkIv5r5hqZpSYD Yxa6mofNKH3PDgcq21aSw6KZhI06Y+JE5/7vjc6nrMOwkzG5e0LK6eZlLnpJfJPUVokv PF+eQZvlbyVbe77iFYs2JjMIs/Iy4nq/q2gm5MFvFGTNq10wqJDHKYltwkBalfGgD9wg t6gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689816893; x=1690421693; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=i/BV8QhI2Yg0HLi9NdTgzIisR0+G5gj8nV7Uz8iT+f4=; b=DPS3HFv7ahulk6btWh/MPUhzWcyJ2xxdu1J7PRm1Wl9b1GDpx2gF7+o3txL/dXOTsH WV6uBw24Sf2QSuj9d5GUlDvAADHigEZP4hBnpm4ozS1huavEsRce9TRlIvq9SVE5k6Ju sXClY2C/mDvcBkSsszqdAyLQJawdzeHfdtzBrq9joDeg6YmRcjFBkmjQY8tUaxJPbGDu ZFCBJA3Jz9w8YJs/hbTzzv/Oxz4FlTMgDwueuFFsuohRa7YXdC43m4Sxc7TQqMCCyScK u8zkny1dOCgxdm5h37Nqf86BUcEvgJ4mMf28N3oryz2Iyj+rMH2fCJMIVyfm1uEf/J9y Wn8g== X-Gm-Message-State: ABy/qLapthWdk8JfcQs3isUk824ImDQd2877MuzRjRovHhQN0zBhp7He YQtmSTOqdTvs2TBob2oCrz1i1t4NSffjLHK2o07ZoA== X-Google-Smtp-Source: APBJJlFlK97JJPv1R+ptJjRZjdQ+0lWWDDmHsXoB1G/kVYpbWmwmp65vrqFWfnMSzidv3vR2GT5mIabA0FQ6WUPHl7Q= X-Received: by 2002:a81:4e8c:0:b0:57a:9384:4c13 with SMTP id c134-20020a814e8c000000b0057a93844c13mr4144906ywb.49.1689816893158; Wed, 19 Jul 2023 18:34:53 -0700 (PDT) MIME-Version: 1.0 References: <20230718004942.113174-1-mike.kravetz@oracle.com> <20230718004942.113174-2-mike.kravetz@oracle.com> In-Reply-To: <20230718004942.113174-2-mike.kravetz@oracle.com> From: Jiaqi Yan Date: Wed, 19 Jul 2023 18:34:40 -0700 Message-ID: Subject: Re: [PATCH v2 1/2] hugetlb: Do not clear hugetlb dtor until allocating vmemmap To: Mike Kravetz Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Naoya Horiguchi , Muchun Song , Miaohe Lin , Axel Rasmussen , James Houghton , Michal Hocko , Andrew Morton , stable@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 48BFC80006 X-Rspam-User: X-Stat-Signature: xateu1q63hjhwioi8q5wdngdtnpejzzq X-Rspamd-Server: rspam01 X-HE-Tag: 1689816894-737530 X-HE-Meta: U2FsdGVkX18t6X0VNWUPQutyoD09kD/VOL2sJR07qLTamEVWNmjHSG0/L6Lcl0kdPQktI15GgQn054+FwP2PxthLr8Xrs7z9gyQxGyyF6LV2WeJtxFkTPpL3NUwqZSu7i+ogeauj5FsTAJvZaoGM7EFhPA13HhUREgvgBKqHNCILD9Wrn09JawToAbWgZqJT1e46jAv2Gz1RWmnKI9XWMFnLcU/30N/llwOGg00wz3jedfsd5u49JK+jHlqYqMfqob4ErNSmwr3ueUSHSjS7pBkGepXtT5YzbO+HhHztX78Zqw2f9ehCod1pJPk85WTzc4hXzvAn59meaTSLa2SFlJlZ34s12rj7w2X/+YIPK5LH96lkV0eKQQqHm/TDVBnY8ET/GrBdKkc2NSFkogIB5GdTvBxkKYEQAvAImU5hwD3DzOU/PisiCUGzX8kSIOjxTLZMeX/FZ45kMNIjGGSlLmGw5vmSBOg+StEOfhDyxstMycFOVbQOlcBaoXBiKM8EYYrnS+qDnhU1LvNdeGOHEO5ZBqJaBVHdZhMktNdIwosb8Hzbo1zLD674/JWTpD3RyEaDwk0UVy9NlORmlqSBfPQbyt+jom4va7fh/g6OldzEFjb8ULn3DHmuri9f7gvDH2tzLT5H/Yt3LcTVusHZof2E1uyMS6+gJuAdXoM3qy9QitNn1PPf8v8vjU4uGTpZInpFCNrHvmXN4/SVycYEopxViwaiCuAuInhSHMxZ7wMlX+w9QUsK0sJgJi8NYCYsui//nrYTiXzOzrm/Ix6+Peqq0P+vBQnP2BZWN5GoiRcRwYwwhXcpjqDfNKELh1vCy9ma1oUi1xst8pwtyPkyDMt3KYZVr5TQnBTOaUmCYKPf/zdgy4ojaCCgnEPbbmKJj91PRMWi8CEE+7NQpp796Z8MxiW5yTQX84+yUwldW4432pPFUhZcRXCLtlO4mS8cz4Ibh9WVkZvKbXZvXaw I3ba3a/Q C9YOVnw6YeE/CRhHDH9KMHofEQU4tpfDySZjYpyMnUGGMCspiST0cWGVjXsTZqoFFBGxUyBnc3RDOdt2fYU1+dVy98utBRqAsYeXaEmLEtuqivFTRmPr8O8mKCLs++2buc3citOmTlQL9wYQ2OCKnRrCmTG31ukayPiXsxepRUdlkxv5kx8Zq3zYOPwOmc2ne6rickHsbhwmrMC5CmfIc8pbfv/19TsY2qku5Z+NczlGPB1b7jZL0cN+G6IX2NcyGKLmzpO4ah+jNSF7BshikkxPkzCEG5NXfKXUuYdjbGTkkb92IJLndTHs3bg== 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: On Mon, Jul 17, 2023 at 5:50=E2=80=AFPM 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 wit= h each HugeTLB page") > Cc: > Signed-off-by: Mike Kravetz > --- > mm/hugetlb.c | 90 ++++++++++++++++++++++++++++++++++++++-------------- > 1 file changed, 66 insertions(+), 24 deletions(-) > > diff --git a/mm/hugetlb.c b/mm/hugetlb.c > index 64a3239b6407..4a910121a647 100644 > --- a/mm/hugetlb.c > +++ b/mm/hugetlb.c > @@ -1579,9 +1579,37 @@ static inline void destroy_compound_gigantic_folio= (struct folio *folio, > unsigned int order) { } > #endif > > +static inline void __clear_hugetlb_destructor(struct hstate *h, > + struct folio *folio) > +{ > + lockdep_assert_held(&hugetlb_lock); > + > + /* > + * Very subtle > + * > + * For non-gigantic pages set the destructor to the normal compou= nd > + * page dtor. This is needed in case someone takes an additional > + * temporary ref to the page, and freeing is delayed until they d= rop > + * 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_folio will turn the folio into = a > + * simple group of pages. After this the destructor does not > + * apply. > + * > + */ > + if (hstate_is_gigantic(h)) > + folio_set_compound_dtor(folio, NULL_COMPOUND_DTOR); > + else > + folio_set_compound_dtor(folio, COMPOUND_PAGE_DTOR); > +} > + > /* > - * Remove hugetlb folio from lists, and update dtor so that the folio ap= pears > - * as just a compound page. > + * Remove hugetlb folio from lists. > + * If vmemmap exists for the folio, update dtor so that the folio appear= s > + * as just a compound page. Otherwise, wait until after allocating vmem= map > + * to update dtor. > * > * A reference is held on the folio, except in the case of demote. > * > @@ -1612,31 +1640,19 @@ static void __remove_hugetlb_folio(struct hstate = *h, struct folio *folio, > } > > /* > - * Very subtle > - * > - * For non-gigantic pages set the destructor to the normal compou= nd > - * page dtor. This is needed in case someone takes an additional > - * temporary ref to the page, and freeing is delayed until they d= rop > - * 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_folio will turn the folio 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_hugetlb_folio is called. > - * > - * In the case of demote we do not ref count the page as it will = soon > - * be turned into a page of smaller size. > + * We can only clear the hugetlb destructor after allocating vmem= map > + * pages. Otherwise, someone (memory error handling) may try to = write > + * to tail struct pages. > + */ > + if (!folio_test_hugetlb_vmemmap_optimized(folio)) > + __clear_hugetlb_destructor(h, folio); > + > + /* > + * In the case of demote we do not ref count the page as it will= soon > + * be turned into a page of smaller size. > */ > if (!demote) > folio_ref_unfreeze(folio, 1); > - if (hstate_is_gigantic(h)) > - folio_set_compound_dtor(folio, NULL_COMPOUND_DTOR); > - else > - folio_set_compound_dtor(folio, COMPOUND_PAGE_DTOR); > > h->nr_huge_pages--; > h->nr_huge_pages_node[nid]--; > @@ -1728,6 +1744,19 @@ static void __update_and_free_hugetlb_folio(struct= hstate *h, > return; > } > > + /* > + * If needed, clear hugetlb destructor under the hugetlb lock. > + * This must be done AFTER allocating vmemmap pages in case there= is an > + * attempt to write to tail struct pages as in memory poison. > + * It must be done BEFORE PageHWPoison handling so that any subse= quent > + * memory errors poison individual pages instead of head. > + */ > + if (folio_test_hugetlb(folio)) { Thanks Mike, this definitely fixed the issue in v1, :) > + spin_lock_irq(&hugetlb_lock); > + __clear_hugetlb_destructor(h, folio); > + spin_unlock_irq(&hugetlb_lock); > + } > + > /* > * Move PageHWPoison flag from head page to the raw error pages, > * which makes any healthy subpages reusable. > @@ -3604,6 +3633,19 @@ static int demote_free_hugetlb_folio(struct hstate= *h, struct folio *folio) > return rc; > } > > + /* > + * The hugetlb destructor could still be set for this folio if vm= emmap > + * was actually allocated above. The ref count on all pages is 0= . > + * Therefore, nobody should attempt access. However, before dest= roying > + * compound page below, clear the destructor. Unfortunately, this > + * requires a lock/unlock cycle. > + */ > + if (folio_test_hugetlb(folio)) { > + spin_lock_irq(&hugetlb_lock); > + __clear_hugetlb_destructor(h, folio); > + spin_unlock_irq(&hugetlb_lock); > + } > + > /* > * Use destroy_compound_hugetlb_folio_for_demote for all huge pag= e > * sizes as it will not ref count folios. > -- > 2.41.0 >