Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2453825imm; Mon, 28 May 2018 08:21:49 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrPsex8XpXL/UJMam6agxQ/pWs9pZO4kTPdeWd8hl8p7RPmriEh/cHhDPtC7yjOxVLOJJTh X-Received: by 2002:a17:902:6105:: with SMTP id t5-v6mr13991358plj.138.1527520909030; Mon, 28 May 2018 08:21:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527520908; cv=none; d=google.com; s=arc-20160816; b=Nt6tiqw+e2r5HFpuhzkOTGV8hDRx9JoKeb5mpd2WKId+JfmV7ZkLz1y4ucDQ7n+g7L ch9TnuUUR6SuSkTKpKPGwM2WBPZl5t32mXikjQgr6dLUWTevbFvkhBeQeqgJNHEFRjtn WoydoBH1zhoqG7rhgwjgW+uXxsKuR2aaXoJ3hYQgMTCgdem5iksbwx8357yWYP7RjW3l ffzpbvJ1yMNsIj/i4h3St0PsQ9rmKLKGu1UU4+CPuvXEgoF/1euMw+IwTQ4Hr0abzTRn 7rLIqBhXLkIsydThhGx7aIBhoMH0fVRJQowzghS01Aud0LoiRb5xkvwNM4FnT4dYQXNE FB9g== 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=fF88oDkWAVYpXWFQw7yzpbWpA1Pq9+6KH0fHdndPwMo=; b=oLXBUkuth6wCGQDLvhyUa58vCMCa0hkaxzfOwwyIJoNzMDuR+3BsO3dqLQfuMPicum gO7CtvX444dyV39dp6vBJwj22iOvCv5E7VY2QY91BRioa2mt47s1bTe1d1uLJB7Af8Qw zPwxJUvE5u/mek0TakO8geeeKdMEPIgXOmXXvhxgS2UUw0+hCzmYDseOzgAdprifhLtT UFr2WS5AmI3nJPajW+duTzV+ODof2YeK0WxXVRGYFkrAORIaJYdTFMSK4/PG/UWUx1Ds xb3ZwZBeVBiid0JuMfTkqw3AnKJ4qq5houuviS0971j4X8NR3DvWRXpNuYQt9UQKqTyb tOeg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=vBiu4DC1; 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 q11-v6si10290530pll.10.2018.05.28.08.21.34; Mon, 28 May 2018 08:21:48 -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=vBiu4DC1; 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 S1033496AbeE1PVA (ORCPT + 99 others); Mon, 28 May 2018 11:21:00 -0400 Received: from mail.kernel.org ([198.145.29.99]:41060 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1033343AbeE1KVf (ORCPT ); Mon, 28 May 2018 06:21:35 -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 8010120876; Mon, 28 May 2018 10:21:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1527502894; bh=Ri8g1p0BpUhZJ9lZeOYFugNkXw55/4aE22FtriEI3VU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vBiu4DC1fY4k1vsyVQYurugC5hn9IEH6ndeUbgm9+kOfGzotaecyb1bWUOq+nZB7p JzimuH7jMsv9o7lAm5mULp+KaGFlVQmB2vpHcdf2syxt6jY9UGqtAHI8vPptOxHZPV oEezIGvWGBhywJMjTVz9C+fBBK6TuDIJA8SqSE2w= 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.4 167/268] mm/ksm: fix interaction with THP Date: Mon, 28 May 2018 12:02:21 +0200 Message-Id: <20180528100221.317780826@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180528100202.045206534@linuxfoundation.org> References: <20180528100202.045206534@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.4-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 @@ -1494,8 +1494,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) { /* @@ -1520,6 +1534,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); } } }