Received: by 10.213.65.68 with SMTP id h4csp301231imn; Mon, 12 Mar 2018 14:23:47 -0700 (PDT) X-Google-Smtp-Source: AG47ELs1Y7aAI74HE6m+3KHoxe/AgmbMswfrP5OfCelNgVbmDX8a2H1ZrNUdECdx/DgCmlZ352UF X-Received: by 2002:a17:902:bf03:: with SMTP id bi3-v6mr9439058plb.343.1520889827856; Mon, 12 Mar 2018 14:23:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1520889827; cv=none; d=google.com; s=arc-20160816; b=IoPx+nHFQz51ZGaPHFm0FYbCgDkCEBkIEMkAIUqlUzt3+OXAy9JQ44LiBiJwc7hXpX GOyzH1NA664T6h2KJU+ndU9G2w4apMp4vDA5GVu3T3arB8L4F6gkqBNlTG+NHKR/Xs8q eLNRZWYMPgRgUFLNdfzVZUtjDDS7m8gKxnnTAdidprBFSBTV5oVEhYKdSk2MXT4w1qii huP47OMt9fP5T89HWkJXX5ywDqmzjD2WGMZSOyesza6phZRyb5FW6c67wM3zecNsy0OD H6pphS1rvLWQ49rdvrI9AaaxwxL6bgbJGGkXjahTh9STJTggMiF9t4FlXIFJ594F/yJD JJwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :arc-authentication-results; bh=UZ6U/D2/d6keZrNWtaq+rQrs+opg/qLQF97QBCRTpOQ=; b=I/Ofu/TKrPYMAaMb1fz2J+ZyDRtbUrQ0l9b2FKA9wsxLVaxeW2u2HQeT4lIamP2WS3 d9LPkm3e+EgEWZFeaw7TgXnSdryMwhE8BKlTGZ+zIRin80cAUPjHk4sxhQlU/KlaVUXy 8qNfpNtZZNmEBYNqAnvAciOS8kBrE5MxqiLbkr9qP3ot0kzyvDSg3wfZNOp+HsyBn1I1 X3Ecr92LD1U7xaR0x2Loj+7zgknJovenhzy3AQOm78iIlIzQMrJTcriHuRQ/AxsxncZA OVme8SFPG4Tw9XvQ1z5z49Y83jiBQK7cDt9czrjp2TO7ihfm6kdb6XR7VeqeuO3ierES JuRw== ARC-Authentication-Results: i=1; mx.google.com; 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 b10si6408234pff.317.2018.03.12.14.23.29; Mon, 12 Mar 2018 14:23:47 -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; 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 S932327AbeCLVWN (ORCPT + 99 others); Mon, 12 Mar 2018 17:22:13 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:40514 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932242AbeCLVWM (ORCPT ); Mon, 12 Mar 2018 17:22:12 -0400 Received: from akpm3.svl.corp.google.com (unknown [104.133.9.71]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id E8FF2F5B; Mon, 12 Mar 2018 21:22:11 +0000 (UTC) Date: Mon, 12 Mar 2018 14:22:10 -0700 From: Andrew Morton To: Claudio Imbrenda Cc: linux-kernel@vger.kernel.org, aarcange@redhat.com, minchan@kernel.org, kirill.shutemov@linux.intel.com, linux-mm@kvack.org, hughd@google.com, pholasek@redhat.com, borntraeger@de.ibm.com, gerald.schaefer@de.ibm.com Subject: Re: [PATCH v1 1/1] mm/ksm: fix interaction with THP Message-Id: <20180312142210.4e664519118369d5d129e6dc@linux-foundation.org> In-Reply-To: <1520872937-15351-1-git-send-email-imbrenda@linux.vnet.ibm.com> References: <1520872937-15351-1-git-send-email-imbrenda@linux.vnet.ibm.com> X-Mailer: Sylpheed 3.6.0 (GTK+ 2.24.31; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 12 Mar 2018 17:42:17 +0100 Claudio Imbrenda wrote: > 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. > > Signed-off-by: Gerald Schaefer > Signed-off-by: Claudio Imbrenda Signoff trail is confusing. Usually people put the primary author's signoff first, which makes me wonder whether you or Gerald was the primary author? > diff --git a/mm/ksm.c b/mm/ksm.c > index 293721f..7a826fa 100644 > --- a/mm/ksm.c > +++ b/mm/ksm.c > @@ -2001,7 +2001,7 @@ static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item) > struct page *kpage; > unsigned int checksum; > int err; > - bool max_page_sharing_bypass = false; > + bool split, max_page_sharing_bypass = false; `split' could be made local to the `if' block where it is used, which improves readability and maintainability somewhat. > stable_node = page_stable_node(page); > if (stable_node) { > @@ -2084,6 +2084,8 @@ static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item) > if (tree_rmap_item) { > kpage = try_to_merge_two_pages(rmap_item, page, > tree_rmap_item, tree_page); > + split = PageTransCompound(page) && PageTransCompound(tree_page) > + && compound_head(page) == compound_head(tree_page); I think a comment explainig what's going on would be useful here. > put_page(tree_page); > if (kpage) { > /* > @@ -2110,6 +2112,11 @@ static void cmp_and_merge_page(struct page *page, struct rmap_item *rmap_item) > break_cow(tree_rmap_item); > break_cow(rmap_item); > } > + } else if (split) { > + if (!trylock_page(page)) > + return; > + split_huge_page(page); > + unlock_page(page); Why did we use trylock_page()? Perhaps for the same reasons which were explained in try_to_merge_one_page(), perhaps for other reasons. cmp_and_merge_page() already does lock_page() and down_read(), so I wonder if those reasons are legitimate. Again, a comment here is needed - otherwise it will be hard for readers to understand your intent. > } > } > }