From patchwork Thu Nov 21 12:24:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "zhen.ni" X-Patchwork-Id: 13881960 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 1204BD6ED15 for ; Thu, 21 Nov 2024 12:24:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 717CD6B00A7; Thu, 21 Nov 2024 07:24:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6C8356B00A8; Thu, 21 Nov 2024 07:24:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5B73D6B00A9; Thu, 21 Nov 2024 07:24:30 -0500 (EST) 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 3D6AE6B00A7 for ; Thu, 21 Nov 2024 07:24:30 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id AFF8214121B for ; Thu, 21 Nov 2024 12:24:29 +0000 (UTC) X-FDA: 82810019754.14.2137F03 Received: from mail-m6023.netease.com (mail-m6023.netease.com [210.79.60.23]) by imf21.hostedemail.com (Postfix) with ESMTP id 020431C0010 for ; Thu, 21 Nov 2024 12:22:46 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=none; spf=none (imf21.hostedemail.com: domain of zhen.ni@easystack.cn has no SPF policy when checking 210.79.60.23) smtp.mailfrom=zhen.ni@easystack.cn; dmarc=fail reason="No valid SPF, No valid DKIM" header.from=easystack.cn (policy=none) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732191621; 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; bh=i5llYFaq5e98k2QMELJvXpzRE449y1V0DkLAZ7IOXa4=; b=E3FcPIZXSa+jBgZsKYqKna1gXGrxDpewrlI+o2X/V9KpKr/yKXpBUoxcFuadu1DWQIa7dX p8PqDmuhK1AjitgikzYgH8x31O91d7Ug4BjRJIlBROURTmbSKU9QcsjhfpzBOl12PwOXAS M8hsS4LaP9B3mJJPiuqPRedMyOmnDcs= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=none; spf=none (imf21.hostedemail.com: domain of zhen.ni@easystack.cn has no SPF policy when checking 210.79.60.23) smtp.mailfrom=zhen.ni@easystack.cn; dmarc=fail reason="No valid SPF, No valid DKIM" header.from=easystack.cn (policy=none) ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732191621; a=rsa-sha256; cv=none; b=k//izA3Gz1Y2WEbaEA6xjHjxVwX1xduZ+c8JyhE0a8UKsFxXEw/q4Tb7Q2MOIDomvswWA0 iwY6vnn/RxKXhPZojrwapAj68ceXh0zMeGpbwUUSqI+V2vs8PhHfqf5fHFxYbl8ZoYOC+c oAf3DpG4XokkiWEmjB0KqqTNhkkgJfo= Received: from localhost.localdomain (unknown [180.111.43.56]) by smtp.qiye.163.com (Hmail) with ESMTP id 1acc091c; Thu, 21 Nov 2024 20:24:20 +0800 (GMT+08:00) From: Zhen Ni To: akpm@linux-foundation.org Cc: linux-mm@kvack.org, Zhen Ni Subject: [PATCH] mm/mempolicy: Fix redundant check and refine lock protection scope in init_nodemask_of_mempolicy Date: Thu, 21 Nov 2024 20:24:09 +0800 Message-Id: <20241121122409.277927-1-zhen.ni@easystack.cn> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 X-HM-Spam-Status: e1kfGhgUHx5ZQUpXWQgPGg8OCBgUHx5ZQUlOS1dZFg8aDwILHllBWSg2Ly tZV1koWUFJQjdXWS1ZQUlXWQ8JGhUIEh9ZQVkZT0NDVkhPGElNQkgdT0pKHlYVFAkWGhdVGRETFh oSFyQUDg9ZV1kYEgtZQVlKQ0tVSkpKVU9IVU5NWVdZFhoPEhUdFFlBWU9LSFVKS0hKTkxKVUpLS1 VKQktLWQY+ X-HM-Tid: 0a934eae38c50229kunm1acc091c X-HM-MType: 1 X-HM-Sender-Digest: e1kMHhlZQR0aFwgeV1kSHx4VD1lBWUc6OSI6Lio*FTcoHU0JNlYUVhU# DjIwCSlVSlVKTEhJSkJKQ01KTExLVTMWGhIXVQETHhVVFRI7HhoIAggPGhgQVRgVRVlXWRILWUFZ SkNLVUpKSlVPSFVOTVlXWQgBWUFKSk1PNwY+ X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 020431C0010 X-Stat-Signature: qox7sbwy8pdcumm1jdqmu614hpqqxwzy X-Rspam-User: X-HE-Tag: 1732191766-712013 X-HE-Meta: U2FsdGVkX1+xXp9SOlyBJweJ63ouID3878qfJNyX8EqDxUYckKam3Lps2+QJbRV7MQI3SIu/rYwg5L0/3mNaz0hklyYY5T6ZtWly7CJ/rsd88In0muREuAdz417MYjeHFySOV43alz6+tR5BG9ONwH+ukdmfNUENn9i5swgDPKPxJy9o7+Hard7PUYFvROc/6Xam2GdVoule27zt3k6Tvevfsio5ASPrHvmkuBwiAi7wJgi/6goJ3xD7IWg9wDz0tZ8NMreC2wyV2poB63Cg0eXt6fqxPzRs4JIdOKKJATo7llLQA2eEGRNRNteEEfiGR70lkImgMR+clIqr1cIgs1LF8hRoGWIEum4Q8s1fhM2E8fhEyezTcfv+In80rGVPillslYg/lt8IAUMA0Ggjar7SJ4tV99Al8kV+5bNwIkUWfsf8Y3BpzJPGBr5alyIm0CmgpG582ZeGcLuDhcy9HkZzH4bZG2VMiJ3wQ+6tHJkI2ih6PTuLiF6KkXBsILF0OxkbWRxj00JS9LqRB62A1K0pdMb2s1UOWo2nJVmiuNwv6NZTb7SjpCBBwzakRx8huob60qvGpblpdimuFUJpzpyd8+76L2zgW1N6cOBm5//7PqAlxAKPzVfBJINiBclucSS2200CadeMBCkh8bhzOO/xaDZje+T23pFRxaCrlg2ctUFRBEqf6XoCvOg7NYqjX+4UKBzgyfnlfKQ+cG+U2AodM8UlEBp3PCXhkZnsvcxbANRT4wKSDefhJ2nN0HVBHogszqXhI+fE+8FsAkgeT+TgLUqJXpZhTXqk5if+5zJpXp73JMZCyZ5mNjby7oc4LbUrI596ZN5E0+tHkYzOO1MIJUcptpptASaKJUhGe+t+KVWkeZIcut2EiySri1I9yDa217Z/Cix4Ofvgx7TUV4hRmEEfTGyQaInHdMkjSVZWYIOjWGuQY1thIDt/4a6MMeAR49K78QXR3lh2B5h NLLMhTKK UUsSenJM3IHzGCQ8ZKQMU+FuKliPf2UaEzTdDIkZy0Rw+u1J9/WKNvy5zIJMbZofwTvwLOw1zTjGQsfUV4Mf49vraWEwrXNJxJq/y1dPOUQQ8+sLbL6uWDleNameIzuCucjGvYPEG0u51gA+Suk5UopNZtQ== 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: 1.Removing redundant checks for current->mempolicy, with a more concise check order. 2.Using READ_ONCE(current->mempolicy) for safe, single access to current->mempolicy to prevent potential race conditions. 3.Optimizing the scope of task_lock(current). The lock now only protects the critical section where mempolicy is accessed, reducing the duration the lock is held. This enhances performance by limiting the scope of the lock to only what is necessary. Signed-off-by: Zhen Ni Signed-off-by: Zhen Ni --- mm/mempolicy.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index b646fab3e45e..8bff8830b7e6 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -2132,11 +2132,14 @@ bool init_nodemask_of_mempolicy(nodemask_t *mask) { struct mempolicy *mempolicy; - if (!(mask && current->mempolicy)) + if (!mask) + return false; + + mempolicy = READ_ONCE(current->mempolicy); + if (!mempolicy) return false; task_lock(current); - mempolicy = current->mempolicy; switch (mempolicy->mode) { case MPOL_PREFERRED: case MPOL_PREFERRED_MANY: