Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp1392440rwr; Thu, 20 Apr 2023 14:28:47 -0700 (PDT) X-Google-Smtp-Source: AKy350aMTR51uZJ8PhJlXnihTBRYx/idKr1Y97SQPxdWzfxY/kZl0eR5tfg2LL+7GDFTGt3oObjq X-Received: by 2002:a17:90b:3b83:b0:23a:177b:5bfa with SMTP id pc3-20020a17090b3b8300b0023a177b5bfamr2891156pjb.22.1682026127375; Thu, 20 Apr 2023 14:28:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682026127; cv=none; d=google.com; s=arc-20160816; b=NkpOzVX5uSQ9be1mz5Uazcgf7y1YXL1Xirz1VzN/hNOXn4qohffYSpbnPPThWGAVlc vjrI+3KAyXPExlDNHH/ft1gs/OH9/Jo4vgP8HVT1wdgN9VoEBDnWhWt5QhOb1/lcw35J 1VKJWdg9QGiysTYsMH7I7V6U/fQwzYxlz+rJ2OBUfsR0nG3qfxY8+qVrYuO0rB3ajfgy XHCElP3524aLVebT86VsYUlVwtY08Mfe9pgg3YIHZ0nfyS+kzQFYv9dS+WMnP+KMSxbl 1qvkCxYdp0zO40z2fLyV3vd0N8A13lLQTpMQd2p+Bzfrr1QqaDqP4OpwEFS3njimsnMp WnjA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:in-reply-to:date:subject :cc:to:from:user-agent:references:feedback-id:dkim-signature :dkim-signature; bh=kypGF2ii73vq7/MLpGRQfGi3Vad0RUnvoKgRKQqP8c4=; b=kVvE+Omtf7n8PABrztGC8/ysIJwEFqjiFrsc9Nq56Ktb7B69b1MEXxBGbyHdfR3QlO Q/KabmkiNeXgZMC7xKuPaAHnEmH+AWuR2nZHhFyGPdi0w7n4zv9sKpMB0572qsMX5WWa eI8gRgtkKco1J8YK2bF/TAraUIsFEPsP5yQnj9+8PWLKHadVXWn0Wlfn77HJRYbppMv/ 68W8OWobcU5WMFLlhfnSebJJP2Ud9xeuQtW8XLTCNyZTFFuYDWdRpAqy/1uJ8OPkVYcb WmdqukXKfy08YCOCFv5ZsVzuZgd+pdht0vxt8B3REtm7pkqbfZIQIS1spQzE5TUkv8th 6rJw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@devkernel.io header.s=fm1 header.b=S48pqTi3; dkim=pass header.i=@messagingengine.com header.s=fm3 header.b=ZSVGEgiu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id lp4-20020a17090b4a8400b0024b342f5239si3878831pjb.95.2023.04.20.14.28.36; Thu, 20 Apr 2023 14:28:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@devkernel.io header.s=fm1 header.b=S48pqTi3; dkim=pass header.i=@messagingengine.com header.s=fm3 header.b=ZSVGEgiu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232030AbjDTV1F (ORCPT + 99 others); Thu, 20 Apr 2023 17:27:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47626 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231464AbjDTV1B (ORCPT ); Thu, 20 Apr 2023 17:27:01 -0400 Received: from wout2-smtp.messagingengine.com (wout2-smtp.messagingengine.com [64.147.123.25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E8C7E30C5; Thu, 20 Apr 2023 14:26:59 -0700 (PDT) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 88F83320024A; Thu, 20 Apr 2023 17:26:56 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Thu, 20 Apr 2023 17:26:57 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=devkernel.io; h= cc:cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:sender :subject:subject:to:to; s=fm1; t=1682026016; x=1682112416; bh=ky pGF2ii73vq7/MLpGRQfGi3Vad0RUnvoKgRKQqP8c4=; b=S48pqTi3g1iCvy/lPY pwdM0OKwkxXwYg17Ey9YitSRRwLJaMco0E1lrPsaKNyVe6of1qlgN7I4WH42eTlz TxbmjMuZcdOBy3qDP9HkgJBk/Kk83EDd+iChTsbxrPGZthrkVWUOkEJkU0OA8YW6 OMBQqPFb1yChqi44/wV8kG1cYWSTX7f/pvv7KIfTDUUZYfv0uz0JAZ3DYCqxQWyA 7/m2hfq9uALNzk7/k5J51z/fbR0e/tFHwjv4zlUHdVKrIAhvTmx7QKcpL4PcE8vz b8Qeszm4CVrJDsQxBXCaJXJ9443SzGXyJaipIOYSv05S3pxLgOLL2k8zrZlGDVGg uBSQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; t=1682026016; x=1682112416; bh=kypGF2ii73vq7 /MLpGRQfGi3Vad0RUnvoKgRKQqP8c4=; b=ZSVGEgiumPxVBspocrub1eSMopJ8S i8lf1W0REVumFGfbjAzaGINMZ9CL+P8MN1MW4/x8eWtIPnqdexAedPqNlUqTERpJ 1TUNjSc+t52G0FBnez71PBFtu5ui03gcgFQXkALg3hm4yNJe23BggW4Xzx3LcV7p a0QFJuLfIXNbAzjuDkKmLV8WSebJZ9ZNGfGJDc4lEBPB96C05jwjKAe6sPvIx+kl 7dqxnrwIJb2veHWTH0DpJW0riSixxKiDI11p3QyYsBl1brQWBNcSGAxN1WyquFBw nfY8uboPHyVQFOXmZrXw3OqL0P7rWWtjjITUXiRCHo3cfchMT02Tb4eEw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrfedtvddgudeitdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpehffgfhvfevufffjgfkgggtsehttdertddtredtnecuhfhrohhmpefuthgv fhgrnhcutfhovghstghhuceoshhhrhesuggvvhhkvghrnhgvlhdrihhoqeenucggtffrrg htthgvrhhnpeevlefggffhheduiedtheejveehtdfhtedvhfeludetvdegieekgeeggfdu geeutdenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpe hshhhrseguvghvkhgvrhhnvghlrdhioh X-ME-Proxy: Feedback-ID: i84614614:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 20 Apr 2023 17:26:53 -0400 (EDT) References: <20230418051342.1919757-1-shr@devkernel.io> <20230418152849.505124-1-david@redhat.com> <20230418152849.505124-2-david@redhat.com> User-agent: mu4e 1.10.1; emacs 28.2.50 From: Stefan Roesch To: David Hildenbrand Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-s390@vger.kernel.org, linux-kselftest@vger.kernel.org, Andrew Morton , Rik van Riel , Johannes Weiner , Michal Hocko , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Heiko Carstens , Vasily Gorbik , Sven Schnelle , Shuah Khan Subject: Re: [PATCH v1 1/3] mm/ksm: unmerge and clear VM_MERGEABLE when setting PR_SET_MEMORY_MERGE=0 Date: Thu, 20 Apr 2023 14:21:42 -0700 In-reply-to: <20230418152849.505124-2-david@redhat.com> Message-ID: MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org David Hildenbrand writes: > Let's unmerge any KSM pages when setting PR_SET_MEMORY_MERGE=0, and clear > the VM_MERGEABLE flag from all VMAs -- just like KSM would. Of course, > only do that if we previously set PR_SET_MEMORY_MERGE=1. > > Signed-off-by: David Hildenbrand > --- > include/linux/ksm.h | 1 + > kernel/sys.c | 7 +------ > mm/ksm.c | 47 +++++++++++++++++++++++++++++++++++++++++++++ > 3 files changed, 49 insertions(+), 6 deletions(-) > > diff --git a/include/linux/ksm.h b/include/linux/ksm.h > index 590934bdddcf..7108bc65dc2a 100644 > --- a/include/linux/ksm.h > +++ b/include/linux/ksm.h > @@ -21,6 +21,7 @@ int ksm_madvise(struct vm_area_struct *vma, unsigned long start, > > void ksm_add_vma(struct vm_area_struct *vma); > int ksm_enable_merge_any(struct mm_struct *mm); > +int ksm_disable_merge_any(struct mm_struct *mm); > > int __ksm_enter(struct mm_struct *mm); > void __ksm_exit(struct mm_struct *mm); > diff --git a/kernel/sys.c b/kernel/sys.c > index 72cdb16e2636..3436376667d7 100644 > --- a/kernel/sys.c > +++ b/kernel/sys.c > @@ -2698,12 +2698,7 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, > if (arg2) { > error = ksm_enable_merge_any(me->mm); > } else { > - /* > - * TODO: we might want disable KSM on all VMAs and > - * trigger unsharing to completely disable KSM. > - */ > - clear_bit(MMF_VM_MERGE_ANY, &me->mm->flags); > - error = 0; > + error = ksm_disable_merge_any(me->mm); > } > nit: can we do: if (arg2) error = ksm_enable_merge_any(me->mm); else error = ksm_disable_merge_any(me->mm); mmap_write_unlock(me->mm); break; > diff --git a/mm/ksm.c b/mm/ksm.c > index a959e8925413..813f7fbc1832 100644 > --- a/mm/ksm.c > +++ b/mm/ksm.c > @@ -2520,6 +2520,22 @@ static void __ksm_add_vma(struct vm_area_struct *vma) > vm_flags_set(vma, VM_MERGEABLE); > } > > +static int __ksm_del_vma(struct vm_area_struct *vma) > +{ > + int err; > + > + if (!(vma->vm_flags & VM_MERGEABLE)) > + return 0; > + > + if (vma->anon_vma) { > + err = unmerge_ksm_pages(vma, vma->vm_start, vma->vm_end); > + if (err) > + return err; > + } > + > + vm_flags_clear(vma, VM_MERGEABLE); > + return 0; > +} > /** > * ksm_add_vma - Mark vma as mergeable if compatible > * > @@ -2542,6 +2558,20 @@ static void ksm_add_vmas(struct mm_struct *mm) > __ksm_add_vma(vma); > } > > +static int ksm_del_vmas(struct mm_struct *mm) > +{ > + struct vm_area_struct *vma; > + int err; > + > + VMA_ITERATOR(vmi, mm, 0); > + for_each_vma(vmi, vma) { > + err = __ksm_del_vma(vma); > + if (err) > + return err; > + } > + return 0; > +} > + > /** > * ksm_enable_merge_any - Add mm to mm ksm list and enable merging on all > * compatible VMA's > @@ -2569,6 +2599,23 @@ int ksm_enable_merge_any(struct mm_struct *mm) > return 0; > } > > +int ksm_disable_merge_any(struct mm_struct *mm) > I understand we want to keep the name "symmetric" with ksm_enable_merge_any, but it also unmerges the ksm pages. Do we want to reflect that in the function name? Can we add a comment for the function? > +{ > + int err; > + > + if (!test_bit(MMF_VM_MERGE_ANY, &mm->flags)) > + return 0; > + > + err = ksm_del_vmas(mm); > + if (err) { > + ksm_add_vmas(mm); > + return err; > + } > + > + clear_bit(MMF_VM_MERGE_ANY, &mm->flags); > We only clear the MMF_VM_MERGE_ANY flag if there are no errors. Is this what we want? This means that if the process creates new memory regions they would still be marked as mergeable. > + return 0; > +} > + > int ksm_madvise(struct vm_area_struct *vma, unsigned long start, > unsigned long end, int advice, unsigned long *vm_flags) > {