Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp3170576rwl; Thu, 13 Apr 2023 17:12:55 -0700 (PDT) X-Google-Smtp-Source: AKy350Y285djg/A44p0P3ntrn7BqYnbkvgxbufc5bSw0AGGwQD5R4mFDHby262E35OXo9XFd/yHy X-Received: by 2002:a17:90b:1207:b0:246:b038:2f53 with SMTP id gl7-20020a17090b120700b00246b0382f53mr3549323pjb.24.1681431174995; Thu, 13 Apr 2023 17:12:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681431174; cv=none; d=google.com; s=arc-20160816; b=enqxM67Tc1/rykx7ib2BDS9cVN4sbENBj2evBBP1cYcCVABe5dZLbxCaAkubR5qbBl EbMHFkh/pDnYXaGwgUF0NQlvVi0y7tnrH0Ro24z08DQcQe5L3OQrnJL9KKy8KBBqb/Ek SNaw9/EBqHggyZPR21meURZ6CXIUkYT2HViRDP1E//gAt/+NTLVwXwudu7Z2Ger2KWEa lfOCZUTe4f8Rn2GIDMvxF89Y+rwYFtjcrjnG2/OB+CSEml3J34yQst3jTEwaypcERoM3 RSmkZtl2Tov3aiS3ZwJVaEWo4YElGX6MCKcVPmvjQLpSajkHUZwI2pjdqoBMgvLVt2sW RI7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=W+vIzhvgnDTeM5yLC6D6DMUdvSTbR9axXaSgRhroXf8=; b=MMLeq23o8Ur/4UxKTSnoa5MoJ4eyeAPvetuYJizYrQVnmgV1VSSNG3aA8KO2KgcZG/ bdYiauEdb3jHYfeiK4x2/FgEk/vuiIaYzAFi4o448mIm7FsKQXekD3w/I89Wg1KGrOmZ hDSprbl363hno3/SLDVQxZtvxtouPHvqkZ+uR/zK3kNC3s4j1kAv7rm0WVwKx44mDgv8 4FgKPlyCRwrD/08fjgU1If/5hMu+S0Jv9b9NkpnBbHsVDIW3xNTUYlzXHPPDQIGs/49S Uz1O1k3c9A101967y+Ed/jFRTl1T5RP0SzDMnj+lTNf89hqAo2TljeBLnUayhOEpexiC ZiGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=mW9eVt20; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 28-20020a630f5c000000b00519009eb504si3122847pgp.96.2023.04.13.17.12.43; Thu, 13 Apr 2023 17:12:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=mW9eVt20; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230452AbjDNAMM (ORCPT + 99 others); Thu, 13 Apr 2023 20:12:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44140 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230371AbjDNAMH (ORCPT ); Thu, 13 Apr 2023 20:12:07 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8C392359A for ; Thu, 13 Apr 2023 17:12:06 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-54f93850a86so69169547b3.2 for ; Thu, 13 Apr 2023 17:12:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1681431125; x=1684023125; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=W+vIzhvgnDTeM5yLC6D6DMUdvSTbR9axXaSgRhroXf8=; b=mW9eVt20/JcXUsgxb+xjqD4VG4js+2q/p5hJJDDZIcnukRm0syQv/TXqiec2heBNck /LJ2OaI6hIksRPbp9XAhh99LqDjoZM2pQCkJ+bmJzc8yZHZYkwMAwFEhAsHbl30CzShC AnjRJgdI7su71kGpp2x6zyUwFmi0ZsMW7HG1IhDvuRpLPR8dBsIbCcLfLX3bb41Nf7ID fPGnb4gcd1xumZwEpzLDR6LXrZNMCwms/UiqwFrs/MG66By8fMMhSBTwwbfU9CItCsXE IVTc6CfCREM1gKWUMxH19y2K58Mf0+JvqHllDRZBSVXeFCA0+XBcsiL9bPxv2h5LcfvI JjvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681431125; x=1684023125; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=W+vIzhvgnDTeM5yLC6D6DMUdvSTbR9axXaSgRhroXf8=; b=WNiLUrxMLdQ2UynPEam5utsvB3/viPoSAUnWIEPu1oDFixRonVEv6Pud8mrQZqYmM8 bWz3MHtZBh+i+hqpBx33+Z3zJYTT0tIoPboqUcD2ym4TUcLhZEvO3F/j7FSUBYlw/edS BKAaA0ZdoJWH0IkIP1MLkiIkGEFO4V+Uu0YaX1dMS8VOlSesmEmU2MN+5yXZJ4EC7G4e 6/48C1JcWNrmXk9osNLBb1AQQDjUpvNMO/XciC3g3YS2ofx3AWo99+HvyWWN+qXrRb1I LBsFkdhZO9Yg8qgaeuRyuZp9wzuJoaEWXEZf2/oa2KO+3dWCMSPmdCCDhScldYy1rC8k SaCA== X-Gm-Message-State: AAQBX9cikgGHM8FMkgk1s9PVEAA+esOnev2bAw2YIdG7BnBiIMoLU4WG CNaW24JXIx+hwCPVUwogo2YwRwH+N8XPJ9Txsw== X-Received: from ackerleytng-cloudtop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1f5f]) (user=ackerleytng job=sendgmr) by 2002:a81:4328:0:b0:545:4133:fc40 with SMTP id q40-20020a814328000000b005454133fc40mr2444452ywa.9.1681431125361; Thu, 13 Apr 2023 17:12:05 -0700 (PDT) Date: Fri, 14 Apr 2023 00:11:53 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.40.0.634.g4ca3ef3211-goog Message-ID: <17bb8e925c08f27c627cd1f2bbb2714daf590c1d.1681430907.git.ackerleytng@google.com> Subject: [RFC PATCH 4/6] mm: mempolicy: Add and expose mpol_create From: Ackerley Tng To: kvm@vger.kernel.org, linux-api@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, qemu-devel@nongnu.org Cc: aarcange@redhat.com, ak@linux.intel.com, akpm@linux-foundation.org, arnd@arndb.de, bfields@fieldses.org, bp@alien8.de, chao.p.peng@linux.intel.com, corbet@lwn.net, dave.hansen@intel.com, david@redhat.com, ddutile@redhat.com, dhildenb@redhat.com, hpa@zytor.com, hughd@google.com, jlayton@kernel.org, jmattson@google.com, joro@8bytes.org, jun.nakajima@intel.com, kirill.shutemov@linux.intel.com, linmiaohe@huawei.com, luto@kernel.org, mail@maciej.szmigiero.name, mhocko@suse.com, michael.roth@amd.com, mingo@redhat.com, naoya.horiguchi@nec.com, pbonzini@redhat.com, qperret@google.com, rppt@kernel.org, seanjc@google.com, shuah@kernel.org, steven.price@arm.com, tabba@google.com, tglx@linutronix.de, vannapurve@google.com, vbabka@suse.cz, vkuznets@redhat.com, wanpengli@tencent.com, wei.w.wang@intel.com, x86@kernel.org, yu.c.zhang@linux.intel.com, muchun.song@linux.dev, feng.tang@intel.com, brgerst@gmail.com, rdunlap@infradead.org, masahiroy@kernel.org, mailhol.vincent@wanadoo.fr, Ackerley Tng Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable 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 mpol_create builds a mempolicy based on mode, nmask and maxnode. mpol_create is exposed for use in memfd_restricted_bind() in a later patch. Signed-off-by: Ackerley Tng --- include/linux/mempolicy.h | 2 ++ mm/mempolicy.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/include/linux/mempolicy.h b/include/linux/mempolicy.h index 9a2a2dd95432..15facd9de087 100644 --- a/include/linux/mempolicy.h +++ b/include/linux/mempolicy.h @@ -125,6 +125,8 @@ struct shared_policy { }; int vma_dup_policy(struct vm_area_struct *src, struct vm_area_struct *dst); +struct mempolicy *mpol_create( + unsigned long mode, const unsigned long __user *nmask, unsigned long maxnode) void mpol_shared_policy_init(struct shared_policy *sp, struct mempolicy *mpol); int __mpol_set_shared_policy(struct shared_policy *info, struct mempolicy *mpol, unsigned long pgoff_start, unsigned long npages); diff --git a/mm/mempolicy.c b/mm/mempolicy.c index f3fa5494e4a8..f4fe241c17ff 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -3181,3 +3181,42 @@ void mpol_to_str(char *buffer, int maxlen, struct mempolicy *pol) p += scnprintf(p, buffer + maxlen - p, ":%*pbl", nodemask_pr_args(&nodes)); } + +/** + * mpol_create - build mempolicy based on mode, nmask and maxnode + * @mode: policy mode, as in MPOL_MODE_FLAGS + * @nmask: node mask from userspace + * @maxnode: number of valid bits in nmask + * + * Will allocate a new struct mempolicy that has to be released with + * mpol_put. Will also take and release the write lock mmap_lock in current->mm. + */ +struct mempolicy *mpol_create( + unsigned long mode, const unsigned long __user *nmask, unsigned long maxnode) +{ + int err; + unsigned short mode_flags; + nodemask_t nodes; + int lmode = mode; + struct mempolicy *mpol; + + err = sanitize_mpol_flags(&lmode, &mode_flags); + if (err) + return ERR_PTR(err); + + err = get_nodes(&nodes, nmask, maxnode); + if (err) + return ERR_PTR(err); + + mpol = mpol_new(mode, mode_flags, &nodes); + if (IS_ERR(mpol)) + return mpol; + + err = mpol_init_from_nodemask(mpol, &nodes, true); + if (err) { + mpol_put(mpol); + return ERR_PTR(err); + } + + return mpol; +} -- 2.40.0.634.g4ca3ef3211-goog