From patchwork Fri Apr 14 00:11:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 83142 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp26456vqo; Thu, 13 Apr 2023 17:12:48 -0700 (PDT) X-Google-Smtp-Source: AKy350aDMhcnf4OPGRohRjxxOJk6D/5Nt9jWuaGZi06yJUWU1Ezat/iud8MR/lKZCvMEs7f1hi4s X-Received: by 2002:a17:903:1107:b0:1a5:206:4c88 with SMTP id n7-20020a170903110700b001a502064c88mr979280plh.18.1681431168092; Thu, 13 Apr 2023 17:12:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681431168; cv=none; d=google.com; s=arc-20160816; b=dE/QndLi6k9xEgRbaPp4PhZNZGXxPDswJt9+5hECkFitpTkiG0sAUd9xrO3Jlbcful Si/Mpx895to2HysnAgPITd+yghnKzMcvxcFHLe8RnevJOxYIuwno4m3b6f0G2hZlMJHR wh4FSagS9dbCw+FYNNb/ce3z4tNiCDVFk3I2fjIr4Vp8DPQ0C5rawHLymV0IbKRjbjkS sykrk5bVHWLe/Mm5PPqywB9zdt0BSKnCaD2ad7ax9WPMK4zjmUTpCT9rOmXyUt/wYkrk //zaV3jTwa3Nw2rHc3wcKsJJT8rgVxgem8rKnzZgXvvT3aFo9aFnW94aG8QqvU4I8DFn F+/Q== 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=LGz+M9nFtzHbGGbLaqDB42+XLCRk5tnoOBaeHE6w0dg=; b=FeyBiVMj9ijs4zPNXBUY97OgTY1fbgpgK6AnseSvpUHBk1wYkT3YOz3rT2RI8dqElf a1oxwR8aZJ4t0tqK2Ag2CarDW6tfNBr0Vz07Dh8hvegyGTFXA38/3euC9lbgh/N7BAt1 J0nlID7riQIjG1Du3KQifc3hWT8tYzoai7gi7BjLZIq7KQ2LnUNo4cle8l+C/G2aCbYA kw/C/V8dRh9wVYUugHXBxQVrqJlngPBcXeZ8M15nFjXFsf0lrIK8zfOPznFs3PNmyFL2 Sws7L+fFIW9Mzxhok1rLON6+V+euFlaHDO+NcD+mAh26iYnFvS+m+0Dj0maW64iIXX5x 83/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=S5LluL5N; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y2-20020a1709029b8200b001a63783159csi3036141plp.238.2023.04.13.17.12.36; Thu, 13 Apr 2023 17:12:48 -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=S5LluL5N; 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 S229625AbjDNAMG (ORCPT + 99 others); Thu, 13 Apr 2023 20:12:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44032 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230117AbjDNAMC (ORCPT ); Thu, 13 Apr 2023 20:12:02 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E6AFE3C21 for ; Thu, 13 Apr 2023 17:12:00 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id z9-20020a170903018900b001a1e8390831so8701543plg.5 for ; Thu, 13 Apr 2023 17:12:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1681431120; x=1684023120; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=LGz+M9nFtzHbGGbLaqDB42+XLCRk5tnoOBaeHE6w0dg=; b=S5LluL5NFzRfMffvrC48pA8vM9jBztEWtYOdVu6RC2ufBdtUUSqD+XyBpvK1yq+IN2 IHnZhiT1W0yssxQ4r7Y9clMkV6zvB2ogiOPODlbRwrRyDr07/qDDpZBBDVLk/N65uZzy CN1ozoAyt0GCn/Q/UB/ZvGZUO3F2PMmedv6i9jH6jJ0uY0cAH01PJa7G0h1cPoPbYq3f IdXBPJH4a6Ro/C6Sct1ltwBhbcLCAMsdz4Y30Y2jgpBNq+x1h2YwjODMyjFmoi8MJRCf dDhFwUgzHhEfG/8I1Qg7KrBG7Crz9lPVWk5qe+WYi0MTtv3ZN9nLmsTMDMwmdbsD2L57 OQPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681431120; x=1684023120; 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=LGz+M9nFtzHbGGbLaqDB42+XLCRk5tnoOBaeHE6w0dg=; b=B2aroOwC1oD8ILdHWIDbbnAanFHWE2GTwyzQ+PdwDTdcNB5GWSw+7tGGsGXmAv5Weg NDOr5wSnBVdvAmCzBCLfXHZW2Y4Eo18VOejZFnucK34k684ElJwrHUbUlNwCrLjfSC5+ RyGkfL0BpvOaZuV5cSGybLmD2RiR0FSmD16gSKXk9X8i2Wlp0zoNYPqAIKmhAYlCEOLo lIvOrx8vyRCyD5Dl6TcaPGBDZmObtiBoKzUNK4KRoLlTI8+tCTNSznXlHUtP4lYsHvbZ jTRg15/54sf6mmbcN0O1+9OQ8kuUFhrV516ZcSnrtTzOA0wmP9GfXod8pS+Ek7S+IzBn 7Lqw== X-Gm-Message-State: AAQBX9e2qcMD+n+/S8OjVb0KdBBArMNKNEbcQWOuuckUuGrkH1IDNvkt GzONyL3PeMf7sb18qXsL5msEvrrUsj0+p8+6gw== X-Received: from ackerleytng-cloudtop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1f5f]) (user=ackerleytng job=sendgmr) by 2002:a17:902:bd90:b0:19a:f9d9:28d4 with SMTP id q16-20020a170902bd9000b0019af9d928d4mr282396pls.3.1681431120479; Thu, 13 Apr 2023 17:12:00 -0700 (PDT) Date: Fri, 14 Apr 2023 00:11:50 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.40.0.634.g4ca3ef3211-goog Message-ID: <476aa5a107994d293dcdfc5a620cc52f625768c2.1681430907.git.ackerleytng@google.com> Subject: [RFC PATCH 1/6] mm: shmem: Refactor out shmem_shared_policy() function 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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763108368811042410?= X-GMAIL-MSGID: =?utf-8?q?1763108368811042410?= Refactor out shmem_shared_policy() to allow reading of a file's shared mempolicy Signed-off-by: Ackerley Tng --- include/linux/shmem_fs.h | 7 +++++++ mm/shmem.c | 10 ++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h index d9e57485a686..bc1eeb4b4bd9 100644 --- a/include/linux/shmem_fs.h +++ b/include/linux/shmem_fs.h @@ -134,6 +134,13 @@ static inline bool shmem_file(struct file *file) return shmem_mapping(file->f_mapping); } +static inline struct shared_policy *shmem_shared_policy(struct file *file) +{ + struct inode *inode = file_inode(file); + + return &SHMEM_I(inode)->policy; +} + /* * If fallocate(FALLOC_FL_KEEP_SIZE) has been used, there may be pages * beyond i_size's notion of EOF, which fallocate has committed to reserving: diff --git a/mm/shmem.c b/mm/shmem.c index b053cd1f12da..4f801f398454 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2248,20 +2248,22 @@ unsigned long shmem_get_unmapped_area(struct file *file, } #ifdef CONFIG_NUMA + static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol) { - struct inode *inode = file_inode(vma->vm_file); - return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol); + struct shared_policy *info; + + info = shmem_shared_policy(vma->vm_file); + return mpol_set_shared_policy(info, vma, mpol); } static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma, unsigned long addr) { - struct inode *inode = file_inode(vma->vm_file); pgoff_t index; index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; - return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index); + return mpol_shared_policy_lookup(shmem_shared_policy(vma->vm_file), index); } #endif From patchwork Fri Apr 14 00:11:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 83143 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp26668vqo; Thu, 13 Apr 2023 17:13:10 -0700 (PDT) X-Google-Smtp-Source: AKy350Zw5kTVF1iVDCcxvigf6FZebuGw+lPqCI0K5uApNoXPicGFcKt+/67p010IB7Vktib7arQZ X-Received: by 2002:a17:903:40c1:b0:19f:36ae:c29f with SMTP id t1-20020a17090340c100b0019f36aec29fmr715169pld.46.1681431190350; Thu, 13 Apr 2023 17:13:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681431190; cv=none; d=google.com; s=arc-20160816; b=yXPozpJbYI8COK5ut3sKd5wUxiRltlkZpccngJ2T+4XktueJWTSpj/QOmOMJ6389Pa BsONoua6Q+j5y+zoro4mAtVi0Fl6uqlH1q2Y+oyYUdtBi/FLlQIjOvCyWIna4B9bO1EC IUbWgEM4kuadqoBrmn4qObwhZEWdYzZ2lYcWJRGmn71IjxR+rJ9/iq9aID79ZFgn4Vk2 UaJz57zMbaXWdMX0hGtBAYEUDbpcxTlKNTtOA13nB9703T1pXZ1YnhDPDl9Se9URo+7K GyCQcwlRnNC8v2QN1qUIm3IsyC0M9U3d4GJdgxSc10PDEWaq/U4fa6oHXqxuxOzoVyj6 hRHQ== 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=BkEj6q2IVKaYOP6wiiRLnj5ts2mBBZa+OIxL/GSTf98=; b=bFDokIrVJwyEQnbf4VWMZT+QSvrA+fDgJVtpZsstnSUkepB20YC9hFu0llEh3YGDhp H68JRsYuDx0m0ruVIPsZr5xIcJC/5jBQvdV84eAIu6uigBhurF86J0e8alGUWgoxkWAy BUuf8ZiJvLX+wFp2yy1F2KCQ4hgPNMs54+Znzkc4NBQgnw/BFUBHxZ0LQkaDoZTzfLps /of+04DVfmcmlAbl/R0OOu6kJTF8iF0OOjFcERlxI+ViGbncheFiQmMN+DLiDOXel1gB Ho0aXM26R6WKl5IDnbStA9niJnD0Okl/iY8LfZZ4KRxxWL1pHUEPTWcZDa1oJTpEWii7 /KhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=hEKe7KJh; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x62-20020a638641000000b0051460f7d4e0si3330142pgd.29.2023.04.13.17.12.58; Thu, 13 Apr 2023 17:13:10 -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=hEKe7KJh; 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 S229493AbjDNAMJ (ORCPT + 99 others); Thu, 13 Apr 2023 20:12:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44022 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230324AbjDNAMD (ORCPT ); Thu, 13 Apr 2023 20:12:03 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CD63335A1 for ; Thu, 13 Apr 2023 17:12:02 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id p2-20020a170902e74200b001a64b5976a5so5975594plf.0 for ; Thu, 13 Apr 2023 17:12:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1681431122; x=1684023122; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=BkEj6q2IVKaYOP6wiiRLnj5ts2mBBZa+OIxL/GSTf98=; b=hEKe7KJhnbtB7HOx0Et2GGRR4tPoUqzTdWnfUxGGsyi9EoTWrZhVqGnrZvvHuOndFb TZMGSQ8vslsUBzDoxn9iNYO9LNL12Zs3VwW97btbxF/u5m0XCRPh93bGl4sbvEzqxT6/ vZR2E4QR8bsqooC9JFGEbUD4GNGVdkQWEnhHWe0gty7hNJfMUG+x6qUJrqOv1s31GKD5 /0uqMrvUHdk6drQi8pnb3EXthHB7+IGInrY92tJ9ElhNKYIt+IMYZonjXRQ9OCvmz8n+ DadYc4CMCAJvwcgOviymbXY411dscM1zCutEA5OdbSJuHP8nTNCFxIaU7gllAACoSnWm TMoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681431122; x=1684023122; 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=BkEj6q2IVKaYOP6wiiRLnj5ts2mBBZa+OIxL/GSTf98=; b=gLPNk7PszY5tfkK8JvTH8mqzHhmLm/xcXmQKTHSLlI7focxV+gwVfbvhW4GUX1SPPk 4UVoxRaLrWqfMR1femdLulyi8kQWCIJo6mWqLueKcACdLERZsYtFttiNDrhXUdkHlFA+ ag6FUWq5NbXsMxi+/FvLZFVnbTQlxMkhm8EnBImZQdKflhrk9UNXJNs/UnOGBIFtXCid 69CJiEnGtERtjn3iQ+dzT1oA8tUyLdsNANVY7ANJmhJRhG65SURtCcAKMvIsgisp5euu MyaZSNVCwa/O88L1+QUKcXiYwFfBXfdP7UztO02v8acAU//Ev/4k+d2nbm3WVhAd233L S0BQ== X-Gm-Message-State: AAQBX9fSaIv2wFOR5hIiWO6bdtkw6z7jFOF5Vkra7rpAGdby88Lp8LJ1 rwjsx/FteOYsRxQ75cBCFx3mQUs8sB3j0c0lMw== X-Received: from ackerleytng-cloudtop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1f5f]) (user=ackerleytng job=sendgmr) by 2002:a63:1c09:0:b0:507:3e33:43e3 with SMTP id c9-20020a631c09000000b005073e3343e3mr240709pgc.7.1681431122125; Thu, 13 Apr 2023 17:12:02 -0700 (PDT) Date: Fri, 14 Apr 2023 00:11:51 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.40.0.634.g4ca3ef3211-goog Message-ID: Subject: [RFC PATCH 2/6] mm: mempolicy: Refactor out mpol_init_from_nodemask 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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763108391779515127?= X-GMAIL-MSGID: =?utf-8?q?1763108391779515127?= Refactor out mpol_init_from_nodemask() to simplify logic in do_mbind(). mpol_init_from_nodemask() will be used to perform similar functionality in do_memfd_restricted_bind() in a later patch. Signed-off-by: Ackerley Tng --- mm/mempolicy.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index a256a241fd1d..a2655b626731 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -1254,6 +1254,25 @@ static struct page *new_page(struct page *page, unsigned long start) } #endif +static long mpol_init_from_nodemask(struct mempolicy *mpol, const nodemask_t *nmask, + bool always_unlock) +{ + long err; + NODEMASK_SCRATCH(scratch); + + if (!scratch) + return -ENOMEM; + + /* Cannot take lock before allocating in NODEMASK_SCRATCH */ + mmap_write_lock(current->mm); + err = mpol_set_nodemask(mpol, nmask, scratch); + if (always_unlock || err) + mmap_write_unlock(current->mm); + + NODEMASK_SCRATCH_FREE(scratch); + return err; +} + static long do_mbind(unsigned long start, unsigned long len, unsigned short mode, unsigned short mode_flags, nodemask_t *nmask, unsigned long flags) @@ -1306,17 +1325,8 @@ static long do_mbind(unsigned long start, unsigned long len, lru_cache_disable(); } - { - NODEMASK_SCRATCH(scratch); - if (scratch) { - mmap_write_lock(mm); - err = mpol_set_nodemask(new, nmask, scratch); - if (err) - mmap_write_unlock(mm); - } else - err = -ENOMEM; - NODEMASK_SCRATCH_FREE(scratch); - } + + err = mpol_init_from_nodemask(new, nmask, false); if (err) goto mpol_out; From patchwork Fri Apr 14 00:11:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 83144 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp26869vqo; Thu, 13 Apr 2023 17:13:31 -0700 (PDT) X-Google-Smtp-Source: AKy350aj2ksg0/xlaF4fLubP6+TUPuRjT2559HSgCifei/eE2t933Nvf43AAqWXpWaRJmfwvt9eU X-Received: by 2002:a17:902:e811:b0:1a5:898:37aa with SMTP id u17-20020a170902e81100b001a5089837aamr992732plg.15.1681431210836; Thu, 13 Apr 2023 17:13:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681431210; cv=none; d=google.com; s=arc-20160816; b=NefIALjEv1qjK8tyytIE9/kWEx9egtvOeJQqS32OPQkLto1XLFVGVNvUqyG5+cUHls RIvjakIS3swZ73b0w9+QFyQPz237EF6n/RdMCgDcPhlHeUhCzjRkXhBOObdmszzOoqsH M6IV5D66fzGEtqY0CykRDsV4gyDALvK6KWlFTfaFsrTIOIzSjr2zvpltHAfhV/v+Ehvj UK7hTDMC36hknPtpALqhVGxMJ55HIFgM+Xcz93XXgCxhK+QpnBo8Um44vfy+WbdpyWCa 53YmJM95SIanH3D6A1nsq8Fg/mucx80B+l3m+FCJ1Jx9quhCC5RipEJW3Qeq2UMAN3Hk f4eg== 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=Gsr9n7GPJt3nQiq4GcQ+TdB+rTZDJHWVuXMgSAc/inc=; b=fNig/aOCcQ8y0+5DSVGyAqKEB1oCjlKnUurjjErKo9QmXXFiAe0QcrGBlFaLGifUnz RtMTDJzD+1dGI1UJvuqeuYGgbfESCGlmb1Cf08IRDsDChkToUmFzlvoWtFcTnzpLM9UY eQLLCXEsocsrRBf57bUx8mguOsRfzZW5sdE6ATQ7jZ9ZmD0d1pXovzsv62gbz79XvtAL IHx2V7DN5FFp1VFW7nfEMBkCI7IX8BLtIOegFxXnh5Qodj5DEwMZa78sTYyidQDFZzij 392ltiLHjb4O8wyvbrWYT7WLpL39zOn69D5mmUj2IlDqPe8HlMhTRXR0PoR31anNmO29 HekA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=kiDTePLk; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b10-20020a170902d50a00b001a647aadbeasi3429321plg.416.2023.04.13.17.13.17; Thu, 13 Apr 2023 17:13:30 -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=kiDTePLk; 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 S231143AbjDNAMW (ORCPT + 99 others); Thu, 13 Apr 2023 20:12:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44118 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230271AbjDNAMG (ORCPT ); Thu, 13 Apr 2023 20:12:06 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D9C723A86 for ; Thu, 13 Apr 2023 17:12:04 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-54ee397553eso171355807b3.19 for ; Thu, 13 Apr 2023 17:12:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1681431124; x=1684023124; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Gsr9n7GPJt3nQiq4GcQ+TdB+rTZDJHWVuXMgSAc/inc=; b=kiDTePLk95tLtwi05EOCo6F4V5CaUODRfRqdJI4Zgzm9FQWvaGZtlrD6yXaeMKkBjy 8RYIMMqxVwtK8hcqiFL4L6XAaUQuBxXj0Ob3OL8FosXLKFkReuzMoaSCAc6uSho3g0AT 1Mad+18Vu1ODUbN+2p4Gx7UN/JCMROb9IKSQZwVC0SqUcslnNIKVGAjm6jXtAj4CVyW6 AubdJjVqBGvAcIQftDMGLDBeWf29B2Tvkh+iboimGU/GxdHwLZqBuIds+tebPgdOIXna fQ+8mzrH61gD5Hs3hKsbNr1LXybHaYbyOLpWo62wPHciL4cbfv9pbXLpD+tC0w5oX1yN Vaaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681431124; x=1684023124; 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=Gsr9n7GPJt3nQiq4GcQ+TdB+rTZDJHWVuXMgSAc/inc=; b=TZj7THnJtuo56a/VKISAeDArM96V6uO8XL4aRmUdTtWYF2jt72xHmHB/cMySIu7nDp /MrVCjZo+LWlxEfCX6qP8bERHRX4E44XFe9EFvQV7T3tb6PJq9nDX5CEHqclvTm8eABt Ze7+iQn/VU4Ob+T5XnYp8DuOuk0RAuemFF8A+0XnrvtRxGe0E9MkRx9HFpa6DlJn3QQ5 84+YfpQiQYYddDfBMOJjnI8kUVeHwMRUjFfRvQsvkOX/MF4eQQpgM6u2W0CaXSAVgpTN BiKP0ljsJik1iPozfzzlGgrowNUa8fswu9izL+DtCYwN9oFHBB/asSSoeXjt9VnnJp+e ae9Q== X-Gm-Message-State: AAQBX9cxvJ1FEIUDOKrapRrSnwN0eVK92agBBwb8adDHi97IlBIvdtLX 18SNH4wENbYFQNVvYQpr0G6gOaSH8dvt5jpxJw== X-Received: from ackerleytng-cloudtop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1f5f]) (user=ackerleytng job=sendgmr) by 2002:a81:af0c:0:b0:54f:8566:495 with SMTP id n12-20020a81af0c000000b0054f85660495mr2640217ywh.1.1681431123807; Thu, 13 Apr 2023 17:12:03 -0700 (PDT) Date: Fri, 14 Apr 2023 00:11:52 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.40.0.634.g4ca3ef3211-goog Message-ID: <43e1c951125d6700586dbd332c2036db0f2f5f2d.1681430907.git.ackerleytng@google.com> Subject: [RFC PATCH 3/6] mm: mempolicy: Refactor out __mpol_set_shared_policy() 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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763108413461177727?= X-GMAIL-MSGID: =?utf-8?q?1763108413461177727?= Refactor out __mpol_set_shared_policy() to remove dependency on struct vm_area_struct, since only 2 parameters from struct vm_area_struct are used. __mpol_set_shared_policy() will be used in a later patch by restrictedmem_set_shared_policy(). Signed-off-by: Ackerley Tng --- include/linux/mempolicy.h | 2 ++ mm/mempolicy.c | 29 +++++++++++++++++++---------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/include/linux/mempolicy.h b/include/linux/mempolicy.h index d232de7cdc56..9a2a2dd95432 100644 --- a/include/linux/mempolicy.h +++ b/include/linux/mempolicy.h @@ -126,6 +126,8 @@ struct shared_policy { int vma_dup_policy(struct vm_area_struct *src, struct vm_area_struct *dst); 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); int mpol_set_shared_policy(struct shared_policy *info, struct vm_area_struct *vma, struct mempolicy *new); diff --git a/mm/mempolicy.c b/mm/mempolicy.c index a2655b626731..f3fa5494e4a8 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -2817,30 +2817,39 @@ void mpol_shared_policy_init(struct shared_policy *sp, struct mempolicy *mpol) } } -int mpol_set_shared_policy(struct shared_policy *info, - struct vm_area_struct *vma, struct mempolicy *npol) +int __mpol_set_shared_policy(struct shared_policy *info, struct mempolicy *mpol, + unsigned long pgoff_start, unsigned long npages) { int err; struct sp_node *new = NULL; - unsigned long sz = vma_pages(vma); + unsigned long pgoff_end = pgoff_start + npages; pr_debug("set_shared_policy %lx sz %lu %d %d %lx\n", - vma->vm_pgoff, - sz, npol ? npol->mode : -1, - npol ? npol->flags : -1, - npol ? nodes_addr(npol->nodes)[0] : NUMA_NO_NODE); + pgoff_start, npages, + mpol ? mpol->mode : -1, + mpol ? mpol->flags : -1, + mpol ? nodes_addr(mpol->nodes)[0] : NUMA_NO_NODE); - if (npol) { - new = sp_alloc(vma->vm_pgoff, vma->vm_pgoff + sz, npol); + if (mpol) { + new = sp_alloc(pgoff_start, pgoff_end, mpol); if (!new) return -ENOMEM; } - err = shared_policy_replace(info, vma->vm_pgoff, vma->vm_pgoff+sz, new); + + err = shared_policy_replace(info, pgoff_start, pgoff_end, new); + if (err && new) sp_free(new); + return err; } +int mpol_set_shared_policy(struct shared_policy *info, + struct vm_area_struct *vma, struct mempolicy *mpol) +{ + return __mpol_set_shared_policy(info, mpol, vma->vm_pgoff, vma_pages(vma)); +} + /* Free a backing policy store on inode delete. */ void mpol_free_shared_policy(struct shared_policy *p) { From patchwork Fri Apr 14 00:11:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 83147 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp32639vqo; Thu, 13 Apr 2023 17:26:57 -0700 (PDT) X-Google-Smtp-Source: AKy350Z7crsW4iz0Anb5Dn+QtqpQBfUIsZb3VyfULgluH1XQDgz9bl2RZLfOOHx+0cDXBkJXdP2u X-Received: by 2002:a17:90b:3807:b0:23d:31c3:c9a4 with SMTP id mq7-20020a17090b380700b0023d31c3c9a4mr3570120pjb.26.1681432016682; Thu, 13 Apr 2023 17:26:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681432016; cv=none; d=google.com; s=arc-20160816; b=PzT0FbgVVg9fqm5AtO9ibAbeoJ/o5bH37MdpAQ0/0xRz2hAQgp6R5gKvAAWNgXzMA0 6aAnnGzbZOKMyYEQMe0Gj/r+AIrG0nvay1IO/u67gl/uXMB/S9Bzg2e0E3nnhHx+8l5l hOC6+fVZ47q64ANOmsi6/EOPRwPCtSRRxPKSnYzgrJDE9cUup9kvA/HhlTBCtO0C8nXl C5xHMi02ECDK95+pbGbIQbCFCHD/p5rdf3oRzzm1ePzY/q50dA1zCQIKDPmxda6aU2qn NH8X/bw2CrqP8icZrCw9frxkGmJR4jNucFsy6Y4hpqg6YxRsvld7quvr0TI6oGS92Yve UH9g== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bg8-20020a17090b0d8800b00244a0640617si3059839pjb.138.2023.04.13.17.26.15; Thu, 13 Apr 2023 17:26:56 -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 S230460AbjDNAMN (ORCPT + 99 others); Thu, 13 Apr 2023 20:12:13 -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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763109258055977938?= X-GMAIL-MSGID: =?utf-8?q?1763109258055977938?= 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; +} From patchwork Fri Apr 14 00:11:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 83145 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp26918vqo; Thu, 13 Apr 2023 17:13:37 -0700 (PDT) X-Google-Smtp-Source: AKy350bKi0TjtRZvT3Ie2meXmX27adQJfaWouoDdMXaDUocVQKVnciWpuAdNUHcajJkHKenaX3Kz X-Received: by 2002:a17:903:187:b0:1a2:19c1:a952 with SMTP id z7-20020a170903018700b001a219c1a952mr1126099plg.61.1681431216870; Thu, 13 Apr 2023 17:13:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681431216; cv=none; d=google.com; s=arc-20160816; b=wDx11lboXO7BqOKbubyHoVpj799KcTi/O412wG/Xr89NnAL+AlU203JnRr3nLY2hdZ QOBoiS9freqExJDP3tY2KKnBqTaTP0VnS8/f5eVjkxG+7WZWzvpNu41bqPG3/Yx1qODT bM9cmgwLsB42mg67O7YyaVxsCq/CW9NsiPe7ifqWXe+lLenw/FiepiPzD84+iHSa7ARv 8of9kBSIsDIXY9ixRN8ZLwP3zBt0gpxCRsce6XIOlkpn7TPB4Co8tRQpnZX5MP4/CaTS qz5WuugQ44GvE6tRzuHSqfH3gMhXtHm+TFWw7NtRUY2uajYnwAjtsWscQ3fuhkf0Wo6p AyIQ== 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=Xv2Sbflv1PdMqZUyau/vGtw69NpiI05I8fyTNzzRBtE=; b=VOR+qmwezuyg/5kBpjTTLJRsWZsFyFlQUtTPFjFYxsVFZqafsDGH/CO8C6kCjRbtnF eIq/0SfDNX7AWgIP41gyKbydS00uwOY9i0l3VfHYzg6yPeqlv5L5A3/HxuZ+FzOWlMV3 V/DPgOK2OwfhJsCateL1kg9Zcx3xyMFaeTvZuxQc48k5+rsM6vq5b68KDxqtD/Q7sVsT Jhu+h0k2u80hQi8lnwuAp4sjmUXnew7MMYT+tLacCYgIdeSjDkNC5TiA/7ItdIMbq0JV TmsZ5BQJMp3AUt2zYckkNRdrNloGN5qiVrmsmdBGs8AD6TaAMU+02eKK1VDev7aqnft4 z5cQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=IeAvbh5L; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y2-20020a1709029b8200b001a63783159csi3036141plp.238.2023.04.13.17.13.24; Thu, 13 Apr 2023 17:13:36 -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=IeAvbh5L; 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 S230289AbjDNAMS (ORCPT + 99 others); Thu, 13 Apr 2023 20:12:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44246 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230420AbjDNAMK (ORCPT ); Thu, 13 Apr 2023 20:12:10 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4A0973C23 for ; Thu, 13 Apr 2023 17:12:08 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 18-20020a250b12000000b00b8f6cf4f5c9so753381ybl.10 for ; Thu, 13 Apr 2023 17:12:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1681431127; x=1684023127; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Xv2Sbflv1PdMqZUyau/vGtw69NpiI05I8fyTNzzRBtE=; b=IeAvbh5LiWqeMu3W4lreAV4pEYXDzFTMIK++YoYtL9HONMbDl2n8JG1cJvXTxQ4W/Z GxRYbHWg5mdzTkvevWjucHzalLOLaRJPEZSOdabNIZoeGxpZJ7sqGOpBbuMaxh+N9UO4 laslticySsTQ5Z1P33WcVcn5XKY3X/szRjrTAuUNdQOCywlR5EjataM835zXeQyYF84o sNh45HAi3xIJdUtIf7JG4FaHzE4kQ9wGOth27DrwXDDPKWE4i8uvOAxFPGUJWILtbFqJ B0wpIq7/vwtgTuDsvfcJaOD5/zbThG99OLcVYbtOP9o7ei2nNW7AfnDuSWHGLmDR7UAC GOjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681431127; x=1684023127; 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=Xv2Sbflv1PdMqZUyau/vGtw69NpiI05I8fyTNzzRBtE=; b=L8WV3GZB1ndpuF/uL9x2gN/uCJ1bVO4ymqkWiFUT1KPC27T9KSXkMr+yseRaCBCndr 4mlntiYJb7BOFHfA9vW6DBl5f+R8io4hYK9bJbFq+aj4IzD8w8uru2YOFpWRxajN5Nin fyaa31Uj6i4G9AHht28evlQBopFeo2vfBlOpDhGg/a2WFPVDaBlZ7i/L/X9WA7ozHamt HywMJUCXvnxvPiRCM7TtKsBW+mDjV8g1VhHH7zJaUvCTKavSYA2JxHt3AO26Yugmcta9 RhtSbh6+WSlYJDgh/mEbqt9wmlFvXmYebNPE0uFbFyYacL4W2bnUJf2ADccc0rvczxqR hrcw== X-Gm-Message-State: AAQBX9eZcZKIWMtSG+rjvGhDMJGNbSo1Y24+2qnmfeD6kbMmcCa/qgeo WXJGBG3a+gGDCeMk/x1GZZUSBLwt43UIFIOmDA== X-Received: from ackerleytng-cloudtop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1f5f]) (user=ackerleytng job=sendgmr) by 2002:a25:68cc:0:b0:a27:3ecc:ffe7 with SMTP id d195-20020a2568cc000000b00a273eccffe7mr5642963ybc.3.1681431127201; Thu, 13 Apr 2023 17:12:07 -0700 (PDT) Date: Fri, 14 Apr 2023 00:11:54 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.40.0.634.g4ca3ef3211-goog Message-ID: Subject: [RFC PATCH 5/6] mm: restrictedmem: Add memfd_restricted_bind() syscall 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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763108419731172638?= X-GMAIL-MSGID: =?utf-8?q?1763108419731172638?= memfd_restricted_bind() sets the NUMA memory policy, which consists of a policy mode and zero or more nodes, for an offset within a restrictedmem file with file descriptor fd and continuing for len bytes. This is intended to be like mbind() but specially for restrictedmem files, which cannot be mmap()ed into userspace and hence has no memory addresses that can be used with mbind(). Unlike mbind(), memfd_restricted_bind() will override any existing memory policy if a new memory policy is defined for the same ranges. For now, memfd_restricted_bind() does not perform migrations and no flags are supported. This syscall is specialised just for restrictedmem files because this functionality is not required by other files. Signed-off-by: Ackerley Tng --- arch/x86/entry/syscalls/syscall_32.tbl | 1 + arch/x86/entry/syscalls/syscall_64.tbl | 1 + include/linux/mempolicy.h | 2 +- include/linux/syscalls.h | 5 ++ include/uapi/asm-generic/unistd.h | 5 +- include/uapi/linux/mempolicy.h | 7 ++- kernel/sys_ni.c | 1 + mm/restrictedmem.c | 75 ++++++++++++++++++++++++++ scripts/checksyscalls.sh | 1 + 9 files changed, 95 insertions(+), 3 deletions(-) diff --git a/arch/x86/entry/syscalls/syscall_32.tbl b/arch/x86/entry/syscalls/syscall_32.tbl index dc70ba90247e..c94e9ce46cc3 100644 --- a/arch/x86/entry/syscalls/syscall_32.tbl +++ b/arch/x86/entry/syscalls/syscall_32.tbl @@ -456,3 +456,4 @@ 449 i386 futex_waitv sys_futex_waitv 450 i386 set_mempolicy_home_node sys_set_mempolicy_home_node 451 i386 memfd_restricted sys_memfd_restricted +452 i386 memfd_restricted_bind sys_memfd_restricted_bind diff --git a/arch/x86/entry/syscalls/syscall_64.tbl b/arch/x86/entry/syscalls/syscall_64.tbl index 06516abc8318..6bd86b45d63a 100644 --- a/arch/x86/entry/syscalls/syscall_64.tbl +++ b/arch/x86/entry/syscalls/syscall_64.tbl @@ -373,6 +373,7 @@ 449 common futex_waitv sys_futex_waitv 450 common set_mempolicy_home_node sys_set_mempolicy_home_node 451 common memfd_restricted sys_memfd_restricted +452 common memfd_restricted_bind sys_memfd_restricted_bind # # Due to a historical design error, certain syscalls are numbered differently diff --git a/include/linux/mempolicy.h b/include/linux/mempolicy.h index 15facd9de087..af62233df0c0 100644 --- a/include/linux/mempolicy.h +++ b/include/linux/mempolicy.h @@ -126,7 +126,7 @@ 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) + 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/include/linux/syscalls.h b/include/linux/syscalls.h index 660be0bf89d5..852b202d3837 100644 --- a/include/linux/syscalls.h +++ b/include/linux/syscalls.h @@ -1059,6 +1059,11 @@ asmlinkage long sys_set_mempolicy_home_node(unsigned long start, unsigned long l unsigned long home_node, unsigned long flags); asmlinkage long sys_memfd_restricted(unsigned int flags); +asmlinkage long sys_memfd_restricted_bind(int fd, struct file_range __user *range, + unsigned long mode, + const unsigned long __user *nmask, + unsigned long maxnode, + unsigned int flags); /* * Architecture-specific system calls diff --git a/include/uapi/asm-generic/unistd.h b/include/uapi/asm-generic/unistd.h index e2ea7cd964f8..b5a1385bb4a7 100644 --- a/include/uapi/asm-generic/unistd.h +++ b/include/uapi/asm-generic/unistd.h @@ -889,10 +889,13 @@ __SYSCALL(__NR_set_mempolicy_home_node, sys_set_mempolicy_home_node) #ifdef __ARCH_WANT_MEMFD_RESTRICTED #define __NR_memfd_restricted 451 __SYSCALL(__NR_memfd_restricted, sys_memfd_restricted) + +#define __NR_memfd_restricted_bind 452 +__SYSCALL(__NR_memfd_restricted_bind, sys_memfd_restricted_bind) #endif #undef __NR_syscalls -#define __NR_syscalls 452 +#define __NR_syscalls 453 /* * 32 bit systems traditionally used different diff --git a/include/uapi/linux/mempolicy.h b/include/uapi/linux/mempolicy.h index 046d0ccba4cd..979499abd253 100644 --- a/include/uapi/linux/mempolicy.h +++ b/include/uapi/linux/mempolicy.h @@ -6,9 +6,9 @@ #ifndef _UAPI_LINUX_MEMPOLICY_H #define _UAPI_LINUX_MEMPOLICY_H +#include #include - /* * Both the MPOL_* mempolicy mode and the MPOL_F_* optional mode flags are * passed by the user to either set_mempolicy() or mbind() in an 'int' actual. @@ -72,4 +72,9 @@ enum { #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */ #define RECLAIM_UNMAP (1<<2) /* Unmap pages during reclaim */ +struct file_range { + __kernel_loff_t offset; + __kernel_size_t len; +}; + #endif /* _UAPI_LINUX_MEMPOLICY_H */ diff --git a/kernel/sys_ni.c b/kernel/sys_ni.c index 7c4a32cbd2e7..db24d3fe6dc5 100644 --- a/kernel/sys_ni.c +++ b/kernel/sys_ni.c @@ -362,6 +362,7 @@ COND_SYSCALL(memfd_secret); /* memfd_restricted */ COND_SYSCALL(memfd_restricted); +COND_SYSCALL(memfd_restricted_bind); /* * Architecture specific weak syscall entries. diff --git a/mm/restrictedmem.c b/mm/restrictedmem.c index 55e99e6c09a1..9c249722c61b 100644 --- a/mm/restrictedmem.c +++ b/mm/restrictedmem.c @@ -1,4 +1,5 @@ // SPDX-License-Identifier: GPL-2.0 +#include #include "linux/sbitmap.h" #include #include @@ -359,3 +360,77 @@ int restrictedmem_get_page(struct file *file, pgoff_t offset, return 0; } EXPORT_SYMBOL_GPL(restrictedmem_get_page); + +static int restrictedmem_set_shared_policy( + struct file *file, loff_t start, size_t len, struct mempolicy *mpol) +{ + struct restrictedmem *rm; + unsigned long end; + + if (!PAGE_ALIGNED(start)) + return -EINVAL; + + len = PAGE_ALIGN(len); + end = start + len; + + if (end < start) + return -EINVAL; + if (end == start) + return 0; + + rm = file->f_mapping->private_data; + return __mpol_set_shared_policy(shmem_shared_policy(rm->memfd), mpol, + start >> PAGE_SHIFT, len >> PAGE_SHIFT); +} + +static long do_memfd_restricted_bind( + int fd, loff_t offset, size_t len, + unsigned long mode, const unsigned long __user *nmask, + unsigned long maxnode, unsigned int flags) +{ + long ret; + struct fd f; + struct mempolicy *mpol; + + /* None of the flags are supported */ + if (flags) + return -EINVAL; + + f = fdget_raw(fd); + if (!f.file) + return -EBADF; + + if (!file_is_restrictedmem(f.file)) + return -EINVAL; + + mpol = mpol_create(mode, nmask, maxnode); + if (IS_ERR(mpol)) { + ret = PTR_ERR(mpol); + goto out; + } + + ret = restrictedmem_set_shared_policy(f.file, offset, len, mpol); + + mpol_put(mpol); + +out: + fdput(f); + + return ret; +} + +SYSCALL_DEFINE6(memfd_restricted_bind, int, fd, struct file_range __user *, range, + unsigned long, mode, const unsigned long __user *, nmask, + unsigned long, maxnode, unsigned int, flags) +{ + loff_t offset; + size_t len; + + if (unlikely(get_user(offset, &range->offset))) + return -EFAULT; + if (unlikely(get_user(len, &range->len))) + return -EFAULT; + + return do_memfd_restricted_bind(fd, offset, len, mode, nmask, + maxnode, flags); +} diff --git a/scripts/checksyscalls.sh b/scripts/checksyscalls.sh index 3c4d2508226a..e253529cf1ec 100755 --- a/scripts/checksyscalls.sh +++ b/scripts/checksyscalls.sh @@ -46,6 +46,7 @@ cat << EOF #ifndef __ARCH_WANT_MEMFD_RESTRICTED #define __IGNORE_memfd_restricted +#define __IGNORE_memfd_restricted_bind #endif /* Missing flags argument */ From patchwork Fri Apr 14 00:11:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 83146 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp26926vqo; Thu, 13 Apr 2023 17:13:38 -0700 (PDT) X-Google-Smtp-Source: AKy350bHvwbGZs3s0FnL6rqGSpR7/1TT0PmtQH9bqH7NNtqiqwxWwPxBX1I/TpMyPawn5CZhpXT+ X-Received: by 2002:a17:902:c70c:b0:1a6:95c3:74a with SMTP id p12-20020a170902c70c00b001a695c3074amr674166plp.17.1681431218043; Thu, 13 Apr 2023 17:13:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681431218; cv=none; d=google.com; s=arc-20160816; b=Cai6xhWrArx9uqNSPjhhugk8yJpDqU8eVhViOdAKqcO+7oyjYTK9th4ziG19K/+/WZ fP+SlxnuGgroD/aYAW4BwUT52y4W04zCHmpwsOZ8yYX9dJm7JVgCg//Acz8FtigM0+9h 8BbbOzzXCO0YRBGxuyx+Jv20a8rhz44L0GdM+Bazil5th8MiQKbzac597ZEPaU8WSOO1 J/QjLVlUySYwOJIKxeYsh/LfcYcpymsslZDSy9C1moWH3L7Jf0Mr+tz/2T76C4d6Pk83 Dk1KfiKF6AC686qQlRbSVoU+ckwzPi4yAp4rPVby85SnK2IfMCGaEwORlXdct/U6eALQ wTdw== 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=JIdXLIzbqoaS1yDLNUT0qQstKE8tePtbIkE0dhqmXxw=; b=qrbHq64pPElHYhKTPFYCrpMzar4jCHrcUu42HmmJQVNVVrXS70paYWyUsFerl6AL6j sQ6yhBx1QrAlWs7nmwx7n+bx92qF7D0D9fBpKjirhU2yWsbx2rVdrFBHLZOCSCDNwJV5 YNSZhA9re8vgI5J9OQgL/1sk7AwCx9CNt6neWvFgQ+jWMPYafDlebB+0TOTSVq9gIuHu /OOJexxsqah+nSB7Ev2vaazYMs8gec9IgTY/p2JAUXJ+iBcVfhza/Fp1rTd8imgvQEjM CZFoABvhHpBH7GS15fvjfrQuoJ/hmrvgvr8DZhOGxHvyLenGHtkTs+TZgz1bWQGth6zZ 9/oA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=tYf5bMIh; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q16-20020a170902dad000b001a63bb1495asi3248502plx.589.2023.04.13.17.13.25; Thu, 13 Apr 2023 17:13:38 -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=tYf5bMIh; 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 S229954AbjDNAM0 (ORCPT + 99 others); Thu, 13 Apr 2023 20:12:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44416 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230474AbjDNAMO (ORCPT ); Thu, 13 Apr 2023 20:12:14 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 52A3E468D for ; Thu, 13 Apr 2023 17:12:10 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-54f8b46f399so77803157b3.10 for ; Thu, 13 Apr 2023 17:12:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1681431129; x=1684023129; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=JIdXLIzbqoaS1yDLNUT0qQstKE8tePtbIkE0dhqmXxw=; b=tYf5bMIhxkr3hMt+lIl1PwjzLoiSUpfQWFuv6YVcAfFgRdEoudCktx2A+d0JqBuvOp z7jDltQapqSgAAhCbia4ma/bs6+Elmvi2Gje0GcQy+s+haBTRtk+BaqUdfLeOeW36uBM 6MypyFUJxdUxMweXKlUl7CO/Q1AO0eUVAwfsLrKRHXW02ZCD/fodZ/BhJs3hNjpCIo+Y cIPpRQQptnvuiKd6Qf6tMjU2sO+NUQFKpX/Achfnm+Vek4M3QErvwjtnfJ6Clswfoz0b wIt6IvVXO9FiMRR1epVuwOCV/qP/1d3QqZG2bG2qG2X3Hmji5CKuZuuD4fO544jPiWvf +lTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681431129; x=1684023129; 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=JIdXLIzbqoaS1yDLNUT0qQstKE8tePtbIkE0dhqmXxw=; b=UV1tlR9SZgehKRGK4T91Fo5SriQaLgjR2hVgOzeqFuk86ppkv49ZdUFkL08gSItuzk eASYwo7RBj1UcWctec6fZhS1JApiJ4v4qA0Gujxip8W3Dj3vG7mwXv8dFH2xMBGwUQHY 9Tk6gRJ+1gymasqJe/RkXZ056OeMvSjF0Sa8GjugFkLh1jXYQ+mqtE5pnaNWtAvAnuqd R/2bNECdx5DTiSNPsvzu3oedmGMu4bPtuaW9loB+Vi8YFMC6o+odjI+kz+RkySYoaIy0 9oy3cuhjQljqEmAsHxMFONE3L/01por/2kh8vOh2e0y7tF2mb1q49rDjX/gGFcQIj0We 6Hvw== X-Gm-Message-State: AAQBX9eOuVU+pcwdS12WGgXfoRJo9HQ4wFDZGNocqmV//VYgEHkIlcXW mMUKrJw7iYtiJvY7yANkEcS7kdkg11a2qM3GTA== X-Received: from ackerleytng-cloudtop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1f5f]) (user=ackerleytng job=sendgmr) by 2002:a25:d288:0:b0:b75:3fd4:1b31 with SMTP id j130-20020a25d288000000b00b753fd41b31mr2693074ybg.1.1681431128801; Thu, 13 Apr 2023 17:12:08 -0700 (PDT) Date: Fri, 14 Apr 2023 00:11:55 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.40.0.634.g4ca3ef3211-goog Message-ID: <7b40fc4afa41e382d72f556399ed5e0808b969b5.1681430907.git.ackerleytng@google.com> Subject: [RFC PATCH 6/6] selftests: mm: Add selftest for memfd_restricted_bind() 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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763108420805890193?= X-GMAIL-MSGID: =?utf-8?q?1763108420805890193?= This selftest uses memfd_restricted_bind() to set the mempolicy for a restrictedmem file, and then checks that pages were indeed allocated according to that policy. Because restrictedmem pages are never mapped into userspace memory, the usual ways of checking which NUMA node the page was allocated on (e.g. /proc/pid/numa_maps) cannot be used. This selftest adds a small kernel module that overloads the ioctl syscall on /proc/restrictedmem to request a restrictedmem page and get the node it was allocated on. The page is freed within the ioctl handler. Signed-off-by: Ackerley Tng --- tools/testing/selftests/mm/.gitignore | 1 + tools/testing/selftests/mm/Makefile | 8 + .../selftests/mm/memfd_restricted_bind.c | 139 ++++++++++++++++++ .../mm/restrictedmem_testmod/Makefile | 21 +++ .../restrictedmem_testmod.c | 89 +++++++++++ tools/testing/selftests/mm/run_vmtests.sh | 6 + 6 files changed, 264 insertions(+) create mode 100644 tools/testing/selftests/mm/memfd_restricted_bind.c create mode 100644 tools/testing/selftests/mm/restrictedmem_testmod/Makefile create mode 100644 tools/testing/selftests/mm/restrictedmem_testmod/restrictedmem_testmod.c diff --git a/tools/testing/selftests/mm/.gitignore b/tools/testing/selftests/mm/.gitignore index fb6e4233374d..10c5701b9645 100644 --- a/tools/testing/selftests/mm/.gitignore +++ b/tools/testing/selftests/mm/.gitignore @@ -31,6 +31,7 @@ map_fixed_noreplace write_to_hugetlbfs hmm-tests memfd_restricted +memfd_restricted_bind memfd_secret soft-dirty split_huge_page_test diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile index 5ec338ea1fed..4a6cf922db45 100644 --- a/tools/testing/selftests/mm/Makefile +++ b/tools/testing/selftests/mm/Makefile @@ -46,6 +46,8 @@ TEST_GEN_FILES += map_fixed_noreplace TEST_GEN_FILES += map_hugetlb TEST_GEN_FILES += map_populate TEST_GEN_FILES += memfd_restricted +TEST_GEN_FILES += memfd_restricted_bind +TEST_GEN_FILES += restrictedmem_testmod.ko TEST_GEN_FILES += memfd_secret TEST_GEN_FILES += migration TEST_GEN_FILES += mlock-random-test @@ -171,6 +173,12 @@ $(OUTPUT)/ksm_tests: LDLIBS += -lnuma $(OUTPUT)/migration: LDLIBS += -lnuma +$(OUTPUT)/memfd_restricted_bind: LDLIBS += -lnuma +$(OUTPUT)/restrictedmem_testmod.ko: $(wildcard restrictedmem_testmod/Makefile restrictedmem_testmod/*.[ch]) + $(call msg,MOD,,$@) + $(Q)$(MAKE) -C restrictedmem_testmod + $(Q)cp restrictedmem_testmod/restrictedmem_testmod.ko $@ + local_config.mk local_config.h: check_config.sh /bin/sh ./check_config.sh $(CC) diff --git a/tools/testing/selftests/mm/memfd_restricted_bind.c b/tools/testing/selftests/mm/memfd_restricted_bind.c new file mode 100644 index 000000000000..64aa44c72d09 --- /dev/null +++ b/tools/testing/selftests/mm/memfd_restricted_bind.c @@ -0,0 +1,139 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest_harness.h" + +int memfd_restricted(int flags, int fd) +{ + return syscall(__NR_memfd_restricted, flags, fd); +} + +int memfd_restricted_bind( + int fd, loff_t offset, unsigned long len, unsigned long mode, + const unsigned long *nmask, unsigned long maxnode, unsigned int flags) +{ + struct file_range range = { + .offset = offset, + .len = len, + }; + + return syscall(__NR_memfd_restricted_bind, fd, &range, mode, nmask, maxnode, flags); +} + +int memfd_restricted_bind_node( + int fd, loff_t offset, unsigned long len, + unsigned long mode, int node, unsigned int flags) +{ + int ret; + struct bitmask *mask = numa_allocate_nodemask(); + + numa_bitmask_setbit(mask, node); + + ret = memfd_restricted_bind(fd, offset, len, mode, mask->maskp, mask->size, flags); + + numa_free_nodemask(mask); + + return ret; +} + +/** + * Allocates a page in restrictedmem_fd, reads the node that the page was + * allocated it and returns it. Returns -1 on error. + */ +int read_node(int restrictedmem_fd, unsigned long offset) +{ + int ret; + int fd; + + fd = open("/proc/restrictedmem", O_RDWR); + if (!fd) + return -ENOTSUP; + + ret = ioctl(fd, restrictedmem_fd, offset); + + close(fd); + + return ret; +} + +bool restrictedmem_testmod_loaded(void) +{ + struct stat buf; + + return stat("/proc/restrictedmem", &buf) == 0; +} + +FIXTURE(restrictedmem_file) +{ + int fd; + size_t page_size; +}; + +FIXTURE_SETUP(restrictedmem_file) +{ + int fd; + int ret; + struct stat stat; + + fd = memfd_restricted(0, -1); + ASSERT_GT(fd, 0); + +#define RESTRICTEDMEM_TEST_NPAGES 16 + ret = ftruncate(fd, getpagesize() * RESTRICTEDMEM_TEST_NPAGES); + ASSERT_EQ(ret, 0); + + ret = fstat(fd, &stat); + ASSERT_EQ(ret, 0); + + self->fd = fd; + self->page_size = stat.st_blksize; +}; + +FIXTURE_TEARDOWN(restrictedmem_file) +{ + int ret; + + ret = close(self->fd); + EXPECT_EQ(ret, 0); +} + +#define ASSERT_REQUIREMENTS() \ + do { \ + struct bitmask *mask = numa_get_membind(); \ + ASSERT_GT(numa_num_configured_nodes(), 1); \ + ASSERT_TRUE(numa_bitmask_isbitset(mask, 0)); \ + ASSERT_TRUE(numa_bitmask_isbitset(mask, 1)); \ + numa_bitmask_free(mask); \ + ASSERT_TRUE(restrictedmem_testmod_loaded()); \ + } while (0) + +TEST_F(restrictedmem_file, memfd_restricted_bind_works_as_expected) +{ + int ret; + int node; + int i; + int node_bindings[] = { 1, 0, 1, 0, 1, 1, 0, 1 }; + + ASSERT_REQUIREMENTS(); + + for (i = 0; i < ARRAY_SIZE(node_bindings); i++) { + ret = memfd_restricted_bind_node( + self->fd, i * self->page_size, self->page_size, + MPOL_BIND, node_bindings[i], 0); + ASSERT_EQ(ret, 0); + } + + for (i = 0; i < ARRAY_SIZE(node_bindings); i++) { + node = read_node(self->fd, i * self->page_size); + ASSERT_EQ(node, node_bindings[i]); + } +} + +TEST_HARNESS_MAIN diff --git a/tools/testing/selftests/mm/restrictedmem_testmod/Makefile b/tools/testing/selftests/mm/restrictedmem_testmod/Makefile new file mode 100644 index 000000000000..11b1d5d15e3c --- /dev/null +++ b/tools/testing/selftests/mm/restrictedmem_testmod/Makefile @@ -0,0 +1,21 @@ +# SPDX-License-Identifier: GPL-2.0-only + +RESTRICTEDMEM_TESTMOD_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST))))) +KDIR ?= $(abspath $(RESTRICTEDMEM_TESTMOD_DIR)/../../../../..) + +ifeq ($(V),1) +Q = +else +Q = @ +endif + +MODULES = restrictedmem_testmod.ko + +obj-m += restrictedmem_testmod.o +CFLAGS_restrictedmem_testmod.o = -I$(src) + +all: + +$(Q)make -C $(KDIR) M=$(RESTRICTEDMEM_TESTMOD_DIR) modules + +clean: + +$(Q)make -C $(KDIR) M=$(RESTRICTEDMEM_TESTMOD_DIR) clean diff --git a/tools/testing/selftests/mm/restrictedmem_testmod/restrictedmem_testmod.c b/tools/testing/selftests/mm/restrictedmem_testmod/restrictedmem_testmod.c new file mode 100644 index 000000000000..d35f55d26408 --- /dev/null +++ b/tools/testing/selftests/mm/restrictedmem_testmod/restrictedmem_testmod.c @@ -0,0 +1,89 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include "linux/printk.h" +#include "linux/types.h" +#include +#include +#include +#include +#include +#include +#include +#include + +MODULE_DESCRIPTION("A kernel module to support restrictedmem testing"); +MODULE_AUTHOR("ackerleytng@google.com"); +MODULE_LICENSE("GPL"); + +void dummy_op(struct restrictedmem_notifier *notifier, pgoff_t start, pgoff_t end) +{ +} + +static const struct restrictedmem_notifier_ops dummy_notifier_ops = { + .invalidate_start = dummy_op, + .invalidate_end = dummy_op, + .error = dummy_op, +}; + +static struct restrictedmem_notifier dummy_notifier = { + .ops = &dummy_notifier_ops, +}; + +static long restrictedmem_testmod_ioctl( + struct file *file, unsigned int cmd, unsigned long offset) +{ + long ret; + struct fd f; + struct page *page; + pgoff_t start = offset >> PAGE_SHIFT; + + f = fdget(cmd); + if (!f.file) + return -EBADF; + + ret = -EINVAL; + if (!file_is_restrictedmem(f.file)) + goto out; + + + ret = restrictedmem_bind(f.file, start, start + 1, &dummy_notifier, true); + if (ret) + goto out; + + ret = restrictedmem_get_page(f.file, (unsigned long)start, &page, NULL); + if (ret) + goto out; + + ret = page_to_nid(page); + + folio_put(page_folio(page)); + + restrictedmem_unbind(f.file, start, start + 1, &dummy_notifier); + +out: + fdput(f); + + return ret; +} + +static const struct proc_ops restrictedmem_testmod_ops = { + .proc_ioctl = restrictedmem_testmod_ioctl, +}; + +static struct proc_dir_entry *restrictedmem_testmod_entry; + +static int restrictedmem_testmod_init(void) +{ + restrictedmem_testmod_entry = proc_create( + "restrictedmem", 0660, NULL, &restrictedmem_testmod_ops); + + return 0; +} + +static void restrictedmem_testmod_exit(void) +{ + proc_remove(restrictedmem_testmod_entry); +} + +module_init(restrictedmem_testmod_init); +module_exit(restrictedmem_testmod_exit); diff --git a/tools/testing/selftests/mm/run_vmtests.sh b/tools/testing/selftests/mm/run_vmtests.sh index 53de84e3ec2c..bdc853d6afe4 100644 --- a/tools/testing/selftests/mm/run_vmtests.sh +++ b/tools/testing/selftests/mm/run_vmtests.sh @@ -40,6 +40,8 @@ separated by spaces: test memadvise(2) MADV_POPULATE_{READ,WRITE} options - memfd_restricted_ test memfd_restricted(2) +- memfd_restricted_bind + test memfd_restricted_bind(2) - memfd_secret test memfd_secret(2) - process_mrelease @@ -240,6 +242,10 @@ CATEGORY="madv_populate" run_test ./madv_populate CATEGORY="memfd_restricted" run_test ./memfd_restricted +test_selected "memfd_restricted_bind" && insmod ./restrictedmem_testmod.ko && \ + CATEGORY="memfd_restricted_bind" run_test ./memfd_restricted_bind && \ + rmmod restrictedmem_testmod > /dev/null + CATEGORY="memfd_secret" run_test ./memfd_secret # KSM MADV_MERGEABLE test with 10 identical pages