From patchwork Fri Jun 9 01:42:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugh Dickins X-Patchwork-Id: 13273093 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 92025C7EE29 for ; Fri, 9 Jun 2023 01:42:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 34A608E0002; Thu, 8 Jun 2023 21:42:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2FA7B8E0001; Thu, 8 Jun 2023 21:42:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1C2BC8E0002; Thu, 8 Jun 2023 21:42:48 -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 09F4B8E0001 for ; Thu, 8 Jun 2023 21:42:48 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id D3AE3A03AB for ; Fri, 9 Jun 2023 01:42:47 +0000 (UTC) X-FDA: 80881510374.16.726B18B Received: from mail-yb1-f169.google.com (mail-yb1-f169.google.com [209.85.219.169]) by imf21.hostedemail.com (Postfix) with ESMTP id 02F351C0004 for ; Fri, 9 Jun 2023 01:42:45 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=kXMH2hIQ; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf21.hostedemail.com: domain of hughd@google.com designates 209.85.219.169 as permitted sender) smtp.mailfrom=hughd@google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1686274966; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=p1k5/eahjbWsfa/uahTffpcykz1TgCrP6tjOVjA4Rhk=; b=YIR0E3sy33xo7QRg2oH2mgAEXOVHWQxVpUOFY8MpboPGgG83PbKwSkKwdsWPqtFTOcNEEw 4+1XGOmLu05WpE2caAigP/1BfWfPuEXazJP8np3Xyqbmb2OfeHLlRjLhI3THUmF1VTEBhb usuU99AP+7rGuD8Aj9YGlTZufU0FD3o= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=kXMH2hIQ; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf21.hostedemail.com: domain of hughd@google.com designates 209.85.219.169 as permitted sender) smtp.mailfrom=hughd@google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1686274966; a=rsa-sha256; cv=none; b=6Ljy9FVkyX5NgNkvaf5fnLXJsMZsKq2BWcuNMKk16JIMlE7wvkwHnLIfZFQTUDi1RDDvvX B7RASX6S4F6Mi8u1/ThYMWcymOXNERt0QDvm6jJFmgLEA8k7YCEOb1UQNMV+CkkAaNuLk5 bn6EOEn1vHWmGW9dKvIp8JMsGaZ9K0I= Received: by mail-yb1-f169.google.com with SMTP id 3f1490d57ef6-bad142be0ebso1284464276.3 for ; Thu, 08 Jun 2023 18:42:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686274965; x=1688866965; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=p1k5/eahjbWsfa/uahTffpcykz1TgCrP6tjOVjA4Rhk=; b=kXMH2hIQkmF2USapaQhiZcpKUKkNYVjPRafKmj8hC8uAM6lWt8Cg7WyWV3i5oVqEWA bi93PCMA7PjdAzeBItn2fVWVBY+/9qgI6tMfBLEeEpjUthSCUQOS9R5oolpPexMofMwP pSgpjbezGBNl2+s7mdLUqylcEDr0Rzwx/U2TjkwQG91WnUJpoHZuxPDmnmO/Yv7J8FdM cwm5pnD7LoAthDQwPzAWpV55QbduVNRTxBSVVH445+SgGv+mnH4NOD/8k4MMNgeNCGj5 /35hYwKb/TBzCQeOI3M6UpGu01ZJaZLpaHwXWmcS91MANZ+pzrD8lrdrhALbZ0SuH8aW G/vA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686274965; x=1688866965; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=p1k5/eahjbWsfa/uahTffpcykz1TgCrP6tjOVjA4Rhk=; b=A9tLEoUIF64Zn98AQqHwXQUB2vEFaVj1/4UhVka8yCz/tml8og0uUP+W3gpHaPYGGl qUo/0kboqrmY00NpUFW7hcv9smd0OKl6r1dlbN9NTB31+L78S5ODQUSV7OaBeyDJlrQs gKgOxY/Z4+Z7YS/81PhiIRW2iTIprB14D1dWZwo0LqfZOfDrUWjsHoG/aAb5SafMHJFL AR5W+LwQthLRi4GfHJLyCaDs5m7Z77SvRwF0FgEEXazVi4NTvuV2IMuepqx6Nj3TWfqP e/4NxeM/9em2HVm9rr6ZRQ3OIBsx+VDhGviU8wgO+bYVjXBSqQFemrh88ibEVOqOFlMd Lzkw== X-Gm-Message-State: AC+VfDzwCrc2IhwlDsl7Pgl+CGDz0UnRaOlveVm/I1f/Wu6PrGiWUHnQ OwYcIRW3721WyrPWaThQKNTFSw== X-Google-Smtp-Source: ACHHUZ5fn3kW9FeGHNpW1xzpcB4LGHphf3k1hTSF+y3EK3Em0GkiW50KAIuHoFANUJ/kQPH4MzH72g== X-Received: by 2002:a25:ab53:0:b0:ba8:972d:e380 with SMTP id u77-20020a25ab53000000b00ba8972de380mr1240110ybi.22.1686274964991; Thu, 08 Jun 2023 18:42:44 -0700 (PDT) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id e188-20020a251ec5000000b00baca49c80dcsm615573ybe.28.2023.06.08.18.42.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jun 2023 18:42:43 -0700 (PDT) Date: Thu, 8 Jun 2023 18:42:40 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@ripple.attlocal.net To: Andrew Morton cc: Mike Kravetz , Mike Rapoport , "Kirill A. Shutemov" , Matthew Wilcox , David Hildenbrand , Suren Baghdasaryan , Qi Zheng , Yang Shi , Mel Gorman , Peter Xu , Peter Zijlstra , Will Deacon , Yu Zhao , Alistair Popple , Ralph Campbell , Ira Weiny , Steven Price , SeongJae Park , Lorenzo Stoakes , Huang Ying , Naoya Horiguchi , Christophe Leroy , Zack Rusin , Jason Gunthorpe , Axel Rasmussen , Anshuman Khandual , Pasha Tatashin , Miaohe Lin , Minchan Kim , Christoph Hellwig , Song Liu , Thomas Hellstrom , Ryan Roberts , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v2 27/32] mm/khugepaged: allow pte_offset_map[_lock]() to fail In-Reply-To: Message-ID: <6513e85-d798-34ec-3762-7c24ffb9329@google.com> References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 02F351C0004 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: rxktgis1d44aizb53epyjrwj5jbg6t9g X-HE-Tag: 1686274965-797311 X-HE-Meta: U2FsdGVkX18tsV+L2MQRiohB7OIjPRooPxVOyzuO6eObJ6kUJ+5a8uweh6+e+i/0LpnceJhXS9YxBH/aiKwHCyg78IttaTIdp0IeibKE5ivs7WqaSSA1HeBD6QsmAhI9YvdxXoCb8kBzo0c1M/7auuz0MXmeBl0z5XnV8GJorNh9c7UJo0e+RUqfXN54v9v2MDc+ShTCw7OgoaTGxEqfzHAOyKP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GA9HDR7MYbX9FrvNACplyo5BTA2F5T2+C3TKgcKzjaWDlDhvDu3vqG8/N78Cuqq7jXWPEAtxd+10u/3q9yEi1ZksG9oMzGdQNO8Bb0PTfF7gAhgVVy3McxIdemyg9BejnwflcAt3L/azrlWeJWRd00Ly/8mMS4= 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: __collapse_huge_page_swapin(): don't drop the map after every pte, it only has to be dropped by do_swap_page(); give up if pte_offset_map() fails; trace_mm_collapse_huge_page_swapin() at the end, with result; fix comment on returned result; fix vmf.pgoff, though it's not used. collapse_huge_page(): use pte_offset_map_lock() on the _pmd returned from clearing; allow failure, but it should be impossible there. hpage_collapse_scan_pmd() and collapse_pte_mapped_thp() allow for pte_offset_map_lock() failure. Signed-off-by: Hugh Dickins Reviewed-by: Yang Shi --- mm/khugepaged.c | 72 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 49 insertions(+), 23 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 732f9ac393fc..49cfa7cdfe93 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -993,9 +993,8 @@ static int check_pmd_still_valid(struct mm_struct *mm, * Only done if hpage_collapse_scan_pmd believes it is worthwhile. * * Called and returns without pte mapped or spinlocks held. - * Note that if false is returned, mmap_lock will be released. + * Returns result: if not SCAN_SUCCEED, mmap_lock has been released. */ - static int __collapse_huge_page_swapin(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd, @@ -1004,23 +1003,35 @@ static int __collapse_huge_page_swapin(struct mm_struct *mm, int swapped_in = 0; vm_fault_t ret = 0; unsigned long address, end = haddr + (HPAGE_PMD_NR * PAGE_SIZE); + int result; + pte_t *pte = NULL; for (address = haddr; address < end; address += PAGE_SIZE) { struct vm_fault vmf = { .vma = vma, .address = address, - .pgoff = linear_page_index(vma, haddr), + .pgoff = linear_page_index(vma, address), .flags = FAULT_FLAG_ALLOW_RETRY, .pmd = pmd, }; - vmf.pte = pte_offset_map(pmd, address); - vmf.orig_pte = *vmf.pte; - if (!is_swap_pte(vmf.orig_pte)) { - pte_unmap(vmf.pte); - continue; + if (!pte++) { + pte = pte_offset_map(pmd, address); + if (!pte) { + mmap_read_unlock(mm); + result = SCAN_PMD_NULL; + goto out; + } } + + vmf.orig_pte = *pte; + if (!is_swap_pte(vmf.orig_pte)) + continue; + + vmf.pte = pte; ret = do_swap_page(&vmf); + /* Which unmaps pte (after perhaps re-checking the entry) */ + pte = NULL; /* * do_swap_page returns VM_FAULT_RETRY with released mmap_lock. @@ -1029,24 +1040,29 @@ static int __collapse_huge_page_swapin(struct mm_struct *mm, * resulting in later failure. */ if (ret & VM_FAULT_RETRY) { - trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 0); /* Likely, but not guaranteed, that page lock failed */ - return SCAN_PAGE_LOCK; + result = SCAN_PAGE_LOCK; + goto out; } if (ret & VM_FAULT_ERROR) { mmap_read_unlock(mm); - trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 0); - return SCAN_FAIL; + result = SCAN_FAIL; + goto out; } swapped_in++; } + if (pte) + pte_unmap(pte); + /* Drain LRU add pagevec to remove extra pin on the swapped in pages */ if (swapped_in) lru_add_drain(); - trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 1); - return SCAN_SUCCEED; + result = SCAN_SUCCEED; +out: + trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, result); + return result; } static int alloc_charge_hpage(struct page **hpage, struct mm_struct *mm, @@ -1146,9 +1162,6 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address, address + HPAGE_PMD_SIZE); mmu_notifier_invalidate_range_start(&range); - pte = pte_offset_map(pmd, address); - pte_ptl = pte_lockptr(mm, pmd); - pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */ /* * This removes any huge TLB entry from the CPU so we won't allow @@ -1163,13 +1176,18 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address, mmu_notifier_invalidate_range_end(&range); tlb_remove_table_sync_one(); - spin_lock(pte_ptl); - result = __collapse_huge_page_isolate(vma, address, pte, cc, - &compound_pagelist); - spin_unlock(pte_ptl); + pte = pte_offset_map_lock(mm, &_pmd, address, &pte_ptl); + if (pte) { + result = __collapse_huge_page_isolate(vma, address, pte, cc, + &compound_pagelist); + spin_unlock(pte_ptl); + } else { + result = SCAN_PMD_NULL; + } if (unlikely(result != SCAN_SUCCEED)) { - pte_unmap(pte); + if (pte) + pte_unmap(pte); spin_lock(pmd_ptl); BUG_ON(!pmd_none(*pmd)); /* @@ -1253,6 +1271,11 @@ static int hpage_collapse_scan_pmd(struct mm_struct *mm, memset(cc->node_load, 0, sizeof(cc->node_load)); nodes_clear(cc->alloc_nmask); pte = pte_offset_map_lock(mm, pmd, address, &ptl); + if (!pte) { + result = SCAN_PMD_NULL; + goto out; + } + for (_address = address, _pte = pte; _pte < pte + HPAGE_PMD_NR; _pte++, _address += PAGE_SIZE) { pte_t pteval = *_pte; @@ -1622,8 +1645,10 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, * lockless_pages_from_mm() and the hardware page walker can access page * tables while all the high-level locks are held in write mode. */ - start_pte = pte_offset_map_lock(mm, pmd, haddr, &ptl); result = SCAN_FAIL; + start_pte = pte_offset_map_lock(mm, pmd, haddr, &ptl); + if (!start_pte) + goto drop_immap; /* step 1: check all mapped PTEs are to the right huge page */ for (i = 0, addr = haddr, pte = start_pte; @@ -1697,6 +1722,7 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, abort: pte_unmap_unlock(start_pte, ptl); +drop_immap: i_mmap_unlock_write(vma->vm_file->f_mapping); goto drop_hpage; }