Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2309088imm; Mon, 28 May 2018 05:50:34 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpNFSfTKqGV4cHBMfjFo+bv9Jj8YCErK8WwC07QNpT2Pe0zyIUO7uOcZNFnMAGsVcuNKzeW X-Received: by 2002:a62:f24a:: with SMTP id y10-v6mr13431916pfl.75.1527511834925; Mon, 28 May 2018 05:50:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527511834; cv=none; d=google.com; s=arc-20160816; b=nvjeZ/jYGhXJsqzDlmjg1CfAh5+SZ+KBHSXPefXTJn0fV3710mIg7l85CmC5J46jIY 0HxPhqwaEbyLO+BFqtWBnRmUDgG147GHMgydnPqCc4HLsBS5oiTQt1yNPqjup39D43Uu nCxOzZ238Fvt0rXqRQvHLTMsgB+fMHBZMKUnY4DTbqj0sa22mWAEIvUu2r9G75DvLlf1 Ag9x5WdRzvliIJ9C0gs6bet1LDj7T+JSbMLFpnPIPu393lIfU20pU0ldPTKykvMtk6pn 01hno2QIcQg+wcDFoXlWo89E60IlpHzXfTYZ+LMF5j3lvYv4nb2TyXJiwyKMWNeyB45b NjyQ== 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 :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=CZBmqkvI/43Sl7fHFxLJJHS3s+wv4CAEcoxU5mKiDbA=; b=NgP4LsW0xjLk4uRJV3usBwEmWAPmbvRADv5pF2IjTx6iq6qJ4USD1x/T+2FoTVfMvc JucJvP99ay8yrC2MCoZgS/NZwBndvz+KKaYWIGlJfw1NCUFl8Ghd6wdhLQEERGA1b9Fu j+q0kpkeFTbHmlUyzoD7H6aC4H5lkrZfJMwDF3LpXhOyoBzCzTmLFEgU7IXOBC2rukGL U4S4xqBwCyL+wZg0Kn6qtNdweSjAerMZM4t+kqNPXIWnR3h1rUM86eoK0136VMU5bQAT Z1rHQTOTYqxq4GM8Ykd0E6PWFJaWYpcyqjr0b6zT1oSeBqB/cDxkkYPOvo05N8TjBibi D7Ug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=pRYlrMez; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k10-v6si16895677pgs.598.2018.05.28.05.50.19; Mon, 28 May 2018 05:50:34 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=pRYlrMez; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1164584AbeE1K4t (ORCPT + 99 others); Mon, 28 May 2018 06:56:49 -0400 Received: from mail.kernel.org ([198.145.29.99]:44856 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1164532AbeE1K4a (ORCPT ); Mon, 28 May 2018 06:56:30 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 8D339208A2; Mon, 28 May 2018 10:56:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1527504990; bh=UhVFG0fd04QF83aekSugtK7c+yqmVf5Wasas2B6gnOA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pRYlrMezqCbpEfafXo7Y77UTrp/SXtoCt+kS5vL2z9Fw427wT0ESljMLnYenrzUYs teNveUwq30mERXlQv8gy5ffvz1/Yn+XtYsrt1qqM/hqyYDTOW33CEzVwGPx5qv+uCr 4oGUmm5pTcruoEx2cNk24wRweC0iOJ1QKOCIQUSM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Claudio Imbrenda , Andrew Morton , Andrea Arcangeli , Minchan Kim , "Kirill A. Shutemov" , Hugh Dickins , Christian Borntraeger , Linus Torvalds , Sasha Levin Subject: [PATCH 4.14 335/496] mm/ksm: fix interaction with THP Date: Mon, 28 May 2018 12:02:00 +0200 Message-Id: <20180528100333.936806626@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180528100319.498712256@linuxfoundation.org> References: <20180528100319.498712256@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Claudio Imbrenda [ Upstream commit 77da2ba0648a4fd52e5ff97b8b2b8dd312aec4b0 ] This patch fixes a corner case for KSM. When two pages belong or belonged to the same transparent hugepage, and they should be merged, KSM fails to split the page, and therefore no merging happens. This bug can be reproduced by: * making sure ksm is running (in case disabling ksmtuned) * enabling transparent hugepages * allocating a THP-aligned 1-THP-sized buffer e.g. on amd64: posix_memalign(&p, 1<<21, 1<<21) * filling it with the same values e.g. memset(p, 42, 1<<21) * performing madvise to make it mergeable e.g. madvise(p, 1<<21, MADV_MERGEABLE) * waiting for KSM to perform a few scans The expected outcome is that the all the pages get merged (1 shared and the rest sharing); the actual outcome is that no pages get merged (1 unshared and the rest volatile) The reason of this behaviour is that we increase the reference count once for both pages we want to merge, but if they belong to the same hugepage (or compound page), the reference counter used in both cases is the one of the head of the compound page. This means that split_huge_page will find a value of the reference counter too high and will fail. This patch solves this problem by testing if the two pages to merge belong to the same hugepage when attempting to merge them. If so, the hugepage is split safely. This means that the hugepage is not split if not necessary. Link: http://lkml.kernel.org/r/1521548069-24758-1-git-send-email-imbrenda@linux.vnet.ibm.com Signed-off-by: Claudio Imbrenda Co-authored-by: Gerald Schaefer Reviewed-by: Andrew Morton Cc: Andrea Arcangeli Cc: Minchan Kim Cc: Kirill A. Shutemov Cc: Hugh Dickins Cc: Christian Borntraeger Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- mm/ksm.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) --- a/mm/ksm.c +++ b/mm/ksm.c @@ -2085,8 +2085,22 @@ static void cmp_and_merge_page(struct pa tree_rmap_item = unstable_tree_search_insert(rmap_item, page, &tree_page); if (tree_rmap_item) { + bool split; + kpage = try_to_merge_two_pages(rmap_item, page, tree_rmap_item, tree_page); + /* + * If both pages we tried to merge belong to the same compound + * page, then we actually ended up increasing the reference + * count of the same compound page twice, and split_huge_page + * failed. + * Here we set a flag if that happened, and we use it later to + * try split_huge_page again. Since we call put_page right + * afterwards, the reference count will be correct and + * split_huge_page should succeed. + */ + split = PageTransCompound(page) + && compound_head(page) == compound_head(tree_page); put_page(tree_page); if (kpage) { /* @@ -2113,6 +2127,20 @@ static void cmp_and_merge_page(struct pa break_cow(tree_rmap_item); break_cow(rmap_item); } + } else if (split) { + /* + * We are here if we tried to merge two pages and + * failed because they both belonged to the same + * compound page. We will split the page now, but no + * merging will take place. + * We do not want to add the cost of a full lock; if + * the page is locked, it is better to skip it and + * perhaps try again later. + */ + if (!trylock_page(page)) + return; + split_huge_page(page); + unlock_page(page); } } }