From patchwork Tue Mar 7 05:20:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Stevens X-Patchwork-Id: 13162796 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 6D5CAC678DB for ; Tue, 7 Mar 2023 05:21:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AA3EA280001; Tue, 7 Mar 2023 00:21:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A2C986B0075; Tue, 7 Mar 2023 00:21:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8CCFA280001; Tue, 7 Mar 2023 00:21:02 -0500 (EST) 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 7CD7F6B0074 for ; Tue, 7 Mar 2023 00:21:02 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 47F121209FC for ; Tue, 7 Mar 2023 05:21:02 +0000 (UTC) X-FDA: 80540953164.18.99A4C00 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) by imf17.hostedemail.com (Postfix) with ESMTP id 5C69E40004 for ; Tue, 7 Mar 2023 05:21:00 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b="c/TSVl2F"; spf=pass (imf17.hostedemail.com: domain of stevensd@chromium.org designates 209.85.214.177 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=1678166460; 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=AjnWe8/2NQ2Rb5ldPsFVe45g3piuxL8Z1VW03Vfk8mw=; b=AGHMQgkQSk4Qb39P+2VzPwrYGggF1eIul2Q+396MEuJDgRwA3OzngsaZQZAewPrXQ9ib5w D+B3OCO1VetG83PDIn/FBGKoIeGpuWjdGIKXBPQkemBNzKR3DT5irRVmlBDa3MOA66D9Af RjQh1VFpeE8K9JW2XoUnqTik9PTjaL0= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b="c/TSVl2F"; spf=pass (imf17.hostedemail.com: domain of stevensd@chromium.org designates 209.85.214.177 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=1678166460; a=rsa-sha256; cv=none; b=6hTg8zQ/wJKAi9283m/a/i1C7M49oPI+wT7uhfF3B1Rjr9L/zsGvzOYFNUMXnmM3R8AsWt w5qHTL/m1r4NcXm1ZVc1wRBXm9O463WJxaamIJJkpuQGWWqBdbjHkqDJry+q+ZTdM9Hy8e z0CfRq/VhF+TBmjcz2E1XOJ9LxVYa7Y= Received: by mail-pl1-f177.google.com with SMTP id i3so12914580plg.6 for ; Mon, 06 Mar 2023 21:21:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1678166459; 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=AjnWe8/2NQ2Rb5ldPsFVe45g3piuxL8Z1VW03Vfk8mw=; b=c/TSVl2FysCwSRDNgHhh/dJyDsqS/5hQaMYLYoqxjFzQWn7AXX3UZF4FuRy7UY1i9K MVejeaJmtA9f3rea8lGt5INqGQcXbzwiy1Um3ot59joo0dm+lvTAIUYirm2QX2HI08/5 HqwOxJAUkUhfHw9+ssbkFifYSHpYKHTiSwPRI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678166459; 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=AjnWe8/2NQ2Rb5ldPsFVe45g3piuxL8Z1VW03Vfk8mw=; b=wOv8NXFqmkp4H/UXYgwPyvs+FcRYScmfHLSaFuVn5GhgL7eKTBHHxFxSRnkVB1aXMf l9pCONIDMW09jOxUipB9QZiA8BtaiMEJj61mxh6Kv+LcheQGdBFl+EIqq7sbF8UFUqTW NqWY9/LuCmQYX9dhbJrcM4t0oor0qT+aEeetxZot0SLycKh7BwPLdqtxcn0UOCjngKQj pnSIlnoq8Ra1fALTfyveyNlsTWDe5Dq6sRCVkMce92Yr8HYOEbO9bnS4LtMS80gDykZP V493S7v4qaRwPwycnEOhy/130Fk5Mow2+oP2bLNPLtajT6XM9HApxzluI16ngcMIQNaC 9d0A== X-Gm-Message-State: AO0yUKXXDrjupstMZsWkN9Cj5qXHND1+0kdonDlpoKNUWtebG92+zT95 CrvwcGZYr66O0VVdg/jcd7hbhRbLCxqUz4m58Ro= X-Google-Smtp-Source: AK7set/sEHWb6Xu3exvXoB/mmv+4TeohXcOBTwkbILkcrj+AmDUd121Z7n91XJwSf4KotxpzMT99NQ== X-Received: by 2002:a05:6a20:3558:b0:bd:17a4:c35f with SMTP id f24-20020a056a20355800b000bd17a4c35fmr11288488pze.23.1678166458635; Mon, 06 Mar 2023 21:20:58 -0800 (PST) Received: from localhost ([2401:fa00:8f:203:1f73:9034:ce28:4421]) by smtp.gmail.com with UTF8SMTPSA id n13-20020aa7904d000000b0058b927b9653sm7293130pfo.92.2023.03.06.21.20.56 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 06 Mar 2023 21:20:58 -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 2/3] mm/khugepaged: skip shmem with userfaultfd Date: Tue, 7 Mar 2023 14:20:35 +0900 Message-Id: <20230307052036.1520708-3-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-Queue-Id: 5C69E40004 X-Stat-Signature: n9hssw8gosyft6jnz5738kdcqag9h31e X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1678166460-454424 X-HE-Meta: U2FsdGVkX1/p+SM10EhvN/d20Mz4W5wCF7BTsUSzOick+7kI9gMwv08Ib3iOp0jUJAS6OS0/Mf4MRdBlOhfGfMdSKEtetMS0XKc6O743FTeK85J/Q7/IAU1On11Jm0DzeXtgWi0DjA+tVR10H3Zhe/TeJWIfBVVjhoEHfEt85OiktQo2Qh3cIvp3BQKfEE0lC0rNzQjsA2kbqakr35mtpaOkPG93Vru8pml8O8axe8mfomwK1khp9GvLztbvYh9S4jbWPi2CflGBFyClb93ffJTRLTmxiHVFjGbqoEuESSicjbWyETUZ64EAIsg2prd4SXAk24zIxm88PDbRmX5/bWso1kqO+zY6is+YN2r+sAVxamhQHmFW25ocbQ6YTyDcpCtlPruKLnjwkOHXuVwihy9x/emZ2T++2IXFnQVltf6zv31Y/9BVfeCIto/veGFGs7pLexmqMhguZMnwjXm+cd6xWptf6E6+ig7O7QM0Bpzuq8WyWL+aoUXnFgITbzHeIooSjopzqgk3Ocq6A3OGnMs1B/pI3UfoiVk2koQnZpBZuCE8jTxo/WiXmENUYlFyrPBQn7DxuvEBl0imoM+0kMjW9SdgBzbdfjH6EvZrtH3MaXxTjqqnLYVZee8mAmgyIe+g0Tw4RbzVtulMLYBQTt/Qh/XuZJv6qkVbvHJsnVjaW1yn9tjHOV89Auo0arOxCS6GVW2PlJ16SLSCUULrZP8Lmb9GCDoK/h7e8PS6jHP0HtVmV5zRsbQu+aUlgsjXhE+euNEEjyYOy+AvMtQQrgvO4080cKDHqKP1epBdjlqWU4vxbgVuWTZUEbdjopWM5D+h4UbiLr2eVoia0vcnx/QIfhiQaqh+ZGO5w5uaQ6P26/DqGsfNEYJBvnWzYkVZWpKbPRisf9owPJF8LGSP5auJbmSTh4U2nGc4G3m/1WMxyDVCnwOV6wYG2O1oJY6wHcqAumUIXLR3ssF+jn9 xLnLW0vQ 7RZcsO30T+lcAqg5wCQOxR+njdVQBHgp5g16kKQEF2bRAF40PqgFb6IfJln52/wsQlDLcdLIGIC9QDBDZf4JOCDC9jLLqexJB1WNbt5uQaNqwdqyi3pXOjnzGAyNm7WlQlLcsEK5VoskiHL3vbsMEWdmWR9MV6c+VEmJHlDPsfc9tyHU/dAg/FKY7UGqhf+F5A5RNIFC+4vm9hyv6lQ6BWLZ8UcKQeFUbFj0BQTZbwW6d5+nlC1Bq3H9O5IVyxHjOJCkCu0AxDziPukUmPKxqhx8NBsC3FnGL4pfTyNFN/CIoBgOAMF3Ge/eEHcT1hklz62XBI0EXY27W6indQGoCngcnGmve5WHH8AZHxBQsXNuRhZg0DPnq+wXBEw== 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 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 | 92 +++++++++++++++++++++++------- 2 files changed, 73 insertions(+), 22 deletions(-) diff --git a/include/trace/events/huge_memory.h b/include/trace/events/huge_memory.h index 46cce509957b..7ee85fff89a3 100644 --- a/include/trace/events/huge_memory.h +++ b/include/trace/events/huge_memory.h @@ -37,7 +37,8 @@ EM( SCAN_CGROUP_CHARGE_FAIL, "ccgroup_charge_failed") \ EM( SCAN_TRUNCATED, "truncated") \ EM( SCAN_PAGE_HAS_PRIVATE, "page_has_private") \ - EMe(SCAN_COPY_MC, "copy_poisoned_page") \ + EM( SCAN_COPY_MC, "copy_poisoned_page") \ + EMe(SCAN_PAGE_FILLED, "page_filled") \ #undef EM #undef EMe diff --git a/mm/khugepaged.c b/mm/khugepaged.c index b954e3c685e7..51ae399f2035 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -57,6 +57,7 @@ enum scan_result { SCAN_TRUNCATED, SCAN_PAGE_HAS_PRIVATE, SCAN_COPY_MC, + SCAN_PAGE_FILLED, }; #define CREATE_TRACE_POINTS @@ -1873,8 +1874,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; @@ -1952,13 +1953,12 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, result = SCAN_TRUNCATED; goto xa_locked; } - xas_set(&xas, index); + xas_set(&xas, index + 1); } if (!shmem_charge(mapping->host, 1)) { result = SCAN_FAIL; goto xa_locked; } - xas_store(&xas, hpage); nr_none++; continue; } @@ -2169,21 +2169,57 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, 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); + if (nr_none) { + struct vm_area_struct *vma; + int nr_none_check = 0; + + i_mmap_lock_read(mapping); + xas_lock_irq(&xas); + + xas_set(&xas, start); + for (index = start; index < end; index++) { + if (!xas_next(&xas)) { + xas_store(&xas, XA_RETRY_ENTRY); + nr_none_check++; + } + } + + if (nr_none != nr_none_check) { + result = SCAN_PAGE_FILLED; + goto immap_locked; + } + + /* + * If userspace observed a missing page in a VMA with an armed + * userfaultfd, then it might expect a UFFD_EVENT_PAGEFAULT for + * that page, so we need to roll back to avoid suppressing such + * an event. Any userfaultfds armed 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, start) { + if (userfaultfd_missing(vma)) { + result = SCAN_EXCEED_NONE_PTE; + goto immap_locked; + } + } + +immap_locked: + i_mmap_unlock_read(mapping); + if (result != SCAN_SUCCEED) { + xas_set(&xas, start); + for (index = start; index < end; index++) { + if (xas_next(&xas) == XA_RETRY_ENTRY) + xas_store(&xas, NULL); + } + + xas_unlock_irq(&xas); + goto rollback; + } + } else { + xas_lock_irq(&xas); } - xas_lock_irq(&xas); if (is_shmem) __mod_lruvec_page_state(hpage, NR_SHMEM_THPS, nr); else @@ -2213,6 +2249,20 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, result = 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 = NULL; + page_ref_unfreeze(page, 1); + ClearPageActive(page); + ClearPageUnevictable(page); + unlock_page(page); + put_page(page); + } + goto out; rollback: @@ -2224,15 +2274,13 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, } xas_set(&xas, start); - xas_for_each(&xas, page, end - 1) { + end = index; + for (index = start; index < end; index++) { + xas_next(&xas); 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; } @@ -2750,12 +2798,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