From patchwork Fri Feb 10 21:50:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13136500 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 DDBF9C636D7 for ; Fri, 10 Feb 2023 21:53:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 705EC28001B; Fri, 10 Feb 2023 16:53:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6B678280003; Fri, 10 Feb 2023 16:53:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5A56028001B; Fri, 10 Feb 2023 16:53:51 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 4E705280003 for ; Fri, 10 Feb 2023 16:53:51 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 2EA2A12069E for ; Fri, 10 Feb 2023 21:53:51 +0000 (UTC) X-FDA: 80452735062.16.D7AD7A6 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf28.hostedemail.com (Postfix) with ESMTP id 8C33FC0010 for ; Fri, 10 Feb 2023 21:53:49 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf28.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676066029; 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; bh=F3ntdbs+KWymOd0gbDPBwV5xBWcXawh1LGHRDxPKvoI=; b=aUX0E5V/glT3qlU28hTJG+ms/aYKL/vuHK4DrLw/ZHRUY5AYyAylfpyD7Xdf55zhtCAScH bgzTvo5WzUobm/ONEr7o+pbpQ1dmiUxse0Gl63nJbfbiHoTb9YeJ5tDasJEgXCEvHSiWvJ VYzDytJVZBZ10R3APuN8PV3MYlaWWrk= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf28.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676066029; a=rsa-sha256; cv=none; b=pOO1vIkGEtLNWDqL87uLkxi3uqz6dG+oOsGZTesCNFPL8+CewBPU4ABrZjaAuxU7LAa0K+ gaF2Vxx4O3/YfCoXg+xrdPdwhaq1acI9kuLIlYNB2rNApAM5dOjMcKxxntkNxZbcjvNs4e i3F3zkQsQcnSXOEm2M0HxnYuvWd+hgI= Received: by dev0134.prn3.facebook.com (Postfix, from userid 425415) id 7BD896BFC2D1; Fri, 10 Feb 2023 13:50:33 -0800 (PST) From: Stefan Roesch To: kernel-team@fb.com Cc: shr@devkernel.io, linux-mm@kvack.org, riel@surriel.com, mhocko@suse.com, david@redhat.com, linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org, akpm@linux-foundation.org Subject: [RFC PATCH v2 06/19] mm: add new prctl option to get and set ksm for a process Date: Fri, 10 Feb 2023 13:50:10 -0800 Message-Id: <20230210215023.2740545-7-shr@devkernel.io> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230210215023.2740545-1-shr@devkernel.io> References: <20230210215023.2740545-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 8C33FC0010 X-Stat-Signature: es6r53fype1hmm9ua8wz7wfbirz7bmik X-HE-Tag: 1676066029-462578 X-HE-Meta: U2FsdGVkX1/j33MUC/DjTyqFxQpVAdp2IONYSe5bewIFrAuzrxrj0OPLHt8mAPqVIlgeIqUnF2g9lWWnDKdd5hOKbIUYTo979z36yDQFKr1h9Xa3ABobmhfsi8IL5YnidpGq0T2O9i056fNGE5QmaBKBRke7VfKJjWFLmzHuUBpHEcDP1wrZUXptcujAE9/oo/RqL+kJiSXn5D+A5h4nRffmfGdiPERrbM49jVYfiacKU8Qbw9CxzN7u44uOGfszjknU2/QORBOumblVOndRCgi6Y40zgwrhj2Se4iE1AEDva58uWcx0eULc3r2LGjmXYOxfXRA0FxbmNWgzf5DiYKngi3YenjE0F+uq0+GinVuyA+C/3j5jgMK4TON/GWBmROUNbfZJLonOaN3sciydIztGAtQpAkroUZF2iP25Xk50+1oJILK0uNw4QUTe9doFMTT3WqeOJKnjj6H2vYnuGC3UqrKFU7n9pP779/8uaV9pX+azMtyg3dAFpwJC4eqiCP41nAN5K/04vDiUVUraXpe8vdF4IO/V3MnQ4k2JMwVVDLlbDyG9GbUTYJRnWCbm4tOJxnkggsG+n1Dgp51oEA0dptYThHstozmXZSITvoUFnZHqTZ51mmLxsHamqsW9gd6/eFUNX93mLDsxcf1ahUy7lcGfm77iEnbWJeBKoF+YOt+QHuruqGv11Es8vhbSKy2xScPtvX4o0qTe5+sYNacBjhGUziLsZZkjkvYg0AJvHp3y4/jCqbmogA+wBeYqR6Tw85gUIyfWfKEqXNr2LuVjZ6BeoMmoRs2tVkHdJE3L37GZ5ZR1n9JDvFfwliRZig3vRubG+nswWDEjAOH1/N7oJWBLY1jMeC+XuMvBpOq1V3XuZ3d4OEXYXjwdgtiCncrK0c5msf5/qM8PGUAzKCdRHURjPWwkN9k+ZC02tvaHPtxDX7uCLZ8pgWO1DAmlX9edPJYM02hWayedXmc sPnIMGYo KH+8rdqSVaTH/UT8tZihikEtPogRD8f/xFVinEAU0zKvyaPPjg5s8ZLZQ45rwsWEIAW3CMMMFo4bGK48= 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: This adds two new options to the prctl system call - enable ksm for all vmas of a process (if the vmas support it). - query if ksm has been enabled for a process. Signed-off-by: Stefan Roesch --- include/uapi/linux/prctl.h | 2 ++ kernel/sys.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index 1312a137f7fb..759b3f53e53f 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -290,4 +290,6 @@ struct prctl_mm_map { #define PR_SET_VMA 0x53564d41 # define PR_SET_VMA_ANON_NAME 0 +#define PR_SET_MEMORY_MERGE 67 +#define PR_GET_MEMORY_MERGE 68 #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/sys.c b/kernel/sys.c index b3cab94545ed..495bab3ed2ad 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -2659,6 +2660,34 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, case PR_SET_VMA: error = prctl_set_vma(arg2, arg3, arg4, arg5); break; +#ifdef CONFIG_KSM + case PR_SET_MEMORY_MERGE: + if (!capable(CAP_SYS_RESOURCE)) + return -EPERM; + + if (arg2) { + if (mmap_write_lock_killable(me->mm)) + return -EINTR; + + if (test_bit(MMF_VM_MERGEABLE, &me->mm->flags)) + error = -EINVAL; + else if (!test_bit(MMF_VM_MERGE_ANY, &me->mm->flags)) + error = __ksm_enter(me->mm, MMF_VM_MERGE_ANY); + mmap_write_unlock(me->mm); + } else { + __ksm_exit(me->mm, MMF_VM_MERGE_ANY); + } + break; + case PR_GET_MEMORY_MERGE: + if (!capable(CAP_SYS_RESOURCE)) + return -EPERM; + + if (arg2 || arg3 || arg4 || arg5) + return -EINVAL; + + error = !!test_bit(MMF_VM_MERGE_ANY, &me->mm->flags); + break; +#endif default: error = -EINVAL; break;