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 A23D9C64EC4 for ; Wed, 8 Mar 2023 16:47:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 446D76B0072; Wed, 8 Mar 2023 11:47:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3F6DB280001; Wed, 8 Mar 2023 11:47:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2BF576B0075; Wed, 8 Mar 2023 11:47:52 -0500 (EST) 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 1B1BF6B0072 for ; Wed, 8 Mar 2023 11:47:52 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id DE0FFC0F11 for ; Wed, 8 Mar 2023 16:47:51 +0000 (UTC) X-FDA: 80546312742.09.7AF8ACE Received: from mail-qt1-f174.google.com (mail-qt1-f174.google.com [209.85.160.174]) by imf30.hostedemail.com (Postfix) with ESMTP id E66F080025 for ; Wed, 8 Mar 2023 16:47:48 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=cmpxchg-org.20210112.gappssmtp.com header.s=20210112 header.b=cgodqzhq; spf=pass (imf30.hostedemail.com: domain of hannes@cmpxchg.org designates 209.85.160.174 as permitted sender) smtp.mailfrom=hannes@cmpxchg.org; dmarc=pass (policy=none) header.from=cmpxchg.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678294069; a=rsa-sha256; cv=none; b=AQ8AdEmjTqv+v8pqAJmlRrZr4WM1pZLVlWyBNI0HQZlPB/25NFBlCi7/R8QuyVTnFJhRaO 3+J6Tyu0y0LxCl0ucvIi2Hq9ODceXyNofZ9yxCTTI1lL7NrigEJgDfrjur40aPDkiZoL+b C912p661Hmo9tgjdrCs690Qt2X+2pxA= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=cmpxchg-org.20210112.gappssmtp.com header.s=20210112 header.b=cgodqzhq; spf=pass (imf30.hostedemail.com: domain of hannes@cmpxchg.org designates 209.85.160.174 as permitted sender) smtp.mailfrom=hannes@cmpxchg.org; dmarc=pass (policy=none) header.from=cmpxchg.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1678294069; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=KxydbB3uyyn9bCiMvWGSHt2Z+kQttveOlsssE2ak6kI=; b=3vs9/YCk19kpgxsBJIl2f0tCDPN1KTFuUMgsA0bDnU1xR8IICMchG1g4V6EIkEZ+opbkkg bv2Zu45fG87Z5lTp46oDHjSqa3CPf+HqpLkKEt76rUfJmG9N274pwcfP+C1WkUmJ9FDWs/ UgsCOKvKEpyWu41QqqHlKI4ZD8cBewM= Received: by mail-qt1-f174.google.com with SMTP id c19so18671837qtn.13 for ; Wed, 08 Mar 2023 08:47:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20210112.gappssmtp.com; s=20210112; t=1678294068; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=KxydbB3uyyn9bCiMvWGSHt2Z+kQttveOlsssE2ak6kI=; b=cgodqzhqamLdzp7EQPagtF1gYGWqDLN3V7I2zEYyuR3gRxt7mr8Akkw5zcmCOvBK69 jj9g+fiGERJm/1MkvMYDfyG7+MrQnrN1DY3MCaUmitsvLqtLAvua6+EdYkZDsIOWJyPB Hy8kx66XXapoIqDmKQeTXgvQEl3WTmJ6IdrD7WGZIVBMsCoEpYMHzvL64dcU/AesBRNR SpVaNdkWzfz4TylRjWIImpxOVhEZytCAFIn2TPlLVzTRe1vbo60sf4qOhcufF17hpJIE LLpKCOc4w8iFIQbzzpb4tXlHnRkvNV2pbZ1b1lfoPU+f9fcOLj1JAopL5qTZJKILuEPC sfbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678294068; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=KxydbB3uyyn9bCiMvWGSHt2Z+kQttveOlsssE2ak6kI=; b=5Iao+fYHwH3AKTCwx4fYxDNi7M8JWsCgCATN1/ly+l9fOr8eumTKZB2kARCfBax4sV 3mqlR6TQfeNtIUEIXRfdY+2c99K0qQZL03Rh/qW98JxgIC+TBQ+Z+j6xVL9m4Apknoko QvSKlzWrrWBCVL2uc3d763LVln6VCIpt1bk5oxDF7EN69KAEVNlsYf2ze+3nrreUMkUt SDTaVbwqiqWbG+E+3tEMw+myy6soZl7GWaJaDkREFaDuQArIDz1J3ADFJg+S/ECpCR1x 0rXcoO04o08FGJGXjvKsuMlUp5Gbkv9vHnAW9seHIwUHwPQ+V7upMnlFBU2wjGn8IR3d qKVA== X-Gm-Message-State: AO0yUKXcf8HERu/cSOAI6Ya0yI3FUjWodKODO+dj8QIK2fzfybr7kLiw KYdaS1TK5mqdOJEVmKkgaXpX9w== X-Google-Smtp-Source: AK7set97fg/D+XxANClcUJyVA0x8gZg5tcID6He26QN4715WeGw5XZyQfVlTn+sZ1wXRnsBx0LOWlQ== X-Received: by 2002:a05:622a:1106:b0:3b9:bd05:bde1 with SMTP id e6-20020a05622a110600b003b9bd05bde1mr31825069qty.8.1678294067979; Wed, 08 Mar 2023 08:47:47 -0800 (PST) Received: from localhost (2603-7000-0c01-2716-8f57-5681-ccd3-4a2e.res6.spectrum.com. [2603:7000:c01:2716:8f57:5681:ccd3:4a2e]) by smtp.gmail.com with ESMTPSA id 29-20020a05620a041d00b0073b8745fd39sm3215975qkp.110.2023.03.08.08.47.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Mar 2023 08:47:47 -0800 (PST) Date: Wed, 8 Mar 2023 11:47:46 -0500 From: Johannes Weiner To: Stefan Roesch Cc: kernel-team@fb.com, 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: Re: [PATCH v3 1/3] mm: add new api to enable ksm per process Message-ID: <20230308164746.GA473363@cmpxchg.org> References: <20230224044000.3084046-1-shr@devkernel.io> <20230224044000.3084046-2-shr@devkernel.io> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230224044000.3084046-2-shr@devkernel.io> X-Rspam-User: X-Rspamd-Queue-Id: E66F080025 X-Rspamd-Server: rspam01 X-Stat-Signature: 9dkmk96o1npzy5yxgjeaq7xuxz6wxspe X-HE-Tag: 1678294068-889166 X-HE-Meta: U2FsdGVkX1//7vJNK33WRs16GvxpFg8A0a0isHyNZ+J0Q9vIR0LCwZGyOsRoJz4Q9XYSNymtxrkStH3ZBhRHDHps4KqxR0aWJ7MF9TYuqUQvgyxvF2gsjECoAEfGwCGWOGledDePwvV7nmwOkRtSyKewdBF6ZEieWkFAGe7SWdI1N4j1p/Bb3X4BIjFJEr9TgC6vi5DX61ZRAZ7iA1vhUUsMpc0KYqFIGfOX7JbWJ84QP34a6SXyldvDIbVsUK0vfH6IP9VxiDL5Fpx5dcPqebzZZwsLNMOIHZ0v9Q65jV55UgWSvpwr/1BbrSnvPrc42yY97c2MkjOVsiv15zUCqUkjSg7TkzGYfwZlE9a5f9Djes6NC+mpwQwokQmouR3IvOaYwCEocmqIli1WSSWz9i4atwAb5ByvWmloIMmBij4dTuXZf6A4+T7xcRjLJa8nWte8g4j4x4h8Grxr8WSUYgGPs/Sh5f79Jd+hXfRRLggyBf/qpTXgzdrFyODklr6Gj6zth92L3OWcrfBnnctfBQ8w5GYV1hPQHmP4GNNLOtruGtIVKvL+U98QWM1wvt7NHSokDqG8f5nHLJk1tZnoUJyXwv+f23p95LzPLqqmj8RHwdvbueQtaVJFXOfwyzpM86nngNGRAplQpycxYh7w0hsZ9hIzEpd2AZgfGBtsE+ywdsYKuiCz2gaCRrGU3iFwkHxJl6GfOhDDpdjBvGWISzwy/aOmBZ+8X+6CvwsAiGSrYnkSOWhly6Ylr2b3Mcm52NttYzp2xRqOO3pS4GtXBJfUMAppFU1C1w/gDnBwy4nERwQmA78vRwaDkUw5Mc+O2XYDNK1zzh06e+NWmHHIp6fum84MijD5FpiwOIiy7p5agEv5vKGlqBW9b1CAzCkjV/uDAg0+B2EzE5W+N1jE4JCFWYaVVV+ncIOFbyPsEDlyVo4sEn3IibN46XE2C3MmcEPXcZDebCQOAPAZQa9 0+nIZu6G EZaDsnWut5wdoTJJHiTg7nt2EdL2uBVmOR38156xO+y6ilrVZBO3EYTXFvJitRMiX+WRnaSN1XghQB0+ZrQGDK78thgMxwyTgQvUhNwM+1Oin7LEGK4tTbd4uaKF9rhSuL9Nrd5p2tky8CYSbvIJg/PbAfGhCTlU6OjZ9VMwKCNI9K3uslYYZmXjZ9RI8EU3nFZ6V4InETcAgi7xtkRlOxnAgtSxIGrXbXoSPcwQ8j1A8aaQD91FEqHcpyx/1oKCAsn6mFsVl21kN9ojnW59VkNFjx+Vms24YK8AYHPJO+p2Nf+rRIyCMBIyHeCh34lOkDbn2folo6crXEwijksKWWa9ss/7htjzfNqAOfSOVMEnZbL3DWXno4e4I1g+KiADdxD+n2pbzG4SDEMT9oTmkd91UHq/umooLbmFb 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: On Thu, Feb 23, 2023 at 08:39:58PM -0800, Stefan Roesch wrote: > This adds a new prctl to API to enable and disable KSM on a per process > basis instead of only at the VMA basis (with madvise). > > 1) Introduce new MMF_VM_MERGE_ANY flag > > This introduces the new flag MMF_VM_MERGE_ANY flag. When this flag is > set, kernel samepage merging (ksm) gets enabled for all vma's of a > process. > > 2) add flag to __ksm_enter > > This change adds the flag parameter to __ksm_enter. This allows to > distinguish if ksm was called by prctl or madvise. > > 3) add flag to __ksm_exit call > > This adds the flag parameter to the __ksm_exit() call. This allows to > distinguish if this call is for an prctl or madvise invocation. > > 4) invoke madvise for all vmas in scan_get_next_rmap_item > > If the new flag MMF_VM_MERGE_ANY has been set for a process, iterate > over all the vmas and enable ksm if possible. For the vmas that can be > ksm enabled this is only done once. > > 5) support disabling of ksm for a process > > This adds the ability to disable ksm for a process if ksm has been > enabled for the process. > > 6) add new prctl option to get and set ksm for a process > > 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 Hey Stefan, thanks for merging the patches into one. I found it much easier to review. Overall this looks straight-forward to me. A few comments below: > @@ -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; So if the workload has already madvised specific VMAs the process-enablement will fail. Why is that? Shouldn't it be possible to override a local decision from an outside context that has more perspective on both sharing opportunities and security aspects? If there is a good reason for it, the -EINVAL should be addressed in the manpage. And maybe add a comment here as well. > + 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; > diff --git a/mm/ksm.c b/mm/ksm.c > index 56808e3bfd19..23d6944f78ad 100644 > --- a/mm/ksm.c > +++ b/mm/ksm.c > @@ -1063,6 +1063,7 @@ static int unmerge_and_remove_all_rmap_items(void) > > mm_slot_free(mm_slot_cache, mm_slot); > clear_bit(MMF_VM_MERGEABLE, &mm->flags); > + clear_bit(MMF_VM_MERGE_ANY, &mm->flags); > mmdrop(mm); > } else > spin_unlock(&ksm_mmlist_lock); > @@ -2329,6 +2330,17 @@ static struct ksm_rmap_item *get_next_rmap_item(struct ksm_mm_slot *mm_slot, > return rmap_item; > } > > +static bool vma_ksm_mergeable(struct vm_area_struct *vma) > +{ > + if (vma->vm_flags & VM_MERGEABLE) > + return true; > + > + if (test_bit(MMF_VM_MERGE_ANY, &vma->vm_mm->flags)) > + return true; > + > + return false; > +} > + > static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page) > { > struct mm_struct *mm; > @@ -2405,8 +2417,20 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page) > goto no_vmas; > > for_each_vma(vmi, vma) { > - if (!(vma->vm_flags & VM_MERGEABLE)) > + if (!vma_ksm_mergeable(vma)) > continue; > + if (!(vma->vm_flags & VM_MERGEABLE)) { IMO, the helper obscures the interaction between the vma flag and the per-process flag here. How about: if (!(vma->vm_flags & VM_MERGEABLE)) { if (!test_bit(MMF_VM_MERGE_ANY, &vma->vm_mm->flags)) continue; /* * With per-process merging enabled, have the MM scan * enroll any existing and new VMAs on the fly. * ksm_madvise(); } > + unsigned long flags = vma->vm_flags; > + > + /* madvise failed, use next vma */ > + if (ksm_madvise(vma, vma->vm_start, vma->vm_end, MADV_MERGEABLE, &flags)) > + continue; > + /* vma, not supported as being mergeable */ > + if (!(flags & VM_MERGEABLE)) > + continue; > + > + vm_flags_set(vma, VM_MERGEABLE); I don't understand the local flags. Can't it pass &vma->vm_flags to ksm_madvise()? It'll set VM_MERGEABLE on success. And you know it wasn't set before because the whole thing is inside the !set branch. The return value doesn't seem super useful, it's only the flag setting that matters: ksm_madvise(vma, vma->vm_start, vma->vm_end, MADV_MERGEABLE, &vma->vm_flags); /* madvise can fail, and will skip special vmas (pfnmaps and such) */ if (!(vma->vm_flags & VM_MERGEABLE)) continue; > + } > if (ksm_scan.address < vma->vm_start) > ksm_scan.address = vma->vm_start; > if (!vma->anon_vma) > @@ -2491,6 +2515,7 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page) > > mm_slot_free(mm_slot_cache, mm_slot); > clear_bit(MMF_VM_MERGEABLE, &mm->flags); > + clear_bit(MMF_VM_MERGE_ANY, &mm->flags); > mmap_read_unlock(mm); > mmdrop(mm); > } else { > @@ -2664,12 +2690,39 @@ int __ksm_enter(struct mm_struct *mm) > return 0; > } > > -void __ksm_exit(struct mm_struct *mm) > +static void unmerge_vmas(struct mm_struct *mm) > +{ > + struct vm_area_struct *vma; > + struct vma_iterator vmi; > + > + vma_iter_init(&vmi, mm, 0); > + > + mmap_read_lock(mm); > + for_each_vma(vmi, vma) { > + if (vma->vm_flags & VM_MERGEABLE) { > + unsigned long flags = vma->vm_flags; > + > + if (ksm_madvise(vma, vma->vm_start, vma->vm_end, MADV_UNMERGEABLE, &flags)) > + continue; > + > + vm_flags_clear(vma, VM_MERGEABLE); ksm_madvise() tests and clears VM_MERGEABLE, so AFAICS for_each_vma(vmi, vma) ksm_madvise(); should do it... > + } > + } > + mmap_read_unlock(mm); > +} > + > +void __ksm_exit(struct mm_struct *mm, int flag) > { > struct ksm_mm_slot *mm_slot; > struct mm_slot *slot; > int easy_to_free = 0; > > + if (!(current->flags & PF_EXITING) && flag == MMF_VM_MERGE_ANY && > + test_bit(MMF_VM_MERGE_ANY, &mm->flags)) { > + clear_bit(MMF_VM_MERGE_ANY, &mm->flags); > + unmerge_vmas(mm); ...and then it's short enough to just open-code it here and drop the unmerge_vmas() helper.