Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2265315imm; Mon, 28 May 2018 05:04:04 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrrxA0Od0MXgqA0eyt/5P9kkPY9T4x0ho9SqS7I9+9BEt1jrCIQAx23TRc3C3SeEITaWg0q X-Received: by 2002:a63:43c6:: with SMTP id q189-v6mr10694171pga.123.1527509044762; Mon, 28 May 2018 05:04:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527509044; cv=none; d=google.com; s=arc-20160816; b=Zpl2N2cXAAiqGsnL7NdbvMsC7EcV1Nt92W++Rgyp5sBmBlAZWUfLic83jAHNwUPK6J YQdD+XqKwMVxZAoF1fTO9DVF8ZhZiXuZKbVI86mggea7Xn3JoVGMat0HzDfJ27Aj0SYK QtAUyhh9FXAsh+NncMgegbvEvKybNrZ3h6JaPXjugeMKxa9qCbjPh9Vr/P/GXamBPYSd vFhDL0OQVpvNfiqL0V5J3e4fDpXEIW6J+IZQCR4UYIwkvvtI523hgfBXaZ8DqN80CEme UnbmLqIqGs/skRpd7dGG/cv/ZWRcBy2aw/Y1kOzvSGt2MGfrwX2Joa6F0JBzqPIURKY1 CeeQ== 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=x1+fKbGJ2P9Rg2VF32ppn5QW7mzYH6kbPuSA7tgPmnA=; b=neEGybAMHqHOKbtaXEXVgFfhEJYRjkIvRyJ2GOVDjVGPjHrhEEawYc6Hiyv9bZTS3o kEmVXeAkcm5WYJZkRQKpdhCYnqEiiGG7eqRLm0uZ7JH80JjxVZZIqmTZSFZBRW90PsoS +h7t1rVFGydW/Lnp/wGmBsEV/W3zXm0OKxBd2n0E5WPVy4IaRsZCrpwbSJVxP61QIv+V 5fJYq/chWTvkgrwRpz000mAsT+pjfxVyr8xLx8Rw0OYkjW3YsQqU+NpzRmNhZyPE3Jz4 zgM7w5Jy1pj6YQKRJfqPx9dGbMQ7z527MrBAIOdH2DkhajroUZ9x3rboj8yoGR+4szpt kQ6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=IAp0V3Q2; 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 c13-v6si2421268pgq.316.2018.05.28.05.03.50; Mon, 28 May 2018 05:04:04 -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=IAp0V3Q2; 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 S1423345AbeE1LHD (ORCPT + 99 others); Mon, 28 May 2018 07:07:03 -0400 Received: from mail.kernel.org ([198.145.29.99]:53756 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1423348AbeE1LHA (ORCPT ); Mon, 28 May 2018 07:07:00 -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 6AA002075C; Mon, 28 May 2018 11:06:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1527505620; bh=GLiJlflypupj6G0Kqw4Xh9df7jYZ56l/iSVwrtatCpg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IAp0V3Q2+YQfRnUnjOYkUusRV0MijeFdhxvVbeLe0oDo1wDtoYpKURNstmEAnKrCd xlLbBuRe5ikQMyS9Ht+ivwJq8sJMg9aGi41RzEDEwqPCjw1zQeMOYvvHR72HZE3O3+ IJzNNxEQOvzrxy9wfdajFtqlC9LRIrFLIq1pEfUY= 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.16 068/272] mm/ksm: fix interaction with THP Date: Mon, 28 May 2018 12:01:41 +0200 Message-Id: <20180528100246.616864458@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180528100240.256525891@linuxfoundation.org> References: <20180528100240.256525891@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.16-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 @@ -2089,8 +2089,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) { /* @@ -2117,6 +2131,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); } } }