Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp703308pxp; Wed, 9 Mar 2022 10:52:56 -0800 (PST) X-Google-Smtp-Source: ABdhPJw7Hs0ithZ0hRyyYehkOFHvjgq7w6Fkk3yHxR2BueGTDTmlOBYqobg4OZY9CnjMnW5b9Ufj X-Received: by 2002:a17:902:b488:b0:14e:fcef:7f0b with SMTP id y8-20020a170902b48800b0014efcef7f0bmr1076315plr.28.1646851975996; Wed, 09 Mar 2022 10:52:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646851975; cv=none; d=google.com; s=arc-20160816; b=YZjnGxT5ZEGRFOkjlAcfpZdTcMgE8vJkwJkFskXVPBkBLVkZ7jJxvJWRs0gv7WpSJO C4XJ7S8ZRbmk1JSNGCYIrNe/4u4SEUOWBNJICTX43jqzAFkK+X0qiJDOwlsydiUol6Uo C4ptPdL0Mp7RelspsgnVOGbmulfsPv1+sgOPF1qHS2H8cci/Xmuq9gs6Xh96OnsyH03B 5nbD4kyxd1a/9C1P68Nz7kKMSFTa/y8wdDKnD4Y6lXFHYdPoZcLZXPqoBfLGVWjWXIxr mfAYfe5naFtMjH7URpt03pyjS66D9jHqtblfJVNScZKFj8JAu8zm3cr3mYxmOL1yi73G RaNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=lvs4wUkEI3AtSweELbG0/fXXinq75WyLekwDxmjo0Pc=; b=flq3zZfwtWFG0N/OFO4aGRjsTB+exgoPt38tdOEj5r/4uEkmyxKMylmMVQPuM30bn9 XfXmiql0V3niUnSWx5PguG/sNesUm8xbe3Utrj8dvIajRztC/Dqj6lQAx+IC/mIFRNPG FPh2Tm4vQdMLWXwTnSybDIZ4TQwqe6DbaZz/dLI9+OZ6f//o0jEznGglm9uZwWHiBNyz 7t/ZIAG5xiE3RVEftjeWiOntsjqgdGTWwQ7pDvxp66vYBoErhVd2/VqzV52H/sCBVTRV 6e1cqpvOEIxPD5KzxhwcbjJGi5p1fThvpzZIB06t05VnshjZ69vhza4Q46uZ4aOU6m/6 j+eQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=YlzjEPe1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l190-20020a6388c7000000b0037d79ea3b81si2571582pgd.94.2022.03.09.10.52.38; Wed, 09 Mar 2022 10:52:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=YlzjEPe1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236890AbiCIStm (ORCPT + 99 others); Wed, 9 Mar 2022 13:49:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53420 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236849AbiCIStg (ORCPT ); Wed, 9 Mar 2022 13:49:36 -0500 Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE99A1A39F0 for ; Wed, 9 Mar 2022 10:48:35 -0800 (PST) Received: by mail-pj1-x1034.google.com with SMTP id m11-20020a17090a7f8b00b001beef6143a8so3102774pjl.4 for ; Wed, 09 Mar 2022 10:48:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=lvs4wUkEI3AtSweELbG0/fXXinq75WyLekwDxmjo0Pc=; b=YlzjEPe1xEmK+sY/CImJXAlfj9ZDiWWMlWq/1+rmGwLnPSArxZVHBK74+hSNPw7JIw yj2X8+VH0+WOt3SSXKiIVHVvKY7AaM6uDCBII9/qOl1vIP8SONFtgN0kx1abVQAIf1Rp OVa7e+k9y4s6Kyk0dc3larGnr8KxDnThJvACaTM2HnhzIKtMjljD0HWjLh1eDYAhBhca pohsGzEjrouJgnSjrPPT6wwOOqyphzaNYcZc7FzFJsBGOIvo+hbBEyMs+YFvPsoBIj9g iZpCJNblczziVOJjA67euin7YRsHsZjTP0eLZQRKJ5ni0/8vGTgiC7RlMDof+sFZp31i 1AiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=lvs4wUkEI3AtSweELbG0/fXXinq75WyLekwDxmjo0Pc=; b=BlN+gk+29USrBuGX/9wnac3RDCRQdBfZL6xxjBLWa0ANwoFZ4zz8JlINjOVPL99qPf n4YHFAskS5+LztEU28zhwhLAEYhs8JfPSReYl3868s7EDFEAIKru02bNPGa+Cbw0V5MD 2rH+MaFPpvqFHPV2t/0vpPtkipZXJG1Pnv57bDJc9QeH+PGzWLYdd/984HZMp4EaOiBr Ta704pmzwDquRytIS63SHAqlT4JUw8N8BsP59NMr/25q6bCS4Bo5t0A42liYAxBAusxL aWbs6TVukrQE9KWE4GgqiHsB2T/ncy+GChglonw5G/SXahPm9MBfPSLrayl5b4YTIz/r m/DQ== X-Gm-Message-State: AOAM531XmjLRyIUtufb6rPuOUSRPf5TmqqU/lyhNmlsIAB3RvnSY2bT/ hSyf8hM0cm/hXmliTLtuWSLMmzEtvBeEQ3UdFxc= X-Received: by 2002:a17:903:124a:b0:151:99fe:1a10 with SMTP id u10-20020a170903124a00b0015199fe1a10mr847964plh.87.1646851715266; Wed, 09 Mar 2022 10:48:35 -0800 (PST) MIME-Version: 1.0 References: <20220131162940.210846-1-david@redhat.com> <20220131162940.210846-4-david@redhat.com> In-Reply-To: <20220131162940.210846-4-david@redhat.com> From: Yang Shi Date: Wed, 9 Mar 2022 10:48:23 -0800 Message-ID: Subject: Re: [PATCH v3 3/9] mm: slightly clarify KSM logic in do_swap_page() To: David Hildenbrand Cc: Linux Kernel Mailing List , Andrew Morton , Hugh Dickins , Linus Torvalds , David Rientjes , Shakeel Butt , John Hubbard , Jason Gunthorpe , Mike Kravetz , Mike Rapoport , "Kirill A . Shutemov" , Matthew Wilcox , Vlastimil Babka , Jann Horn , Michal Hocko , Nadav Amit , Rik van Riel , Roman Gushchin , Andrea Arcangeli , Peter Xu , Donald Dutile , Christoph Hellwig , Oleg Nesterov , Jan Kara , Liang Zhang , Linux MM Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jan 31, 2022 at 8:33 AM David Hildenbrand wrote: > > Let's make it clearer that KSM might only have to copy a page > in case we have a page in the swapcache, not if we allocated a fresh > page and bypassed the swapcache. While at it, add a comment why this is > usually necessary and merge the two swapcache conditions. > > Signed-off-by: David Hildenbrand > --- > mm/memory.c | 38 +++++++++++++++++++++++--------------- > 1 file changed, 23 insertions(+), 15 deletions(-) > > diff --git a/mm/memory.c b/mm/memory.c > index 923165b4c27e..3c91294cca98 100644 > --- a/mm/memory.c > +++ b/mm/memory.c > @@ -3615,21 +3615,29 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) > goto out_release; > } > > - /* > - * Make sure try_to_free_swap or reuse_swap_page or swapoff did not We could remove the reference to "reuse_swap_page", right? > - * release the swapcache from under us. The page pin, and pte_same > - * test below, are not enough to exclude that. Even if it is still > - * swapcache, we need to check that the page's swap has not changed. > - */ > - if (unlikely((!PageSwapCache(page) || > - page_private(page) != entry.val)) && swapcache) > - goto out_page; > - > - page = ksm_might_need_to_copy(page, vma, vmf->address); > - if (unlikely(!page)) { > - ret = VM_FAULT_OOM; > - page = swapcache; > - goto out_page; > + if (swapcache) { > + /* > + * Make sure try_to_free_swap or reuse_swap_page or swapoff did > + * not release the swapcache from under us. The page pin, and > + * pte_same test below, are not enough to exclude that. Even if > + * it is still swapcache, we need to check that the page's swap > + * has not changed. > + */ > + if (unlikely(!PageSwapCache(page) || > + page_private(page) != entry.val)) > + goto out_page; > + > + /* > + * KSM sometimes has to copy on read faults, for example, if > + * page->index of !PageKSM() pages would be nonlinear inside the > + * anon VMA -- PageKSM() is lost on actual swapout. > + */ > + page = ksm_might_need_to_copy(page, vma, vmf->address); > + if (unlikely(!page)) { > + ret = VM_FAULT_OOM; > + page = swapcache; > + goto out_page; > + } > } > > cgroup_throttle_swaprate(page, GFP_KERNEL); > -- > 2.34.1 >