From patchwork Sat Apr 22 20:54:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13221118 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6A766C77B78 for ; Sat, 22 Apr 2023 20:54:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C0BB66B0072; Sat, 22 Apr 2023 16:54:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B9DAA6B0078; Sat, 22 Apr 2023 16:54:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8D91F6B007B; Sat, 22 Apr 2023 16:54:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 72CB56B0072 for ; Sat, 22 Apr 2023 16:54:44 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 3D199120197 for ; Sat, 22 Apr 2023 20:54:44 +0000 (UTC) X-FDA: 80710230888.27.BC6DCD6 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf19.hostedemail.com (Postfix) with ESMTP id 4A4081A0004 for ; Sat, 22 Apr 2023 20:54:42 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=iuuRZzP2; spf=pass (imf19.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1682196882; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=On9gaPWUj2gDwc8v5O2MPdxyKHVxh4guNHZMK6fskK4=; b=H0usv7YQgMrw//lAC/6Ng+6fv9PJYL9PXv4e3H4xWtKjWPUi+jz/r1TIOYjRgdAFL9YNVq 5hW+L0beXtIkVFLtYdrn35RClotnWSMbDbNjHlcpySGoGbCgWhiRMFVFFVd8V788Sd8+xd TznjAhokARoKaoZ0FsBmMOBXmSCAI94= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682196882; a=rsa-sha256; cv=none; b=LXydScCV9jrU0mGpksHBAxTbltD+57kWEqZV9cblBArTLw6/BSoysYnb1xIxMSB+a7H8qA dBGikIzSGFgPjPVOHmpAsKyfoi8i9je7PYZ8VEPVt2qhlhX2YgTlPmKcr9t4yaxnJIfj0Q D6SNwU3y+kzohN34yGGQWDdXFK7mB2g= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=iuuRZzP2; spf=pass (imf19.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1682196881; 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=On9gaPWUj2gDwc8v5O2MPdxyKHVxh4guNHZMK6fskK4=; b=iuuRZzP2gCZFC4ocSCwpPj85oCBFzLLn+G+Zw+VedDI9utg+P6doTpVfbLATGmuof1YHB0 geDaCEbXjB04+nQga/kseABisIo17lPgtjIXHRpP8v9mWDr+yxTxo461DiNp5O/y+wpjqQ gbn07RPJAQcj00c/DRbtbxRwd+7CVNA= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-270-5rH3yC36P-a5ZrO3rh9FCA-1; Sat, 22 Apr 2023 16:54:40 -0400 X-MC-Unique: 5rH3yC36P-a5ZrO3rh9FCA-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0C80E1C0514D; Sat, 22 Apr 2023 20:54:39 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.39.192.6]) by smtp.corp.redhat.com (Postfix) with ESMTP id 22953492B03; Sat, 22 Apr 2023 20:54:34 +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 mm-stable v2 1/3] mm/ksm: unmerge and clear VM_MERGEABLE when setting PR_SET_MEMORY_MERGE=0 Date: Sat, 22 Apr 2023 22:54:18 +0200 Message-Id: <20230422205420.30372-2-david@redhat.com> In-Reply-To: <20230422205420.30372-1-david@redhat.com> References: <20230422205420.30372-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Rspam-User: X-Rspamd-Queue-Id: 4A4081A0004 X-Rspamd-Server: rspam09 X-Stat-Signature: 3x9i8qxatujedzm1858zosstnwnubmy6 X-HE-Tag: 1682196882-754066 X-HE-Meta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Dp/j66vCfg/BwJGBrNIzOilyiF9LnSggiB7Zys2GsK5ItezufJOPP9YVqbMiKF1tCoga+G++PlVWjAtXm7Uk4FcL+R1KLGZUVfRIGT6zeE4fUkMsoeQVVTGDzD5vmoYieRfUSWWIBoIAd6 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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. Acked-by: Stefan Roesch Signed-off-by: David Hildenbrand --- include/linux/ksm.h | 1 + kernel/sys.c | 12 +++------ mm/ksm.c | 59 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 9 deletions(-) diff --git a/include/linux/ksm.h b/include/linux/ksm.h index 7a9b76fb6c3f..429efa6ff4ae 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..339fee3eff6a 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2695,16 +2695,10 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, if (mmap_write_lock_killable(me->mm)) return -EINTR; - if (arg2) { + 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; - } + else + error = ksm_disable_merge_any(me->mm); mmap_write_unlock(me->mm); break; case PR_GET_MEMORY_MERGE: diff --git a/mm/ksm.c b/mm/ksm.c index 9e48258985d2..823bb3475a68 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,35 @@ int ksm_enable_merge_any(struct mm_struct *mm) return 0; } +/** + * ksm_disable_merge_any - Disable merging on all compatible VMA's of the mm, + * previously enabled via ksm_enable_merge_any(). + * + * Disabling merging implies unmerging any merged pages, like setting + * MADV_UNMERGEABLE would. If unmerging fails, the whole operation fails and + * merging on all compatible VMA's remains enabled. + * + * @mm: Pointer to mm + * + * Returns 0 on success, otherwise error code + */ +int ksm_disable_merge_any(struct mm_struct *mm) +{ + 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); + return 0; +} + int ksm_madvise(struct vm_area_struct *vma, unsigned long start, unsigned long end, int advice, unsigned long *vm_flags) { From patchwork Sat Apr 22 20:54:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13221120 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A280FC77B78 for ; Sat, 22 Apr 2023 20:56:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0C40B6B0072; Sat, 22 Apr 2023 16:56:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 075C96B0074; Sat, 22 Apr 2023 16:56:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E7DCF6B0075; Sat, 22 Apr 2023 16:56:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id D462D6B0072 for ; Sat, 22 Apr 2023 16:56:36 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 8F47EC01A9 for ; Sat, 22 Apr 2023 20:56:36 +0000 (UTC) X-FDA: 80710235592.25.4846787 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf02.hostedemail.com (Postfix) with ESMTP id DC46480003 for ; Sat, 22 Apr 2023 20:56:34 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=aMnj9mBp; spf=pass (imf02.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1682196994; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Ui2WFc3XFE2+tQTFbxU0igT43p1ZENYfzH1ADoeycVQ=; b=WocEmkIqp/tQaC0vNL/FhAG492JdMpClUQpCYMyjog2EbLV7eeaXQFKixntlxPo5UZrTIh wqq//4xMhNFevxLTVnoO8yw8D1i4fL+YiysAcI65d8zrAEe3zRPRjtDZa997kVxs+SfJAH PBD9xYL8ntLIggn/DXT2SOViF+xsZOM= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=aMnj9mBp; spf=pass (imf02.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682196994; a=rsa-sha256; cv=none; b=NnU4RzsekxXrouVAoSK0vMdG1N2k8f3Srl4cWzp5Tv9RKAoSrrLF6t+iy7RFBFYUzYat1M ei+0HOzuy7DjlXlCs6P5RoiSqybghRbo9lIl99wGBEmDiXkWVcx/AsAVohEL9/7q9pAc7N LZUThw+qOaZhZ0lBh/APnSGjn7Ko5VU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1682196994; 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=Ui2WFc3XFE2+tQTFbxU0igT43p1ZENYfzH1ADoeycVQ=; b=aMnj9mBpEe1hRn+HWbO2vwzciNRyVLZVWMXctjQ1mwEx8Rz1kNlvUCPfNd5JDeMv7fw3qg nXL65UNHPTKwiXRzT6/w8fuh5g7bWdN0P/eF6dyfAY7fn11Ims3DaEM/kmMCC7L39QeGLL FSSjnEG5WDAsocBBx7uWD7QkJA6LaHQ= 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-517-1KOLu_GvNNudAnb8kvSgFA-1; Sat, 22 Apr 2023 16:54:43 -0400 X-MC-Unique: 1KOLu_GvNNudAnb8kvSgFA-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0CA0D884EC4; Sat, 22 Apr 2023 20:54:43 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.39.192.6]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6C40E492B03; Sat, 22 Apr 2023 20:54:39 +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 mm-stable v2 2/3] selftests/ksm: ksm_functional_tests: add prctl unmerge test Date: Sat, 22 Apr 2023 22:54:19 +0200 Message-Id: <20230422205420.30372-3-david@redhat.com> In-Reply-To: <20230422205420.30372-1-david@redhat.com> References: <20230422205420.30372-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Stat-Signature: tzo7d4fn6yiyzf44dxy5acq8aubsbatp X-Rspam-User: X-Rspamd-Queue-Id: DC46480003 X-Rspamd-Server: rspam06 X-HE-Tag: 1682196994-201069 X-HE-Meta: U2FsdGVkX19h/yxzOXZccpm9y8udZFDq7XyL5/8NQ2mpRYhkE11ntK+n+BDaEJsY1vTrC1qfKWfS1fC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VkeuUyGrgRkadylJ1quZEKU5OHqYBa/5wzDP9k3nQbEfMpZ+sHb96rwWHa5NYPm1SRiS8bh X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Let's test whether setting PR_SET_MEMORY_MERGE to 0 after setting it to 1 will unmerge pages, similar to how setting MADV_UNMERGEABLE after setting MADV_MERGEABLE would. Acked-by: Stefan Roesch Signed-off-by: David Hildenbrand --- .../selftests/mm/ksm_functional_tests.c | 46 ++++++++++++++++--- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c index 7bc9fc17c9f0..26853badae70 100644 --- a/tools/testing/selftests/mm/ksm_functional_tests.c +++ b/tools/testing/selftests/mm/ksm_functional_tests.c @@ -91,9 +91,10 @@ static int ksm_merge(void) return 0; } -static char *mmap_and_merge_range(char val, unsigned long size) +static char *mmap_and_merge_range(char val, unsigned long size, bool use_prctl) { char *map; + int ret; map = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); @@ -110,7 +111,17 @@ static char *mmap_and_merge_range(char val, unsigned long size) /* Make sure each page contains the same values to merge them. */ memset(map, val, size); - if (madvise(map, size, MADV_MERGEABLE)) { + + if (use_prctl) { + ret = prctl(PR_SET_MEMORY_MERGE, 1, 0, 0, 0); + if (ret < 0 && errno == EINVAL) { + ksft_test_result_skip("PR_SET_MEMORY_MERGE not supported\n"); + goto unmap; + } else if (ret) { + ksft_test_result_fail("PR_SET_MEMORY_MERGE=1 failed\n"); + goto unmap; + } + } else if (madvise(map, size, MADV_MERGEABLE)) { ksft_test_result_fail("MADV_MERGEABLE failed\n"); goto unmap; } @@ -133,7 +144,7 @@ static void test_unmerge(void) ksft_print_msg("[RUN] %s\n", __func__); - map = mmap_and_merge_range(0xcf, size); + map = mmap_and_merge_range(0xcf, size, false); if (map == MAP_FAILED) return; @@ -155,7 +166,7 @@ static void test_unmerge_discarded(void) ksft_print_msg("[RUN] %s\n", __func__); - map = mmap_and_merge_range(0xcf, size); + map = mmap_and_merge_range(0xcf, size, false); if (map == MAP_FAILED) return; @@ -187,7 +198,7 @@ static void test_unmerge_uffd_wp(void) ksft_print_msg("[RUN] %s\n", __func__); - map = mmap_and_merge_range(0xcf, size); + map = mmap_and_merge_range(0xcf, size, false); if (map == MAP_FAILED) return; @@ -323,9 +334,31 @@ static void test_prctl_fork(void) ksft_test_result_pass("PR_SET_MEMORY_MERGE value is inherited\n"); } +static void test_prctl_unmerge(void) +{ + const unsigned int size = 2 * MiB; + char *map; + + ksft_print_msg("[RUN] %s\n", __func__); + + map = mmap_and_merge_range(0xcf, size, true); + if (map == MAP_FAILED) + return; + + if (prctl(PR_SET_MEMORY_MERGE, 0, 0, 0, 0)) { + ksft_test_result_fail("PR_SET_MEMORY_MERGE=0 failed\n"); + goto unmap; + } + + ksft_test_result(!range_maps_duplicates(map, size), + "Pages were unmerged\n"); +unmap: + munmap(map, size); +} + int main(int argc, char **argv) { - unsigned int tests = 4; + unsigned int tests = 5; int err; #ifdef __NR_userfaultfd @@ -355,6 +388,7 @@ int main(int argc, char **argv) test_prctl(); test_prctl_fork(); + test_prctl_unmerge(); err = ksft_get_fail_cnt(); if (err) From patchwork Sat Apr 22 21:01:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13221121 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B7445C7618E for ; Sat, 22 Apr 2023 21:03:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 508446B0072; Sat, 22 Apr 2023 17:03:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 48F6C6B0074; Sat, 22 Apr 2023 17:03:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3314F6B0075; Sat, 22 Apr 2023 17:03:21 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 211C66B0072 for ; Sat, 22 Apr 2023 17:03:21 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id E357C1C61D4 for ; Sat, 22 Apr 2023 21:03:20 +0000 (UTC) X-FDA: 80710252560.03.ABD8996 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf08.hostedemail.com (Postfix) with ESMTP id 28CFE160006 for ; Sat, 22 Apr 2023 21:03:18 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Zn8BQvmm; spf=pass (imf08.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1682197399; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=/CVWL6KZbwiyfGCJe2ZhJdYm/T0Tfz0MO2xrMQw1s4A=; b=DAod/8aKoZ1SsXZoZQwxQGRFkHGRtlWUI0ZDqcVzRlHmsdKigJKr06LPsHHwoCc8FZu1YT JCeV+6SYtvISDqG8RLQgcGBiBaVvbZHfnQByEfUMdJgNBh413QllVlSVTIu9dptLFjkV1E w7uqK/IvVSz6gwB1jyZslAA8acs0NrA= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Zn8BQvmm; spf=pass (imf08.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682197399; a=rsa-sha256; cv=none; b=sWCbqBugN2gYCI7nZyD+EZO46kzTY9rTvQxhdjzYCoJpOrTYFFNGG8YzlV4zqkVxVNuMYP aC8n/NByFVCC5/xCrb/GE5NdOdayqe23sYQXY0NNbUipeGiMTrQJHjL2JF6k6Rgseh0jPk i9MNccP9Szeqtr8yNfKK1pF7zaQnuio= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1682197398; 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=/CVWL6KZbwiyfGCJe2ZhJdYm/T0Tfz0MO2xrMQw1s4A=; b=Zn8BQvmmcyvHRDdf3MCK4Ug52Cum+R38xie36lLgsrIKafFA77m0XMzM55RCDNQzH0L6RS JJjvLFDZX2g/IsNCpAChBAE+p/9H5prr5EK3tIwddzkh4QppwwmdTb3qH0DoZkYUO4koOb g0Pl7/R0Ervy9CyaA8F2HX+KFPGs2aE= 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-546-1Vn2ezJ4Ofek4Z8PatTQEA-1; Sat, 22 Apr 2023 17:02:09 -0400 X-MC-Unique: 1Vn2ezJ4Ofek4Z8PatTQEA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 915FB85A5B1; Sat, 22 Apr 2023 21:02:08 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.39.192.6]) by smtp.corp.redhat.com (Postfix) with ESMTP id CFA2A2023163; Sat, 22 Apr 2023 21:01:58 +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, 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 , David Hildenbrand Subject: [PATCH mm-stable v2 3/3] mm/ksm: move disabling KSM from s390/gmap code to KSM code Date: Sat, 22 Apr 2023 23:01:56 +0200 Message-Id: <20230422210156.33630-1-david@redhat.com> In-Reply-To: <20230422205420.30372-1-david@redhat.com> References: <20230422205420.30372-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 28CFE160006 X-Rspam-User: X-Stat-Signature: rt6tdw77y5exgcfm84fenxd53uyhwufb X-HE-Tag: 1682197398-530708 X-HE-Meta: U2FsdGVkX19HP0/bymUsa1O5eTOWGb8vsOFP5J5lORoIjX+iyU55TTsfjpL9spfU70bI5vLMruiLmRaGc4JVNFtOSmXlrJFRxn6xJW8a3Bghj4sHb7DIjIY0qpjWR8dD8+GtfspmyLYOb5IV55UKNiLMvxKZugXajK3FvPIzO/v4AN6UNfvFm3ukqTKm4OhESXruHTFP6hhvhv3hmsfgcmp3lgwlNU9adcTeNwIkNw6I/nUerf83b6yJo2xWBQiDuODAmUimkF6d9aoSCJ8GCyNsko3kSh4C2auZI+1ZvOywpwHyntATYtqbirXw8k+B3Z5TIHc/XrQNbJCY9/sjCzXbhvxCnzHNMClwDzN++YHh7kmcG1/htc2FqZMD53dw1zmSGrx2ZRuR72D/PS5NZHeB5Xrz3PGJd4/8uSx2qdLslJp7gxLbaPnmBpwjaVjBbTGFBvKNwOIE98BCRMjCCNxYKaYMFpeAqKFhFGl5+x4hOJbyQHXyY5A3k7gdGn35aLR5Js8Cb3tfryMOJTkV/p9Xo5IV39/XZpve590xQQmi/GyzSzT6vQZ3/jAmZoPHpWpxmu7s2E/cdV+R6SLIyoFdWrVrlos05u4pQaIs3suWPQhtCzO/bJ4d42Bsum50ewxoaxQ1z2BJlBLNQu9cvADqRlZwyOGSFmuSG++mbYo8OnFbFhItv4vJnB2jjF8aF+9kB2KTyLSzg4KmS/VqqlYrfcAyq+IhXPp1VuZDaL/kjSHYfsSCBBBGHOVESX3asvbXO5tZlG1YgV2dVksdXbEvsLOozt9xDcLYNopxVKLD5XmTBpVRgLOvpEVs+EXwA30OX/O4uybHnhyAfH0GeEnwbW6nxeqW5Bv816MGKNxPII4cowg1OHIie+1RytvSRQ5tu08sBgBJ8FTLI/a3inKgw3/CguTUSnmaSZ1YtN3ubfEHXR1M9rNjc7rLUNVT7N5lBBjWpllUscy1yPB Ghg5R7Ch cBGFvP+ADNwozVZIvIV6ifp+5PptSuO2x8zxWaz/pQjBpMNnln16cNoscC05sIEEbVzcvaIqZbsvwC3eAqG9DWyHBW5MabehIL96mjq3I8sJs4gTKlN015DglVRSnnVhDQN2Trr95QSRnriAnolYS5Rwr+YNV9TjygCknbvzMnAviIxGlVKEWfn9R0jiu/YCF3raUrCtKSNPDRcg= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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. Acked-by: Janosch Frank Acked-by: Stefan Roesch 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 429efa6ff4ae..899a314bc487 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); @@ -80,6 +81,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 823bb3475a68..0156bded3a66 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -2628,6 +2628,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) {