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 B6877C47258 for ; Mon, 15 Jan 2024 08:57:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3D5966B007B; Mon, 15 Jan 2024 03:57:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 384546B0080; Mon, 15 Jan 2024 03:57:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 24BD56B0081; Mon, 15 Jan 2024 03:57:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 12EA86B007B for ; Mon, 15 Jan 2024 03:57:54 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id D21E91204D0 for ; Mon, 15 Jan 2024 08:57:53 +0000 (UTC) X-FDA: 81680942826.01.C588FDE Received: from out-184.mta0.migadu.com (out-184.mta0.migadu.com [91.218.175.184]) by imf18.hostedemail.com (Postfix) with ESMTP id CA75C1C0004 for ; Mon, 15 Jan 2024 08:57:50 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Ad4tuJWU; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf18.hostedemail.com: domain of gang.li@linux.dev designates 91.218.175.184 as permitted sender) smtp.mailfrom=gang.li@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1705309071; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=bXiuxGjT6S8rglErry6pNbf96Enl3N9c2dT+qili78s=; b=XEprCsjUGZIWo1bh8SISgq2i6L0c+5QvnsKtGfxTCCXVepiT99+N27c2VMpkFmhb4FiNc7 CFf4V1rNiXIMm4aiLZzE71Y1B8BqmTf54OLVSK6pSmtYCgctURerprOUeNBDzQ/NuE2Epm PkDAc9e1vZKQiNT5D0czQFYFHBCAQS8= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Ad4tuJWU; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf18.hostedemail.com: domain of gang.li@linux.dev designates 91.218.175.184 as permitted sender) smtp.mailfrom=gang.li@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1705309071; a=rsa-sha256; cv=none; b=aJNissYbop3QctnDnepaR4Cpx61+BN6/Azg6am4topsJAif0F65OfQhQISaqpRusLjM51d W0lT6Rr41ew4xhyvyPAFcFImKGzedTorYdMAtJanh8g17e3VAUNN4dyXSC8YjczOEwu9mR FH6T8qciKW9jS08rSs5pcX64+kMbEeg= Message-ID: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1705309068; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=bXiuxGjT6S8rglErry6pNbf96Enl3N9c2dT+qili78s=; b=Ad4tuJWUuxQE3Th3u+z9dP6aAcsQ36Pp8YaJVA7kJaKoRl1ebzoRCL+Wo9fK6nZNOLMf0K oNds8mr/QjNtjzWoQS1HxD/ez1Yn78+V1RiiqSlk+bloenFBPcJN12I8pwBK8GUhxpcb+H ypYAnJayr4U6+K7RUlm2o2cge/+woBw= Date: Mon, 15 Jan 2024 16:57:40 +0800 MIME-Version: 1.0 Subject: Re: [PATCH v3 3/7] padata: dispatch works on different nodes To: Tim Chen Cc: linux-mm@kvack.org, Andrew Morton , Mike Kravetz , David Rientjes , linux-kernel@vger.kernel.org, ligang.bdlg@bytedance.com, David Hildenbrand , Muchun Song , Gang Li References: <20240102131249.76622-1-gang.li@linux.dev> <20240102131249.76622-4-gang.li@linux.dev> <1d9074955618ea0b4b155701f7c1b8b18a43fa8d.camel@linux.intel.com> <1bd6ee64a600daad58866ce684b591d39879c470.camel@linux.intel.com> Content-Language: en-US X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Gang Li In-Reply-To: <1bd6ee64a600daad58866ce684b591d39879c470.camel@linux.intel.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: CA75C1C0004 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: pyny1myrkz5p8kjckr4szoqm8t5opr1g X-HE-Tag: 1705309070-299255 X-HE-Meta: U2FsdGVkX19WXc/QM6wFtzcYxt1fZc6aRR+pgoJU4h54GAmP8KgHPG8V0eaUMxfJczSIXAxHTSxZdP/xDWlONyfOYV3UeH/LAJ3OgmjfBv0nuf+uNURupCspeEj+eSD+fU5L4RB82jSopNYDz9FuRBuZSbnZF+cNusf8LdCs6WfZ1MDFnkbga+kh6QP6H7Py9RAZpChBb4FnWqu3Vd11G4yFqMS25V7ywTZtzVw8IsiA2/avBwcm4yjjXod/6XkM1HFWqRK6hUO4r3ab27rOJlD6kL+jYgOHZcPP/fLKjIp353/SEGiWWO8n6gQn3g2Uk1tD4lYVv0zbj/oroxq5YA35uFn+twUIj3wiXAFhaycVbSjJB5tcaZ1dDJXDyGlGDrSpiOYLPJDFj8D5/He/LlGU4Cjw2rvwF8UA7wyQS382ILR2fM1QZYm17dmpCOEfV+cl7lYtWOS8vGsulQlPLABx3/0VDYtXvVSJHR6KMptXXfY8zTg2OwzgzmV3qRzdpdeMwVwLtpI1iB8+DnFfISPvyE+AhqZ4Fye3HVVKWAY8AOAJfJ8zG3oNchsOoddBjpd35zHl/5+jMwolQxGm5RkCKrTI0pg4fOgUWbfDnmZdAYx8i8ivKZOlJ6zKLTY+T4gnpjdi1wXvQsG8E3zeNieJR4gh5rbbyge8o4ZAiDrGhmK1J6gLOxGa39J3LDjl9IRzdD1nHSmERh/G54rIrZqv1Rqy5l/r5DbqTEV3RgUeh2SQTTB9X1trRHQIk42PV5+Qkt+v3yXWcpld2nOC+ShZP4WbpTiS98sD3ql02tSjpkeq3DAw0aUvqN+EVBqNL/j/YF1gMjTxZZpwmr+kfdD/8N161W5ak4FfZkQpCgvRSkz7yOwss6uHQcmTDPcXqU0T+Usj917J5PnglpiOAobKiYy1oX7f8wS6UHQ6sUt/HBzQSqWFydWJCbCVNaUzySGRrwWuhGPsUpWHCRh jdSJvy0l L9h1/Q6iSU24MwdERzA84aUAZ34jchAbRIPyG+jE8FtCuoWL4Kup6tFu6DG4x2TxbH3mrgaIzeb8m77M= 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: On 2024/1/13 02:27, Tim Chen wrote: > On Fri, 2024-01-12 at 15:09 +0800, Gang Li wrote: >> On 2024/1/12 01:50, Tim Chen wrote: >>> On Tue, 2024-01-02 at 21:12 +0800, Gang Li wrote: >>>> When a group of tasks that access different nodes are scheduled on the >>>> same node, they may encounter bandwidth bottlenecks and access latency. >>>> >>>> Thus, numa_aware flag is introduced here, allowing tasks to be >>>> distributed across different nodes to fully utilize the advantage of >>>> multi-node systems. >>>> >>>> Signed-off-by: Gang Li >>>> --- >>>> include/linux/padata.h | 3 +++ >>>> kernel/padata.c | 8 ++++++-- >>>> mm/mm_init.c | 1 + >>>> 3 files changed, 10 insertions(+), 2 deletions(-) >>>> >>>> diff --git a/include/linux/padata.h b/include/linux/padata.h >>>> index 495b16b6b4d72..f79ccd50e7f40 100644 >>>> --- a/include/linux/padata.h >>>> +++ b/include/linux/padata.h >>>> @@ -137,6 +137,8 @@ struct padata_shell { >>>> * appropriate for one worker thread to do at once. >>>> * @max_threads: Max threads to use for the job, actual number may be less >>>> * depending on task size and minimum chunk size. >>>> + * @numa_aware: Dispatch jobs to different nodes. If a node only has memory but >>>> + * no CPU, dispatch its jobs to a random CPU. >>>> */ >>>> struct padata_mt_job { >>>> void (*thread_fn)(unsigned long start, unsigned long end, void *arg); >>>> @@ -146,6 +148,7 @@ struct padata_mt_job { >>>> unsigned long align; >>>> unsigned long min_chunk; >>>> int max_threads; >>>> + bool numa_aware; >>>> }; >>>> >>>> /** >>>> diff --git a/kernel/padata.c b/kernel/padata.c >>>> index 179fb1518070c..1c2b3a337479e 100644 >>>> --- a/kernel/padata.c >>>> +++ b/kernel/padata.c >>>> @@ -485,7 +485,7 @@ void __init padata_do_multithreaded(struct padata_mt_job *job) >>>> struct padata_work my_work, *pw; >>>> struct padata_mt_job_state ps; >>>> LIST_HEAD(works); >>>> - int nworks; >>>> + int nworks, nid = 0; >>> >>> If we always start from 0, we may be biased towards the low numbered node, >>> and not use high numbered nodes at all. Suggest you do >>> static nid = 0; >>> >> >> When we use `static`, if there are multiple parallel calls to >> `padata_do_multithreaded`, it may result in an uneven distribution of >> tasks for each padata_do_multithreaded. >> >> We can make the following modifications to address this issue. >> >> ``` >> diff --git a/kernel/padata.c b/kernel/padata.c >> index 1c2b3a337479e..925e48df6dd8d 100644 >> --- a/kernel/padata.c >> +++ b/kernel/padata.c >> @@ -485,7 +485,8 @@ void __init padata_do_multithreaded(struct >> padata_mt_job *job) >> struct padata_work my_work, *pw; >> struct padata_mt_job_state ps; >> LIST_HEAD(works); >> - int nworks, nid = 0; >> + int nworks, nid; >> + static volatile int global_nid = 0; >> >> if (job->size == 0) >> return; >> @@ -516,12 +517,15 @@ void __init padata_do_multithreaded(struct >> padata_mt_job *job) >> ps.chunk_size = max(ps.chunk_size, job->min_chunk); >> ps.chunk_size = roundup(ps.chunk_size, job->align); >> >> + nid = global_nid; >> list_for_each_entry(pw, &works, pw_list) >> - if (job->numa_aware) >> - queue_work_node((++nid % num_node_state(N_MEMORY)), >> - system_unbound_wq, &pw->pw_work); >> - else >> + if (job->numa_aware) { >> + queue_work_node(nid, system_unbound_wq, >> &pw->pw_work); >> + nid = next_node(nid, node_states[N_CPU]); >> + } else >> queue_work(system_unbound_wq, &pw->pw_work); >> + if (job->numa_aware) >> + global_nid = nid; > > Thinking more about it, there could still be multiple threads working > at the same time with stale global_nid. We should probably do a compare > exchange of global_nid with new nid only if the global nid was unchanged. > Otherwise we should go to the next node with the changed global nid before > we queue the job. > > Tim > How about: ``` nid = global_nid; list_for_each_entry(pw, &works, pw_list) if (job->numa_aware) { int old_node = nid; queue_work_node(nid, system_unbound_wq, &pw->pw_work); nid = next_node(nid, node_states[N_CPU]); cmpxchg(&global_nid, old_node, nid); } else queue_work(system_unbound_wq, &pw->pw_work); ```