From patchwork Tue Mar 7 05:20:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Stevens X-Patchwork-Id: 13162795 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 714B1C678DB for ; Tue, 7 Mar 2023 05:20:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EC5156B0072; Tue, 7 Mar 2023 00:20:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E72496B0073; Tue, 7 Mar 2023 00:20:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D3A396B0074; Tue, 7 Mar 2023 00:20:56 -0500 (EST) 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 C568F6B0072 for ; Tue, 7 Mar 2023 00:20:56 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 9D28780B91 for ; Tue, 7 Mar 2023 05:20:56 +0000 (UTC) X-FDA: 80540952912.22.FA3A179 Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) by imf25.hostedemail.com (Postfix) with ESMTP id A340AA0007 for ; Tue, 7 Mar 2023 05:20:54 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=CMO72NJk; spf=pass (imf25.hostedemail.com: domain of stevensd@chromium.org designates 209.85.215.181 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=1678166454; 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=xQ8JLh1t+SQa8MFtRCjvqWkhYIqZz1dASMHutsA+ZIA=; b=hrVTlNGzuD3sq8JE+3tKb7E75zaumuw46QD4WMCLMWx2zJhyUiL/HTDtBlE8LEgDXoZD+d PhZzI+3Y1aDcFQ2Bz29UVPGVMY80//JscPu/HZbdWkV3s5Itt5n8GNHEcCVA0mxBsCkbf+ 6r3UOVb5Us/wCZzVHI9VB+tyxiHWdIk= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=CMO72NJk; spf=pass (imf25.hostedemail.com: domain of stevensd@chromium.org designates 209.85.215.181 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=1678166454; a=rsa-sha256; cv=none; b=UL+7BLMH1iSZ+z0oUof9rKy1S9MuHVv+OthS8pEX2fRPK33BM+9/LvtXvmXL6mfqzIfk3x b5Nd4mn+MyZmtaMsfdxNWCVjvrU2FMS/TLrO7s1O3r6JYIE/vTuBGjVRelRrkfEwLbWRdj c5nulo64C0CRedmZZca2yDRt5TaD4Ak= Received: by mail-pg1-f181.google.com with SMTP id bn17so6908663pgb.10 for ; Mon, 06 Mar 2023 21:20:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678166453; 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=xQ8JLh1t+SQa8MFtRCjvqWkhYIqZz1dASMHutsA+ZIA=; b=CMO72NJkW0Gjo9VKTtSKmbPwn1x80TIoUa7QDwO/WtPJ3SB4S9uE4uELHTnI6cLvRr 3/+EAcw807ziz5ELFf3PmVbEwRg4I0QT0bKP84kgt9Zew2qiUWL34pwhrz3umv9if16g EGPNFHXrFVzRkYWfFwOHqdM1K875EV6Hcx8KQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678166453; 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=xQ8JLh1t+SQa8MFtRCjvqWkhYIqZz1dASMHutsA+ZIA=; b=rlL1NG5YznE/4Jy/849QxCpDIZ9w4DEd1PjnNqezyIxz2MlBMKbxt5Yl6bb0bps5Ph aoP3v3KXN50MrNPyDNEi0BM+3liXCWBK/J3sJMqXp8i/KRyEZClJNimUw1V0B6CguhZU k3SIrRkh86RIy+CD2uyj3Y392iB8nlWO2qYlglmzGYPuKzdvERIXfb2K61XFr5O/dK5N Qs3JMjlgWdnELUn9UQyCdh0t9/U/LRqHjnt5OATelXORIzveZWET4rOb2tNOP/IZv+x0 mCv6qJhm2OKY73o62a4sIuUHDZNQPEeueg0DtCIVgEk4QdJla74TGn/h0zItNdpn9rMO zq9Q== X-Gm-Message-State: AO0yUKU9agIx4i9uhhqMvVPnIVxR8EdBt0CecCo9ZImKygCmpGh0mIqm Sw46kE39X6geBqEh94aNFa/akMYDnEeKjq9rIJQ= X-Google-Smtp-Source: AK7set/WWK3FBlMA7qczOE6tpe9xjw7BHBGdOTgOWHL2+jS1vRIuoH1POzid1TxtaatIr6JYfANBWQ== X-Received: by 2002:a62:1ad4:0:b0:5a8:d407:60f9 with SMTP id a203-20020a621ad4000000b005a8d40760f9mr10448268pfa.29.1678166453057; Mon, 06 Mar 2023 21:20:53 -0800 (PST) Received: from localhost ([2401:fa00:8f:203:1f73:9034:ce28:4421]) by smtp.gmail.com with UTF8SMTPSA id a4-20020aa78644000000b005a8ba70315bsm7096096pfo.6.2023.03.06.21.20.50 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 06 Mar 2023 21:20:52 -0800 (PST) From: David Stevens X-Google-Original-From: David Stevens To: linux-mm@kvack.org, Andrew Morton Cc: Peter Xu , Matthew Wilcox , "Kirill A . Shutemov" , Yang Shi , David Hildenbrand , Hugh Dickins , Jiaqi Yan , linux-kernel@vger.kernel.org, David Stevens Subject: [PATCH v5 1/3] mm/khugepaged: refactor collapse_file control flow Date: Tue, 7 Mar 2023 14:20:34 +0900 Message-Id: <20230307052036.1520708-2-stevensd@google.com> X-Mailer: git-send-email 2.40.0.rc0.216.gc4246ad0f0-goog In-Reply-To: <20230307052036.1520708-1-stevensd@google.com> References: <20230307052036.1520708-1-stevensd@google.com> MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: A340AA0007 X-Stat-Signature: iqx8i7exaac3iy5c975cjm9mrx8hft7i X-Rspam-User: X-HE-Tag: 1678166454-248328 X-HE-Meta: U2FsdGVkX1+++9R2W5xxLPryE6UR1e2ZBYi43naJCtL15LHu0StR9Mpl6NVMtZ78tYzrqnQNNNvQe/S2dEp/COIiGfW8LaUpProcH5UGeptmxCIsP3Gx/TVbGJIj8uZOt/kFxQ5siO6pcYv8eZ+AVTOIP+0k9f3F7lnCSXpKzXnlV7f5aj9cYjCHop004XazSA8UzWO6qZhFfXiyqPr4Ry8+qgDNT2pVgE2T2ysisTJF/SdArxfPonJ9n5dUSE0qb2awPGG0UOwiSLiANgWWXEnSQ3dG2nnso2+aAV74n4eT2xnB7DhWeauqAQdQ6QiZF3QQW4IIZl0I20dUQVdEZZkJbmDWrpASUxsVhDbkbBLJPY019ExysZBnt8NeGgnGLZD33VPHmmrjozkrueAEWbqKPST+fiG+QVvb0dj5+fAXp0hgT5YQMK5RhTbnANUPdyeNMyrzEAUkt6Eb91NuAptjXGLgRC521o5ulDnmU0WEuR0PzAZMQRn1tseHry1zrishvgeL5xb0FoJIOVrbyuKPcZjCY4wZjLQW+2O6mhRFliJjOzjayflggyW9DoFd8epoC43GsD+4TpO34ehm2Uor8tjhYBvQMr1pmMM68CKsCZQN7FYo6DJ8IrP5ZB/LTbApJwDYz+zT3RnieXO1qhu0DwVm1rZuGFhhFjS+TYfu5zMljb0QH7Za69f2pmjx2fl1AQyKpZFNvQrdRGsRgAv7muU7Rel+Hk94+PZDeLWAPk8ckEZtLEOfK92eMUl854MeT1wityAhpg0JNhbIKwofXjdg5sA1kq8m1EC2yRLpDCmuJXrVldO1CNuIPZO+87X0kcGpIMt4MtsfGcJIfbCZUeEai57f5HWypXT0mleIlv8J/nXSSvTw/cOMkUCHZBN8TvrY3wa876WAP86v8hKaVdrifhdCsyQRXjg4VDZWVykA5YXV0cUT7j6Rytz8ioicrsbFIDxENpwucGj A+jZas5V 4LSArVfyl2aswKbNt1H89C+y3n9th5qgQnoXMUoctqhvXBvmKQOr+rQOvE9YMQ6w+7Abn/71oP3tGR2kCsojXhJhsKeXu5MU8+8EKMWhUqSDscH6FVQrtbTKb9oOtoPRL6mlFWHbxAVIepDFa4lYRJoJZRSo5Bnkw2w/9lFwmkGenNGsJ7jni2ZZIqIWXBO8Y4TQ0O7OZofj6RzyElr1dtckQ8u6Bz9fL2zXpGcT0JOcGOkZk9zMak9sx3GetXh65QCwXypBdgmqTWAtaBUL+5B9ekwRcK+QYEy7RaMiO+D8MDlYgzgesyBSTkNNr6BnKDeeL50OcpRdNCpggbJOrZeYM/Ng2D+1kN2PKQoaSgCTSefqctaw63TqGcVrmiRhm7P+a6LhtAgOaMNNriwAr5AE20kgWepq7RyJxKS6Y/Ye7zXr8mSvcjeTDBR+fhfD9CDi8 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 | 219 ++++++++++++++++++++++++------------------------ 1 file changed, 108 insertions(+), 111 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 3ea2aa55c2c5..b954e3c685e7 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1907,6 +1907,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 @@ -1919,16 +1925,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 @@ -2145,129 +2145,126 @@ 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++; } - 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); - } + /* + * 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); - 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); + /* + * 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); + } + + 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); + /* + * 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); - /* 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); + xas_unlock_irq(&xas); - xas_unlock_irq(&xas); + hpage->mapping = NULL; - hpage->mapping = NULL; - } + unlock_page(hpage); + put_page(hpage); - if (hpage) - unlock_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; }