From mboxrd@z Thu Jan 1 00:00:00 1970 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 28A83CD68FE for ; Tue, 10 Oct 2023 08:33:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 992CA8D00C9; Tue, 10 Oct 2023 04:33:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 943D28D006D; Tue, 10 Oct 2023 04:33:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8315E8D00C9; Tue, 10 Oct 2023 04:33:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 730EC8D006D for ; Tue, 10 Oct 2023 04:33:01 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 4823F120222 for ; Tue, 10 Oct 2023 08:33:01 +0000 (UTC) X-FDA: 81328886562.06.F101990 Received: from outbound-smtp52.blacknight.com (outbound-smtp52.blacknight.com [46.22.136.236]) by imf08.hostedemail.com (Postfix) with ESMTP id 59AB116000A for ; Tue, 10 Oct 2023 08:32:59 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf08.hostedemail.com: domain of mgorman@techsingularity.net designates 46.22.136.236 as permitted sender) smtp.mailfrom=mgorman@techsingularity.net ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696926779; 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=SOVuz6PeodUZ6+6h/MdPpWqORSHDAF7ytS8/hyk6E8s=; b=YFHUa29VBacqagCUGZqLlJwRDDPkU5PP/qmbk0WYGBjrs2QPDnJmiHOIRLUj/tQ4rKdr5N ta2OiSc7/EH1cx01yIR9YKzzvEYkgR5+9mAxg4Ijc30eSs4TCuwHXSQ40D8QHC+a4IGd34 dqDRXZIyKHgLLMuDFurIOPXN3sA1Ezw= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf08.hostedemail.com: domain of mgorman@techsingularity.net designates 46.22.136.236 as permitted sender) smtp.mailfrom=mgorman@techsingularity.net ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696926779; a=rsa-sha256; cv=none; b=a3212OVQEB4VBTJ++rYqDN+xKU4Sy3cQ0fVlEDLk1NE0bhg8Vkrvw9XVlxcc38O0/dR4E/ t/wOtRFqruI3nXTIs2/8M/MHSmYph5GXYxz/QdP7T9VEn5et3FrFq0kDqoVg+/AbyMZqui nM1RevWnJL0wbF6eINDBnwAjeMZaf3g= Received: from mail.blacknight.com (pemlinmail04.blacknight.ie [81.17.254.17]) by outbound-smtp52.blacknight.com (Postfix) with ESMTPS id D259DFABE1 for ; Tue, 10 Oct 2023 09:32:57 +0100 (IST) Received: (qmail 9460 invoked from network); 10 Oct 2023 08:32:57 -0000 Received: from unknown (HELO morpheus.112glenside.lan) (mgorman@techsingularity.net@[84.203.197.19]) by 81.17.254.9 with ESMTPA; 10 Oct 2023 08:32:57 -0000 From: Mel Gorman To: Peter Zijlstra Cc: Raghavendra K T , K Prateek Nayak , Bharata B Rao , Ingo Molnar , LKML , Linux-MM , Mel Gorman Subject: [PATCH 6/6] sched/numa: Complete scanning of inactive VMAs when there is no alternative Date: Tue, 10 Oct 2023 09:31:43 +0100 Message-Id: <20231010083143.19593-7-mgorman@techsingularity.net> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20231010083143.19593-1-mgorman@techsingularity.net> References: <20231010083143.19593-1-mgorman@techsingularity.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 59AB116000A X-Stat-Signature: 168xrgde37gu5h3m3cpu9tsw35jbejqi X-Rspam-User: X-HE-Tag: 1696926779-102955 X-HE-Meta: U2FsdGVkX18H7cUvq+XLcrk79B3cdpzcoLz0HZMXS9I8B6k/V/i/9KQxZEriJ+KX3Ttg/dbcZmArNN5C4Wuo6UGngXtp+qFeUEtu3HpubTPxlMpL7objvPau7MwD9i8C16EUWrM7czndBvF3RA26wBGcC9nxZZYamGpKY+5m5u8NxtjsC+0hBmJQpY8Tt8mpNnkCzLoaBwyf3lSaJ49myZnSFH1cu252u0I51GAykSgy28aXLiNzY6/Zfd7o4TOFmL9vDb0siP96u1F5ualMCY6sScmUzm1sPtYzTtViWhPesEwmm5srAzyACcWgOc8vSp3ZsucRA9zrb1lxbKoxNWe1IdvjW/MUMQJNqfU9aOmR/F6D2nN7h1uTtLulypmOm9KUStiZNK5zWLK0f09ifeGhek/7L2Yli9tlaOh4eWypmsqqe6dTj24Jn0luiMpkgwejYdw315r1qGuaLlUEmiFSv6cpIHocUom1Q05fyCZR9KTDBbhmwEAgXRRX0vFmNNgNtn0uT8sq98xTHDE5svBwmsRB6G7uIv6s/IEzOzoUPg8votNk/UDJ9z517tE988ztF1tyQG84v0vhZyv8KqPplAM48kuWvMoBVA3QVSJMvw9IL1f7nUPX5yYoYttdk+Ib0YYR3fmjdX5mOwbPTCw9qTqS0GeUyTOUcRh+YJZc/hP782O6JKu2+pIxYTVeOB6cV8GdOI0E2o/UmukIT1DZGkPFSw4TPLL/H7XhoUZbKsyF11ltMSChYqpeqpoPSRYVNwOzOA//sNTX6v6oMRRCdg0uApDwEy//Jz79re3k3rNS+j7wSblJdr2ZORCHgRwIVuPYsLcXZ5UAsfo/2mcd+AS+3r/XUg0QkUSjkLAXfABMpjmhQgVybHKOvyvrtDvqiXx2YyN+vuwKYRj0Itc/AHwrlSHCxZg4NHB5sXWDtlRw5TOx7h5kRSOmoTCMQm9GuesfAwt/Z8qERu6 LMmzzgW3 9FTVj55M3bm/cpoxih1bjuGg1WX1hAG8L2BjviOGsOZ/ISz7tH5CODOUd7cmiTXZkXLBY6UoEyyj9wLb1//A69VcHSVTe6/dGxo2I1lFjIJDwEVnBiPTMIFAFNE21aLQUJJzAb3fd6I8SvmwFjjmjjqxurKBSfzb2jNl2yLtchi2CNIRDIQnCF7t/rzH2LT60aY3cn2cVrTrUnI5kTFwaFgtZkQ== 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: VMAs are skipped if there is no recent fault activity but this represents a chicken-and-egg problem as there may be no fault activity if the PTEs are never updated to trap NUMA hints. There is an indirect reliance on scanning to be forced early in the lifetime of a task but this may fail to detect changes in phase behaviour. Force inactive VMAs to be scanned when all other eligible VMAs have been updated within the same scan sequence. Test results in general look good with some changes in performance, both negative and positive, depending on whether the additional scanning and faulting was beneficial or not to the workload. The autonuma benchmark workload NUMA01_THREADLOCAL was picked for closer examination. The workload creates two processes with numerous threads and thread-local storage that is zero-filled in a loop. It exercises the corner case where unrelated threads may skip VMAs that are thread-local to another thread and still has some VMAs that inactive while the workload executes. The VMA skipping activity frequency with and without the patch is as follows; 6.6.0-rc2-sched-numabtrace-v1 649 reason=scan_delay 9094 reason=unsuitable 48915 reason=shared_ro 143919 reason=inaccessible 193050 reason=pid_inactive 6.6.0-rc2-sched-numabselective-v1 146 reason=seq_completed 622 reason=ignore_pid_inactive 624 reason=scan_delay 6570 reason=unsuitable 16101 reason=shared_ro 27608 reason=inaccessible 41939 reason=pid_inactive Note that with the patch applied, the PID activity is ignored (ignore_pid_inactive) to ensure a VMA with some activity is completely scanned. In addition, a small number of VMAs are scanned when no other eligible VMA is available during a single scan window (seq_completed). The number of times a VMA is skipped due to no PID activity from the scanning task (pid_inactive) drops dramatically. It is expected that this will increase the number of PTEs updated for NUMA hinting faults as well as hinting faults but these represent PTEs that would otherwise have been missed. The tradeoff is scan+fault overhead versus improving locality due to migration. On a 2-socket Cascade Lake test machine, the time to complete the workload is as follows; 6.6.0-rc2 6.6.0-rc2 sched-numabtrace-v1 sched-numabselective-v1 Min elsp-NUMA01_THREADLOCAL 174.22 ( 0.00%) 117.64 ( 32.48%) Amean elsp-NUMA01_THREADLOCAL 175.68 ( 0.00%) 123.34 * 29.79%* Stddev elsp-NUMA01_THREADLOCAL 1.20 ( 0.00%) 4.06 (-238.20%) CoeffVar elsp-NUMA01_THREADLOCAL 0.68 ( 0.00%) 3.29 (-381.70%) Max elsp-NUMA01_THREADLOCAL 177.18 ( 0.00%) 128.03 ( 27.74%) The time to complete the workload is reduced by almost 30% 6.6.0-rc2 6.6.0-rc2 sched-numabtrace-v1 sched-numabselective-v1 / Duration User 91201.80 63506.64 Duration System 2015.53 1819.78 Duration Elapsed 1234.77 868.37 In this specific case, system CPU time was not increased but it's not universally true. >From vmstat, the NUMA scanning and fault activity is as follows; 6.6.0-rc2 6.6.0-rc2 sched-numabtrace-v1 sched-numabselective-v1 Ops NUMA base-page range updates 64272.00 26374386.00 Ops NUMA PTE updates 36624.00 55538.00 Ops NUMA PMD updates 54.00 51404.00 Ops NUMA hint faults 15504.00 75786.00 Ops NUMA hint local faults % 14860.00 56763.00 Ops NUMA hint local percent 95.85 74.90 Ops NUMA pages migrated 1629.00 6469222.00 Both the number of PTE updates and hint faults is dramatically increased. While this is superficially unfortunate, it represents ranges that were simply skipped without the patch. As a result of the scanning and hinting faults, many more pages were also migrated but as the time to completion is reduced, the overhead is offset by the gain. Signed-off-by: Mel Gorman --- include/linux/mm_types.h | 6 +++ include/linux/sched/numa_balancing.h | 1 + include/trace/events/sched.h | 3 +- kernel/sched/fair.c | 55 ++++++++++++++++++++++++++-- 4 files changed, 61 insertions(+), 4 deletions(-) diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 8cb1dec3e358..a123c1a58617 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -578,6 +578,12 @@ struct vma_numab_state { * VMA_PID_RESET_PERIOD * jiffies. */ + int prev_scan_seq; /* MM scan sequence ID when + * the VMA was last completely + * scanned. A VMA is not + * eligible for scanning if + * prev_scan_seq == numa_scan_seq + */ }; /* diff --git a/include/linux/sched/numa_balancing.h b/include/linux/sched/numa_balancing.h index 7dcc0bdfddbb..b69afb8630db 100644 --- a/include/linux/sched/numa_balancing.h +++ b/include/linux/sched/numa_balancing.h @@ -22,6 +22,7 @@ enum numa_vmaskip_reason { NUMAB_SKIP_SCAN_DELAY, NUMAB_SKIP_PID_INACTIVE, NUMAB_SKIP_IGNORE_PID, + NUMAB_SKIP_SEQ_COMPLETED, }; #ifdef CONFIG_NUMA_BALANCING diff --git a/include/trace/events/sched.h b/include/trace/events/sched.h index 27b51c81b106..010ba1b7cb0e 100644 --- a/include/trace/events/sched.h +++ b/include/trace/events/sched.h @@ -671,7 +671,8 @@ DEFINE_EVENT(sched_numa_pair_template, sched_swap_numa, EM( NUMAB_SKIP_INACCESSIBLE, "inaccessible" ) \ EM( NUMAB_SKIP_SCAN_DELAY, "scan_delay" ) \ EM( NUMAB_SKIP_PID_INACTIVE, "pid_inactive" ) \ - EMe(NUMAB_SKIP_IGNORE_PID, "ignore_pid_inactive" ) + EM( NUMAB_SKIP_IGNORE_PID, "ignore_pid_inactive" ) \ + EMe(NUMAB_SKIP_SEQ_COMPLETED, "seq_completed" ) /* Redefine for export. */ #undef EM diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 150f01948ec6..72ef60f394ba 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -3175,6 +3175,8 @@ static void task_numa_work(struct callback_head *work) unsigned long nr_pte_updates = 0; long pages, virtpages; struct vma_iterator vmi; + bool vma_pids_skipped; + bool vma_pids_forced = false; SCHED_WARN_ON(p != container_of(work, struct task_struct, numa_work)); @@ -3217,7 +3219,6 @@ static void task_numa_work(struct callback_head *work) */ p->node_stamp += 2 * TICK_NSEC; - start = mm->numa_scan_offset; pages = sysctl_numa_balancing_scan_size; pages <<= 20 - PAGE_SHIFT; /* MB in pages */ virtpages = pages * 8; /* Scan up to this much virtual space */ @@ -3227,6 +3228,16 @@ static void task_numa_work(struct callback_head *work) if (!mmap_read_trylock(mm)) return; + + /* + * VMAs are skipped if the current PID has not trapped a fault within + * the VMA recently. Allow scanning to be forced if there is no + * suitable VMA remaining. + */ + vma_pids_skipped = false; + +retry_pids: + start = mm->numa_scan_offset; vma_iter_init(&vmi, mm, start); vma = vma_next(&vmi); if (!vma) { @@ -3277,6 +3288,13 @@ static void task_numa_work(struct callback_head *work) /* Reset happens after 4 times scan delay of scan start */ vma->numab_state->pids_active_reset = vma->numab_state->next_scan + msecs_to_jiffies(VMA_PID_RESET_PERIOD); + + /* + * Ensure prev_scan_seq does not match numa_scan_seq + * to prevent VMAs being skipped prematurely on the + * first scan. + */ + vma->numab_state->prev_scan_seq = mm->numa_scan_seq - 1; } /* @@ -3298,8 +3316,19 @@ static void task_numa_work(struct callback_head *work) vma->numab_state->pids_active[1] = 0; } - /* Do not scan the VMA if task has not accessed */ - if (!vma_is_accessed(mm, vma)) { + /* Do not rescan VMAs twice within the same sequence. */ + if (vma->numab_state->prev_scan_seq == mm->numa_scan_seq) { + mm->numa_scan_offset = vma->vm_end; + trace_sched_skip_vma_numa(mm, vma, NUMAB_SKIP_SEQ_COMPLETED); + continue; + } + + /* + * Do not scan the VMA if task has not accessed unless no other + * VMA candidate exists. + */ + if (!vma_pids_forced && !vma_is_accessed(mm, vma)) { + vma_pids_skipped = true; trace_sched_skip_vma_numa(mm, vma, NUMAB_SKIP_PID_INACTIVE); continue; } @@ -3328,8 +3357,28 @@ static void task_numa_work(struct callback_head *work) cond_resched(); } while (end != vma->vm_end); + + /* VMA scan is complete, do not scan until next sequence. */ + vma->numab_state->prev_scan_seq = mm->numa_scan_seq; + + /* + * Only force scan within one VMA at a time to limit the + * cost of scanning a potentially uninteresting VMA. + */ + if (vma_pids_forced) + break; } for_each_vma(vmi, vma); + /* + * If no VMAs are remaining and VMAs were skipped due to the PID + * not accessing the VMA previously then force a scan to ensure + * forward progress. + */ + if (!vma && !vma_pids_forced && vma_pids_skipped) { + vma_pids_forced = true; + goto retry_pids; + } + out: /* * It is possible to reach the end of the VMA list but the last few -- 2.35.3