Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2510315imm; Mon, 28 May 2018 09:26:18 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqngPEk01E70/xsDvd7+zPDx7Mz+USLMumfzWV6LSJVbo3AYETYa1zJ6DKSryyn65sEuPa+ X-Received: by 2002:a17:902:b611:: with SMTP id b17-v6mr14116561pls.284.1527524778689; Mon, 28 May 2018 09:26:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527524778; cv=none; d=google.com; s=arc-20160816; b=dKdlJ3NVTD0qKTfzZ8cdfRiw0xOSnCUenDyoaYyweqHorOJCK11a5kN0doVwpVlSc+ gb2iqNVCshh12OmFNOf3QleTqD3etVCbU9tdRJuH2rWdQZK6pcZcrFqk7xRfSb3y0o2E 2u/iWlWu9SVMw9F+/nXIlTE5OOcqVaeHgVU8LaKaFzQ2gse2cpXuUN739/cP1ZVYtHAE BiNWSzGaq16goODVfWDLx1QLtUvbKLR4Kn5bYhxxgpZjMATr/5vsnHYNj2zcFgt1VXr0 RVM0FOhUpS0OEGyhuiOktO05pU0ajYoB0XNLFEMZKQ+1u04i2o/ccL7u9xZ2I0KxuoND NbkQ== 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=i8ytGruwtkCjM6zqH5Te7A0Dyd7jbUX63+SBo6U0HUw=; b=sMWOu0C4GlAS4cpUcnB7/p5s8l4i01EhEZ/l5rpK7GcYVfjW4OG4k0CN0k9G+E1DqX 0qF+skfS7FS4QwAigX8ggOFADEV8MX2zqujbceUfBKVAioTSGG56P+z/SbygX7F9rA5Y sN8JO5EaNL2VUVYPrTZVRnpuozxCYYKgOmwOZhtZ0umJjZD8H04TOktXwD2hAa+CZHrN qhuumz6ujXY6+qlEWeLTKbNEbHHhXwtjvSvKVui/GO3aly4awbHYWabX6tOWPYUellVS lIgBo3/D5fTtKnkgFd5Ku1fxhmG7Qi6QXtvw8iFwqo7eeDJBuutLI+IW/HU7AAvS7Pyd 3VyQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=StOHmlUw; 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 z26-v6si30575874pfd.26.2018.05.28.09.26.04; Mon, 28 May 2018 09:26:18 -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=StOHmlUw; 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 S966074AbeE1QZg (ORCPT + 99 others); Mon, 28 May 2018 12:25:36 -0400 Received: from mail.kernel.org ([198.145.29.99]:58962 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935013AbeE1KKq (ORCPT ); Mon, 28 May 2018 06:10:46 -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 66348208AF; Mon, 28 May 2018 10:10:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1527502245; bh=F5VLT6EJsVNklWKvqV38K9OQU5xkQFimq5nj0bbG8Rw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=StOHmlUwe716l1C84VI//Id8AHGSBYRQ3+1W+HFWUiiOXjmXkEc7G+imqvyemnoKJ KybjystiOJy4hyN16U3yg8J/TqTyOslCWBrG37+jRMsYQb5Pfm7U4pQtDgQKj+Y+WI bobuToGP8AiFspVOv2Ec2ketwWjxolIIFIhsSrZ4= 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 3.18 116/185] mm/ksm: fix interaction with THP Date: Mon, 28 May 2018 12:02:37 +0200 Message-Id: <20180528100101.823061351@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180528100050.700971285@linuxfoundation.org> References: <20180528100050.700971285@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 3.18-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 @@ -1475,8 +1475,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) { /* @@ -1501,6 +1515,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); } } }