Received: by 2002:a05:7412:e79e:b0:f3:1519:9f41 with SMTP id o30csp198797rdd; Wed, 22 Nov 2023 13:14:20 -0800 (PST) X-Google-Smtp-Source: AGHT+IFoQYzO8H9vVtWq64cSbnf/h92jtGF4RckFCURXSRevdbunz3bqOEgmQLmM31APuDifqdjw X-Received: by 2002:a05:6a20:38a2:b0:187:604a:3add with SMTP id n34-20020a056a2038a200b00187604a3addmr3200309pzf.24.1700687660438; Wed, 22 Nov 2023 13:14:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700687660; cv=none; d=google.com; s=arc-20160816; b=RkPsDglslbPuVvn3Q+VE3Uxr9BzW6eMc2LmTRgW0kgj+vjlcKeC2yxAjpkCFhjnfgm jQJmZVr8K5o84xEZ2w5wHdGRX60YX0bdLLtq3f3WyNcffcqCBKZZifKlbXr3oE6+2hya eV9bB4FxD3xJ8QBvrZluNrruCKwYrrH10mSBOedPcAoRgpAT+Of4CAy8LTPE09jcaghb ebzvRWm52V/Lyxi1TscnyFBFa+14uZwxHoikz8ByBuXYaC7lXkhPUBzvtMNvHW7ktAKL 62VDZInIkFR+x8eADIOGqs/NyiuITxwAAqDqujjrcuaKdoc/xfcI0nzh5ImxKSgqvNxU QSjg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=GABulzaAiHjU3X3l/c8xxKNbyTu6XjRf70gPWHFnEeU=; fh=oGSAV5gRGUhpxAQmPPuhXynNS/gQQ/xIjVhrkaeHVfk=; b=AX2m2cLMb07p6/Phud1egdc1DeFqD/GLS9+pijnRlCBl9ovfd1LAzmqhAHVcNZ744W DbPmzeoOuJK0u3jgRE4R1V0De9Df112UFC9t/c4/3RDmGfQ9ZcTjYctH1YbFrqdk7A8b aBhtRLmYwFgOM1L2IkxwhbeK3xGhEoVOX6QpDi0IMamJmgPvIHwHTeCYknPXdE+I0MXB k/knngJuIPkbrHvbq+i2GK+sobm8PhHikvV9YrkaD0AxRcmYWmlXehdyx9Oe2aSWayrC oPKSWxug42hEgM4cqEVKjUEs8F/LP1apPas4zLiINqfkqWLVf0378BOUM453cdywpUnO 7Dpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=ZbnpJgNj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id j10-20020a056a00174a00b006cb6fc94847si329822pfc.172.2023.11.22.13.14.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Nov 2023 13:14:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=ZbnpJgNj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id EAA4A8218E4B; Wed, 22 Nov 2023 13:12:54 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344497AbjKVVMd (ORCPT + 99 others); Wed, 22 Nov 2023 16:12:33 -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-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: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 22 Nov 2023 13:12:55 -0800 (PST) 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 = NULL; else mmget(mm); } To retain the original behavior, if (task == current) we access the task->mm directly, but if (task != 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 == 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 long addr) } /* 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 *nmask, if (flags & (MPOL_F_NODE|MPOL_F_ADDR)) return -EINVAL; *policy = 0; /* just so it's initialized */ - task_lock(current); - *nmask = cpuset_current_mems_allowed; - task_unlock(current); + task_lock(task); + *nmask = task->mems_allowed; + task_unlock(task); return 0; } @@ -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 = current->mm; + if (task == 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 = task->mm; + mmget(mm); + } else + mm = get_task_mm(task); mmap_read_lock(mm); vma = 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 = current->mempolicy; + task_lock(task); + pol = task->mempolicy; mpol_get(pol); + task_unlock(task); } if (!pol) { @@ -962,12 +974,13 @@ static long do_get_mempolicy(int *policy, nodemask_t *nmask, vma = NULL; mmap_read_unlock(mm); err = lookup_node(mm, addr); + mmput(mm); if (err < 0) goto out; *policy = err; - } else if (pol == current->mempolicy && + } else if (pol == task->mempolicy && pol->mode == MPOL_INTERLEAVE) { - *policy = next_node_in(current->il_prev, pol->nodes); + *policy = next_node_in(task->il_prev, pol->nodes); } else { err = -EINVAL; goto out; @@ -987,9 +1000,9 @@ static long do_get_mempolicy(int *policy, nodemask_t *nmask, if (mpol_store_user_nodemask(pol)) { *nmask = pol->w.user_nodemask; } else { - task_lock(current); + task_lock(task); get_policy_nodemask(pol, nmask); - task_unlock(current); + task_unlock(task); } } @@ -1704,7 +1717,8 @@ SYSCALL_DEFINE4(migrate_pages, pid_t, pid, unsigned long, maxnode, } /* 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, addr = untagged_addr(addr); - err = do_get_mempolicy(&pval, &nodes, addr, flags); + err = do_get_mempolicy(task, &pval, &nodes, addr, flags); 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); } bool vma_migratable(struct vm_area_struct *vma) -- 2.39.1