From patchwork Sat Sep 2 05:26:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: wolfgang huang X-Patchwork-Id: 13373003 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 3E669CA0FF8 for ; Sat, 2 Sep 2023 05:26:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 54D3C280003; Sat, 2 Sep 2023 01:26:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4D5A98D0002; Sat, 2 Sep 2023 01:26:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 34F6B280003; Sat, 2 Sep 2023 01:26:24 -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 1F7578D0002 for ; Sat, 2 Sep 2023 01:26:24 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id DF1E5B24DD for ; Sat, 2 Sep 2023 05:26:23 +0000 (UTC) X-FDA: 81190521846.05.CE99057 Received: from m126.mail.126.com (m126.mail.126.com [220.181.12.37]) by imf19.hostedemail.com (Postfix) with ESMTP id 7903A1A000A for ; Sat, 2 Sep 2023 05:26:20 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=126.com header.s=s110527 header.b=Z93nO3sW; spf=pass (imf19.hostedemail.com: domain of wolfgang9277@126.com designates 220.181.12.37 as permitted sender) smtp.mailfrom=wolfgang9277@126.com; dmarc=pass (policy=none) header.from=126.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1693632381; a=rsa-sha256; cv=none; b=NfmqmZya2Ka0GxzLbN7aoTk6xLAlh5Fai2vSTvPSDTe+EEkrAQ82MYe2MJYsz8yBn2pUTr M0Ug+IwH5A//B0FuxRNWVS7iw/NOAedsTCPDiuZC/wCpa3XIZSghxeI+MwBap2IZ02T64H +SX6+yK/mJs1U5/CtIACNcqvzIv0Olk= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=126.com header.s=s110527 header.b=Z93nO3sW; spf=pass (imf19.hostedemail.com: domain of wolfgang9277@126.com designates 220.181.12.37 as permitted sender) smtp.mailfrom=wolfgang9277@126.com; dmarc=pass (policy=none) header.from=126.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1693632381; 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:references:dkim-signature; bh=dOTTB67tzuxAQv6GZuSp1AOBl3+c8/FAXh+3+qA78E8=; b=xEjRr1RR+AFb/pL+AkmCNWEC1I4aBXPjc4p1KPa+8oJ4gA50Dtt8NExIDWBc2nECNyfP8P cZkfw8/QxnQlRYFoRWUj2hnnUzrZRNKxd9JWy3VArPRetwB6AQd5Ys1smW6MjgFepFCcXc mom6ElKL505XjJMFwkPy8FGGbDnw004= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=dOTTB 67tzuxAQv6GZuSp1AOBl3+c8/FAXh+3+qA78E8=; b=Z93nO3sW/uK7DZGaONaTl ZEKGIz0aJfVGmYcQg3g3NpQDjNzLdkXMfeGLmfdkOmolqek2rdsdq8eW6FJ6gmbe ssxDbB5gUXtijjtmk3w9IMQSrrLlNr4faFLl2Pk57iGWtmF+y63SKaXNmiv0UOyq x8++7cvC0cs0hhX1OJk0tA= Received: from wolfgang-dell.localdomain (unknown [113.247.71.247]) by zwqz-smtp-mta-g3-0 (Coremail) with SMTP id _____wDHGil2x_JkwGItAQ--.26023S2; Sat, 02 Sep 2023 13:26:14 +0800 (CST) From: wolfgang huang To: akpm@linux-foundation.org Cc: linux-mm@kvack.org, wolfgang huang Subject: [PATCH] mm/khugepaged: make reserved memory adaptively Date: Sat, 2 Sep 2023 13:26:11 +0800 Message-Id: <20230902052611.144972-1-wolfgang9277@126.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-CM-TRANSID: _____wDHGil2x_JkwGItAQ--.26023S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxJFyUtrW5Ww1xXFykGw4kWFg_yoW5CF17pF W7Gr13GFyUGFs7Gr43Xwn8Zr4rtws7JFWUGryaya4DJan8Jr4xWry8Z3y5X3yUArW8Zr47 Zay7ZFn09w18A37anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07j1RRiUUUUU= X-Originating-IP: [113.247.71.247] X-CM-SenderInfo: xzrowwpdqjmjixx6ij2wof0z/1tbi5RjeFVpD+rgUdwAAsO X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 7903A1A000A X-Stat-Signature: dtg8ec4js7t4taabh7u3jirrsfmmbhe5 X-Rspam-User: X-HE-Tag: 1693632380-538229 X-HE-Meta: U2FsdGVkX1+qnzaX24Dr+xgXgwcRNxyaboyikelmBw1RMv16Ka7E0uovZzh4IeHqztLp4IJ3Ed90bc2K/ofIcI55hgk2N6cgPTtOkdbRJkwMZdWpfoDZNDj4vteYrbpzmbQXdeD2Blt68FeOW0boXsQX76tw4hJ1OS3fiCtKjYyesW0e26Q6AVQuCBASQYEqmQMa4aYZsehhn/7sjXusRaBrGo6i0y4N2WXotm+ZdoBNCmXx4X+1AA0NoGay7oUJvIdAmY1D+d+5DLa41wyJOKfYHiFDkpQ+KWcLMK7iX2NoJM+jWY1N1usHsbzrVqGGWz5C0eJClAjzzojJzG/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bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: wolfgang huang In the 64k page configuration of ARM64, the size of THP is 512MB, which usually reserves almost 5% of memory. However, the probability of THP usage is not high, especially in the madvise configure. and THP is not usually used, but a large amount of memory is reserved for THP use, resulting in a lot of memory waste. So a dynamic method of adjusting reserved memory is proposed. when scan and trying to merge into THP, the reserved memory is pulled up, and when the system does not need to synthesize THP, the reserved memory is kept low. this method can save a lot of memory in scenarios without synthesizing THP, and can effectively pull up the reserved memory to support when THP synthesis is needed. especially in the case of madvise configure, the running effect will be better. Signed-off-by: wolfgang huang --- mm/khugepaged.c | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 88433cc25d8a..41a837e618a5 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -129,6 +129,10 @@ static struct khugepaged_scan khugepaged_scan = { .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head), }; +/* khugepaged should scan or not, trying to be thp, default as false */ +static unsigned int khugepaged_thp_scan_state; +static void set_recommended_min_free_kbytes(void); + #ifdef CONFIG_SYSFS static ssize_t scan_sleep_millisecs_show(struct kobject *kobj, struct kobj_attribute *attr, @@ -2549,6 +2553,33 @@ static void khugepaged_wait_work(void) wait_event_freezable(khugepaged_wait, khugepaged_wait_event()); } +static int khugepaged_threshold(void) +{ + /* thp size threshold check */ + if ((PAGE_SIZE << HPAGE_PMD_ORDER) >= SZ_512M) + return true; + return false; +} + +static void khugepaged_update_wmarks(void) +{ + if (!khugepaged_threshold()) + return; + + /* __khugepaged_enter push khugepaged to work, raise watermark */ + if (khugepaged_has_work()) { + /* Once set, do not repeat distrub watermark */ + if (!khugepaged_thp_scan_state) { + khugepaged_thp_scan_state = true; + mutex_lock(&khugepaged_mutex); + set_recommended_min_free_kbytes(); + mutex_unlock(&khugepaged_mutex); + } + } else { + khugepaged_thp_scan_state = false; + } +} + static int khugepaged(void *none) { struct khugepaged_mm_slot *mm_slot; @@ -2559,6 +2590,7 @@ static int khugepaged(void *none) while (!kthread_should_stop()) { khugepaged_do_scan(&khugepaged_collapse_control); khugepaged_wait_work(); + khugepaged_update_wmarks(); } spin_lock(&khugepaged_mm_lock); @@ -2576,7 +2608,9 @@ static void set_recommended_min_free_kbytes(void) int nr_zones = 0; unsigned long recommended_min; - if (!hugepage_flags_enabled()) { + if (!hugepage_flags_enabled() || + (khugepaged_threshold() && + !khugepaged_thp_scan_state)) { calculate_min_free_kbytes(); goto update_wmarks; }