From patchwork Fri Jun 9 01:32:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugh Dickins X-Patchwork-Id: 13273083 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 A038EC7EE25 for ; Fri, 9 Jun 2023 01:32:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3C3B68E0003; Thu, 8 Jun 2023 21:32:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 34C4E8E0001; Thu, 8 Jun 2023 21:32:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 214C58E0003; Thu, 8 Jun 2023 21:32:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 150BA8E0001 for ; Thu, 8 Jun 2023 21:32:55 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id E1FB31602C7 for ; Fri, 9 Jun 2023 01:32:54 +0000 (UTC) X-FDA: 80881485468.16.D660C44 Received: from mail-yw1-f176.google.com (mail-yw1-f176.google.com [209.85.128.176]) by imf03.hostedemail.com (Postfix) with ESMTP id 147F32001A for ; Fri, 9 Jun 2023 01:32:52 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=E4DRQK86; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf03.hostedemail.com: domain of hughd@google.com designates 209.85.128.176 as permitted sender) smtp.mailfrom=hughd@google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1686274373; a=rsa-sha256; cv=none; b=dWSxCFkmMQsEu9Fe9cdSjSLDGGadTVsk78NMmqR2ZlEEuvwsusXhmdZH/sNm2RUkXTfDSm t413uFphMv1U2EXG3RBjrOhNWL9osoTJeU9ahQ2/TrxivAZWq9AUkjb3TtgF4xw8h9Udbh iPcBaOVS4IhJtSmXs+FtJkRbHVnqBn4= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=E4DRQK86; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf03.hostedemail.com: domain of hughd@google.com designates 209.85.128.176 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=1686274373; 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=dPoyGlkGpegFUTFofVDIHWRBddk7VxC7U3Cl7g+dttQ=; b=CHfO5MzYCokAKGRrmeNmThEbhDGCOlOJHpnyvMgKu8tV4deH0WSBVqArvw9lefTJh8ADXV pXsC1cHQ+3lLLrRxLNd8GL5P0b4G6Iec0HFakD/2YWd/X0PQFtDMtC/wQ4G+/e9B9PQ35V jgxXavpUe5fFPlP036K1YXoJ66/7Vys= Received: by mail-yw1-f176.google.com with SMTP id 00721157ae682-561b7729a12so35138187b3.1 for ; Thu, 08 Jun 2023 18:32:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686274372; x=1688866372; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=dPoyGlkGpegFUTFofVDIHWRBddk7VxC7U3Cl7g+dttQ=; b=E4DRQK86HkB1M0oL3CGdKUO2AI9NqeHpyBpHLli2aF+Pqlit3JP/3FLe7keJkIgcP9 Ui3kDjUhzLuUpOKUr/BBsh3paM313vr/kZTbKcMrzy1rnCosbjtatNixhCUekN/A7pkf A1U0Xx3rYpDhvjGoSHww7Px2zZjjdooHl41F3udSS11WEeWkhqn3r8hfZs7GDrCg2BMs cW8rdWF1rLhD4DHvb+mCyaR83x60p+qcJdlOzarzjsn+EOUWDt8OE4eCU+8SOfMHlxy4 XQHaWxY2xWWX/2GxHjET5xLBgwJ5RGHZxlXYqQkMUK5vHAdHkIfUkQT70qyxIHs4Ypp2 ur1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686274372; x=1688866372; 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=dPoyGlkGpegFUTFofVDIHWRBddk7VxC7U3Cl7g+dttQ=; b=PDYEqr87WszKpdKk68a3Po4ML7xcTlMI7ZK3/Od63bFxdhfa7NJAt4F7TBNDNxtSqK pBuRpPwH5/DaXRa1UGik/hJwH7IxixOTPWal0mGQkeUvbQAQ1wabvg24VSwMAfGkh6Qb xknup/HwIiTe9DC4mCqo6bk6TcZxIxsy1lk51ISJGoxwgefOhUirn1x24oQ77qMsUt7+ AB9msv9lGJQZMeZQcAS09Q5rwXDN2V7Npp63iu/5K7nG1uepqSJAmcbG3LyYhxIOubND vTtd/tY6iR/mDCyRCtYicah+t6mOex7+0jGD778BB+zyWbePt5c++qzwweF2uez/QV9c KE6Q== X-Gm-Message-State: AC+VfDx/M4JrFPa1wOcAknVRnQbia7ewNpL1Kbo5qlz86WZYTg3raHQc RrEDFt/Y7ruJhZIV5f5Ph1xdYQ== X-Google-Smtp-Source: ACHHUZ6C2iTOD9MuiuvdKguC1n6gRPJbYpY/VQ3aTwN0L3f5aJC3JH3HR0E7O5S3i52MFj8yhumCLw== X-Received: by 2002:a81:4f4c:0:b0:561:1cb6:f3d6 with SMTP id d73-20020a814f4c000000b005611cb6f3d6mr549240ywb.0.1686274371904; Thu, 08 Jun 2023 18:32:51 -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 u195-20020a0debcc000000b00565c29cf592sm309752ywe.10.2023.06.08.18.32.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jun 2023 18:32:51 -0700 (PDT) Date: Thu, 8 Jun 2023 18:32:47 -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 19/32] mm/mremap: retry if either pte_offset_map_*lock() fails In-Reply-To: Message-ID: <65e5e84a-f04-947-23f2-b97d3462e1e@google.com> References: MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 147F32001A X-Stat-Signature: d91ni8bnhqidi94iuk7fatno7sbq6qco X-HE-Tag: 1686274372-303270 X-HE-Meta: U2FsdGVkX19gGqFopOOlOTSdQppBOl13MC57YXiW0r7tOWhOFMXBvleLEjouMbToBW1clV69gU6pehYkTYcvCWqeKOu8rATpgEpq0LZ9NBLjS7Sa6V+SD4SUZNFwX2Zn/X0kQooNqZbrGHR46f/QXXrJSOLVoDNV7c+h4HR9v4USW5RBCxiMlM7cikaskwSyCwxp9bzo0OFshpwKsopu3Qdobru2hKIZe7pcLqDzdqm8zSU90puPH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CzH0Z40VkpKpRi6LEkEcnGirVWc7x7XDK0LV8++r2QmRH29n/CLrqf69skBWVXS4GedB+9pNPlAUal60zwWx3oTLOxbXkbuAg== 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: move_ptes() return -EAGAIN if pte_offset_map_lock() of old fails, or if pte_offset_map_nolock() of new fails: move_page_tables() retry if so. But that does need a pmd_none() check inside, to stop endless loop when huge shmem is truncated (thank you to syzbot); and move_huge_pmd() must tolerate that a page table might have been allocated there just before (of course it would be more satisfying to remove the empty page table, but this is not a path worth optimizing). Signed-off-by: Hugh Dickins --- mm/huge_memory.c | 5 +++-- mm/mremap.c | 28 ++++++++++++++++++++-------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 624671aaa60d..d4bd5fa7c823 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1760,9 +1760,10 @@ bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr, /* * The destination pmd shouldn't be established, free_pgtables() - * should have release it. + * should have released it; but move_page_tables() might have already + * inserted a page table, if racing against shmem/file collapse. */ - if (WARN_ON(!pmd_none(*new_pmd))) { + if (!pmd_none(*new_pmd)) { VM_BUG_ON(pmd_trans_huge(*new_pmd)); return false; } diff --git a/mm/mremap.c b/mm/mremap.c index b11ce6c92099..1fc47b4f38d7 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -133,7 +133,7 @@ static pte_t move_soft_dirty_pte(pte_t pte) return pte; } -static void move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd, +static int move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd, unsigned long old_addr, unsigned long old_end, struct vm_area_struct *new_vma, pmd_t *new_pmd, unsigned long new_addr, bool need_rmap_locks) @@ -143,6 +143,7 @@ static void move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd, spinlock_t *old_ptl, *new_ptl; bool force_flush = false; unsigned long len = old_end - old_addr; + int err = 0; /* * When need_rmap_locks is true, we take the i_mmap_rwsem and anon_vma @@ -170,8 +171,16 @@ static void move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd, * pte locks because exclusive mmap_lock prevents deadlock. */ old_pte = pte_offset_map_lock(mm, old_pmd, old_addr, &old_ptl); - new_pte = pte_offset_map(new_pmd, new_addr); - new_ptl = pte_lockptr(mm, new_pmd); + if (!old_pte) { + err = -EAGAIN; + goto out; + } + new_pte = pte_offset_map_nolock(mm, new_pmd, new_addr, &new_ptl); + if (!new_pte) { + pte_unmap_unlock(old_pte, old_ptl); + err = -EAGAIN; + goto out; + } if (new_ptl != old_ptl) spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING); flush_tlb_batched_pending(vma->vm_mm); @@ -208,8 +217,10 @@ static void move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd, spin_unlock(new_ptl); pte_unmap(new_pte - 1); pte_unmap_unlock(old_pte - 1, old_ptl); +out: if (need_rmap_locks) drop_rmap_locks(vma); + return err; } #ifndef arch_supports_page_table_move @@ -537,6 +548,7 @@ unsigned long move_page_tables(struct vm_area_struct *vma, new_pmd = alloc_new_pmd(vma->vm_mm, vma, new_addr); if (!new_pmd) break; +again: if (is_swap_pmd(*old_pmd) || pmd_trans_huge(*old_pmd) || pmd_devmap(*old_pmd)) { if (extent == HPAGE_PMD_SIZE && @@ -544,8 +556,6 @@ unsigned long move_page_tables(struct vm_area_struct *vma, old_pmd, new_pmd, need_rmap_locks)) continue; split_huge_pmd(vma, old_pmd, old_addr); - if (pmd_trans_unstable(old_pmd)) - continue; } else if (IS_ENABLED(CONFIG_HAVE_MOVE_PMD) && extent == PMD_SIZE) { /* @@ -556,11 +566,13 @@ unsigned long move_page_tables(struct vm_area_struct *vma, old_pmd, new_pmd, true)) continue; } - + if (pmd_none(*old_pmd)) + continue; if (pte_alloc(new_vma->vm_mm, new_pmd)) break; - move_ptes(vma, old_pmd, old_addr, old_addr + extent, new_vma, - new_pmd, new_addr, need_rmap_locks); + if (move_ptes(vma, old_pmd, old_addr, old_addr + extent, + new_vma, new_pmd, new_addr, need_rmap_locks) < 0) + goto again; } mmu_notifier_invalidate_range_end(&range);