From patchwork Tue Apr 18 15:28:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13215828 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 5C95CC6FD18 for ; Tue, 18 Apr 2023 15:29:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 98A228E0002; Tue, 18 Apr 2023 11:29:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 910BB8E0001; Tue, 18 Apr 2023 11:29:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7B3608E0002; Tue, 18 Apr 2023 11:29:05 -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 6C2AA8E0001 for ; Tue, 18 Apr 2023 11:29:05 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 1AB0B120123 for ; Tue, 18 Apr 2023 15:29:05 +0000 (UTC) X-FDA: 80694895050.25.9C60153 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf18.hostedemail.com (Postfix) with ESMTP id 206F11C0011 for ; Tue, 18 Apr 2023 15:29:02 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=D4CeN0iV; spf=pass (imf18.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=1681831743; 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=lecfMikStSmpt+Q0WpS+JI6gz8j1FT0LsSw/UhC5P0k=; b=oTTf0tRrBpWOi4Ct2i3Eb2prZUyIy55HXZHnICBtC+9m0/oBc1qVF7nsee1A6mmMw9FPYJ MaS45Fj0vZWFNh9G/Wy1c54RLqEQYwHUzgenMZQtwcMx0Fi36wVsjKO00HKNkJLp/cf61l XmhEYRePBIDpmnwScmnFRbqEd1h4otE= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=D4CeN0iV; spf=pass (imf18.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=1681831743; a=rsa-sha256; cv=none; b=IMcvtZJWgD4Sr3bAzKWA8BNgCcSBBPXR+HkXvIKfRq7DFIGLx2jnY2mgIvhT3VXeAhdric YX7jRgTHDrdygX9t0gvSebnUDGik95ZdZgtzRD/C4mV38kR0E/1Ffvh2ZU9RIE+bGRBfVH eTbAtuCwJaNTjT+vBAo6dj2Pi72XjOA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681831742; 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=lecfMikStSmpt+Q0WpS+JI6gz8j1FT0LsSw/UhC5P0k=; b=D4CeN0iVZ9Ny4G1Iizm7SNNV23gI8l4tiMFKfpTpoH42ilEsc+ajUupAHmKHKVinGhwowU nK84AkXfX62c94pBPjisQrgbUoFtJAEZdwoxYTB/KTcUEiFSphzhjuS/yWnXWI0GQWaz8n RIVsobOItMb+h84M/higjYR/vJnG7Vg= 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-637-58a_-S71MqC04QAAf1VC5A-1; Tue, 18 Apr 2023 11:28:58 -0400 X-MC-Unique: 58a_-S71MqC04QAAf1VC5A-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 78A5588905A; Tue, 18 Apr 2023 15:28:57 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.39.194.149]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5EE7914171B8; Tue, 18 Apr 2023 15:28:54 +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 1/3] mm/ksm: unmerge and clear VM_MERGEABLE when setting PR_SET_MEMORY_MERGE=0 Date: Tue, 18 Apr 2023 17:28:47 +0200 Message-Id: <20230418152849.505124-2-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 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Stat-Signature: zhiyucpzy6zac7338en317pxd3w8ce3k X-Rspam-User: X-Rspamd-Queue-Id: 206F11C0011 X-Rspamd-Server: rspam06 X-HE-Tag: 1681831742-342042 X-HE-Meta: U2FsdGVkX1/a4hm2O0Xh0avAiThW2+SYPg7ALSbie6O0wULCTLKyiBJVish/pxi8y2Z+Kokie2G3c7/LiQPaKH76YUz82M1Vh7PvgTPa1O7LjL4nGB4S3HrZrnVYIVn4yzSsIIXgBW8exbEBBpD2TObwGEnyTdSkbFj33kEO19FY56CTwFuC8l8GJmqTuywInQIylcbUdV/DMFMzrdsGx/Cd41mlfYl+WLNMVMTD4ezIM6LqZiufhpFpgDiuKFj+8eYQLCSpQIoR8dqAM+zEQmIQLlC7b4j8mR6U3miOnV/0IZ2z/kWBXaRR0RZ3M843n7D0xyQR1aGSuDxWwWeU+1OUNiif2hyfV94xzq+ZPyaNMTdekMO1HHCetQ4XZvj6sOJzcNbWsfL4c3M27gl3XQbj4Dt5A4D8apj2NRPbsJ5wNJiaD5hi7Uk9mtHdO2P2hja/ki6aSfsugL72xujtS5KxZA34TioYFRfhgNKBINDOSTonOwa1mv9mB9BW3JV2PR8df/UlUruPiUS+mNeT7LtyHcBbPEH2gBWL+S0wwkucmN0IqULcwl7nm+eqhd4Ow1a6wMDFr/E0GR/ivEp/sTlpVEZFwk+ZULh92P1NQzv81YvlNpike3D+lwgLke1HG+K0eNT5oJxdfRaCv2aaQUAD1UlJsNmBEaTtclm1D3IBSjFp6MedgQkV/q76L5sCBqT1tq+N4TfDBwquUss2ZWe7YdlvyP4ZPijmnZNg84A/GY9o0VwQQgUbwj7xNUiT+b61PswlZ1lD2TFSledQ+4fD3uFoTChEFXppUcAiP2/DbRv2YqP7taWcob2mOKhq+GHSSiZ17cJgaCLGEuliTC7kE3slMC4GlKetz71EBVXUy/MhYigNAIOOLjif88Q3sA98Xp8x5bOfbPHfgSEp5AihgTuyL0wQiHhfnArse1LaCj/79vnDRzBSNmakjzy9eM/gU6kYor5+zt9PA1J t7gJTrAK mb4Pa+nm826Gah+38cYxhhFe67mJaKnaa4FFsPp07hXctt7DjsCF/BMAXyKdxx1jGJJ8TFXa1jL1dk9lGi4Ig23aeerb0zpGXGqvLnIjUIlRJ+CDzYVBrGN0v1KM+GIEktbdrYyvqRqA+tDVo4ZFfvSeRfAozpGUrOf3DeOWRezvdxlS4+WMb4yXyXcgbFXfAKyaWqXE6T1xh3zDTQch4sLWvDBnh8L84bZ/ynnKtSjpasXc= 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. Signed-off-by: David Hildenbrand Acked-by: Stefan Roesch --- 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); } 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) +{ + 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 Tue Apr 18 15:28:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13215829 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 A87EFC77B75 for ; Tue, 18 Apr 2023 15:29:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A74858E0001; Tue, 18 Apr 2023 11:29:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9D5B2900002; Tue, 18 Apr 2023 11:29:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7D8CF8E0005; Tue, 18 Apr 2023 11:29:07 -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 6BC0A8E0001 for ; Tue, 18 Apr 2023 11:29:07 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 01422160342 for ; Tue, 18 Apr 2023 15:29:06 +0000 (UTC) X-FDA: 80694895134.07.F819DA0 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf19.hostedemail.com (Postfix) with ESMTP id 302641A0022 for ; Tue, 18 Apr 2023 15:29:05 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=bmfoqXYv; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf19.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681831745; 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=O7hwLzEYfyCC+1HV0+CQuKr6XrxBkPdmXIv3+pwAsAU=; b=yZtopHsJsmaL6cwAEoBKOx0oL/eY2dZgcVcvo03puYSELZdVzuEY7gH+fmzW0KLox6+uwd aSovz3yeepePfeNFTfpdBMVRPMP4VptriD4dtJDFVk0f0E3TipzHw1xWzt8gbSRCoO+6dS CQ6M+aqKGTwt5/aRGZvJZDtnhLhjNIg= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=bmfoqXYv; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf19.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681831745; a=rsa-sha256; cv=none; b=rMUxCpxBbeExr55Tv1xV1ftCF1vRnVvuLa0mi1+X8sQEYTolD282TfJH7m4tLRyrhavyr1 j4nJ/1L4gtsKqhYvXTtzQ87K5Iet/9VivZPVYOXgtvOAY5O8x/FAIYi3BMWc+vfI/NgX87 7HEu6US8H/zpGRoUUE/6muSqwoK/q68= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681831744; 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=O7hwLzEYfyCC+1HV0+CQuKr6XrxBkPdmXIv3+pwAsAU=; b=bmfoqXYvEjJIxh9L2VOAgzyxW6hxU1TVQEii7fKesGNn04IKohG0D51mitHh4XoyMpvVNG dMloIUPNiWlTzyb14lzh6OjvhpEUE8rU8n71TdfgDBBUJnwhBKh3DHUChk1ii7zUu9aCZT 4RiNPu6hRGq9t3fzWT+/LpOAcH23SAw= 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-283-tx5a0GuAONunj9TrLi1nxQ-1; Tue, 18 Apr 2023 11:29:01 -0400 X-MC-Unique: tx5a0GuAONunj9TrLi1nxQ-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 048921C0A58F; Tue, 18 Apr 2023 15:29:00 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.39.194.149]) by smtp.corp.redhat.com (Postfix) with ESMTP id B148314152F6; Tue, 18 Apr 2023 15:28:57 +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 2/3] selftests/ksm: ksm_functional_tests: add prctl unmerge test Date: Tue, 18 Apr 2023 17:28:48 +0200 Message-Id: <20230418152849.505124-3-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 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Rspamd-Queue-Id: 302641A0022 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: 35cmd41uxyq6rag6dkg8nerzok3skkjg X-HE-Tag: 1681831745-171818 X-HE-Meta: U2FsdGVkX19D7n8KFYA5vwRXSr0olLbKKO+x2Zg82gP6yiRRmfIngCo412ky8gDD/yWCRTcRJ8qACHDk3Yjj0X4J64DIbqwdXBhS0heGgn+ruM7KANseLIhZ1HuDT7vcWn05l0f2WXCHQlijlshf/T9YSA4nmAyvFuhsdphLr5pb3fRuH5fVIfSuorsQEEaDht47SGtFRBL4UTI16BnAsVToyGPNzzfyYfLJ0qzazz+vgV94XnCnqjimebpnLEVv4J0G/zw86OARbu6yzfg81W7q5SX+BPux+ePg+4egMFaBuwWYx+3nc+jopAAUyflz1hs2LscN1YnsbdvP6dxKGJU6ei7NlKQCbNyjWknUjOBzy6/5pIRQFSzt0hd1/R9n02KkvrMZLpxK7lqtfaF2MQd4UEkh4s18zF/pU34ViHTxIOaRjjs3YIxcf/zk+lBjMeCRfrPxpUtzZU6t6DgnQm5+Oi1a2CMaJoD3C5fyBG9YqFlFi/aTsgDZR5N0PDB62WH5OQ4Elw39WQnnEKhUf5ztQ2CcjZ7dCk3gA9+S7I8oJnkS//jY7gTCrhosDrHgFtgTxYnnWAvOsbHqaFbz2VcvtTsxzfm5x++eFS+iCr9FkZ9OWYXp4By6Y040s0fcrU/E33AWCuM2rGe+h7SaCS+oDdk60AypTLL+imTNqLPM0b+mKNpVXuncBRk8sES6n8yQAtXfY+Elh9IIU218vCHTzLAfcogdwiPzX9wVzWSLFei8FS2ld9Yg+WKTNq+0YF5gW+x2EGt0tepVgqzzDEhr6hrJ4S5+SDc6+1cB7Bygkm86NQoKZYyxMKuV0riv9Qj3yJnU1z85evJxcJJIdVhHWi6pS9tjdfRTvHday1hoob4228RCN8xpwHf/DmBj9WFJEp4Y5hfUz9FA4Y7eZGQ1g8fjfh/Fr8ltbgHj9whcaGEoZu+GgJOxMayY2v8i8L6SajHWHsKnT9+AVuN YyozyfTm nq2GME2x2mZfk7RGvlGiNniHaf2ObEDIkFtV5YbD7GefTyOItZ1jX1vJXoVEypkhnYsjHCI7YMRMDM0eS523gpHPZQmSe/duLUZMRfjhgvmaM9T/xW7aSj4m1FZRAxYL2esIKFVXco/5Md1f99QzC+e307ce3YaH2rfT7v0jmp/I6uvw96CkglA10urmkJXv7KeirGbGQ7dZseylPwccC6UiRMuM1pyQgFWdj5KQ8BpVc4Ro= 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. Signed-off-by: David Hildenbrand Acked-by: Stefan Roesch --- .../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 Tue Apr 18 15:28:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13215830 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 9D907C6FD18 for ; Tue, 18 Apr 2023 15:29:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2D148900003; Tue, 18 Apr 2023 11:29:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 25C36900002; Tue, 18 Apr 2023 11:29:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 01227900003; Tue, 18 Apr 2023 11:29:09 -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 DB618900002 for ; Tue, 18 Apr 2023 11:29:09 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id A62E714028E for ; Tue, 18 Apr 2023 15:29:09 +0000 (UTC) X-FDA: 80694895218.15.AEB1C94 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf15.hostedemail.com (Postfix) with ESMTP id D086EA0014 for ; Tue, 18 Apr 2023 15:29:07 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Tebp7ToX; spf=pass (imf15.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=1681831747; 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=d+AIWczdbQmYcywSh4w8GvqvVINNbh2DvrncyfYADn8=; b=sQx6yaZd/02DgjBs4GtlbqqTQCtgnxyV36OCP8DDQnULMftsb21Y8BJNmbS7WlF+JAJwId O6fhYuDsbp2d4Db5ZlLJ12DbQHOclKPApd9joUgxg9+G190LjyZVgdWFSgNZ0H38rdwqDc O3i3B0OR21rX2E7efJkDjzykHUL9jJ4= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Tebp7ToX; spf=pass (imf15.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=1681831747; a=rsa-sha256; cv=none; b=0KOBK8Zo0epCr0SnkuI+HF0Kjyyj+dlMXkB/QPeo6ll0XP/bnlL8tOhYTqTn0d1+pCMFXl N1NQjWSNg1sAv+lDwIvmqS6zE34FbkvziX8D3WiTA+ovwKWPFli0IqxDdwZtvlUq78LaLC dKZkXjcnuzcR7yXYtFg6r3qhBte97N8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681831747; 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=Tebp7ToXI5Y+FHze1qnRFu6atex+UfTwWLBWVAaqKcT0HKlJOCTnm0YdDn4M+YaJtmc3jE z+9Xqico39bS7QednKc0HtwOIMiExiRjQNIxwhcvPpLdPXacFcSWj3Ic3LSZVHHCHqxOVg /LSPNWWjvQF8FQR6rd4WWfEDqdosoFs= 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 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: D086EA0014 X-Stat-Signature: kqpcpkzdnaajtgbxjqy4b8sqz7txrtxz X-Rspam-User: X-HE-Tag: 1681831747-223687 X-HE-Meta: U2FsdGVkX1/1ZpHZpA/4zrqJcGsL6AU8MdGBn5RpZmbxZfkO3rZgGznsFZJjFJ1DqNo6QMgU9CjYveP1F/0PVBTN5JhPidDjF9pWuunFNpn+7JQnoOh0HOqgWbsY+vIPgSE6RXGXaOeXt77nr52cpIEQIVTFDrMEvnvs4VTmo7WodxfBDjj43ou4i0iwgV55F6h6cVkm/XJ5cFoegdNiWdeXfU9//3T+GDYKMDUVnL0vJulyI4IMCeyCrb2qnEdJHr3U5FQ6uiGzP0e19S5vmULWfFY4rKlfM40/Ta5myKcU0cFNFQWtjS5S7Msvuj+ZbSSvKhHHWYUKBjJvVRRg/O53AVFlGUeuzZrjru4lVHoWhJyIG3OQkN18+E4iDfEfxEpf3JUNoeV8+UrYQX/DQlMXyyZqHSlKuJmzrcK2pRho4DFbdd2F3T65ojKnkbXFC0HSwuMF8B9p47CEaXWhRyFiopgYA0nHnIlA3MV01Akn6ThhDYs/pfOORct8slTivPBBt986aojsUlMJywDpB/HSsYx8bMcAopJ0FeE//IKrPEInTxr6cciF8xwRLVsdIBLgY4J6bcmaMXj/m9599v81Vva4mSpuKULSA47x5+QLU5F5J0n0PkslW3CmLnziXQfSl/aIA/RxolxD8YjUtJU2MJc0DgOSH7S+K8gzHLXUsWXMWKAQHLXTSnZnDC92K8Ya3NJ1bV1+a+dZgQMde+urrsJSZ7oNUt5umudJpCoo/+lyBaFtWX54jVlmlmf7uEsjTSEarb+jV52oxZsnKomEsVHyO5snCDKzTrA7NPBJ1utAl9P0c9GdnVFgjIEBzpy5JPenvOW4XLFBQ9xdxUBnOFFDdhAWyFG8qtA4H4UsvB7rovEYhpWG4jxTuMe8kSt/qvEJ+3geYaCR+3+QmPhHVrLYf9uJySamEMcuAd+FkXostOSWe2tyzXvn/cdK/82d5VPESYFRWYgY7fM gNSoTI0C 5Boq2SX9nC6/JqDxsU2G/Z+eqmm8F5gTDlWL2igUZmXPH2MoHpI19dK7YkZ5fIR+PhKNAEFIiCKCIg3DTUUS9wprx6CiL7GxNkkhGdCvswbJb6ChH22U2a9+ZHt2dIyUEr2uagVtylcSYzx06h6JL7QmU/pM8RqD06WW+7vme7B1G7BiE2s8grBMqx8z+V1/LbgZS 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. Signed-off-by: David Hildenbrand Acked-by: Janosch Frank Acked-by: Stefan Roesch --- 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) {