From patchwork Tue Apr 4 12:01:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Stevens X-Patchwork-Id: 13199707 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 229A7C6FD1D for ; Tue, 4 Apr 2023 12:01:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AFEAC6B0078; Tue, 4 Apr 2023 08:01:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AAE506B007D; Tue, 4 Apr 2023 08:01:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 94F226B007E; Tue, 4 Apr 2023 08:01:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 86FCF6B0078 for ; Tue, 4 Apr 2023 08:01:38 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 3DCEA1C5CF3 for ; Tue, 4 Apr 2023 12:01:38 +0000 (UTC) X-FDA: 80643569076.23.6709B82 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) by imf06.hostedemail.com (Postfix) with ESMTP id F0821180008 for ; Tue, 4 Apr 2023 12:01:35 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b="LpKJkwC/"; spf=pass (imf06.hostedemail.com: domain of stevensd@chromium.org designates 209.85.214.173 as permitted sender) smtp.mailfrom=stevensd@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680609696; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=XOz9ZvZBBEKBeMMkHNXogZGyfPp/Ut91O6nybFFIxig=; b=60dlecz6TBgcKz1U559Ey9Imt28+0mctm0tMe8O/iPJP0pCK/RThXZoO8vNQpv+oWGTLd9 F+ywB67XfiViCdyhPxVfYcdjKupQPbB1ygQbyFmgLkgvvXuT6EqAxqhXoKclALqV6y3gtU irQKb30xDXzj8deGMnbDJeGYxdzrSww= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b="LpKJkwC/"; spf=pass (imf06.hostedemail.com: domain of stevensd@chromium.org designates 209.85.214.173 as permitted sender) smtp.mailfrom=stevensd@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680609696; a=rsa-sha256; cv=none; b=7i2wvJXSN6UUffXfZovjcDAeY8CVGsJR1dcOPNSCrFnW5EoGQIcDW4YYbsKeX94aqi8tx7 Bvg9dvA2j1Ergx8/750UkJY0dV2D+ZdjplxSAZ/2vtmgY41tfnaC8X/FwY8Ym2CnWb/qgp C+AOgszONqaLIi6NBj15hAFGOcGfqXM= Received: by mail-pl1-f173.google.com with SMTP id le6so31014586plb.12 for ; Tue, 04 Apr 2023 05:01:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1680609694; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=XOz9ZvZBBEKBeMMkHNXogZGyfPp/Ut91O6nybFFIxig=; b=LpKJkwC/Xvom7WI58Uh3lqj2ua0zYirTPTwcp394etxw0rK3IwhkM96vdC6V3HusTq goPgMnjzSxw+XBSabm8XtESIEkMpOPmHzi9fwkbw9wzsV9CXLyzpw728M3cSAQEtzgO4 cG2js/CXkOtBJhMLfCxTPgx3KpeWs3kP21T+g= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680609694; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XOz9ZvZBBEKBeMMkHNXogZGyfPp/Ut91O6nybFFIxig=; b=2N/1zF0ab7XvaSTNw6mUdBSoc69RKdV9QmohgbdgZqxnVVJ2QnYbclSGPTulXgaGfx XoNmWjFQcd1WMya0FtrQ5LM8/NJuOo33tSZio0CdLjiMEceyKAId7qgBSXdVx4i9cawa WzpCzC8jv1ZnNfK/Vr9RLz/f+Ab7ydQa9auUA8VSgPiIrTihoT9fEnXCl69Kj2i4sHiL +L/d3amLouaYlllHYQ6EOu384ekts7Yta1d55Ykptqfyy2waZAE8PNY8QBnE8FzESA0z Qh6bAh0DE7/4m2NWTTAV+Tp3nawvOhvDkSixnExA1JSbQYmfQpykRzbDafKQXE8JlRC3 4foQ== X-Gm-Message-State: AAQBX9e+nD1QHWguC8hIRhGJhuzu+2DcOMVhoVnOTIgvUNLZz3t5ZKlm feM3xrwYraujhGMeVJW1PFIis0joTtxF20E3b+Q= X-Google-Smtp-Source: AKy350YMLghR1MHGUC8Rhnn9F5I2hBH6L2PbmBMQmf2fM+UAqA1gmblC6yVFeYDBpiXa/jdBNsdClg== X-Received: by 2002:a17:90b:1b06:b0:234:ba34:71bf with SMTP id nu6-20020a17090b1b0600b00234ba3471bfmr2848124pjb.1.1680609694233; Tue, 04 Apr 2023 05:01:34 -0700 (PDT) Received: from localhost ([2401:fa00:8f:203:959b:21ea:166b:c273]) by smtp.gmail.com with UTF8SMTPSA id s17-20020a63d051000000b0051322a5aa64sm7658703pgi.3.2023.04.04.05.01.31 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 04 Apr 2023 05:01:33 -0700 (PDT) From: David Stevens X-Google-Original-From: David Stevens To: linux-mm@kvack.org, Peter Xu , Hugh Dickins Cc: Andrew Morton , Matthew Wilcox , "Kirill A . Shutemov" , Yang Shi , David Hildenbrand , Jiaqi Yan , linux-kernel@vger.kernel.org, David Stevens Subject: [PATCH v6 2/4] mm/khugepaged: refactor collapse_file control flow Date: Tue, 4 Apr 2023 21:01:15 +0900 Message-Id: <20230404120117.2562166-3-stevensd@google.com> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog In-Reply-To: <20230404120117.2562166-1-stevensd@google.com> References: <20230404120117.2562166-1-stevensd@google.com> MIME-Version: 1.0 X-Stat-Signature: oua38phi9kp96s8u69c1paghndxi5ftm X-Rspam-User: X-Rspamd-Queue-Id: F0821180008 X-Rspamd-Server: rspam06 X-HE-Tag: 1680609695-520937 X-HE-Meta: U2FsdGVkX1+6qylEz1BwcbgVezI0d6Hn5sZL+hdU1fdXgKPJHuh/6lOWqr3AqjLqlOJ4BMxyQt+YVpDpzpoyuKA+Sm7ZwLreERocfKvzhwNMjvxT/108jFN2gOktfOrEdL2gFnEW+c7IwQ7OCe5HfwKcQeLUlCgbbvT3L+cZ//0+rlPLQSizF+O/9PVuMhgIc+8RR45RU1uZCKqbOagpR0Nn4VX0RZ2JyBXlMD8uiALODeszW4nEhEV8XcTf3xEj8qTy2zCQ6CNAfI0lKHOpnNPGszkGHJBS5IkiPKq77TkcfVjr1eGrw7FrfKKAPpBdnCcu8UUUo0tQ+q7mERRlTh3yxzEMtaOK13Vvpd/j8430s7IpNfrCiTiPdrs+2xBw44OEF2Lt1VWvJ0fWzr8LhqZnNOlVT1I0ILCkwplN7MRLLqxQH7NrLfsExD/loXdz4yVF0ukqKN3wZ1EMt/yW89qyAgsJ2i6YqF1qgcI2aZgRtKI2NOWa2wVjtOeRNYL9owMrt1KwkGgMXKRruxRZ29nPtUFitJk19A0kqVNQWinn5aKtfhBiGRPkRvmm31JmBCO6iBwUgYvLLOD3SbGv1Zov9tnOlUMVz6EdVJv5L88UusuDTnzvh9dFLNltEBbXCkio7wl1hlrWdM/AZFh2lkedr2fcPHcu5W+dKJNvhlT53PH4t5bDlQYFtOy5/MHbZXqEiy/A0oA1JZl9qmQ/Fb21VIY56hNiTKg73v9hUviW62zzNtrAHq3nV4HAuFYSjAFfXMvhS9NC5ALP8XCxK1Y0t0oOcDLP28x4jUcuaZFEab1AE0sqKCKSv/VEzFBkpm3nssP9W+3T4SH8tdCIjXYma1D1H3SiUI+25AiWZyYn/qaJ0gqFf5Hnf7fI3ESLonWRXlht+HY2grv3mrJO+ZawUhW47xloUozwWVAOmNvw5rV7pQ1HlSbpJ1LZOcPPaNjAyG/CmQjippJ75TL jg24iq+W tZ/PcRHNY8gls4bU4TMolk29MUDzvlkJFUxEpzpN/sjjTh7A8bCC6MTidZ+BOuU7xog2JhRg/X/GF6aWkN8oeDuhG8n+YY8XHVTCuCa+1uiyS0nlsmRNmoBPgh0BWFZt9x8w5w23nwGMzne0m5OFoYhysTHew2y1eO/exTz4aGUm9pZdIBcRt0Wy3cvi+qpGMe7qmdfJXFlt2YzQ3GjehQmX36siX+Im3tQ8Y30QKsF8pSbwE9IfwRvByo8DRgBIo1cF0rU0uPyfKyfRrczpArpNov/9H3iUWXkewgnZXYeAvfW24KiHDmpuirb4UJbZiBx1oLNwXoYjDiR06uqjcJYV9Y4GiGi2GaqKlYoS093UO5sRcUniatr2VkLEuel9x3x26B2ubKgaGsyB2sHoFP8J078vAOulnauUyhxmnuzAklG9reVLRRvSyBXRQzLOWeN9EO9Y4E6vSCth5ZhccYi6XxA== 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: From: David Stevens Add a rollback label to deal with failure, instead of continuously checking for RESULT_SUCCESS, to make it easier to add more failure cases. The refactoring also allows the collapse_file tracepoint to include hpage on success (instead of NULL). Signed-off-by: David Stevens Acked-by: Peter Xu Reviewed-by: Yang Shi Acked-by: Hugh Dickins --- mm/khugepaged.c | 230 ++++++++++++++++++++++++------------------------ 1 file changed, 113 insertions(+), 117 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 90577247cfaf..90828272a065 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1890,6 +1890,12 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, if (result != SCAN_SUCCEED) goto out; + __SetPageLocked(hpage); + if (is_shmem) + __SetPageSwapBacked(hpage); + hpage->index = start; + hpage->mapping = mapping; + /* * Ensure we have slots for all the pages in the range. This is * almost certainly a no-op because most of the pages must be present @@ -1902,16 +1908,10 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, xas_unlock_irq(&xas); if (!xas_nomem(&xas, GFP_KERNEL)) { result = SCAN_FAIL; - goto out; + goto rollback; } } while (1); - __SetPageLocked(hpage); - if (is_shmem) - __SetPageSwapBacked(hpage); - hpage->index = start; - hpage->mapping = mapping; - /* * At this point the hpage is locked and not up-to-date. * It's safe to insert it into the page cache, because nobody would @@ -2137,137 +2137,133 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, */ try_to_unmap_flush(); - if (result == SCAN_SUCCEED) { - /* - * Replacing old pages with new one has succeeded, now we - * attempt to copy the contents. - */ - index = start; - list_for_each_entry(page, &pagelist, lru) { - while (index < page->index) { - clear_highpage(hpage + (index % HPAGE_PMD_NR)); - index++; - } - if (copy_mc_highpage(hpage + (page->index % HPAGE_PMD_NR), - page) > 0) { - result = SCAN_COPY_MC; - break; - } - index++; - } - while (result == SCAN_SUCCEED && index < end) { + if (result != SCAN_SUCCEED) + goto rollback; + + /* + * Replacing old pages with new one has succeeded, now we + * attempt to copy the contents. + */ + index = start; + list_for_each_entry(page, &pagelist, lru) { + while (index < page->index) { clear_highpage(hpage + (index % HPAGE_PMD_NR)); index++; } + if (copy_mc_highpage(hpage + (page->index % HPAGE_PMD_NR), page) > 0) { + result = SCAN_COPY_MC; + goto rollback; + } + index++; + } + while (index < end) { + clear_highpage(hpage + (index % HPAGE_PMD_NR)); + index++; + } + + /* + * Copying old pages to huge one has succeeded, now we + * need to free the old pages. + */ + list_for_each_entry_safe(page, tmp, &pagelist, lru) { + list_del(&page->lru); + page->mapping = NULL; + page_ref_unfreeze(page, 1); + ClearPageActive(page); + ClearPageUnevictable(page); + unlock_page(page); + put_page(page); } nr = thp_nr_pages(hpage); - if (result == SCAN_SUCCEED) { - /* - * Copying old pages to huge one has succeeded, now we - * need to free the old pages. - */ - list_for_each_entry_safe(page, tmp, &pagelist, lru) { - list_del(&page->lru); - page->mapping = NULL; - page_ref_unfreeze(page, 1); - ClearPageActive(page); - ClearPageUnevictable(page); - unlock_page(page); - put_page(page); - } + xas_lock_irq(&xas); + if (is_shmem) + __mod_lruvec_page_state(hpage, NR_SHMEM_THPS, nr); + else + __mod_lruvec_page_state(hpage, NR_FILE_THPS, nr); - xas_lock_irq(&xas); - if (is_shmem) - __mod_lruvec_page_state(hpage, NR_SHMEM_THPS, nr); - else - __mod_lruvec_page_state(hpage, NR_FILE_THPS, nr); + if (nr_none) { + __mod_lruvec_page_state(hpage, NR_FILE_PAGES, nr_none); + /* nr_none is always 0 for non-shmem. */ + __mod_lruvec_page_state(hpage, NR_SHMEM, nr_none); + } + /* Join all the small entries into a single multi-index entry. */ + xas_set_order(&xas, start, HPAGE_PMD_ORDER); + xas_store(&xas, hpage); + xas_unlock_irq(&xas); - if (nr_none) { - __mod_lruvec_page_state(hpage, NR_FILE_PAGES, nr_none); - /* nr_none is always 0 for non-shmem. */ - __mod_lruvec_page_state(hpage, NR_SHMEM, nr_none); - } - /* Join all the small entries into a single multi-index entry. */ - xas_set_order(&xas, start, HPAGE_PMD_ORDER); - xas_store(&xas, hpage); - xas_unlock_irq(&xas); + folio = page_folio(hpage); + folio_mark_uptodate(folio); + folio_ref_add(folio, HPAGE_PMD_NR - 1); - folio = page_folio(hpage); - folio_mark_uptodate(folio); - folio_ref_add(folio, HPAGE_PMD_NR - 1); + if (is_shmem) + folio_mark_dirty(folio); + folio_add_lru(folio); - if (is_shmem) - folio_mark_dirty(folio); - folio_add_lru(folio); + /* + * Remove pte page tables, so we can re-fault the page as huge. + */ + result = retract_page_tables(mapping, start, mm, addr, hpage, + cc); + unlock_page(hpage); + goto out; + +rollback: + /* Something went wrong: roll back page cache changes */ + xas_lock_irq(&xas); + if (nr_none) { + mapping->nrpages -= nr_none; + shmem_uncharge(mapping->host, nr_none); + } - /* - * Remove pte page tables, so we can re-fault the page as huge. - */ - result = retract_page_tables(mapping, start, mm, addr, hpage, - cc); - unlock_page(hpage); - hpage = NULL; - } else { - /* Something went wrong: roll back page cache changes */ - xas_lock_irq(&xas); - if (nr_none) { - mapping->nrpages -= nr_none; - shmem_uncharge(mapping->host, nr_none); + xas_set(&xas, start); + xas_for_each(&xas, page, end - 1) { + page = list_first_entry_or_null(&pagelist, + struct page, lru); + if (!page || xas.xa_index < page->index) { + if (!nr_none) + break; + nr_none--; + /* Put holes back where they were */ + xas_store(&xas, NULL); + continue; } - xas_set(&xas, start); - xas_for_each(&xas, page, end - 1) { - page = list_first_entry_or_null(&pagelist, - struct page, lru); - if (!page || xas.xa_index < page->index) { - if (!nr_none) - break; - nr_none--; - /* Put holes back where they were */ - xas_store(&xas, NULL); - continue; - } + VM_BUG_ON_PAGE(page->index != xas.xa_index, page); - VM_BUG_ON_PAGE(page->index != xas.xa_index, page); - - /* Unfreeze the page. */ - list_del(&page->lru); - page_ref_unfreeze(page, 2); - xas_store(&xas, page); - xas_pause(&xas); - xas_unlock_irq(&xas); - unlock_page(page); - putback_lru_page(page); - xas_lock_irq(&xas); - } - VM_BUG_ON(nr_none); + /* Unfreeze the page. */ + list_del(&page->lru); + page_ref_unfreeze(page, 2); + xas_store(&xas, page); + xas_pause(&xas); + xas_unlock_irq(&xas); + unlock_page(page); + putback_lru_page(page); + xas_lock_irq(&xas); + } + VM_BUG_ON(nr_none); + /* + * Undo the updates of filemap_nr_thps_inc for non-SHMEM + * file only. This undo is not needed unless failure is + * due to SCAN_COPY_MC. + */ + if (!is_shmem && result == SCAN_COPY_MC) { + filemap_nr_thps_dec(mapping); /* - * Undo the updates of filemap_nr_thps_inc for non-SHMEM - * file only. This undo is not needed unless failure is - * due to SCAN_COPY_MC. + * Paired with smp_mb() in do_dentry_open() to + * ensure the update to nr_thps is visible. */ - if (!is_shmem && result == SCAN_COPY_MC) { - filemap_nr_thps_dec(mapping); - /* - * Paired with smp_mb() in do_dentry_open() to - * ensure the update to nr_thps is visible. - */ - smp_mb(); - } + smp_mb(); + } - xas_unlock_irq(&xas); + xas_unlock_irq(&xas); - hpage->mapping = NULL; - } + hpage->mapping = NULL; - if (hpage) - unlock_page(hpage); + unlock_page(hpage); + put_page(hpage); out: VM_BUG_ON(!list_empty(&pagelist)); - if (hpage) - put_page(hpage); - trace_mm_khugepaged_collapse_file(mm, hpage, index, is_shmem, addr, file, nr, result); return result; }