From nobody Sun Feb 8 08:42:28 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 E0BA5C7EE29 for ; Fri, 9 Jun 2023 01:36:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237454AbjFIBgT (ORCPT ); Thu, 8 Jun 2023 21:36:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45696 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230138AbjFIBgR (ORCPT ); Thu, 8 Jun 2023 21:36:17 -0400 Received: from mail-yw1-x1131.google.com (mail-yw1-x1131.google.com [IPv6:2607:f8b0:4864:20::1131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 32AFA1FDF for ; Thu, 8 Jun 2023 18:36:16 -0700 (PDT) Received: by mail-yw1-x1131.google.com with SMTP id 00721157ae682-565cfe4ece7so11390107b3.2 for ; Thu, 08 Jun 2023 18:36:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686274575; x=1688866575; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=7TjV4RyPitx7LIAI3Pj7MSABK+uluBQcp84mqUzVC6k=; b=a5ewmipktwitFlsPba8meBOEdOaTniMgfwOwR66LxzGfjmzDH4IFIjppt+zCAFlN0g 4qVFbiljADzlv99p9uKv0I6LLrtlgpWP5C7ZN5jYvFhWguH9wpOq9f3yGPT9NIjCbt8f ccDNMfVoLAT69ZaBFF/iOjiPqCtqRwYPT0P1Yb6UZGbQci3A4kQCZ/DTEhdlS+lQWdYY KzmqHWulcwNNrAp2wlegE+sh4YyxODsI8618mTIxeZ3NAKuS71wgysjyvDWVHPesaDpk PafA85IWdjWaVeXqTpvoM+ur2RY2SxdVNDgFZ9WN6yrvvp/2wYtNHHpsHPpA7aoAQsWI /+rA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686274575; x=1688866575; 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=7TjV4RyPitx7LIAI3Pj7MSABK+uluBQcp84mqUzVC6k=; b=Dr0g3tJVQRJk1oseruzU8LjbDYcxIE3mTc0rVo3UUwLy3yEFWvRLlbXAFltfB+uvRs /SdDc1lerothQDoUxiEQMOVQC4+R5B/kpsU08A+ijRe3Hr5+wknarGDIyVqsWyEIRCMc yIoThA+80I/yF1FSJhkfH1+j3bN9ksqJdn2gMvTDNXuV9Ix/lS0ivWJCeZgIC/EafNqo MajkBNYhTD5SzY4aYKM3ktq1BA3RbB3c14CjuJ2nthtG2kZohYFtWdcQrocG2zCcoe+R 0406a/TFSM8Q7Qi3e8l6y4KWflEHLrUZhJ/JBLGb7YHJoSo7D5HaUR2Us5PxFZj7n2RX a4ag== X-Gm-Message-State: AC+VfDyiBbIsFTW5HGLny9BLgdM+InmvrFHGWtJOxFkRp0CqL/x3G0Ki NLd156vzZpMZouMKi+U7Fu0kHg== X-Google-Smtp-Source: ACHHUZ41tpEmUAG3j7GY9w97TvrJQ50NkLe4OKcO0eJ30STZxTamEDNBY6XFXEyrE/Y12cF7SVrDgA== X-Received: by 2002:a81:a0c9:0:b0:568:a870:314f with SMTP id x192-20020a81a0c9000000b00568a870314fmr1280229ywg.30.1686274575219; Thu, 08 Jun 2023 18:36:15 -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 d191-20020a0ddbc8000000b00569fdf7f58bsm293760ywe.66.2023.06.08.18.36.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jun 2023 18:36:14 -0700 (PDT) Date: Thu, 8 Jun 2023 18:36:11 -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 22/32] mm/swapoff: allow pte_offset_map[_lock]() to fail In-Reply-To: Message-ID: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Adjust unuse_pte() and unuse_pte_range() to allow pte_offset_map_lock() and pte_offset_map() failure; remove pmd_none_or_trans_huge_or_clear_bad() from unuse_pmd_range() now that pte_offset_map() does all that itself. Signed-off-by: Hugh Dickins --- mm/swapfile.c | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 274bbf797480..12d204e6dae2 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1774,7 +1774,7 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_= t *pmd, hwposioned =3D true; =20 pte =3D pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); - if (unlikely(!pte_same_as_swp(*pte, swp_entry_to_pte(entry)))) { + if (unlikely(!pte || !pte_same_as_swp(*pte, swp_entry_to_pte(entry)))) { ret =3D 0; goto out; } @@ -1827,7 +1827,8 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_= t *pmd, set_pte_at(vma->vm_mm, addr, pte, new_pte); swap_free(entry); out: - pte_unmap_unlock(pte, ptl); + if (pte) + pte_unmap_unlock(pte, ptl); if (page !=3D swapcache) { unlock_page(page); put_page(page); @@ -1839,17 +1840,22 @@ static int unuse_pte_range(struct vm_area_struct *v= ma, pmd_t *pmd, unsigned long addr, unsigned long end, unsigned int type) { - swp_entry_t entry; - pte_t *pte; + pte_t *pte =3D NULL; struct swap_info_struct *si; - int ret =3D 0; =20 si =3D swap_info[type]; - pte =3D pte_offset_map(pmd, addr); do { struct folio *folio; unsigned long offset; unsigned char swp_count; + swp_entry_t entry; + int ret; + + if (!pte++) { + pte =3D pte_offset_map(pmd, addr); + if (!pte) + break; + } =20 if (!is_swap_pte(*pte)) continue; @@ -1860,6 +1866,8 @@ static int unuse_pte_range(struct vm_area_struct *vma= , pmd_t *pmd, =20 offset =3D swp_offset(entry); pte_unmap(pte); + pte =3D NULL; + folio =3D swap_cache_get_folio(entry, vma, addr); if (!folio) { struct page *page; @@ -1878,8 +1886,7 @@ static int unuse_pte_range(struct vm_area_struct *vma= , pmd_t *pmd, if (!folio) { swp_count =3D READ_ONCE(si->swap_map[offset]); if (swp_count =3D=3D 0 || swp_count =3D=3D SWAP_MAP_BAD) - goto try_next; - + continue; return -ENOMEM; } =20 @@ -1889,20 +1896,17 @@ static int unuse_pte_range(struct vm_area_struct *v= ma, pmd_t *pmd, if (ret < 0) { folio_unlock(folio); folio_put(folio); - goto out; + return ret; } =20 folio_free_swap(folio); folio_unlock(folio); folio_put(folio); -try_next: - pte =3D pte_offset_map(pmd, addr); - } while (pte++, addr +=3D PAGE_SIZE, addr !=3D end); - pte_unmap(pte - 1); + } while (addr +=3D PAGE_SIZE, addr !=3D end); =20 - ret =3D 0; -out: - return ret; + if (pte) + pte_unmap(pte); + return 0; } =20 static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud, @@ -1917,8 +1921,6 @@ static inline int unuse_pmd_range(struct vm_area_stru= ct *vma, pud_t *pud, do { cond_resched(); next =3D pmd_addr_end(addr, end); - if (pmd_none_or_trans_huge_or_clear_bad(pmd)) - continue; ret =3D unuse_pte_range(vma, pmd, addr, next, type); if (ret) return ret; --=20 2.35.3