Received: by 10.213.65.68 with SMTP id h4csp304540imn; Tue, 13 Mar 2018 05:05:46 -0700 (PDT) X-Google-Smtp-Source: AG47ELukElbVr2w9wmKNhMdKzUTqVRRYW8uMJiWxFj8DVtpU26ZQnXV7rMzcK23xDJf0+8cvaQUB X-Received: by 10.98.9.130 with SMTP id 2mr358437pfj.149.1520942746236; Tue, 13 Mar 2018 05:05:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1520942746; cv=none; d=google.com; s=arc-20160816; b=hCesCn6gh7X/JqDV0j6B2BEcTkGm2pAbSOmyXOZ9s9jpnDacC1VZgUzYYPR313PhpT RSrO8YOOILCSGNTsEm93cI5Dd70lAdOCGAXp/Z9jlttKHrCECU7/rHoNWl4HoEudmBvT 8CRgdNCvQmC4oBMfV4TA2XJy8XpifHBf0crWppzH1RsSdx+HgIaNnAlUGF+Tq9QO5eur SgLEXiEsYXm8B5D2/BQz/h3TQqXqlDw4a3T7gnTaMYEGApEuNG9GAHiY+PCssBJZb7dU kdNU+9bxX9QQjml2S1cf0P+bh5LTATii8N5ZgwqUL3ChYHkw6Qx6HAVAzQEFrc71DH7Z tm/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:content-transfer-encoding :mime-version:organization:references:in-reply-to:subject:cc:to:from :date:arc-authentication-results; bh=UD5cHux11qtVi40Tq1Uxq2+dz6GN+FusfRWJXLW6bto=; b=iamBrBy1fMc/8DaqYSsX6Q1qGJh4BravfbUVQFSUvbgRcWq0d3vfOiIIS55hqYPy0I 8Hlft1pcLw+iAZmmbwikKDvzm0mLyDJyi6ohj4KHBDB+4/RMLBkXchkmwlmDPzO6eBvu KFzXti+BoAwIopqkxLqskTGtQ4M9mhUQx3vnvZqbTMuZjGznxKb7zfolG4k1imGc9T56 vXGcdR0/azXr2T4ahtJ9BAJvWxkJEqmpIirvi3pY8XR+tWDaRlacrHdqeIJnN97vzl16 f+a0LFfXSS+dQz3F3sXtsKDkAFByx3H27YeXI2Nezilac+o6A5vV65x5HdOLoICDb08+ NIoQ== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o62si26836pga.158.2018.03.13.05.05.30; Tue, 13 Mar 2018 05:05:46 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752462AbeCMMEb (ORCPT + 99 others); Tue, 13 Mar 2018 08:04:31 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:43748 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752306AbeCMMEa (ORCPT ); Tue, 13 Mar 2018 08:04:30 -0400 Received: from pps.filterd (m0098417.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w2DC48Yh004394 for ; Tue, 13 Mar 2018 08:04:29 -0400 Received: from e06smtp14.uk.ibm.com (e06smtp14.uk.ibm.com [195.75.94.110]) by mx0a-001b2d01.pphosted.com with ESMTP id 2gpbyvq4qk-1 (version=TLSv1.2 cipher=AES256-SHA256 bits=256 verify=NOT) for ; Tue, 13 Mar 2018 08:04:27 -0400 Received: from localhost by e06smtp14.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 13 Mar 2018 12:03:42 -0000 Received: from b06cxnps4074.portsmouth.uk.ibm.com (9.149.109.196) by e06smtp14.uk.ibm.com (192.168.101.144) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 13 Mar 2018 12:03:38 -0000 Received: from d06av25.portsmouth.uk.ibm.com (d06av25.portsmouth.uk.ibm.com [9.149.105.61]) by b06cxnps4074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w2DC3cPX56033464; Tue, 13 Mar 2018 12:03:38 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id D4D4211C04A; Tue, 13 Mar 2018 11:56:17 +0000 (GMT) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 7DF9511C04C; Tue, 13 Mar 2018 11:56:17 +0000 (GMT) Received: from p-imbrenda.boeblingen.de.ibm.com (unknown [9.152.224.168]) by d06av25.portsmouth.uk.ibm.com (Postfix) with ESMTP; Tue, 13 Mar 2018 11:56:17 +0000 (GMT) Date: Tue, 13 Mar 2018 13:03:36 +0100 From: Claudio Imbrenda To: Andrew Morton Cc: linux-kernel@vger.kernel.org, aarcange@redhat.com, minchan@kernel.org, kirill.shutemov@linux.intel.com, linux-mm@kvack.org, hughd@google.com, borntraeger@de.ibm.com, gerald.schaefer@de.ibm.com Subject: Re: [PATCH v1 1/1] mm/ksm: fix interaction with THP In-Reply-To: <20180312142210.4e664519118369d5d129e6dc@linux-foundation.org> References: <1520872937-15351-1-git-send-email-imbrenda@linux.vnet.ibm.com> <20180312142210.4e664519118369d5d129e6dc@linux-foundation.org> Organization: IBM X-Mailer: Claws Mail 3.13.2 (GTK+ 2.24.30; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-TM-AS-GCONF: 00 x-cbid: 18031312-0016-0000-0000-00000530F553 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18031312-0017-0000-0000-0000286E2700 Message-Id: <20180313130336.3afb8789@p-imbrenda.boeblingen.de.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2018-03-13_06:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1709140000 definitions=main-1803130144 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 12 Mar 2018 14:22:10 -0700 Andrew Morton wrote: > 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? we literally wrote it together; I'll change Gerald's Signoff tag with a Co-authored one, since he doesn't really have the time to follow this patch > > 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. will fix > > 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. will add > > 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. yes, that was our idea. > cmp_and_merge_page() already does lock_page() and down_read(), so I > wonder if those reasons are legitimate. since the new code splits the page but doesn't actually merge it, we thought it was better not to add the cost of a full lock. The full locks in the rest of the function are only taken when there is good probability or certainty that the lock will bring benefits. > Again, a comment here is needed - otherwise it will be hard for > readers to understand your intent. I'll add a comment to clarify why we use trylock. > > } > > } > > }