From patchwork Thu Aug 29 10:20:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhongkun He X-Patchwork-Id: 13782955 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 13470C83030 for ; Thu, 29 Aug 2024 10:20:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9DAAF6B00A9; Thu, 29 Aug 2024 06:20:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 98A7D6B00AA; Thu, 29 Aug 2024 06:20:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 82B536B00AB; Thu, 29 Aug 2024 06:20:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 63E596B00A9 for ; Thu, 29 Aug 2024 06:20:53 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 2AF94A06AD for ; Thu, 29 Aug 2024 10:20:53 +0000 (UTC) X-FDA: 82504889586.03.0238D92 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by imf18.hostedemail.com (Postfix) with ESMTP id 4CA901C0012 for ; Thu, 29 Aug 2024 10:20:51 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=kPX8qUuK; spf=pass (imf18.hostedemail.com: domain of hezhongkun.hzk@bytedance.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=hezhongkun.hzk@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724926781; 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=IjshDs/Uxrgcfjr5jDqtbHwONktfCrAQk2fGGuPSrO0=; b=iWi1qeijiV/JHQW12b775QZfD77AkXLf3nBDgkXzhwH9yKzqMJnKJ0sQGk79Ky2G3xdaUL LDtToI6gKD2y+/k1mQ33gQ+bcbKwd/BgiRf8E5PRJi8cTA3X/C/m0pGTGIm6v/IHwnZIle pwferSvW9rv2zk5Jzx24uLaavkx2sJg= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=kPX8qUuK; spf=pass (imf18.hostedemail.com: domain of hezhongkun.hzk@bytedance.com designates 209.85.214.174 as permitted sender) smtp.mailfrom=hezhongkun.hzk@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724926781; a=rsa-sha256; cv=none; b=s+W26zi3pjEnClko6z4+LQqGX5H4qzh6bbXjbL0qXdGa2UqInYT/cdIglgFEVEhUGJ6ETo k/xYhNutX84j6XpFElbC7DjVxiSawi9D1+9MVxzA0c2Z/kiUPN4GO4LwfjxkCeKMMzNiUB UMehEgNgs0ICOeoamDVG+Ks2kZ3Q1UQ= Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-204f52fe74dso4205025ad.1 for ; Thu, 29 Aug 2024 03:20:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1724926850; x=1725531650; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=IjshDs/Uxrgcfjr5jDqtbHwONktfCrAQk2fGGuPSrO0=; b=kPX8qUuKpXn150X1HinGtJj/PgPD7yhN9DWnjp88a9T/jb4o8qxJ3mQsZibtWq6IKy +ypsOMRErDzf1Zzi1yQZ02c1XlhSIF9SzDs40n2iRZYs6udWgPCrvnNVLUZJKnWj3OMn g1sYCwedqiN/iID5si2P05JCSRfDdt1OQLp0BCxAxIjBf3tJroGb35JagYPJvssnwp7t f++AYBUEisWVXI41vw6AxFGFlQO6m6WHaon65ZggYdj3a4/rGoBFBp6rUBMyXdOYkpLq 5bn+wHHSCw+eQMm9eEN8Tizke1I1T5FM5/gJMQxOP8NinqdqOILVZXjwOtFxc8V8prJ9 byDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724926850; x=1725531650; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=IjshDs/Uxrgcfjr5jDqtbHwONktfCrAQk2fGGuPSrO0=; b=sW2fZBVuZPJ/RhZ13Eb1h1mUQQVkrI1YAmvTYgKFG8NFVXyeyxHwtxVR0eaop0Etq5 8f+HlN3io4Bw+fOx12cQnzGVs8OYDJS/r5Ka5IBKoa9yvQN4rlBf19aFR9BY8cWdWXBl rHn8lOXhR4XQOR0XU0JLsYjvgP5q0Qhs8hXGLlCMRbyaPFKzAS4RFgHD/FSw4tldrpGi F9uA+j+IKYmdMtTYJgvQqmW8C+kHo9pt6MUv/Pa6m7hjZTKV3moz+c0fLOk3MwArCFBq JxQQHGDasxJFJcJcnf3TflrFe68svs8120qRxXZRzdZVjIpkTon1cvoUcNiquDvO8rbD JooA== X-Forwarded-Encrypted: i=1; AJvYcCVCa3x09uYVSOKotXze8BsqpNN1PinimV43e8h0GcQgb8/BvlY4/CJ/Vf5NzeWa7NH6zbrbPfP5LA==@kvack.org X-Gm-Message-State: AOJu0YyJy+J48RRW4mTR6oVaSLEXiugblPcHbm/p5xhSymRIY8Htuc1X apzjDhqgNJRGz1q8xxqsp6WINiXqxfwe5tPSOyfYA6t0e8wWexwkC+Y/etIy5yc= X-Google-Smtp-Source: AGHT+IGdl0UcdnYDfDgFEzVC4k/yFpeWbl6UrHniAh9IltC24M18uuh9SesGq3bbAB7noPRGR0hr/Q== X-Received: by 2002:a17:902:e74e:b0:1fb:8aa9:e2a9 with SMTP id d9443c01a7336-2050c345affmr28095435ad.15.1724926849984; Thu, 29 Aug 2024 03:20:49 -0700 (PDT) Received: from n37-034-248.byted.org ([180.184.51.134]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-205155673a2sm8355725ad.303.2024.08.29.03.20.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Aug 2024 03:20:48 -0700 (PDT) From: Zhongkun He To: akpm@linux-foundation.org, hannes@cmpxchg.org, mhocko@kernel.org Cc: roman.gushchin@linux.dev, shakeel.butt@linux.dev, muchun.song@linux.dev, lizefan.x@bytedance.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, Zhongkun He Subject: [RFC PATCH 1/2] mm: vmscan: modify the semantics of scan_control.may_unmap to UNMAP_ANON and UNMAP_FILE Date: Thu, 29 Aug 2024 18:20:38 +0800 Message-Id: <20240829102039.3455842-1-hezhongkun.hzk@bytedance.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 X-Stat-Signature: jqcgaxncberem94qytiyhbeg5i9og7xy X-Rspam-User: X-Rspamd-Queue-Id: 4CA901C0012 X-Rspamd-Server: rspam02 X-HE-Tag: 1724926851-952313 X-HE-Meta: U2FsdGVkX1+mthIViAldHVvd4RvLjrQCfna7IBZTovaS3kxWY6wVmU9BqMqUhxoMvOaiatTvx7PvakfSdRs9LK5d7gP28n9/N591AGfv6O05toIIKs4YF1WLvjZ5mx5Fa/1aq38OWn16Fa8g7MCLyPaulva6goBGP3WMEMe75vnGTBUyKAc17UqH61IXM8lQI94GedtPknaXeoEXieTPHRdrfDsckZlBWO1dbROTM/8KkF5h62nqp/eZ/rz20JC6j2KrnJWARy/0uu2mgNLQ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Hpw6pbGpXDqwzA4zYEcBc+Q= 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: This is a preparation patch to add disable_unmap_file arg to memory.reclaim. So far, the value of scan_control.may_unmap has only two types, true or false, which represents whether pages can be unmapped for reclamation in the reclamation path. It cannot distinguish between file pages and anonymous pages, so we cannot make a more accurate choice when proactively reclaiming memory in user space. In practical experience, mapped file pages are crucial for the operation of the program, usually containing important executable code, data, and shared libraries, etc. Therefore, it is necessary to make a more accurate distinction. Signed-off-by: Zhongkun He --- mm/vmscan.c | 61 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 14 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 283e3f9d652b..50ac714cba2f 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -107,8 +107,11 @@ struct scan_control { /* Writepage batching in laptop mode; RECLAIM_WRITE */ unsigned int may_writepage:1; + #define UNMAP_ANON 1 + #define UNMAP_FILE 2 + #define UNMAP_ANON_AND_FILE UNMAP_ANON + UNMAP_FILE /* Can mapped folios be reclaimed? */ - unsigned int may_unmap:1; + unsigned int may_unmap:2; /* Can folios be swapped as part of reclaim? */ unsigned int may_swap:1; @@ -1083,8 +1086,23 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, if (unlikely(!folio_evictable(folio))) goto activate_locked; - if (!sc->may_unmap && folio_mapped(folio)) - goto keep_locked; + if (folio_mapped(folio)) { + switch (sc->may_unmap) { + /* The most likely case. */ + case UNMAP_ANON_AND_FILE: + break; + case UNMAP_ANON: + if (!folio_test_anon(folio)) + goto keep_locked; + break; + case UNMAP_FILE: + if (folio_test_anon(folio)) + goto keep_locked; + break; + default: + goto keep_locked; + } + } /* folio_update_gen() tried to promote this page? */ if (lru_gen_enabled() && !ignore_references && @@ -1563,7 +1581,7 @@ unsigned int reclaim_clean_pages_from_list(struct zone *zone, { struct scan_control sc = { .gfp_mask = GFP_KERNEL, - .may_unmap = 1, + .may_unmap = UNMAP_ANON_AND_FILE, }; struct reclaim_stat stat; unsigned int nr_reclaimed; @@ -1688,8 +1706,23 @@ static unsigned long isolate_lru_folios(unsigned long nr_to_scan, if (!folio_test_lru(folio)) goto move; - if (!sc->may_unmap && folio_mapped(folio)) - goto move; + + if (folio_mapped(folio)) { + switch (sc->may_unmap) { + case UNMAP_ANON_AND_FILE: + break; + case UNMAP_ANON: + if (!folio_test_anon(folio)) + goto move; + break; + case UNMAP_FILE: + if (folio_test_anon(folio)) + goto move; + break; + default: + goto move; + } + } /* * Be careful not to clear the lru flag until after we're @@ -2135,7 +2168,7 @@ static unsigned int reclaim_folio_list(struct list_head *folio_list, struct scan_control sc = { .gfp_mask = GFP_KERNEL, .may_writepage = 1, - .may_unmap = 1, + .may_unmap = UNMAP_ANON_AND_FILE, .may_swap = 1, .no_demotion = 1, }; @@ -5467,7 +5500,7 @@ static ssize_t lru_gen_seq_write(struct file *file, const char __user *src, int err = -EINVAL; struct scan_control sc = { .may_writepage = true, - .may_unmap = true, + .may_unmap = UNMAP_ANON_AND_FILE, .may_swap = true, .reclaim_idx = MAX_NR_ZONES - 1, .gfp_mask = GFP_KERNEL, @@ -6482,7 +6515,7 @@ unsigned long try_to_free_pages(struct zonelist *zonelist, int order, .nodemask = nodemask, .priority = DEF_PRIORITY, .may_writepage = !laptop_mode, - .may_unmap = 1, + .may_unmap = UNMAP_ANON_AND_FILE, .may_swap = 1, }; @@ -6526,7 +6559,7 @@ unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg, .nr_to_reclaim = SWAP_CLUSTER_MAX, .target_mem_cgroup = memcg, .may_writepage = !laptop_mode, - .may_unmap = 1, + .may_unmap = UNMAP_ANON_AND_FILE, .reclaim_idx = MAX_NR_ZONES - 1, .may_swap = !noswap, }; @@ -6572,7 +6605,7 @@ unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, .target_mem_cgroup = memcg, .priority = DEF_PRIORITY, .may_writepage = !laptop_mode, - .may_unmap = 1, + .may_unmap = UNMAP_ANON_AND_FILE, .may_swap = !!(reclaim_options & MEMCG_RECLAIM_MAY_SWAP), .proactive = !!(reclaim_options & MEMCG_RECLAIM_PROACTIVE), }; @@ -6837,7 +6870,7 @@ static int balance_pgdat(pg_data_t *pgdat, int order, int highest_zoneidx) struct scan_control sc = { .gfp_mask = GFP_KERNEL, .order = order, - .may_unmap = 1, + .may_unmap = UNMAP_ANON_AND_FILE, }; set_task_reclaim_state(current, &sc.reclaim_state); @@ -7304,7 +7337,7 @@ unsigned long shrink_all_memory(unsigned long nr_to_reclaim) .reclaim_idx = MAX_NR_ZONES - 1, .priority = DEF_PRIORITY, .may_writepage = 1, - .may_unmap = 1, + .may_unmap = UNMAP_ANON_AND_FILE, .may_swap = 1, .hibernation_mode = 1, }; @@ -7462,7 +7495,7 @@ static int __node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned in .order = order, .priority = NODE_RECLAIM_PRIORITY, .may_writepage = !!(node_reclaim_mode & RECLAIM_WRITE), - .may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP), + .may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP) ? UNMAP_ANON_AND_FILE : 0, .may_swap = 1, .reclaim_idx = gfp_zone(gfp_mask), };