Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp556411pxk; Sun, 30 Aug 2020 14:02:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwkwOzoDF5zYGBitFafllFJ2ACDE4b1gnNaFZGoURNBzT8pT4qzgcqxNssOty+OZVXQQpfH X-Received: by 2002:a17:906:3acb:: with SMTP id z11mr8684327ejd.530.1598821365255; Sun, 30 Aug 2020 14:02:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598821365; cv=none; d=google.com; s=arc-20160816; b=qg/NZVgb2mEcgq8vtwhjSQYkkaRbHuEi8A1TwGwStABVdiysZW8F3+oNHGIt5ePLzm jJPPv1Mcha9MUm1Z5EvUmvs0I71zHc45rDy6Ec3TpCZzZTXogOCCuxZL3NayW10H1baG Ltwf9da8InJXLeVho4agHEBGpAS+U42a9fseieErmWGnCfZH47/yFkwgM6tCsJgfdVeQ SeWKp/vaX3GuEIG8EUzdWawDeVzR00Y8Us0q+oWFIGfLJD9r1XvM7lt6nwoiGDrDrggG DKaVxgm+pUFT9fMhYgoMI1dl0weu+rBGkENwmzsTw9f6BVgJceVOP99IyyYN2qndw4Bk LEuQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :message-id:in-reply-to:subject:cc:to:from:date:dkim-signature; bh=QBRmCzycKmVnu23sFsGovUalfChtQnjWcQWYwI41Axs=; b=cfHQ2TKaBiGqmrVcPmHpCvMVU+Aw3CDc9vnYP9d0+xyKIhDM8zxJjgcMueheK5cmzw t6iAT02aD+Iaaq5shqZPMLwCDu4sBm+7bMAPhfU03s1zQHTz7FoGkzwdJ748WBKQe0TM GceGgQpt7xDQDNr/J8dmj9ukwj4xC3CO3Tw+vAprO6u/3bqqBY+7hjclcVDcQwrMVlqs XbNDz89Xu3AZsqJJcDNqep5GLujTLWJa21yMtoEhJT63YpqC7ffkz0bBiukYSP7NqZCO kFG6SwrcSAPaPJFdWyA3yeEUpOZYrXIiDyxcanyRjXHfAFNbVJXix8CeA9KZTNucSOMp xjcw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=oRJZiql1; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l21si4392544eje.61.2020.08.30.14.02.22; Sun, 30 Aug 2020 14:02:45 -0700 (PDT) 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=@google.com header.s=20161025 header.b=oRJZiql1; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726404AbgH3U7l (ORCPT + 99 others); Sun, 30 Aug 2020 16:59:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52440 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726150AbgH3U7j (ORCPT ); Sun, 30 Aug 2020 16:59:39 -0400 Received: from mail-oi1-x241.google.com (mail-oi1-x241.google.com [IPv6:2607:f8b0:4864:20::241]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 09BBFC061573 for ; Sun, 30 Aug 2020 13:59:38 -0700 (PDT) Received: by mail-oi1-x241.google.com with SMTP id z22so5491908oid.1 for ; Sun, 30 Aug 2020 13:59:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:in-reply-to:message-id:references :user-agent:mime-version; bh=QBRmCzycKmVnu23sFsGovUalfChtQnjWcQWYwI41Axs=; b=oRJZiql1EBss2KvDcVmuLG7L3aCyu2+FBgk3j1giZZqACibEjKQb6SkstVBGYZU4QK bRrTWKp2d3YEVyAooD3KGlCqvmb6iSkk4Me6fnFWAAquDC1qrySjhUCG2z/DiQ3qN7qh mWKoNV40sLxpuJbCb8yMKMfwcLwJtPPdadVpvHiX/sUfeVrFocScInFqHGt2zKRCHjLD hXWib8afQ6yXkkv7yDORDIYjd42qidf6OqaPSyyRGcQIAXEIQPbSC17BKRwiURY+uoLm TiKpyPEFCFrS7VE1GQ1LnEdUuPjBlcxhYR5ZNIaPhWTWW8O252C6J+xMDzYWhX7KV7el PU9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:in-reply-to:message-id :references:user-agent:mime-version; bh=QBRmCzycKmVnu23sFsGovUalfChtQnjWcQWYwI41Axs=; b=WRwjWA+xjzIAGuVkvJQLX/v8rRjbL1c4jUt13VXYE2BAfSIl4BY5XGjk1eX6NkQt34 2xRfNxXfh3klci7nCx7RJe/XD8/8vDdOl6cTz+I9ZYJiCzlxjlvh0aXePGVERtEVIs+C 7jmX1V2rUx7tnPZm/Ik12hM3Yzfeg7NShL6/xkE8VYCUaTZZ3UhAt//3+lJC4d1QdCUM 6Fkt7mqyAQZYS79AR/YqjrUCcoTa7GrmJ4jB5bYAqJT0OmqOUFmCjbwTK+l0E0xuFYw8 0EZXfDwDWgpAKlCh028kyV//w9QBEjTmCSo/IWQM/cpgrvn2g/wZBBYBocl3/5hC9Id0 v90Q== X-Gm-Message-State: AOAM531M7FDhznARkPwEFzs9/uk54WSSXpjwbygPIHiBJ3k5FW1Ihgq4 mKBc1IAzcWqg67yafh/Am+KK6A== X-Received: by 2002:aca:1904:: with SMTP id l4mr1911081oii.67.1598821178001; Sun, 30 Aug 2020 13:59:38 -0700 (PDT) Received: from eggly.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id w136sm1256385oif.14.2020.08.30.13.59.36 (version=TLS1 cipher=ECDHE-ECDSA-AES128-SHA bits=128/128); Sun, 30 Aug 2020 13:59:36 -0700 (PDT) Date: Sun, 30 Aug 2020 13:59:35 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@eggly.anvils To: Andrew Morton cc: Alex Shi , Johannes Weiner , Michal Hocko , Mike Kravetz , Shakeel Butt , Matthew Wilcox , Qian Cai , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 1/5] ksm: reinstate memcg charge on copied pages In-Reply-To: Message-ID: References: User-Agent: Alpine 2.11 (LSU 23 2013-08-11) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In 5.8 some instances of memcg charging in do_swap_page() and unuse_pte() were removed, on the understanding that swap cache is now already charged at those points; but a case was missed, when ksm_might_need_to_copy() has decided it must allocate a substitute page: such pages were never charged. Fix it inside ksm_might_need_to_copy(). This was discovered by Alex Shi's prospective commit "mm/memcg: warning on !memcg after readahead page charged". But there is a another surprise: this also fixes some rarer uncharged PageAnon cases, when KSM is configured in, but has never been activated. ksm_might_need_to_copy()'s anon_vma->root and linear_page_index() check sometimes catches a case which would need to have been copied if KSM were turned on. Or that's my optimistic interpretation (of my own old code), but it leaves some doubt as to whether everything is working as intended there - might it hint at rare anon ptes which rmap cannot find? A question not easily answered: put in the fix for missed memcg charges. Fixes: 4c6355b25e8b ("mm: memcontrol: charge swapin pages on instantiation") Signed-off-by: Hugh Dickins Cc: stable@vger.kernel.org # v5.8 --- mm/ksm.c | 4 ++++ 1 file changed, 4 insertions(+) --- 5.9-rc2/mm/ksm.c 2020-08-16 17:32:50.645506940 -0700 +++ linux/mm/ksm.c 2020-08-28 17:42:07.967278385 -0700 @@ -2582,6 +2582,10 @@ struct page *ksm_might_need_to_copy(stru return page; /* let do_swap_page report the error */ new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address); + if (new_page && mem_cgroup_charge(new_page, vma->vm_mm, GFP_KERNEL)) { + put_page(new_page); + new_page = NULL; + } if (new_page) { copy_user_highpage(new_page, page, address, vma);