Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp898938pxb; Tue, 1 Feb 2022 12:44:27 -0800 (PST) X-Google-Smtp-Source: ABdhPJzpZpqxfY0eha71f+QXMbcZoAWvULCVXcUJ1Kgoq6HqlGneMYVcy1th2ClvL833dhBfkH1Z X-Received: by 2002:a63:e704:: with SMTP id b4mr22013539pgi.315.1643748267710; Tue, 01 Feb 2022 12:44:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643748267; cv=none; d=google.com; s=arc-20160816; b=NWs59007/EY7cRH2L8EntNWBLEi5z5MQ3u819K0sY43RBXxPiHaKna01tlX+HiKtNm Wbbhv1KRsvrpSfzc9MhxkCllXuBjWR9kuCVKIAlyo+KPHalmiMyb/ntHqMxpem41Fvmq Mw7dnSbjSQg1Bk4V4LClYi2yxFU1wd3Ekh6ss47yckpTweDpB5UQg7pqqXh9sJaJ4/+O qVw/znwfrReaEfu9dIGaPMdZ+3j01vAgT/PbTL5gYx2ot5DknpxaTCj62NOMBBFpNv18 jWgGm/4VQX1dGLrN8sTxgGlCyTdU/u3i9zr+zRmYOqdxmvs59sALNqH+MEPOC2jm88qO Ms8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Of3jZ+VHPXQ5Dt61jaIUicZu7ZwPC24uJWxR+ti2d3o=; b=yoYs1mJlr83G85A8uCLsillYNcuJ/PA1a85To5nay2DwLhODoIwZIh0MKQvXzV8dJv WknPgoKZ/HPTzrcZ3si3L2pOfY7EsZCy7JajW+JXxQi7S8ZYe5e2AUBid4Ap7GgB6Gcl bYIqn9DpzmqpbBPa/2SDDkitGucBIBLqTXqG/3bYJwQjLWy3HPzuHBKQfgXM95fs5JEw xWYuGS6YSox0lHBnVnjvqJn3JPhaBFBRV78T4Jkp1W8MVPSXWNCwuaGW9pgmpthKA59S sAfmWllEZBEprQ1LpTgLSw0ai71qfbsCCFtCKBqAaqoVk7s+XHPzvNmLduiF8PswW9ki 83OA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=RPkBrZyo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id oc18si3630723pjb.188.2022.02.01.12.44.16; Tue, 01 Feb 2022 12:44:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=RPkBrZyo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1380475AbiAaQdI (ORCPT + 99 others); Mon, 31 Jan 2022 11:33:08 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:21545 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1380456AbiAaQdE (ORCPT ); Mon, 31 Jan 2022 11:33:04 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643646783; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Of3jZ+VHPXQ5Dt61jaIUicZu7ZwPC24uJWxR+ti2d3o=; b=RPkBrZyow2wu/gvMX1Q5i5UJot6oJ401uo6IEPNLjejyHEAN28v150TX24avlLRKSCDmmv 23Ep6An6KxwoAMCGPUWo7MEq42OHe6JXa4YSyNMuGSMMdlr6FU8SMiZ90/RCY/ePIzME/m 2ACCTxzlSJI8cUq8FqcDlk+M6C2r2FI= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-115-3cmnDHY0Oh2_v_JeAp2-Iw-1; Mon, 31 Jan 2022 11:32:59 -0500 X-MC-Unique: 3cmnDHY0Oh2_v_JeAp2-Iw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 42E0486A066; Mon, 31 Jan 2022 16:32:56 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.39.193.115]) by smtp.corp.redhat.com (Postfix) with ESMTP id 601CB798CD; Mon, 31 Jan 2022 16:32:50 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: Andrew Morton , Hugh Dickins , Linus Torvalds , David Rientjes , Shakeel Butt , John Hubbard , Jason Gunthorpe , Mike Kravetz , Mike Rapoport , Yang Shi , "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@kvack.org, David Hildenbrand Subject: [PATCH v3 3/9] mm: slightly clarify KSM logic in do_swap_page() Date: Mon, 31 Jan 2022 17:29:33 +0100 Message-Id: <20220131162940.210846-4-david@redhat.com> In-Reply-To: <20220131162940.210846-1-david@redhat.com> References: <20220131162940.210846-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 - * 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