Received: by 2002:a05:6358:53a8:b0:117:f937:c515 with SMTP id z40csp5573996rwe; Tue, 18 Apr 2023 08:32:57 -0700 (PDT) X-Google-Smtp-Source: AKy350bmJDNDh1mLZNJcaQHzECek+T87D4PyNXlk8u84L1VcXvwCgrZ9a/ruiXBbz9m/exezSOFC X-Received: by 2002:a05:6a20:394b:b0:f0:9de1:193e with SMTP id r11-20020a056a20394b00b000f09de1193emr236830pzg.6.1681831977642; Tue, 18 Apr 2023 08:32:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681831977; cv=none; d=google.com; s=arc-20160816; b=zc1ctwAeYFjkG6jdizeO16/wHJAvd+seDKl6I7or1APruOQIzsJ8XfGnjCqky9e0FC nQa8nii3itJRzoVJoem9LtXe0uMvtQGlxoYF8BtWyPljawdglrBI16w1jpPPzA8U+qE1 JIqtCZ2RiuoIjDd69zC4X/5szk11gnON8mfafu42/CklCGIUD98/I1cwdpgJ8jfTRfIz syOTeqJKq3bCNiqqjsWie1qjVMZkAMrcXmJvAnMo0pVbIV9AHUxkmgU3Xmaj27SWaexE NwxtnwfFkv69Dstw0hQmjWHzXpnAKsUd0WieW8VhyJ1Bk/p6+y40bicUfXZGTBxMzFbA mI5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=d+AIWczdbQmYcywSh4w8GvqvVINNbh2DvrncyfYADn8=; b=GqpE4b2sELgOx1GHLSpqye/eRnDAf8Y4NsiSu1gsNdXjVFmgNx0E8ORtnmHkWkxK5t Tex6G4lC/hc4y/ck/BtrX0vb8Vy9os86HiI/sZV7zckCaHSvW26TRBIutfp631auZWvm qpcPR5HuvkxN2r8nIewc3RefzMn6W7zqN9+77ZPniVLwtSak9zKngJW5ZmX9RRDddkCr 3j0UfGg8dS8wfGrkilqXQ7anbRtOSrJJiUHJdqcOoyY+xdL0JU9QtuKd6yaxy/xQ286E 7WvJLQhyzVCqZRwv+Yq4FftPdem9kYI8HNmx9VKmJhP0x7iG6I1QD4vEZZMrV2GJ0ZUI UYkQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="R0/AC+qW"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 5-20020a630f45000000b0051f9ef9f57dsi1047233pgp.364.2023.04.18.08.32.44; Tue, 18 Apr 2023 08:32:57 -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=@redhat.com header.s=mimecast20190719 header.b="R0/AC+qW"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232279AbjDRPai (ORCPT + 99 others); Tue, 18 Apr 2023 11:30:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45834 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231635AbjDRPaf (ORCPT ); Tue, 18 Apr 2023 11:30:35 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AEB03A257 for ; Tue, 18 Apr 2023 08:29:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681831746; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=d+AIWczdbQmYcywSh4w8GvqvVINNbh2DvrncyfYADn8=; b=R0/AC+qWnLKKnSZT0XIv9wX6XiEG8qMQX9QEVq+v8MbSKHI9QiooTHPRXqILRzmFYyiF9m NBwX8kePFfH9wkd9n//ma4NqXb/HkkccOcdrWWbx2dfiC7mcTjpmzlpK3X7Shjx6Z/1snM SokdkfrUHuqxUoACx7MuOiP6ic1QFXk= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-42-Je6gkD7QPgStNU4pQnR1yQ-1; Tue, 18 Apr 2023 11:29:03 -0400 X-MC-Unique: Je6gkD7QPgStNU4pQnR1yQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D3CDF8996F7; Tue, 18 Apr 2023 15:29:02 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.39.194.149]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3F63F14171B8; Tue, 18 Apr 2023 15:29:00 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-s390@vger.kernel.org, linux-kselftest@vger.kernel.org, David Hildenbrand , Andrew Morton , Stefan Roesch , Rik van Riel , Johannes Weiner , Michal Hocko , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Heiko Carstens , Vasily Gorbik , Sven Schnelle , Shuah Khan Subject: [PATCH v1 3/3] mm/ksm: move disabling KSM from s390/gmap code to KSM code Date: Tue, 18 Apr 2023 17:28:49 +0200 Message-Id: <20230418152849.505124-4-david@redhat.com> In-Reply-To: <20230418152849.505124-1-david@redhat.com> References: <20230418051342.1919757-1-shr@devkernel.io> <20230418152849.505124-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,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 Let's factor out actual disabling of KSM. The existing "mm->def_flags &= ~VM_MERGEABLE;" was essentially a NOP and can be dropped, because def_flags should never include VM_MERGEABLE. Note that we don't currently prevent re-enabling KSM. This should now be faster in case KSM was never enabled, because we only conditionally iterate all VMAs. Further, it certainly looks cleaner. Signed-off-by: David Hildenbrand --- arch/s390/mm/gmap.c | 20 +------------------- include/linux/ksm.h | 6 ++++++ mm/ksm.c | 11 +++++++++++ 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/arch/s390/mm/gmap.c b/arch/s390/mm/gmap.c index 0949811761e6..dfe905c7bd8e 100644 --- a/arch/s390/mm/gmap.c +++ b/arch/s390/mm/gmap.c @@ -2585,30 +2585,12 @@ EXPORT_SYMBOL_GPL(s390_enable_sie); int gmap_mark_unmergeable(void) { - struct mm_struct *mm = current->mm; - struct vm_area_struct *vma; - unsigned long vm_flags; - int ret; - VMA_ITERATOR(vmi, mm, 0); - /* * Make sure to disable KSM (if enabled for the whole process or * individual VMAs). Note that nothing currently hinders user space * from re-enabling it. */ - clear_bit(MMF_VM_MERGE_ANY, &mm->flags); - - for_each_vma(vmi, vma) { - /* Copy vm_flags to avoid partial modifications in ksm_madvise */ - vm_flags = vma->vm_flags; - ret = ksm_madvise(vma, vma->vm_start, vma->vm_end, - MADV_UNMERGEABLE, &vm_flags); - if (ret) - return ret; - vm_flags_reset(vma, vm_flags); - } - mm->def_flags &= ~VM_MERGEABLE; - return 0; + return ksm_disable(current->mm); } EXPORT_SYMBOL_GPL(gmap_mark_unmergeable); diff --git a/include/linux/ksm.h b/include/linux/ksm.h index 7108bc65dc2a..b3d8b7849e18 100644 --- a/include/linux/ksm.h +++ b/include/linux/ksm.h @@ -22,6 +22,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_disable(struct mm_struct *mm); int __ksm_enter(struct mm_struct *mm); void __ksm_exit(struct mm_struct *mm); @@ -75,6 +76,11 @@ static inline void ksm_add_vma(struct vm_area_struct *vma) { } +static inline int ksm_disable(struct mm_struct *mm) +{ + return 0; +} + static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) { return 0; diff --git a/mm/ksm.c b/mm/ksm.c index 813f7fbc1832..208311cbb019 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -2616,6 +2616,17 @@ int ksm_disable_merge_any(struct mm_struct *mm) return 0; } +int ksm_disable(struct mm_struct *mm) +{ + mmap_assert_write_locked(mm); + + if (!test_bit(MMF_VM_MERGEABLE, &mm->flags)) + return 0; + if (test_bit(MMF_VM_MERGE_ANY, &mm->flags)) + return ksm_disable_merge_any(mm); + return ksm_del_vmas(mm); +} + int ksm_madvise(struct vm_area_struct *vma, unsigned long start, unsigned long end, int advice, unsigned long *vm_flags) { -- 2.39.2