From nobody Wed Feb 11 06:53:01 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8CB3DC6FD1D for ; Tue, 4 Apr 2023 12:01:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234564AbjDDMBf (ORCPT ); Tue, 4 Apr 2023 08:01:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41490 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234530AbjDDMBa (ORCPT ); Tue, 4 Apr 2023 08:01:30 -0400 Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C374335BD for ; Tue, 4 Apr 2023 05:01:29 -0700 (PDT) Received: by mail-pj1-x102b.google.com with SMTP id gp15-20020a17090adf0f00b0023d1bbd9f9eso35910225pjb.0 for ; Tue, 04 Apr 2023 05:01:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1680609689; 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=BXv1GcIP0b01WCB0dxAHE5qtL4a0jd48WmxV6aMJGEI=; b=GhUPSo9sh6Zwwfo4h+KoyiwNBesV7G+G8J1L0pQX0JIU2wZv09kHmO7svPlTwq7tWY W0Wc6i0y6qoBqfdiEgvioqzZqSn479UQH0LZb+QYhGBOawXvCQgAYwZy+crfeg95NXWc /4gHmZ1MbS8u0pbiY4snCi0+AH22BTnSmnjRM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680609689; 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=BXv1GcIP0b01WCB0dxAHE5qtL4a0jd48WmxV6aMJGEI=; b=0f1tej/pgTWeO228olyOCajS/CUl4dCVzmlxRTz0MCGomPNF+F0h3S3By41HYaS5Sx Zp0HLaSk3mmszvNEwpsT6x865eGeLayoVm8VhipmpyWU/WKBPRT0xjvhOyAg4yt5eNLU wl29EbLki7y/sArY0WyfZdMoEVcOfN/aq7bwTKXXnLcIcxCGWZ77YXNs+tyTlTmALKKI wEVtrGchhhaaDv4+23y2o3sR/7ZpuRjtgWupRFoDpzMY5o94EsNWdI17olcW4/eYMe5K +D/bWTp+bXvCFntNWXsNGohMjTuTeOLJzNkBhALsPnEPef/HdSz8xnst/Pz8P8dr19NT nGlw== X-Gm-Message-State: AAQBX9dVhtuCjkscNNl1cbAxgxLGpZpSpIgOtm3ACB6wYMn+N92n4jJw Yf5JhOcVbix/bHPBqFdId5zJcQ== X-Google-Smtp-Source: AKy350asjKP3U0+twDNYb29W50NssXzM9KfQVg707hZ1pElnupMPu2X9cFoRAu3bB4exQZa/RnwHDg== X-Received: by 2002:a17:903:32c2:b0:1a2:98b1:1ee2 with SMTP id i2-20020a17090332c200b001a298b11ee2mr3128683plr.15.1680609689270; Tue, 04 Apr 2023 05:01:29 -0700 (PDT) Received: from localhost ([2401:fa00:8f:203:959b:21ea:166b:c273]) by smtp.gmail.com with UTF8SMTPSA id l2-20020a17090aec0200b0022335f1dae2sm7834214pjy.22.2023.04.04.05.01.26 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 04 Apr 2023 05:01:28 -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 1/4] mm/khugepaged: drain lru after swapping in shmem Date: Tue, 4 Apr 2023 21:01:14 +0900 Message-Id: <20230404120117.2562166-2-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 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: David Stevens Call lru_add_drain after swapping in shmem pages so that isolate_lru_page is more likely to succeed. Signed-off-by: David Stevens --- mm/khugepaged.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 666d2c4e38dd..90577247cfaf 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1963,6 +1963,8 @@ static int collapse_file(struct mm_struct *mm, unsign= ed long addr, result =3D SCAN_FAIL; goto xa_unlocked; } + /* drain pagevecs to help isolate_lru_page() */ + lru_add_drain(); page =3D folio_file_page(folio, index); } else if (trylock_page(page)) { get_page(page); --=20 2.40.0.348.gf938b09366-goog From nobody Wed Feb 11 06:53:01 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B2C71C6FD1D for ; Tue, 4 Apr 2023 12:01:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234591AbjDDMBt (ORCPT ); Tue, 4 Apr 2023 08:01:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41782 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234625AbjDDMBk (ORCPT ); Tue, 4 Apr 2023 08:01:40 -0400 Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 11FA130DB for ; Tue, 4 Apr 2023 05:01:35 -0700 (PDT) Received: by mail-pl1-x631.google.com with SMTP id u10so31030247plz.7 for ; Tue, 04 Apr 2023 05:01:34 -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=ZlZV6ezLBA86pNN2Ofae+VxGBKKK3TtnR43ADCKqp/fqWuIwPTmFvY8qhBgYG/7uoA ywT9lMQ0VAutcLBflRUtwP03AUl9xD0iXvCX94FD92m0Px0WnX078gy1U95EqX1+UEk7 zgApDwMhsElGEuEM6CtV2oKPh9q47bqHaomhIZv8WmRRBKF4EvPMxP0DRAKHXm8FBIt4 lyP0Y7z6W+SdXUvgp14QoUrMvJBGV2Ezbd6pRIOJwvNHkBflYEfNJgLs605qXKzkQ0+f 7KkgoL5pgJhByy22ZyiQMjGxU8KX82NE6RzDUOoY6jiP9cIs5++F9/MNFrc5zgqtFvDt 1Wiw== X-Gm-Message-State: AAQBX9fZdCneuJy0buQHuKkLvoibKiRWclZA8zcbK4fqvG3EhJPonYkl Zw1RSTmS09WbjqiN/gkTn8USZg== 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 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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, unsig= ned long addr, if (result !=3D SCAN_SUCCEED) goto out; =20 + __SetPageLocked(hpage); + if (is_shmem) + __SetPageSwapBacked(hpage); + hpage->index =3D start; + hpage->mapping =3D 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, unsi= gned long addr, xas_unlock_irq(&xas); if (!xas_nomem(&xas, GFP_KERNEL)) { result =3D SCAN_FAIL; - goto out; + goto rollback; } } while (1); =20 - __SetPageLocked(hpage); - if (is_shmem) - __SetPageSwapBacked(hpage); - hpage->index =3D start; - hpage->mapping =3D 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, un= signed long addr, */ try_to_unmap_flush(); =20 - if (result =3D=3D SCAN_SUCCEED) { - /* - * Replacing old pages with new one has succeeded, now we - * attempt to copy the contents. - */ - index =3D 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 =3D SCAN_COPY_MC; - break; - } - index++; - } - while (result =3D=3D SCAN_SUCCEED && index < end) { + if (result !=3D SCAN_SUCCEED) + goto rollback; + + /* + * Replacing old pages with new one has succeeded, now we + * attempt to copy the contents. + */ + index =3D 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 =3D 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 =3D NULL; + page_ref_unfreeze(page, 1); + ClearPageActive(page); + ClearPageUnevictable(page); + unlock_page(page); + put_page(page); } =20 nr =3D thp_nr_pages(hpage); - if (result =3D=3D 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 =3D 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); =20 - 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); =20 - 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 =3D page_folio(hpage); + folio_mark_uptodate(folio); + folio_ref_add(folio, HPAGE_PMD_NR - 1); =20 - folio =3D 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); =20 - 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 =3D 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 -=3D nr_none; + shmem_uncharge(mapping->host, nr_none); + } =20 - /* - * Remove pte page tables, so we can re-fault the page as huge. - */ - result =3D retract_page_tables(mapping, start, mm, addr, hpage, - cc); - unlock_page(hpage); - hpage =3D NULL; - } else { - /* Something went wrong: roll back page cache changes */ - xas_lock_irq(&xas); - if (nr_none) { - mapping->nrpages -=3D nr_none; - shmem_uncharge(mapping->host, nr_none); + xas_set(&xas, start); + xas_for_each(&xas, page, end - 1) { + page =3D 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; } =20 - xas_set(&xas, start); - xas_for_each(&xas, page, end - 1) { - page =3D 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 !=3D xas.xa_index, page); =20 - VM_BUG_ON_PAGE(page->index !=3D 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 =3D=3D 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 =3D=3D 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(); + } =20 - xas_unlock_irq(&xas); + xas_unlock_irq(&xas); =20 - hpage->mapping =3D NULL; - } + hpage->mapping =3D NULL; =20 - 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; } --=20 2.40.0.348.gf938b09366-goog From nobody Wed Feb 11 06:53:01 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DCC40C761A6 for ; Tue, 4 Apr 2023 12:01:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234736AbjDDMBz (ORCPT ); Tue, 4 Apr 2023 08:01:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41844 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234680AbjDDMBs (ORCPT ); Tue, 4 Apr 2023 08:01:48 -0400 Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4035435B5 for ; Tue, 4 Apr 2023 05:01:39 -0700 (PDT) Received: by mail-pl1-x62e.google.com with SMTP id le6so31014756plb.12 for ; Tue, 04 Apr 2023 05:01:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1680609698; 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=4qUt+bgMbpOhTRgQhL+AHDV3zlnHqzmbomM5Wqo2t8M=; b=gGkyBqM897kTx3mCEJLjxur+giZZePN0hsSQZRggvEHfuxcLZ/xAHZn7d/zocZ+B/t 1cIzL09rih7OgQJFA0TJpSLo18rTYMeEnbExDfttmiQbIW+B8qdcJ1V6KdSL3C2CVoIk uhn8K/3B8O1z7wv+u/gk9GUet3CqJe2QvDgOU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680609698; 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=4qUt+bgMbpOhTRgQhL+AHDV3zlnHqzmbomM5Wqo2t8M=; b=LafoohDVQcxP7rs9TkF8HOCOp94SQVin3vg/ntn4KKIC/o+NhVb1q2aqn8r1S3VlWO 9jqMyPJ2FNfgVRIDywshE4DgCkltxlp/H7J6QYrXxUqS9N5mccaFXuiFQPWgp5l2SPMe vA+kXgaZBnxvab8IgP2ynzhjgpxGXlH5weh33MgIHRygWaZZyP/h0KxeRCaJsF1VGsdn 3GWqZPv5J/uNBuEw1ps6zvh57/ZfH//2lBX8hM32c03NWDNanDv/oFnQx6qdZaRdo/Y5 bVoDsFfh4Sd10RwGkjW6BHrTNjQ/tBjO+tbzgF4q5yAfIlOelOqvRWA7s01O0xwQgINw 8AQw== X-Gm-Message-State: AAQBX9diAY+Mw2u3HXjRcrxipi1R5SHcGv9jpBaaT8Zqs4nPCM3kko+X WGFcvxhubKXEpf1JO6hdivf3rA== X-Google-Smtp-Source: AKy350YJmn368SsJA0s1UjNOINFx3s/OTYEKafflR1j6AXeAzEBTEXX+E3A7d0NoY4XJPEiZqkDcUw== X-Received: by 2002:a17:90b:1a86:b0:23f:7c82:2463 with SMTP id ng6-20020a17090b1a8600b0023f7c822463mr2874580pjb.9.1680609698541; Tue, 04 Apr 2023 05:01:38 -0700 (PDT) Received: from localhost ([2401:fa00:8f:203:959b:21ea:166b:c273]) by smtp.gmail.com with UTF8SMTPSA id q3-20020a17090a938300b0023b15e61f07sm7879485pjo.12.2023.04.04.05.01.35 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 04 Apr 2023 05:01:38 -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 3/4] mm/khugepaged: skip shmem with userfaultfd Date: Tue, 4 Apr 2023 21:01:16 +0900 Message-Id: <20230404120117.2562166-4-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 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: David Stevens Make sure that collapse_file respects any userfaultfds registered with MODE_MISSING. If userspace has any such userfaultfds registered, then for any page which it knows to be missing, it may expect a UFFD_EVENT_PAGEFAULT. This means collapse_file needs to be careful when collapsing a shmem range would result in replacing an empty page with a THP, to avoid breaking userfaultfd. Synchronization when checking for userfaultfds in collapse_file is tricky because the mmap locks can't be used to prevent races with the registration of new userfaultfds. Instead, we provide synchronization by ensuring that userspace cannot observe the fact that pages are missing before we check for userfaultfds. Although this allows registration of a userfaultfd to race with collapse_file, it ensures that userspace cannot observe any pages transition from missing to present after such a race occurs. This makes such a race indistinguishable to the collapse occurring immediately before the userfaultfd registration. The first step to provide this synchronization is to stop filling gaps during the loop iterating over the target range, since the page cache lock can be dropped during that loop. The second step is to fill the gaps with XA_RETRY_ENTRY after the page cache lock is acquired the final time, to avoid races with accesses to the page cache that only take the RCU read lock. The fact that we don't fill holes during the initial iteration means that collapse_file now has to handle faults occurring during the collapse. This is done by re-validating the number of missing pages after acquiring the page cache lock for the final time. This fix is targeted at khugepaged, but the change also applies to MADV_COLLAPSE. MADV_COLLAPSE on a range with a userfaultfd will now return EBUSY if there are any missing pages (instead of succeeding on shmem and returning EINVAL on anonymous memory). There is also now a window during MADV_COLLAPSE where a fault on a missing page will cause the syscall to fail with EAGAIN. The fact that intermediate page cache state can no longer be observed before the rollback of a failed collapse is also technically a userspace-visible change (via at least SEEK_DATA and SEEK_END), but it is exceedingly unlikely that anything relies on being able to observe that transient state. Signed-off-by: David Stevens Acked-by: Peter Xu --- include/trace/events/huge_memory.h | 3 +- mm/khugepaged.c | 109 +++++++++++++++++++++-------- 2 files changed, 81 insertions(+), 31 deletions(-) diff --git a/include/trace/events/huge_memory.h b/include/trace/events/huge= _memory.h index eca4c6f3625e..877cbf9fd2ec 100644 --- a/include/trace/events/huge_memory.h +++ b/include/trace/events/huge_memory.h @@ -38,7 +38,8 @@ EM( SCAN_TRUNCATED, "truncated") \ EM( SCAN_PAGE_HAS_PRIVATE, "page_has_private") \ EM( SCAN_STORE_FAILED, "store_failed") \ - EMe(SCAN_COPY_MC, "copy_poisoned_page") + EM( SCAN_COPY_MC, "copy_poisoned_page") \ + EMe(SCAN_PAGE_FILLED, "page_filled") \ =20 #undef EM #undef EMe diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 90828272a065..7679551e9540 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -57,6 +57,7 @@ enum scan_result { SCAN_PAGE_HAS_PRIVATE, SCAN_COPY_MC, SCAN_STORE_FAILED, + SCAN_PAGE_FILLED, }; =20 #define CREATE_TRACE_POINTS @@ -1856,8 +1857,8 @@ static int retract_page_tables(struct address_space *= mapping, pgoff_t pgoff, * - allocate and lock a new huge page; * - scan page cache replacing old pages with the new one * + swap/gup in pages if necessary; - * + fill in gaps; * + keep old pages around in case rollback is required; + * - finalize updates to the page cache; * - if replacing succeeds: * + copy data over; * + free old pages; @@ -1935,22 +1936,12 @@ static int collapse_file(struct mm_struct *mm, unsi= gned long addr, result =3D SCAN_TRUNCATED; goto xa_locked; } - xas_set(&xas, index); + xas_set(&xas, index + 1); } if (!shmem_charge(mapping->host, 1)) { result =3D SCAN_FAIL; goto xa_locked; } - xas_store(&xas, hpage); - if (xas_error(&xas)) { - /* revert shmem_charge performed - * in the previous condition - */ - mapping->nrpages--; - shmem_uncharge(mapping->host, 1); - result =3D SCAN_STORE_FAILED; - goto xa_locked; - } nr_none++; continue; } @@ -2161,22 +2152,66 @@ static int collapse_file(struct mm_struct *mm, unsi= gned long addr, index++; } =20 - /* - * 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 =3D NULL; - page_ref_unfreeze(page, 1); - ClearPageActive(page); - ClearPageUnevictable(page); - unlock_page(page); - put_page(page); + if (nr_none) { + struct vm_area_struct *vma; + int nr_none_check =3D 0; + + i_mmap_lock_read(mapping); + xas_lock_irq(&xas); + + xas_set(&xas, start); + for (index =3D start; index < end; index++) { + if (!xas_next(&xas)) { + xas_store(&xas, XA_RETRY_ENTRY); + if (xas_error(&xas)) { + result =3D SCAN_STORE_FAILED; + goto immap_locked; + } + nr_none_check++; + } + } + + if (nr_none !=3D nr_none_check) { + result =3D SCAN_PAGE_FILLED; + goto immap_locked; + } + + /* + * If userspace observed a missing page in a VMA with a MODE_MISSING + * userfaultfd, then it might expect a UFFD_EVENT_PAGEFAULT for that + * page. If so, we need to roll back to avoid suppressing such an + * event. Since wp/minor userfaultfds don't give userspace any + * guarantees that the kernel doesn't fill a missing page with a zero + * page, so they don't matter here. + * + * Any userfaultfds registered after this point will not be able to + * observe any missing pages due to the previously inserted retry + * entries. + */ + vma_interval_tree_foreach(vma, &mapping->i_mmap, start, end) { + if (userfaultfd_missing(vma)) { + result =3D SCAN_EXCEED_NONE_PTE; + goto immap_locked; + } + } + +immap_locked: + i_mmap_unlock_read(mapping); + if (result !=3D SCAN_SUCCEED) { + xas_set(&xas, start); + for (index =3D start; index < end; index++) { + if (xas_next(&xas) =3D=3D XA_RETRY_ENTRY) + xas_store(&xas, NULL); + } + + xas_unlock_irq(&xas); + goto rollback; + } + } else { + xas_lock_irq(&xas); } =20 nr =3D thp_nr_pages(hpage); - xas_lock_irq(&xas); if (is_shmem) __mod_lruvec_page_state(hpage, NR_SHMEM_THPS, nr); else @@ -2206,6 +2241,20 @@ static int collapse_file(struct mm_struct *mm, unsig= ned long addr, result =3D retract_page_tables(mapping, start, mm, addr, hpage, cc); unlock_page(hpage); + + /* + * The collapse has succeeded, so free the old pages. + */ + list_for_each_entry_safe(page, tmp, &pagelist, lru) { + list_del(&page->lru); + page->mapping =3D NULL; + page_ref_unfreeze(page, 1); + ClearPageActive(page); + ClearPageUnevictable(page); + unlock_page(page); + put_page(page); + } + goto out; =20 rollback: @@ -2217,15 +2266,13 @@ static int collapse_file(struct mm_struct *mm, unsi= gned long addr, } =20 xas_set(&xas, start); - xas_for_each(&xas, page, end - 1) { + end =3D index; + for (index =3D start; index < end; index++) { + xas_next(&xas); page =3D 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; } =20 @@ -2749,12 +2796,14 @@ static int madvise_collapse_errno(enum scan_result = r) case SCAN_ALLOC_HUGE_PAGE_FAIL: return -ENOMEM; case SCAN_CGROUP_CHARGE_FAIL: + case SCAN_EXCEED_NONE_PTE: return -EBUSY; /* Resource temporary unavailable - trying again might succeed */ case SCAN_PAGE_COUNT: case SCAN_PAGE_LOCK: case SCAN_PAGE_LRU: case SCAN_DEL_PAGE_LRU: + case SCAN_PAGE_FILLED: return -EAGAIN; /* * Other: Trying again likely not to succeed / error intrinsic to --=20 2.40.0.348.gf938b09366-goog From nobody Wed Feb 11 06:53:01 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BD3A3C6FD1D for ; Tue, 4 Apr 2023 12:02:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234927AbjDDMCM (ORCPT ); Tue, 4 Apr 2023 08:02:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234517AbjDDMBx (ORCPT ); Tue, 4 Apr 2023 08:01:53 -0400 Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C05B3C03 for ; Tue, 4 Apr 2023 05:01:44 -0700 (PDT) Received: by mail-pj1-x1033.google.com with SMTP id lr16-20020a17090b4b9000b0023f187954acso33699775pjb.2 for ; Tue, 04 Apr 2023 05:01:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1680609704; 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=Vyhfi+1sWMxl+7ZSluYYXlOU9BFaVL7c1FFz74Xev1Y=; b=cpPgK8/nWgVUBBTA+Q+NHmPq/3BrBdxiVC2lvjhc5fGgylaFPc5/panj0rvENCI0Sh GGcuIYS+4UxwSAR2K0MukNoiOP1u4VJYUoTPIALoJ1eIGK9MyEaSkPQDH/xxCeN3oa1F cQPVr2GzWPoKMusf6zXvu113yINmDfSEd8s4U= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680609704; 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=Vyhfi+1sWMxl+7ZSluYYXlOU9BFaVL7c1FFz74Xev1Y=; b=5hjl6fHLeZ46hwY9p3kK7TksTK8AoGA2wWFnPAi8puQgG/X54asvVUyV4RqFgt3b2C FohFu0RyG8keWUXEDar85QoYhF0NTzwmj8hfgU7MySxyf1fbMs4HF3ti9FOWkhFjL+ak HV294Ltph7ZYo2AR1gec9IEaTYlYv653P7QbtEt6gKBPtZyVXECfu741f2lwl0+JKzYG mYUuZLgw8J5GjZaOEYQn9lNNUWxOQRc5Y7d3eu8Rqv25F267b2WLKsxwPIbquwR0EVA0 ygDsE1KX8MnZhGpnj8gR40YJysogLKxKNQMskDXwVCQHnEk0SCLo1KKebEWP7LfY2bXD /IFA== X-Gm-Message-State: AAQBX9cJz77X7NZhPRpwsAtrPx42cDJytwoQiTCbLrYjIswjiXZ0CgW9 ehmBDkwXyVPhl+xRfj3P7c57Aw== X-Google-Smtp-Source: AKy350ZGp0by7zBEDeJP7f167x6LyxoDREyVG0zApneHT5l/sdT8Kwof8HJrTkmCawNC1ggmn03V2g== X-Received: by 2002:a17:90b:3b8a:b0:237:161d:f5ac with SMTP id pc10-20020a17090b3b8a00b00237161df5acmr2422009pjb.36.1680609703010; Tue, 04 Apr 2023 05:01:43 -0700 (PDT) Received: from localhost ([2401:fa00:8f:203:959b:21ea:166b:c273]) by smtp.gmail.com with UTF8SMTPSA id e5-20020a170902744500b0019ee045a2b3sm8193256plt.308.2023.04.04.05.01.40 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 04 Apr 2023 05:01:42 -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 4/4] mm/khugepaged: maintain page cache uptodate flag Date: Tue, 4 Apr 2023 21:01:17 +0900 Message-Id: <20230404120117.2562166-5-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 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: David Stevens Make sure that collapse_file doesn't interfere with checking the uptodate flag in the page cache by only inserting hpage into the page cache after it has been updated and marked uptodate. This is achieved by simply not replacing present pages with hpage when iterating over the target range. The present pages are already locked, so replacing them with the locked hpage before the collapse is finalized is unnecessary. However, it is necessary to stop freezing the present pages after validating them, since leaving long-term frozen pages in the page cache can lead to deadlocks. Simply checking the reference count is sufficient to ensure that there are no long-term references hanging around that would the collapse would break. Similar to hpage, there is no reason that the present pages actually need to be frozen in addition to being locked. This fixes a race where folio_seek_hole_data would mistake hpage for an fallocated but unwritten page. This race is visible to userspace via data temporarily disappearing from SEEK_DATA/SEEK_HOLE. This also fixes a similar race where pages could temporarily disappear from mincore. Fixes: f3f0e1d2150b ("khugepaged: add support of collapse for tmpfs/shmem p= ages") Signed-off-by: David Stevens --- mm/khugepaged.c | 79 ++++++++++++++++++------------------------------- 1 file changed, 29 insertions(+), 50 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 7679551e9540..a19aa140fd52 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1855,17 +1855,18 @@ static int retract_page_tables(struct address_space= *mapping, pgoff_t pgoff, * * Basic scheme is simple, details are more complex: * - allocate and lock a new huge page; - * - scan page cache replacing old pages with the new one + * - scan page cache, locking old pages * + swap/gup in pages if necessary; - * + keep old pages around in case rollback is required; + * - copy data to new page + * - handle shmem holes + * + re-validate that holes weren't filled by someone else + * + check for userfaultfd * - finalize updates to the page cache; * - if replacing succeeds: - * + copy data over; - * + free old pages; * + unlock huge page; + * + free old pages; * - if replacing failed; - * + put all pages back and unfreeze them; - * + restore gaps in the page cache; + * + unlock old pages * + unlock and free huge page; */ static int collapse_file(struct mm_struct *mm, unsigned long addr, @@ -1913,12 +1914,6 @@ static int collapse_file(struct mm_struct *mm, unsig= ned long addr, } } while (1); =20 - /* - * 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 - * be able to map it or use it in another way until we unlock it. - */ - xas_set(&xas, start); for (index =3D start; index < end; index++) { page =3D xas_next(&xas); @@ -2076,12 +2071,16 @@ static int collapse_file(struct mm_struct *mm, unsi= gned long addr, VM_BUG_ON_PAGE(page !=3D xas_load(&xas), page); =20 /* - * The page is expected to have page_count() =3D=3D 3: + * We control three references to the page: * - we hold a pin on it; * - one reference from page cache; * - one from isolate_lru_page; + * If those are the only references, then any new usage of the + * page will have to fetch it from the page cache. That requires + * locking the page to handle truncate, so any new usage will be + * blocked until we unlock page after collapse/during rollback. */ - if (!page_ref_freeze(page, 3)) { + if (page_count(page) !=3D 3) { result =3D SCAN_PAGE_COUNT; xas_unlock_irq(&xas); putback_lru_page(page); @@ -2089,13 +2088,9 @@ static int collapse_file(struct mm_struct *mm, unsig= ned long addr, } =20 /* - * Add the page to the list to be able to undo the collapse if - * something go wrong. + * Accumulate the pages that are being collapsed. */ list_add_tail(&page->lru, &pagelist); - - /* Finally, replace with the new page. */ - xas_store(&xas, hpage); continue; out_unlock: unlock_page(page); @@ -2132,8 +2127,7 @@ static int collapse_file(struct mm_struct *mm, unsign= ed long addr, goto rollback; =20 /* - * Replacing old pages with new one has succeeded, now we - * attempt to copy the contents. + * The old pages are locked, so they won't change anymore. */ index =3D start; list_for_each_entry(page, &pagelist, lru) { @@ -2222,11 +2216,11 @@ static int collapse_file(struct mm_struct *mm, unsi= gned long addr, /* 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); =20 + /* + * Mark hpage as uptodate before inserting it into the page cache so + * that it isn't mistaken for an fallocated but unwritten page. + */ folio =3D page_folio(hpage); folio_mark_uptodate(folio); folio_ref_add(folio, HPAGE_PMD_NR - 1); @@ -2235,6 +2229,11 @@ static int collapse_file(struct mm_struct *mm, unsig= ned long addr, folio_mark_dirty(folio); folio_add_lru(folio); =20 + /* 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); + /* * Remove pte page tables, so we can re-fault the page as huge. */ @@ -2248,47 +2247,29 @@ static int collapse_file(struct mm_struct *mm, unsi= gned long addr, list_for_each_entry_safe(page, tmp, &pagelist, lru) { list_del(&page->lru); page->mapping =3D NULL; - page_ref_unfreeze(page, 1); ClearPageActive(page); ClearPageUnevictable(page); unlock_page(page); - put_page(page); + folio_put_refs(page_folio(page), 3); } =20 goto out; =20 rollback: /* Something went wrong: roll back page cache changes */ - xas_lock_irq(&xas); if (nr_none) { + xas_lock_irq(&xas); mapping->nrpages -=3D nr_none; shmem_uncharge(mapping->host, nr_none); + xas_unlock_irq(&xas); } =20 - xas_set(&xas, start); - end =3D index; - for (index =3D start; index < end; index++) { - xas_next(&xas); - page =3D list_first_entry_or_null(&pagelist, - struct page, lru); - if (!page || xas.xa_index < page->index) { - nr_none--; - continue; - } - - VM_BUG_ON_PAGE(page->index !=3D xas.xa_index, page); - - /* Unfreeze the page. */ + list_for_each_entry_safe(page, tmp, &pagelist, lru) { 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); + put_page(page); } - 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 @@ -2303,8 +2284,6 @@ static int collapse_file(struct mm_struct *mm, unsign= ed long addr, smp_mb(); } =20 - xas_unlock_irq(&xas); - hpage->mapping =3D NULL; =20 unlock_page(hpage); --=20 2.40.0.348.gf938b09366-goog