From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-yw1-f173.google.com (mail-yw1-f173.google.com [209.85.128.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4DBE1EADC for ; Wed, 8 Apr 2026 00:09:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.173 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775606947; cv=none; b=YoiJwVYoMrpZpfaYcjfN8fZ/SYb7abOZxxPHnuAU2P8cqXkrm3/v/kGtl39cwilkCct8njnJS3rOKYZkoZGRpwhI6q/pZxTWxJn+CBBw0s9MPiZ6XbKsg0mIycnCI29rJAETwPf7XnN3PPj7dq9Q+eq0DhSFL+l+X7BvXQH41zM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775606947; c=relaxed/simple; bh=VRVEWJ6wdaYC3CQ4cY1WIEFGmxEq9vH0RIIKq3oaj2Q=; h=Date:From:To:cc:Subject:In-Reply-To:Message-ID:References: MIME-Version:Content-Type; b=ERfFJUlXlGUu4Ms/k7RIYE79YBCEX2+YvZapG2rfu1agQWkBrkdYk8GJ6QpbMIJCo+dxdkt0XsEX50aZrpnHZgLPM6+J9hr4CVS6st9FSbZMr+rg/WEzsNMpX7jVqXYWdLvKY6itdBsoy7wFe177HJIgqLyoXhrEB+/nsS8wFAU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=jufbyWD4; arc=none smtp.client-ip=209.85.128.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="jufbyWD4" Received: by mail-yw1-f173.google.com with SMTP id 00721157ae682-7982c3b7da9so51400317b3.1 for ; Tue, 07 Apr 2026 17:09:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1775606945; x=1776211745; darn=vger.kernel.org; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=OXWPRutFUvP5+kWF/q7BaLPPyBphyv3qf5bUsWG0GcA=; b=jufbyWD4qlbnHo0HdXneWqZTr3CcMRJpb3bhuNuZJXe4dSZtlRAw0d6iWyn41OWiQk RHg4BCqsTQfFTzvPjHlQlWMd+fZX+v6aDwnEnszYk0UAY54k3U+MNScx/kOblJB0m28m h11qp5I1SqKpdRuRaoPGPqnJ0qyNf3t6QLh2JU79Pw2ZZlh6xkoSGm4meAa3NfkH7mtL EVYwSfan664KLYtuKnIWDlcOGXY1KExBpws2bc7htRssvMWVTYvLJEbb+ERYGSXxogTk YwuNnYuI1xtuzDeH3wv4mRghEXV0uaiaE6R1pI9yPfPvh06hIsYzuViIWe4dxMfUmMv1 qL+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775606945; x=1776211745; h=mime-version:references:message-id:in-reply-to:subject:cc:to:from :date:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=OXWPRutFUvP5+kWF/q7BaLPPyBphyv3qf5bUsWG0GcA=; b=kUUN+DctRPMvGFmKmvC/586AMppjTq9CUAnBge5aWWcrnw0Miyk/2HVyjElymBKGC9 8yTh+2K6GRSoEzU86WgrfoZYERQfPpioeE0Eaq9vNdtnN3lxV+7SXUaPmPc1YAbFNlRd JOxjdq3UILqU656p1lECdlBDE2346xpvTVsmz+t9WJDdjd2qWY9k2nV51rr9GK4Gd6VN Z+inPo4i0xzqKUPWhuREetX4aLpJDAc/9p9wovk6A9PTRO2PgdCifvXAMO0EDkqke+VQ xabvcSWokLZXXQVtBfWKsxONoPlFtunUatybaBFrYRWIXNYWvVFHN30FtFRdJikTz8qc MzJg== X-Forwarded-Encrypted: i=1; AJvYcCWTrOSwCpawlshaeWsX6SU7TxfzwnlsFhFiikWtgY1UogXd02F/kfszhAht+0t9SGUCEZq53izDvR0XQ3o=@vger.kernel.org X-Gm-Message-State: AOJu0YykMjRWV7pPp6ID5dUogUHyLK9cKqcXrwTOxS0D+3VyglbyMkiO kP+0YoFl25qRPupJuYxLU7CbKvU3/Ee/znPfUnMa+cax/x7S+f0/daSCM2vkJPVsgA== X-Gm-Gg: AeBDievPcKVMPpGA8+4jumIhnc2SQ8W4L8WVSpPiHCHwt6TO8MxHLODPu368ZBNIl9w ndq/cLCtPm2p3uIFTnTqDYa46ow8r+unrtFXrI1+ugL0aqyh+wISGPUeOfO5q9P8at6hpcCo+/m CJyMIXkgyKObTrPBWoQ6RbCzRwfAz2bZI8c1xezFH2wZbLZht+PkEndzwYEnn/miHkDYC1EhfP/ fD4oePYyiOMRL7SHySlvI3CqrdYHaxH/Ao0/BS8HS4k1rkCJieWBQXinFc/X673Yv+hgF4Kzz1v stZbrpm2cWKGcZp2g3i8IMAoNYJWfEPMvRiLvjqLpKqxZr42v3hdcFkbE+7B/hWTgPaLCb9871q 64GNi5x9pGaB5Fhe6VZGSeF5/ev4xZAivIYw2uiWOvBj4ser7oGDDYuVOOd24W57FP5Ks2vGKNs OAUhdg/7faoSSOgw86JT7He7khJWKzNN71fLXAQuZUrN/3TV+Se9eiXz6iRuFWlibXEmqyYF7D5 w6mmddGkUY= X-Received: by 2002:a05:690c:38a:b0:79a:d32f:e4e5 with SMTP id 00721157ae682-7a4d5d5c544mr201447147b3.45.1775606944740; Tue, 07 Apr 2026 17:09:04 -0700 (PDT) Received: from darker.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id 00721157ae682-7a3712f53f1sm77239007b3.49.2026.04.07.17.09.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Apr 2026 17:09:04 -0700 (PDT) Date: Tue, 7 Apr 2026 17:08:50 -0700 (PDT) From: Hugh Dickins To: "David Hildenbrand (Arm)" cc: xu.xin16@zte.com.cn, hughd@google.com, akpm@linux-foundation.org, michel@lespinasse.org, ljs@kernel.org, chengming.zhou@linux.dev, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: Re: ksm: add mremap selftests for ksm_rmap_walk In-Reply-To: Message-ID: <2513cca9-0f5f-3b5b-e758-b9cc304c8699@google.com> References: <20260407140805858ViqJKFhfmYSfq0FynsaEY@zte.com.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII On Tue, 7 Apr 2026, David Hildenbrand (Arm) wrote: > On 4/7/26 08:08, xu.xin16@zte.com.cn wrote: > > From: xu xin > > > > The existing tools/testing/selftests/mm/rmap.c has already one testcase > > for ksm_rmap_walk in TEST_F(migrate, ksm), which takes use of migration > > of page from one NUMA node to another NUMA node. However, it just lacks > > the senario of mremapped VMAs. > > > > Before migrating, we add the calling of mremap() to address mapped with KSM > > pages, which is specailly to test a optimization which is introduced by this > > patch ("ksm: Optimize rmap_walk_ksm by passing a suitable address range") > > https://lore.kernel.org/all/20260212193045556CbzCX8p9gDu73tQ2nvHEI@zte.com.cn/ > > > > Result: > > TAP version 13 > > 1..5 > > ok 1 migrate.anon > > ok 2 migrate.shm > > ok 3 migrate.file # SKIP Failed in worker > > ok 4 migrate.ksm > > ok 5 migrate.ksm_and_mremap > > > > Signed-off-by: xu xin > > --- > > tools/testing/selftests/mm/rmap.c | 69 ++++++++++++++++++++++++++++ > > tools/testing/selftests/mm/vm_util.c | 38 +++++++++++++++ > > tools/testing/selftests/mm/vm_util.h | 2 + > > 3 files changed, 109 insertions(+) > > > > diff --git a/tools/testing/selftests/mm/rmap.c b/tools/testing/selftests/mm/rmap.c > > index 53f2058b0ef2..65470def2bf1 100644 > > --- a/tools/testing/selftests/mm/rmap.c > > +++ b/tools/testing/selftests/mm/rmap.c > > @@ -430,4 +430,73 @@ TEST_F(migrate, ksm) > > propagate_children(_metadata, data); > > } > > > > +/* To test if ksm page can be migrated when it's mremapped */ > > +int merge_mremap_and_migrate(struct global_data *data) > > +{ > > + int ret = 0; > > + /* Allocate range and set the same data */ > > + data->mapsize = 3*getpagesize(); > > + data->region = mmap(NULL, data->mapsize, PROT_READ|PROT_WRITE, > > + MAP_PRIVATE|MAP_ANON, -1, 0); > > + if (data->region == MAP_FAILED) > > + ksft_exit_fail_perror("mmap failed"); > > + > > + memset(data->region, 0x77, data->mapsize); (Not crucial at all, but to avoid confusion between our results, I'll point out that my testcase only memset 2*getpagesize() there, leaving the last page unpopulated - just one less complication.) > > What happens if you mremap() after faulting, but before merging? > > rmap_item->address always holds the user space address of the entry in > the parent process. It must match the one in the child process, because > mremap() will unmerge/unshare in the child. > > And it must match the one in the parent, as mremap() would similarly > unmerge/unshare. > > Maybe doing the mremap() before merging (but after faulting) would > trigger what Hugh described. > > break_cow() and friends don't care about the rmap, as they simply jump > directly to the user space address in the process. > > In rmap_walk_ksm(), I think the concern Hugh raised is that we are using > > const pgoff_t pgoff = rmap_item->address >> PAGE_SHIFT; > > but we'd actually need a pgoff into the anon_vma. Without mremap, it > does not matter, they are the same (tests keep passing). But with mremap > it's not longer the same. > > I think one could store it in the ksm_rmap_item, but that would increase > it's size. It's essentially the folio->index of the original page we are > replacing. > > Or we could just remember "pgoff is not that simple because mremap was > involved, so walk the whole damn thing". > > We could also just try walking all involved processes, looking only at > that user space address (but that gets more tricky with rmap locking etc > ...). > > Anyhow, I think that's the concern Hugh raised, IIUC. Yes, you and Lorenzo are seeing the same seed for doubt as I saw: as you say, "pgoff is not that simple because mremap involved"; but it is confusing, so hard for us to be sure about it. > > > + > > + if (ksm_start() < 0) > > + return FAIL_ON_CHECK; > > + > > + /* 1 2 expected */ > > + ksft_print_msg("Shared: %ld (1 expected) Sharing: %ld (2 expected)\n", > > + ksm_get_pages_shared(), ksm_get_pages_sharing()); > > + > > + /* > > + * Mremap the second pagesize address range into the third pagesize > > + * address. > > + */ > > + data->region = mremap(data->region + getpagesize(), getpagesize(), getpagesize(), > > + MREMAP_MAYMOVE|MREMAP_FIXED, data->region + 2*getpagesize()); > > There would not be a KSM page after this mremap(), no? Oh, good thinking: yes, a side-effect of mremap's non-persistent MADV_UNMERGEABLE would be that there's no KSM page in the mremapped "subregion" at this instant, so the try_to_move_page() which follows is likely to have no trouble succeeding; so, as it stands, this test is not testing what's required. Sorry, I won't be able to give this more attention for a week: I wanted to advertise my doubt before 7.1 merge window, while awkwardly knowing I'd have to back out of ensuing discussion and testing for a few days. It seems agreed that we won't endanger 7.1 until this is resolved: I just hope I'm not guilty of raising a false alarm. Hugh