From patchwork Fri Mar 21 17:37:26 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sourav Panda X-Patchwork-Id: 14025836 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 2003CC36002 for ; Fri, 21 Mar 2025 17:37:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 08F1F280005; Fri, 21 Mar 2025 13:37:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 01A91280001; Fri, 21 Mar 2025 13:37:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D887C280005; Fri, 21 Mar 2025 13:37:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id AFE4D280001 for ; Fri, 21 Mar 2025 13:37:40 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id ACD05AA33C for ; Fri, 21 Mar 2025 17:37:41 +0000 (UTC) X-FDA: 83246265522.04.F1980CD Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) by imf27.hostedemail.com (Postfix) with ESMTP id E928640010 for ; Fri, 21 Mar 2025 17:37:39 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=PbuF+9qh; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf27.hostedemail.com: domain of 34qPdZwsKCOUZVbYHcWHUKHNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--souravpanda.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=34qPdZwsKCOUZVbYHcWHUKHNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--souravpanda.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1742578660; a=rsa-sha256; cv=none; b=nv4D1NjypRzYnBJ7S+ss94gGwtBABZI4VhMkE3py0zP5tFXh72rPz1BoIxPVRdnu1f6Zo8 bebz6Rdcx9+3ZHJNrjj2Hwsx653oYYyWrIRhBH24EBc6zwG/HFoKPiTT8m/Evn1sSR1Qxs nxtNekRsDXZvVYvczxHGJawsgHsJub4= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=PbuF+9qh; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf27.hostedemail.com: domain of 34qPdZwsKCOUZVbYHcWHUKHNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--souravpanda.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=34qPdZwsKCOUZVbYHcWHUKHNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--souravpanda.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1742578660; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=JhyKuUbIG2qUFhxjEu6Kg0kSJWlrJ10xlnpfsY0CXs8=; b=KR/oUYOQQSeEOCMmHAb+PiwL6A0NlJN3LzUGvONB94BrT+ZI+Et9zyc8OfVrXg+ixqO+PF AstXXSXi88byMIyNUiwTlHOMWY08VN7+oIvgp3fQszn0TCUanN/u8GJ4kQYiX27iZg+ZsV sF6qSLaJkq3ih4ooUZop5BoT4qnLrsU= Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-3011c150130so3492874a91.2 for ; Fri, 21 Mar 2025 10:37:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1742578659; x=1743183459; darn=kvack.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=JhyKuUbIG2qUFhxjEu6Kg0kSJWlrJ10xlnpfsY0CXs8=; b=PbuF+9qh4LDeeoj4z7GdXLT/eEDHunRkuk2gOGONO08sw6EAu43l3xPdNdtKuS41LS ziPI0Cdv9MxCikcPSSa7X1FPn8673aUz56eJfpZmLJmbLDZrNnNjb/FF+mus4M5KFvjW YDmQswg1LC9AgtK4Yrw9Tp5GrkgYiSQaJan7yVRlYIrZ9PYYCplVGvNb6A39odak15+V dIa0W0+GPgQ/9VOphxR32vdECqtpEelbQ3oJ8nZ//20c0TWJray0CWVaQgxxdKFjqVJt S5gs5fmUh+K9OMdm6Zge1xGTYaq7CGTVJCZpE3vThT/buu+evhfI/RX9mMh05MQccMjV SpDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742578659; x=1743183459; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=JhyKuUbIG2qUFhxjEu6Kg0kSJWlrJ10xlnpfsY0CXs8=; b=SrUP6rYSDaUS9X6lOVECDFcvo1KUXHVUuJR6F320WXx3oTHH7ZghKCHauHanWKCrIU pfn/to6fWyCrVFCUMuMqIKV7dcXi5QF00g+/7XL7LV/Zvw6SdohUPkQwEdiLaMfr5BHY INQU9X26yAygyNge6HQHcTOOVbJkU7cJKIfy3nuSP/fJVA7mhe5XdmRxTVFBN8XkUFfR BJp1kRJsrxCZlYw7zFje+6hzgy/E3dhAuAl/L0groYDAO7UYjZPE4mB9oNGp+9I/hm0W pKTioWRCOrA8luZXsE97RgbJVBVFdj2PDrWvpDgt65ggdGg1uGk95EfvLZSANXqK3nLS hHaQ== X-Forwarded-Encrypted: i=1; AJvYcCXjWF03B18a2URBW0oQOdZz1SQDPxAexVO0sJCEIIBtf/BWcFnkB+TwevkbgKAS8kUy5WfmIpr/Dw==@kvack.org X-Gm-Message-State: AOJu0YxLThagVcFcwulsx9YE/zVVvaKe5rzyi7oaZ8fc4rFYAW5xOKWw MhTUTjK7ZvrZbVUBqzorkyOjShxKrATmKOqyc9bqA65sMejq5p2Un/TlB4pTRYJtqiWXF3uZXP2 x6NnwctONg+F0eO3ZEF+mYA== X-Google-Smtp-Source: AGHT+IErW6hIRZXrutBcTb+9kRL8XmrfeSil0yg5cL4+Y+ZPuvGKGUahbLis4vVKFNz4GDCbx8eM7VDHYQJtpswc8A== X-Received: from pgar25.prod.google.com ([2002:a05:6a02:2e99:b0:ad7:adb7:8c14]) (user=souravpanda job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:1582:b0:1f3:2e85:c052 with SMTP id adf61e73a8af0-1fe4330249cmr6556407637.35.1742578658785; Fri, 21 Mar 2025 10:37:38 -0700 (PDT) Date: Fri, 21 Mar 2025 17:37:26 +0000 In-Reply-To: <20250321173729.3175898-1-souravpanda@google.com> Mime-Version: 1.0 References: <20250321173729.3175898-1-souravpanda@google.com> X-Mailer: git-send-email 2.49.0.395.g12beb8f557-goog Message-ID: <20250321173729.3175898-4-souravpanda@google.com> Subject: [RFC PATCH 3/6] mm: make Selective KSM partitioned From: Sourav Panda To: mathieu.desnoyers@efficios.com, willy@infradead.org, david@redhat.com, pasha.tatashin@soleen.com, rientjes@google.com, akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, weixugc@google.com, gthelen@google.com, souravpanda@google.com, surenb@google.com X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: E928640010 X-Stat-Signature: ixhscjy14ep63k8iz5yr8xumy76qtcyk X-HE-Tag: 1742578659-118839 X-HE-Meta: U2FsdGVkX19XHDJciqCb+JtjybuxrPxus+v+vA4VmjMQAq1Cs3yEzKXXjtLgUaEqJOnTNWh4GiUregzPpaj9NGnOnQQyzV7mVkSzKpncx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iOBGrIuTvKk9grZoXjaSKWObxDQTG6Zxqr8wV23kiOFs9c0uMcb94YWdHhh/MrCHQNwJZHGccDi0ynQEbbQXCPF/9XgqJyeTPOc6FqkcFGw5Ua2yasNnz5E5fF+xBOCtYTx5nK9jlrUwCDBrpSC0qcc9hfe1SpF9flnhL/3Sc/0LLh/XE4i/F7FWjX7eQJPuR2CGQnUOoGlvvu5kp1Zpaj7HRIZ0m96IoCLz9NvQrBVmRnL2bqyp1D+I8LGh4Nmy4E2Uf3Re/raHERNjQjhQYiPaKuP/j6j5s6Jc+IJ8gV3zR3NVysnILzpC7uP5OWXBohwAmgixPoSs+PXC0LTBPt+/X7pXAZXPYcjbBRWO0Es52ncFj/Kx9LodNUXH4nFy3rMSWcJvA= 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: List-Subscribe: List-Unsubscribe: Create a sysfs interface to partition the KSM merge space. We add a new sysfs file, namely add_partition. Which is used to specify the name of the new partition. Once a partition is created, we would get the traditional files typcally available in KSM under each partition. This sysfs interface changes are in preparation of the following patch that shall actually partition the merge space (e.g., prevent page-comparison and merging across partitions). KSM_SYSFS=/sys/kernel/mm/ksm echo "part_1" > ${KSM_SYSFS}/ksm/control/add_partition ls ${KSM_SYSFS}/part_1/ pages_scanned pages_to_scan sleep_millisecs ... echo "pid start_addr end_addr" > ${KSM_SYSFS}/part_1/trigger_merge Signed-off-by: Sourav Panda --- mm/ksm.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 95 insertions(+), 6 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index b2f184557ed9..927e257c48b5 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -3832,7 +3832,17 @@ static ssize_t full_scans_show(struct kobject *kobj, } KSM_ATTR_RO(full_scans); -#ifndef CONFIG_SELECTIVE_KSM +#ifdef CONFIG_SELECTIVE_KSM +static struct kobject *ksm_base_kobj; + +struct partition_kobj { + struct kobject *kobj; + struct list_head list; +}; + +static LIST_HEAD(partition_list); + +#else /* CONFIG_SELECTIVE_KSM */ static ssize_t smart_scan_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { @@ -4015,15 +4025,22 @@ static struct attribute *ksm_attrs[] = { static const struct attribute_group ksm_attr_group = { .attrs = ksm_attrs, +#ifndef CONFIG_SELECTIVE_KSM .name = "ksm", +#endif }; -static int __init ksm_sysfs_init(void) +static int __init ksm_sysfs_init(struct kobject *kobj, + const struct attribute_group *grp) { - return sysfs_create_group(mm_kobj, &ksm_attr_group); + int err; + + err = sysfs_create_group(kobj, grp); + return err; } #else /* CONFIG_SYSFS */ -static int __init ksm_sysfs_init(void) +static int __init ksm_sysfs_init(struct kobject *kobj, + const struct attribute_group *grp) { ksm_run = KSM_RUN_MERGE; /* no way for user to start it */ return 0; @@ -4031,9 +4048,81 @@ static int __init ksm_sysfs_init(void) #endif /* CONFIG_SYSFS */ #ifdef CONFIG_SELECTIVE_KSM +static ssize_t add_partition_store(struct kobject *kobj, + struct kobj_attribute *attr, + const char *buf, size_t count) +{ + struct partition_kobj *new_partition_kobj; + char partition_name[50]; + int err; + + mutex_lock(&ksm_thread_mutex); + + if (count >= sizeof(partition_name)) { + err = -EINVAL; /* Prevent buffer overflow */ + goto unlock; + } + + snprintf(partition_name, sizeof(partition_name), + "%.*s", (int)(count - 1), buf); /* Remove newline */ + + /* Allocate memory for new dynamic kobject entry */ + new_partition_kobj = kmalloc(sizeof(*new_partition_kobj), GFP_KERNEL); + if (!new_partition_kobj) { + err = -ENOMEM; + goto unlock; + } + + new_partition_kobj->kobj = kobject_create_and_add(partition_name, + ksm_base_kobj); + if (!new_partition_kobj) { + kfree(new_partition_kobj); + err = -ENOMEM; + goto unlock; + } + + err = sysfs_create_group(new_partition_kobj->kobj, &ksm_attr_group); + if (err) { + pr_err("ksm: register sysfs failed\n"); + kfree(new_partition_kobj); + err = -ENOMEM; + goto unlock; + } + + list_add(&new_partition_kobj->list, &partition_list); + +unlock: + mutex_unlock(&ksm_thread_mutex); + return err ? err : count; +} + +static struct kobj_attribute add_kobj_attr = __ATTR(add_partition, 0220, NULL, + add_partition_store); + +/* Array of attributes for base kobject */ +static struct attribute *ksm_base_attrs[] = { + &add_kobj_attr.attr, + NULL, /* NULL-terminated */ +}; + +/* Attribute group for base kobject */ +static struct attribute_group ksm_base_attr_group = { + .name = "control", + .attrs = ksm_base_attrs, +}; + static int __init ksm_thread_sysfs_init(void) { - return ksm_sysfs_init(); + int err; + + ksm_base_kobj = kobject_create_and_add("ksm", mm_kobj); + if (!ksm_base_kobj) { + err = -ENOMEM; + return err; + } + + err = ksm_sysfs_init(ksm_base_kobj, &ksm_base_attr_group); + return err; } #else /* CONFIG_SELECTIVE_KSM */ static int __init ksm_thread_sysfs_init(void) @@ -4048,7 +4137,7 @@ static int __init ksm_thread_sysfs_init(void) return err; } - err = ksm_sysfs_init(); + err = ksm_sysfs_init(mm_kobj, &ksm_attr_group); if (err) { pr_err("ksm: register sysfs failed\n"); kthread_stop(ksm_thread);