From patchwork Sun Dec 4 16:14:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhongkun He X-Patchwork-Id: 13063885 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 D123FC4321E for ; Sun, 4 Dec 2022 16:15:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6CED98E0006; Sun, 4 Dec 2022 11:15:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6A6D08E0001; Sun, 4 Dec 2022 11:15:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 547738E0006; Sun, 4 Dec 2022 11:15:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 4618B8E0001 for ; Sun, 4 Dec 2022 11:15:18 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 277231C1F65 for ; Sun, 4 Dec 2022 16:15:18 +0000 (UTC) X-FDA: 80205123516.10.9B0AEE0 Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) by imf20.hostedemail.com (Postfix) with ESMTP id 8D4701C000E for ; Sun, 4 Dec 2022 16:15:17 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=CrJxh2mL; spf=pass (imf20.hostedemail.com: domain of hezhongkun.hzk@bytedance.com designates 209.85.215.172 as permitted sender) smtp.mailfrom=hezhongkun.hzk@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1670170517; a=rsa-sha256; cv=none; b=l4RUHNprnX4jPjEBe+/MJ6hKJarZsLsbir1FW3JwGTi/VPaR4cTFW/iEGQcZpEJ2NF73NY SmnZL1WhuGArWFAYhigOe+sWhTO4sXn5VAjDZS1y5rS/oArJ9O/9eaVLlSQfypTZ21EtkS N+TWN8fu3ROefuJ1NNJnQB69u9LLopc= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=CrJxh2mL; spf=pass (imf20.hostedemail.com: domain of hezhongkun.hzk@bytedance.com designates 209.85.215.172 as permitted sender) smtp.mailfrom=hezhongkun.hzk@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1670170517; 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:dkim-signature; bh=svRUdELzl130u78ZHoVrQ8kLS3/B/K+fs5QOXxsPpJs=; b=Toz1Rc94ftvI7wMCDAEQsYZg7MN74TsYJr7s7zZ9Wqpz8qFGMhfo216J7BV7zv1dZ9KgZb V9maMeCKEpFNGfvt5h8ex2YZ+PQ+s7tswIYOqwuTY8e6qwNwx84VBqmDd2eNBrATqpxm66 eEjKMBlQ70DezWsP932pSMMgA3/MX60= Received: by mail-pg1-f172.google.com with SMTP id s196so8472210pgs.3 for ; Sun, 04 Dec 2022 08:15:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=svRUdELzl130u78ZHoVrQ8kLS3/B/K+fs5QOXxsPpJs=; b=CrJxh2mLnr2Kc1c43TsZ5hHQRYXrbNQuNpWgBlRAvtB/ZBHABi5itLHDIbZd9ExahR 3YhQ/UvkZtGBvY2umOJvuIjTbtfwpJVuACQgmYsiIFvBCm8OMQ9nQcISiO/c1kTwDUFb 5YK2bw4i6KPFIMryLYNRkzut5BKoOOW7855jORF73gHk67NU1Lb+PjeQX16PBo8F6vrJ EqCEfGdMdTesLZ0vAlCC9I/lmpri57kmdp1Jv/+R1qNOJaOEdq+bACUBpqwWHURkDW/+ pV0OhPAxrXggkfQmCHVVuyH1zHQ+E2N0SbOEgaTovcU1BNRjQgD27KP7d1RX6X5q+7Y0 Iwjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=svRUdELzl130u78ZHoVrQ8kLS3/B/K+fs5QOXxsPpJs=; b=1SatJnhgWYOkNAvEiNxkiXqH9FBHCZDv+pJ9+dAqm/ThsC1h8yerc2p5YcStTc4fik hZux2jiNcMQN5cYh6NEkvh9wQ5btxDGnLowGzxLF/ya3TdLp/TUk1lm1Ij+QL6P1ZEFm vybV79nIICYryeQ+4xthAyzknpOwtfk9YsxaejyzUIvFhphBJC1sTJU+Lp7yBDivtc3I VAoTmRvusihSgGGdcvKbND7abz5XwMPQHuw1Njw9PCZm9LRSH4087brX2AxfvQze0/3N JbGiCU7HK3SpWrxACsVGlvteJ334EW1h4dHsqetUcTco4B6WoRzOzHBbkOwOmtgDzT9M Ey8w== X-Gm-Message-State: ANoB5pnMm1+UuxnjhUSqv5RCCiaoRPI2yoVdvWRdZi4FN0TAATRYqgLI j4fK8KkvmCdJb4YSSlNzm7m5xkBumL/EVQtH0to= X-Google-Smtp-Source: AA0mqf697SbKrqDrxqm/6K/eBjLuuHJ5KWVPXCJa7SzwMYf019swaV3VwP+EN36vxJGvS2skm/TMSA== X-Received: by 2002:a63:1f63:0:b0:460:ec46:3645 with SMTP id q35-20020a631f63000000b00460ec463645mr73298033pgm.92.1670170516616; Sun, 04 Dec 2022 08:15:16 -0800 (PST) Received: from Tower.bytedance.net ([139.177.225.248]) by smtp.gmail.com with ESMTPSA id n16-20020a170903111000b0016cf3f124e1sm9000323plh.234.2022.12.04.08.15.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Dec 2022 08:15:16 -0800 (PST) From: Zhongkun He To: mhocko@suse.com, akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, wuyun.abel@bytedance.com, Zhongkun He Subject: [PATCH 3/3] mm: add __rcu symbol for task->mempolicy Date: Mon, 5 Dec 2022 00:14:32 +0800 Message-Id: <20221204161432.2149375-4-hezhongkun.hzk@bytedance.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221204161432.2149375-1-hezhongkun.hzk@bytedance.com> References: <20221204161432.2149375-1-hezhongkun.hzk@bytedance.com> MIME-Version: 1.0 X-Rspam-User: X-Spamd-Result: default: False [1.69 / 9.00]; BAYES_HAM(-5.91)[99.81%]; SORBS_IRL_BL(3.00)[209.85.215.172:from]; R_MISSING_CHARSET(2.50)[]; MID_CONTAINS_FROM(1.00)[]; SUBJECT_HAS_UNDERSCORES(1.00)[]; RCVD_NO_TLS_LAST(0.10)[]; MIME_GOOD(-0.10)[text/plain]; BAD_REP_POLICIES(0.10)[]; TO_DN_SOME(0.00)[]; ARC_NA(0.00)[]; RCPT_COUNT_SEVEN(0.00)[7]; R_DKIM_ALLOW(0.00)[bytedance-com.20210112.gappssmtp.com:s=20210112]; MIME_TRACE(0.00)[0:+]; FROM_EQ_ENVFROM(0.00)[]; R_SPF_ALLOW(0.00)[+ip4:209.85.128.0/17]; DMARC_POLICY_ALLOW(0.00)[bytedance.com,none]; TO_MATCH_ENVRCPT_SOME(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[linux-mm@kvack.org]; DKIM_TRACE(0.00)[bytedance-com.20210112.gappssmtp.com:+]; ARC_SIGNED(0.00)[hostedemail.com:s=arc-20220608:i=1]; RCVD_COUNT_THREE(0.00)[3]; FROM_HAS_DN(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[] X-Rspamd-Queue-Id: 8D4701C000E X-Rspamd-Server: rspam01 X-Stat-Signature: kxe6bqq5n81umtqzxx8ozuj6nfn7pb61 X-HE-Tag: 1670170517-588135 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: The task->mempolicy is protected by task_lock in slow path, but there is no locking and reference in hot path for performance. It will be difficult if other processes want to adjust it. It is for these reasons to add __rcu symbol for task mempolicy. There is no need to add RCU protection to vma mempolicy, which is protected by mmap_lock. Suggested-by: Michal Hocko Signed-off-by: Zhongkun He --- include/linux/sched.h | 2 +- mm/mempolicy.c | 9 ++++----- mm/slab.c | 5 +++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/linux/sched.h b/include/linux/sched.h index ffb6eb55cd13..c8a297ca61ab 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1252,7 +1252,7 @@ struct task_struct { #endif #ifdef CONFIG_NUMA /* Protected by alloc_lock: */ - struct mempolicy *mempolicy; + struct mempolicy __rcu *mempolicy; short il_prev; short pref_node_fork; #endif diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 0feffb7ff01e..837083fff9c8 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -894,8 +894,7 @@ static long do_set_mempolicy(unsigned short mode, unsigned short flags, goto out; } - old = current->mempolicy; - current->mempolicy = new; + old = rcu_replace_pointer(current->mempolicy, new, true); if (new && new->mode == MPOL_INTERLEAVE) current->il_prev = MAX_NUMNODES-1; task_unlock(current); @@ -999,7 +998,7 @@ static long do_get_mempolicy(int *policy, nodemask_t *nmask, if (err < 0) goto out; *policy = err; - } else if (pol == current->mempolicy && + } else if (pol == rcu_access_pointer(current->mempolicy) && pol->mode == MPOL_INTERLEAVE) { *policy = next_node_in(current->il_prev, pol->nodes); } else { @@ -2065,7 +2064,7 @@ bool init_nodemask_of_mempolicy(nodemask_t *mask) { struct mempolicy *mempolicy; - if (!(mask && current->mempolicy)) + if (!(mask && rcu_access_pointer(current->mempolicy))) return false; task_lock(current); @@ -2426,7 +2425,7 @@ struct mempolicy *__mpol_dup(struct mempolicy *old) return ERR_PTR(-ENOMEM); /* task's mempolicy is protected by alloc_lock */ - if (old == current->mempolicy) { + if (old == rcu_access_pointer(current->mempolicy)) { task_lock(current); *new = *old; task_unlock(current); diff --git a/mm/slab.c b/mm/slab.c index 59c8e28f7b6a..f205869d6c36 100644 --- a/mm/slab.c +++ b/mm/slab.c @@ -3052,7 +3052,7 @@ static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags) nid_alloc = nid_here = numa_mem_id(); if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD)) nid_alloc = cpuset_slab_spread_node(); - else if (current->mempolicy) + else if (rcu_access_pointer(current->mempolicy)) nid_alloc = mempolicy_slab_node(); if (nid_alloc != nid_here) return ____cache_alloc_node(cachep, flags, nid_alloc); @@ -3188,7 +3188,8 @@ __do_cache_alloc(struct kmem_cache *cachep, gfp_t flags, int nodeid) int slab_node = numa_mem_id(); if (nodeid == NUMA_NO_NODE) { - if (current->mempolicy || cpuset_do_slab_mem_spread()) { + if (rcu_access_pointer(current->mempolicy) || + cpuset_do_slab_mem_spread()) { objp = alternate_node_alloc(cachep, flags); if (objp) goto out;