From nobody Thu Dec 18 05:36:01 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7C994C61DA7 for ; Wed, 22 Nov 2023 21:12:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344512AbjKVVMf (ORCPT ); Wed, 22 Nov 2023 16:12:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37578 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344390AbjKVVMZ (ORCPT ); Wed, 22 Nov 2023 16:12:25 -0500 Received: from mail-pf1-x443.google.com (mail-pf1-x443.google.com [IPv6:2607:f8b0:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D4A9D66; Wed, 22 Nov 2023 13:12:17 -0800 (PST) Received: by mail-pf1-x443.google.com with SMTP id d2e1a72fcca58-6c3363a2b93so257892b3a.3; Wed, 22 Nov 2023 13:12:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700687537; x=1701292337; darn=vger.kernel.org; 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=GABulzaAiHjU3X3l/c8xxKNbyTu6XjRf70gPWHFnEeU=; b=ZbnpJgNjs4dBcrBJ0t4DDz+Qat2I9JACrZNqlr2ESg/gcT51vNGjOgZwn0oG/UVAZ7 pJiA6uVge8W7h/gzkcmsR6DUHTetxIvGJqOSV47Rko3Ffwaft7y1svuz9csW8JOfKLst UeZ/WS/tSq8V78tl02Gk6rL9qCu/kFMK2EUH0x4Zk+vATlDcAh0bfjKAh4GTtlf/5Kl5 /KnwIAdgGletqP3STCEdDb6Hm03stzzF778hqlA4qxhllUXSwW7Q3J30iAusEn8Iiukg gnvsKd1ElELo2OPc+yT9YeDbJhkbGxr7vJaf5B5HT3zxgBMGfytdZTdih7/tEsgqD1rU EFmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700687537; x=1701292337; 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=GABulzaAiHjU3X3l/c8xxKNbyTu6XjRf70gPWHFnEeU=; b=Pqh01OrqJ8E5Z1Eo5OlaF5TmLPsujF1+jIEJyEqNE36+s6JI05fjmdM+RKQxw1pdpI uQkS/yL4lD4vvUqTHrhTGGQRn2IDNvV0NUs2Lxq5djPmrfqIB4Zjc+iHPgE0XTBjHiju IbTJEPsBNbxa1qUcaybcTb7/4Rn0ywjyb+kOBt66Q2v3C7tgitl7LAdiIEbXR9BM50qM IBblLLQ7PZIrZYTG4NL3K2H6is59JpTPnS+TQcdEKABfNt9uYm5g4wRMAfHlz3HeX+BW AOxTYATiVOcVQznA/gSiAH9l0nrFTXgOvvmNovLzGxelyFGxM/ZIMeR0/Fm0yzZQy8wR 8y6g== X-Gm-Message-State: AOJu0YwI0KPTngJ108aALE16euTT5pcyVT29EDFrk+nhkmoEGeqeD7Rs 6XZ7mBivv6Fa0BuENetchs8v+EYAP9YS X-Google-Smtp-Source: AGHT+IFXvdmudROHD0YlWi+YK0a7lcqiiD79ewyUHoexoGYHQI8f+8OjJBq0l9lLV64qzqt5Crv6dA== X-Received: by 2002:a05:6a00:1916:b0:6cb:bc1a:dcff with SMTP id y22-20020a056a00191600b006cbbc1adcffmr3982392pfi.13.1700687536917; Wed, 22 Nov 2023 13:12:16 -0800 (PST) Received: from fedora.mshome.net ([75.167.214.230]) by smtp.gmail.com with ESMTPSA id j18-20020a635512000000b005bdbce6818esm132136pgb.30.2023.11.22.13.12.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Nov 2023 13:12:16 -0800 (PST) From: Gregory Price X-Google-Original-From: Gregory Price To: linux-mm@kvack.org Cc: linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, akpm@linux-foundation.org, arnd@arndb.de, tglx@linutronix.de, luto@kernel.org, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, mhocko@kernel.org, tj@kernel.org, ying.huang@intel.com, Gregory Price Subject: [RFC PATCH 04/11] mm/mempolicy: modify get_mempolicy call stack to take a task argument Date: Wed, 22 Nov 2023 16:11:53 -0500 Message-Id: <20231122211200.31620-5-gregory.price@memverge.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20231122211200.31620-1-gregory.price@memverge.com> References: <20231122211200.31620-1-gregory.price@memverge.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" To make mempolicy fetchable by external tasks, we must first change the callstack to take a task as an argument. Modify the following functions to require a task argument: do_get_mempolicy kernel_get_mempolicy The way the task->mm is acquired must change slightly to enable this change. Originally, do_get_mempolicy would acquire the task->mm directly via (current->mm). This is unsafe to do in a non-current context. However, utilizing get_task_mm would break the original functionality of do_get_mempolicy due to the following check in get_task_mm: if (mm) { if (task->flags & PF_KTHREAD) mm =3D NULL; else mmget(mm); } To retain the original behavior, if (task =3D=3D current) we access the task->mm directly, but if (task !=3D current) we will utilize get_task_mm to safely access the mm. We simplify the get/put mechanics by always taking a reference to the mm, even if we are in the context of (task =3D=3D current). Additionally, since the mempolicy will become externally modifiable, we need to take the task lock to acquire task->mempolicy safely, regardless of whether we are operating on current or not. Signed-off-by: Gregory Price --- mm/mempolicy.c | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 9ea3e1bfc002..4519f39b1a07 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -899,8 +899,9 @@ static int lookup_node(struct mm_struct *mm, unsigned l= ong addr) } =20 /* Retrieve NUMA policy */ -static long do_get_mempolicy(int *policy, nodemask_t *nmask, - unsigned long addr, unsigned long flags) +static long do_get_mempolicy(struct task_struct *task, int *policy, + nodemask_t *nmask, unsigned long addr, + unsigned long flags) { int err; struct mm_struct *mm; @@ -915,9 +916,9 @@ static long do_get_mempolicy(int *policy, nodemask_t *n= mask, if (flags & (MPOL_F_NODE|MPOL_F_ADDR)) return -EINVAL; *policy =3D 0; /* just so it's initialized */ - task_lock(current); - *nmask =3D cpuset_current_mems_allowed; - task_unlock(current); + task_lock(task); + *nmask =3D task->mems_allowed; + task_unlock(task); return 0; } =20 @@ -928,7 +929,16 @@ static long do_get_mempolicy(int *policy, nodemask_t *= nmask, * vma/shared policy at addr is NULL. We * want to return MPOL_DEFAULT in this case. */ - mm =3D current->mm; + if (task =3D=3D current) { + /* + * original behavior allows a kernel task changing its + * own policy to avoid the condition in get_task_mm, + * so we'll directly access + */ + mm =3D task->mm; + mmget(mm); + } else + mm =3D get_task_mm(task); mmap_read_lock(mm); vma =3D vma_lookup(mm, addr); if (!vma) { @@ -947,8 +957,10 @@ static long do_get_mempolicy(int *policy, nodemask_t *= nmask, return -EINVAL; else { /* take a reference of the task policy now */ - pol =3D current->mempolicy; + task_lock(task); + pol =3D task->mempolicy; mpol_get(pol); + task_unlock(task); } =20 if (!pol) { @@ -962,12 +974,13 @@ static long do_get_mempolicy(int *policy, nodemask_t = *nmask, vma =3D NULL; mmap_read_unlock(mm); err =3D lookup_node(mm, addr); + mmput(mm); if (err < 0) goto out; *policy =3D err; - } else if (pol =3D=3D current->mempolicy && + } else if (pol =3D=3D task->mempolicy && pol->mode =3D=3D MPOL_INTERLEAVE) { - *policy =3D next_node_in(current->il_prev, pol->nodes); + *policy =3D next_node_in(task->il_prev, pol->nodes); } else { err =3D -EINVAL; goto out; @@ -987,9 +1000,9 @@ static long do_get_mempolicy(int *policy, nodemask_t *= nmask, if (mpol_store_user_nodemask(pol)) { *nmask =3D pol->w.user_nodemask; } else { - task_lock(current); + task_lock(task); get_policy_nodemask(pol, nmask); - task_unlock(current); + task_unlock(task); } } =20 @@ -1704,7 +1717,8 @@ SYSCALL_DEFINE4(migrate_pages, pid_t, pid, unsigned l= ong, maxnode, } =20 /* Retrieve NUMA policy */ -static int kernel_get_mempolicy(int __user *policy, +static int kernel_get_mempolicy(struct task_struct *task, + int __user *policy, unsigned long __user *nmask, unsigned long maxnode, unsigned long addr, @@ -1719,7 +1733,7 @@ static int kernel_get_mempolicy(int __user *policy, =20 addr =3D untagged_addr(addr); =20 - err =3D do_get_mempolicy(&pval, &nodes, addr, flags); + err =3D do_get_mempolicy(task, &pval, &nodes, addr, flags); =20 if (err) return err; @@ -1737,7 +1751,8 @@ SYSCALL_DEFINE5(get_mempolicy, int __user *, policy, unsigned long __user *, nmask, unsigned long, maxnode, unsigned long, addr, unsigned long, flags) { - return kernel_get_mempolicy(policy, nmask, maxnode, addr, flags); + return kernel_get_mempolicy(current, policy, nmask, maxnode, addr, + flags); } =20 bool vma_migratable(struct vm_area_struct *vma) --=20 2.39.1