From patchwork Fri Feb 10 21:50:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13136496 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 D31FFC636D4 for ; Fri, 10 Feb 2023 21:50:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 33961280016; Fri, 10 Feb 2023 16:50:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 29B05280003; Fri, 10 Feb 2023 16:50:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1186E280016; Fri, 10 Feb 2023 16:50:54 -0500 (EST) 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 E34A5280003 for ; Fri, 10 Feb 2023 16:50:53 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id B9BB1120455 for ; Fri, 10 Feb 2023 21:50:53 +0000 (UTC) X-FDA: 80452727586.07.62987BE Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf30.hostedemail.com (Postfix) with ESMTP id 298788000A for ; Fri, 10 Feb 2023 21:50:51 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf30.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=1676065852; 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=tH1aMisEakR+8J/dUNezqYTnMcIISoU8PJF94N16sdY=; b=2sG85/ONQnX4JwHDavOgA/aWV0xbtGH56OMCg6WEh1VHvMKDypvaOKIc53t+AmKDwSYY8b baNVV0h+D4ljfLKFPgknj8QrQ95YxC08osMLeU8yXMDjfvEbGlxqyCm87VRTnbOEIF7E+2 yCH5Bz5h+JVNIrRAhMBVhYhUNrmW13g= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf30.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=1676065852; a=rsa-sha256; cv=none; b=VelE/XPYNYzm3jNPxVa2qMOTHGfiIh1/VRFyqdWdohRAWqSPTzcnOw0BMqgNsmH8W59ZKy T5Oj152etcPVfKQXqdO3K3qqSP2vjmC+DTT9C1l0veGF3pGrCzvG840onkNMU+K2FBZe/x 2c+Lg2Iw9upKCmBCoXp8L30GJ2mJaBY= Received: by dev0134.prn3.facebook.com (Postfix, from userid 425415) id 6E3806BFC2C9; 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 03/19] mm: add flag to __ksm_exit call Date: Fri, 10 Feb 2023 13:50:07 -0800 Message-Id: <20230210215023.2740545-4-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-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 298788000A X-R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JRUSu/Pi06EZO9azOIooVcY7pIJ5zXu/1GtUHsR1C1DuQATJ7HIa5YcNJ11cQ6sU0I1N5/jkVN9e+Z/SQkX 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 the flag parameter to the __ksm_exit() call. This allows to distinguish if this call is for an prctl or madvise invocation. Signed-off-by: Stefan Roesch --- include/linux/ksm.h | 8 +++++--- mm/ksm.c | 8 ++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/include/linux/ksm.h b/include/linux/ksm.h index 50e6b56092f3..d38a05a36298 100644 --- a/include/linux/ksm.h +++ b/include/linux/ksm.h @@ -18,8 +18,8 @@ #ifdef CONFIG_KSM int ksm_madvise(struct vm_area_struct *vma, unsigned long start, unsigned long end, int advice, unsigned long *vm_flags); -void __ksm_exit(struct mm_struct *mm); int __ksm_enter(struct mm_struct *mm, int flag); +void __ksm_exit(struct mm_struct *mm, int flag); static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) { @@ -32,8 +32,10 @@ static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) static inline void ksm_exit(struct mm_struct *mm) { - if (test_bit(MMF_VM_MERGEABLE, &mm->flags)) - __ksm_exit(mm); + if (test_bit(MMF_VM_MERGE_ANY, &mm->flags)) + __ksm_exit(mm, MMF_VM_MERGE_ANY); + else if (test_bit(MMF_VM_MERGEABLE, &mm->flags)) + __ksm_exit(mm, MMF_VM_MERGEABLE); } /* diff --git a/mm/ksm.c b/mm/ksm.c index afc0980592af..3597ae52fe7b 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -2665,12 +2665,16 @@ int __ksm_enter(struct mm_struct *mm, int flag) return 0; } -void __ksm_exit(struct mm_struct *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); + /* * This process is exiting: if it's straightforward (as is the * case when ksmd was never running), free mm_slot immediately. @@ -2697,7 +2701,7 @@ void __ksm_exit(struct mm_struct *mm) if (easy_to_free) { mm_slot_free(mm_slot_cache, mm_slot); - clear_bit(MMF_VM_MERGEABLE, &mm->flags); + clear_bit(flag, &mm->flags); mmdrop(mm); } else if (mm_slot) { mmap_write_lock(mm);