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: 13210785 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 704B7C77B6F for ; Fri, 14 Apr 2023 00:12:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DCFD46B0075; Thu, 13 Apr 2023 20:12:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D586E6B0078; Thu, 13 Apr 2023 20:12:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B5D2E900002; Thu, 13 Apr 2023 20:12:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id A5B2F6B0075 for ; Thu, 13 Apr 2023 20:12:03 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 76268C04C7 for ; Fri, 14 Apr 2023 00:12:03 +0000 (UTC) X-FDA: 80678068926.25.EBA9467 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) by imf08.hostedemail.com (Postfix) with ESMTP id A4B34160006 for ; Fri, 14 Apr 2023 00:12:01 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=S5LluL5N; spf=pass (imf08.hostedemail.com: domain of 3UJo4ZAsKCI8tv3xA4xHC6zz77z4x.v75416DG-553Etv3.7Az@flex--ackerleytng.bounces.google.com designates 209.85.215.201 as permitted sender) smtp.mailfrom=3UJo4ZAsKCI8tv3xA4xHC6zz77z4x.v75416DG-553Etv3.7Az@flex--ackerleytng.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681431121; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=LGz+M9nFtzHbGGbLaqDB42+XLCRk5tnoOBaeHE6w0dg=; b=0pWzozyYPVT7cu6rgiWgAt3QYzSxd4Ii02xI1n/BSkuhLX9B1HRwby2/a2OSUKBx/dWS0g fsiVx97cFfdAjwxFsdKzWu5Av0247ZQoNYKlTXvwTBIqTsmd8MRw0WAyMxY3cL4NfjnZ8l aak5cH6qjFrDV8z8Prf0LyM3FRXmw3s= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=S5LluL5N; spf=pass (imf08.hostedemail.com: domain of 3UJo4ZAsKCI8tv3xA4xHC6zz77z4x.v75416DG-553Etv3.7Az@flex--ackerleytng.bounces.google.com designates 209.85.215.201 as permitted sender) smtp.mailfrom=3UJo4ZAsKCI8tv3xA4xHC6zz77z4x.v75416DG-553Etv3.7Az@flex--ackerleytng.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681431121; a=rsa-sha256; cv=none; b=GV0MMnSBG1YqRbFITJ+X3kXFMe8eLkQvtKvPLRXXARCPXJzHD1/dmR67MNsy+xzA4JzEUK DppnHQA9leDaJ21AlxcuVzAeUEeIE3g3znFaykKEN0YnyJdDLARBhy0mDnMnJuEnBr3F9s cq4Yvz5pCJQKsXI0NphE1DjvWp5PsaM= Received: by mail-pg1-f201.google.com with SMTP id g129-20020a636b87000000b005072b17a298so7104305pgc.14 for ; Thu, 13 Apr 2023 17:12:01 -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=IRxkFMuc1JpHhwEaWtLm0flqxYswm8NhG8WqMS9JyNqcWT7vPWIN0ZgpihntdGNbKR rYcSoQl8Q0NmHEfucFFWuFUVlEeIFrlNziyOUKFF0tBQ+lzqN5QZ1eDkMffFbWIXv1Do i++OfwN00bOA+yDY+ysmqypBzajlFFtx8tSxzbkUF8Gb863iq88RIyhuQIJOJ81rWeOG pH7rR65Zv0RFDy+qptAxE0UF3FMp7SPxsC9IVTtvKlHUqzUi3lLlz9L3RGC3SZ6aWETh KZz8qy3JZWT5Kt1DQJgSuboha9G9dEvRzEeDa4/ZR9oCJvWxOkbnCJ/7tV2nQ67Idtqm /ebg== X-Gm-Message-State: AAQBX9cvmFnAyxDpx3D+7rTa5CzoAucf/Tcvn+RfIlB8DBl2I+T/e2h8 YS3BmXtJnsFp3KuFzAMCmjx3dhEMB8A1z3ruCQ== X-Google-Smtp-Source: AKy350YIYIzzkTKMTgAPyGCMhA2HHi565MHAj4RU3xofJGGDi/ggUb7Aj3J6+1abX8zL+AMGXGGHN+6shLHAP2f2mA== 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-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: A4B34160006 X-Stat-Signature: ubjpsgwi8qnrg8dyw7z4jo5ub4idir1b X-Rspam-User: X-HE-Tag: 1681431121-908693 X-HE-Meta: U2FsdGVkX19fFVtaD7p+riuUp5LKuTuSAQVSu1OEX2it21JV+dCUok2O4WK22YX3v3JqishxcIHzD568RIP2XJKFJ8a7GbCGaPhcyyEH4NDPLt3YzXtt/ez4SiAtQzEle/jh7Y+/KYyJRkqOHuO4h2LIgG8VmqSsoX+pAvg9UqlBViq3YwWocFeWNWVYd9FkJFmCH3BwkPaK+KYsw38qmutXRLlODNxloT5nrGGNhzGrO2nSZDv1uHKrQDwLcnFfyPrKZPLMDO34kId5J7KJ0qPTL2TKN43EtdDpm7YeDf4+FZtCFjBU+c6bM/bBTyBh+GOOZM3g2QVxim9KI11dmiNwM/JGMvoHhfY8ASPQPsWQToJj3k1upJOAMNh2zMGZA76RxFoPtw6Cjo0g9I/MKzCtww0ieFWjwdEGMmCNlPL/nxRl79TMWa3gG1fFZsYTFBx/XJT7VDLRXVj5TFIHN5P/gTkSkzqrBlb9YB/ewsj2Nv/D4HxkcMgHEiR4vFcLur86K4Dyb7URLgN6/OinXEXv6nGhyJqpGEpD/aBt7Azzocc+sJWg41Ium83xN0l9mc4Y4w9MQHFit5c4I68QtZ3f5eEJFaNJUg35JZ2B/D4VgKyPSu12MKcDkd3IXMOG/Rdcu5+tk3IsNJA6NBXUFbadbBn7K2x1iVUyIfRdCxVyZYuARMCeyr6e3EgvtB6jyuK9qYB9sCyC56jhU1aU9XI4K/MkP9auqcInkcdtxkz+uETKLrNbO8ZeI8YkHBkKcoln3PnQtOZw5c+hMXQHkDN4EpSMmzb1KEz9QGKp41Nc+hQ1U7X4xR7djFHU5GOdXVKaGDtv+9bYG71yq92UbA+fP/Cvp5Ok+1SfcS0jAuThwjLlwXYo9FvtDeg9D3XoYxj0rSvOVkZtPcu18wXnWGCxMRc+YloHDtnYoCdkk/o/XgBAuyc9VUphcTg/gJteoRbaRVgyZZ24auE2VOJ ah/FJY/N xEyMnZstQ+d4lJ8//rGp4huo492Lu8NFDX+18Rp56jrN30CG8eGCzvwDjyJAxckisaHKbD9q5NPkKYZMJ/ixDe4bg+afeiT++TNyy7Mj/INIPZhRFkoeDN0UrPh/ovt8vZ/gbSSO7fIDM8ny0YiyX9avwe4gs9m2mhedSR+hLt6qI4doHIQpCOFvA6r3//CW1Q98mX+BI3wl1/LmQxpsY4gfhAtfb9KydkX0CCeujwKjyPYF5YYE0TfgFZNOIS5KaF7ld0fgUXr5jBEmQDv0USpreQmQfbNLt8AwOnEbnMQK9zII7lqIHwV0DjeZdYFQLuavJZGA+B/twP7saz/rqfIzftlKDMmz2HF4aL3Rpo4AeJIYn7Emt1lORkOPlyF2uCa5bPhz4g47qKYx4Au/tZPmeiW2BHvGw8kKPFYtnm6sUKEQzqbDK0isoX05g5mjIrMRROPDipMLXXS8g8iUVAqHXiiyGmMiCZq5aVEf2IW0IZUd4NNG4sJEqM600o1Pk424bhJS7y0BQe5mzHOPfjssUpytFQkDDroxHJK9glsVrxRWWA+i2wH2S2ouzvogxfLqJ7R9Gy859O4UMeNNB/rppICobqBXDauLsKDG98Sjww5Sg8NoDbHHRksq4AutC5jpFUKfm6DhVgvzPv5YzQwD4zmRlIEcc7D5sNcMaJBbJ1dgGSGuIsGG0UFJoDfYU34Fo508TQ5ppfMU= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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: 13210784 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id CA270C77B78 for ; Fri, 14 Apr 2023 00:12:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 656076B0078; Thu, 13 Apr 2023 20:12:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5E0BB6B007B; Thu, 13 Apr 2023 20:12:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3E338900002; Thu, 13 Apr 2023 20:12:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 2BBD06B0078 for ; Thu, 13 Apr 2023 20:12:05 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id EDC441404EF for ; Fri, 14 Apr 2023 00:12:04 +0000 (UTC) X-FDA: 80678068968.19.2BE0436 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) by imf16.hostedemail.com (Postfix) with ESMTP id 2D52E180010 for ; Fri, 14 Apr 2023 00:12:02 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=hEKe7KJh; spf=pass (imf16.hostedemail.com: domain of 3Upo4ZAsKCJEvx5zC6zJE8119916z.x97638FI-775Gvx5.9C1@flex--ackerleytng.bounces.google.com designates 209.85.215.201 as permitted sender) smtp.mailfrom=3Upo4ZAsKCJEvx5zC6zJE8119916z.x97638FI-775Gvx5.9C1@flex--ackerleytng.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681431123; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=BkEj6q2IVKaYOP6wiiRLnj5ts2mBBZa+OIxL/GSTf98=; b=1Fojur9VC4V/UcWBsrPhvygo60IUQq3G8mU6TYLbAXIqkRA59l+P6cS1p0esmGPb7+hXWk C1kLo790KmjcfSMSgs3U3xme0EnZxBexC5FwNIyHpUxpE86pCHjg4dh/O9MM0eE0ZiY3E8 OtItGRdX1gVRFopQA4xFE/IJCp2hb8Q= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=hEKe7KJh; spf=pass (imf16.hostedemail.com: domain of 3Upo4ZAsKCJEvx5zC6zJE8119916z.x97638FI-775Gvx5.9C1@flex--ackerleytng.bounces.google.com designates 209.85.215.201 as permitted sender) smtp.mailfrom=3Upo4ZAsKCJEvx5zC6zJE8119916z.x97638FI-775Gvx5.9C1@flex--ackerleytng.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681431123; a=rsa-sha256; cv=none; b=ALAbtx4BjMifRaa7yidVog+m00t3Cttww4MxTIK71so1TixL/Gb6BX39iC4pg3hayL4Ybs tdNy7iPl6dqg3PoxTv+SEXrpQ3iX5lLSt5oLwGNWJMv1Sm0LCwAX7FHyPPO7FVYSGH8hg5 0gAyFZRWTUAaubGVu46+xFrlr+nTwow= Received: by mail-pg1-f201.google.com with SMTP id k191-20020a6384c8000000b0051b6cf91f74so199505pgd.2 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=QUUP3feN6fJblU7zZD1RWQvH2yFblYs9MzvvI+8bOXMND94I0D+3RPw5oK4kyxZp8s lFTwbBpCaXe5cD52jtDOi+/Hu9in9x7gGRW+1hEJZpTeunuq2d0Hz6rUe8v2S2ItzAdQ M7u5bqFZzdYRr2IP3pafGaCRje9JFLHKVxtRZSbIXUYFU2/6wmD0gA+qo2pjXqJ8IgJr MzwHPkOF9WulnTb2EV++RFG4ej65cyKvAhHn1tddL331cMti1d5FO9cvFN1pV1w1RUnJ AASLx08pAPRmec7X6PNwDK/P0vXMhPxm4MNRljWBhMP46W96wkIFC7zhRHkakh0I/xju dDHw== X-Gm-Message-State: AAQBX9cWs9Y9KsPNBidqwn0w4nzkSbvEKTuVAE8RgXnUSOTvbzLK3HXg ma0eLPP7vRt9+J5jCvvs3HvAI+STjt/PYbtjjg== X-Google-Smtp-Source: AKy350avtllPlWwvIfTF15qlG1lqqPqwu/NAHA4tctgmwlguwviMhGUhrJ6084VLBRdQnA8mKuN5Lp3jk9IuHlWLyQ== 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-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 2D52E180010 X-Rspam-User: X-Stat-Signature: g5c63ig3eg719cmgy6wwf8w51c1df5dp X-HE-Tag: 1681431122-938880 X-HE-Meta: U2FsdGVkX18IiDhzPCK30EQaZfLt73Y9BgUtAUsvHQhrvSaJ2z9D3FupdPTwvVze9nDjk8zxyywDrYkDqeGBNZyMIMbmLQWNyb3q83Y3VuWYx5yDuXUqwuwusIzAI/Zs+I2JWhGDa4UHzsb5bVPiCAzG3F44oUtZH78s8KyHbOywcwSIWf2LLRdMu+5g8471IXJnlFuZZua0g9rvPGWplErFXFC1yFhZnwly45VaafWq7Yj7+fxHaUcVSFyHt9nkHSE6+RdM4Vgew180h2/RZJRs+XQgs1nSYxNOTbUx459FrvKzsJcdqu8P/ZkCnYD6UaSJlShYmOP4blHu0fc1NFxwpn9vPvSKFO+z/HqusZ+qk71sj0z4nRyoSZcsKtSFvhT0jilHp1P8Rwb2vFguZjruiA5ObbgwRrYtZdaJaijcRxDMGNZDZGb2qnzqWfzfIt04H6Huyj9hxCA2ZThGBVZI8CV2r7gQAg4NL6lt4GuBE4WPonrbXjGkbjf9J64OwdbRJaQRTc91r1bsbNepXnBLyQh8nrOybbzOxtOChDRneMLoQMEUl9Y6SMy7JKsnaxDHhyki0JlHQAdIP94BJ8CZloPan8ZX6gy2wW/zN5Dah8FCcDOZgClVMlRpf28fUs6PntqS9S67d8hJLkiDZRDBmAXB+eopAAIP382c8AZocvDGlEKFaElhVbXN9nnCmKG6AGEJpbuLfYQ6RTpc30giPlEOsIhWvMMxMgFz6yeMFRO34esPaa/NZI1rQqdjlYBbsQ8pOv5Jf42HTb/nQraarUXF7To9//UP45zQYYo5pS0bzq6lAnLz/vW68dWh8xgTZv37K2F7qbxE1tLKcBvmk47S9Nuaw+xEAF2m0xiF4ZMIBuFFGLo+Fl2now8IUAWXJMPuUBFFpf0HdWaOi3RVsUkCTvgPerDnwKd7DY7dXKeoeGsgChXqwAWmNJKTfYHdIJ4TomaICq1s1Wl 2dJmLuXN TyP1/gyT3478TTOqJ/zf6FHekVj8gsT9issg4ayqCBKINmxuUu7wocxEm/DFVtmsgMGzYVwGU6NCz9X23MIrKHBSRWadaHkpFOuxFS5yYoBCI0rQ3g5jvSGS072KhP7xIZfNP4SzY5UNLwzUB2mA1eJqgyPnvuElJnOx8VdUlhSwK0Eyat/mLeCzRTkIhOzxRNqmh8UE+cOzA6mBxKWNsvSLKPxoqjyqmUihHDBBGqhM5fDaOcHTDEzaS1GnujAqcHREkWEb0PxndehJConhXn8AddC8HcInrXo6T1+543+Nnz/63ubnEytk+WY6w9x1ipxdMOVM+fJCQneNIH93bBRUvxuzFsjxsRRhqLqWz707K+7pc8CrDONrfBNRZsYw3hPpLkeHGmszJHmipEHzULrnlUsWuAaGCgJRdlW6w2c3gOHDOyvYM9pYOYRIeLsEKCTHvIoQXnz/ShpTkXBgTio04A/gE0Xx+P2k7LhaGG3LKuYeZ+drApBATKcQ1Z5iphB3iTUwg9jZCTTQDKkapn/i0b+8afCPuzH2/ZjyeQHEC9rfio83oqNoMCzIVk4wfSRylzAS/QOdSGF32a7oRpA5LvU2v49B178GqpsPH8MZ6+yjhxu5gkSLhsGeafOEKinqGPQAMraX5ab579MFvsdVFE9zUGfR0AGVivBfBM0xkT0pascCysgTnMAYkTC8T0PrKzf9+yT/PTk4= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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: 13210786 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id AF4AFC77B7E for ; Fri, 14 Apr 2023 00:12:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E1F306B007B; Thu, 13 Apr 2023 20:12:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DA7756B007D; Thu, 13 Apr 2023 20:12:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BD37E900002; Thu, 13 Apr 2023 20:12:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id A98306B007B for ; Thu, 13 Apr 2023 20:12:06 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 6706E1604EB for ; Fri, 14 Apr 2023 00:12:06 +0000 (UTC) X-FDA: 80678069052.06.3886E48 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf24.hostedemail.com (Postfix) with ESMTP id A489B18000D for ; Fri, 14 Apr 2023 00:12:04 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=kiDTePLk; spf=pass (imf24.hostedemail.com: domain of 3U5o4ZAsKCJIwy60D70KF922AA270.yA8749GJ-886Hwy6.AD2@flex--ackerleytng.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3U5o4ZAsKCJIwy60D70KF922AA270.yA8749GJ-886Hwy6.AD2@flex--ackerleytng.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681431124; a=rsa-sha256; cv=none; b=Q8S9RgN2LJ8FNAtF3g3b5YjIa+ctNVptjJyvsxso4F0zS4DiR4N469aQKrLUJLC5WGe4CC CkWR7+byaob8WnVY09pIeUSrO3rkK0k19vZ3DTOIKFc+UV0tNWcM48FBsreJutSD+kblQ1 RW/6YLptUcvkbzHEfhOuKLk0FaySnq4= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=kiDTePLk; spf=pass (imf24.hostedemail.com: domain of 3U5o4ZAsKCJIwy60D70KF922AA270.yA8749GJ-886Hwy6.AD2@flex--ackerleytng.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3U5o4ZAsKCJIwy60D70KF922AA270.yA8749GJ-886Hwy6.AD2@flex--ackerleytng.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681431124; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Gsr9n7GPJt3nQiq4GcQ+TdB+rTZDJHWVuXMgSAc/inc=; b=oDWV5elwJlT9TlfuxoUhR5iyrSFYLpPy64GiMWoQQrLaPaF1VFCX7x2xFXC/fAFg8Tt7GG XojG6lf+vLJZYH8cnra8+RdYD9lHfodvRQZS0NyqNV6Q3D58xnbJWPmU51CMSDJIyV9PcI g1sq0rpTDKvfvqWxTkrl6gIz0C8FP9c= Received: by mail-yb1-f202.google.com with SMTP id 132-20020a250c8a000000b00b8f4e12dd57so5572387ybm.1 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=bEBRyKwhzHjISa9kEbaiDkyRB4Es5QSUE0NQudNr5IWONFOinQ/ncS29kvH5RyZbm7 Q3M0wRT1vUxyfg8Nvvyorxf6ZAT8egOw6XkqhbTKDrH7Mc+HLTv+k4qWD/rVceOpsqdg 88ySUtKis9xBZ57QPliKKQ4ecwFSleVYDumfjCrlPjcC2biPvbD8/pOYxG9RoF8QxpTT K3vLxWe1c8J1AJhVjWLXR19G4/d65UaLq/nNDjUWAGotIxciYtsXnNQUBHOtE6o0cHIB xfSrXvJGpO/jWcpggQdxdQwefd6vNchBWXiNzkTSQ0lcLF1LIe8fqcz6TKe91rgmQrmH /i4g== X-Gm-Message-State: AAQBX9eL8zDOtrvkUAVgOCwFd2tmeZ4j/1HzkoWFDiewsBuPHBRbPXGM P38q9+jeCCHTK6DNeDD1ELGHNy9EgYAvWBazMQ== X-Google-Smtp-Source: AKy350aRre2/mW24vntSSS5eHQnqj/lI3imd9ledXWOeS14bBFvJmCzkihwUza6OXJc/lSYwICNxvky6ENjOvWGfVA== 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-Rspam-User: X-Rspamd-Queue-Id: A489B18000D X-Rspamd-Server: rspam01 X-Stat-Signature: 98ccgwacet73k6n7u9ropyu8mjabmgng X-HE-Tag: 1681431124-532604 X-HE-Meta: U2FsdGVkX1+YK6nDxZuULyUme2dPygtV474j4UO7/wdMAwi+nQBMJEA7p9LnxnK/XtertqqNZ+XvviruSUbm34eFdxCUFiLTvwyrNZseJK2khxNy/m/20vvy/vv+LYoAeOWIxd546Zi6I04YTrLKuoLKgE2DPnVbBVANxMeBqa2f21ERSNFIco0yGJZpFP52Zc8quTHBG3RXqbGTkA2j412tkqxSvdw2O6QuV08sKDd+Vk0G3ON97TMYYPjzQ+Z6matK30m1Ddvx1mgXTOaXIijLOLk5KUX8z6ynPJCxxyrqNpNuH01Q3EeFuAiGyVMY9u8CYgtGV2+rDktWRsb4oqe3Fz/1iL+ORc/6t6jMXim07FWfvXEmcBTw0RLdMi7AshUk3LnCh+S4D7DMnZX8doTCJpe95EznU+R2i+IdFcrqp4mBtPoPNv1HRBlje87Zz0TuLwSkYObKSkrEJKF6NLbxonrMJk4/MFVvuvXtM/JG44USfXOVveCNuGB2mSp9rBcfGlDUzwrVZS+mpH8E93/dDUvcPdhCzb7FkXo6W/2HrO+PUgeQeW+SFI+ZSBYAjWIOFtr1QNOJIJlh/xIPOAIX9RNlzQiHZJkP2RdIqXFYuLbYbwbSWDZSFoASeFHl1MZgVIL+FxGOjwtEmtGTt2KVvSqE2djh9vQWBe7CP/k9LNoAgfAIno9qzSKvSYezPst8L9kyOyPnqJcs9I5zXBcFdM9rIqObMlZeiELByMNWQJOaPVceEqAJIHv0x4/D1Omug7QOkAdFoNRRAKKEQbJm9/j/0l4AFKobYL3Cem5ytaVyZe6XZrGul+bx+C/6Z2PjLhr/dAIk7rnSOLZ4DJElHjIri5SPaG96E8ccqRWPOUq2WxGQLRRzxgCJLGtvbe6yIsGEIiIM9vnmjJlEpEvWrFcdrj1W+kfb+mJN2F2AEMTDwC9kMJWyDoq8GFTWk5eCO9iM291WICrAD3H XRZRsXhE DlyAVoidW/kofN50P55UR8h8DKFV8AITh0eNiHupnD6ZD3RQMXPAzyVYwDAkwQh17XiL3bi/XQ2aleVbNsRBYv7Y8D/bXAvme/Y6ntivOBlm+bPpKTQxJSbVpppkbQhFqeHIdOJu6ALXTYJkv6KCoeYo7TBvr0uzlCj/7IYD+6Zxj+PLdo03w9vUtQQ1M6c5+tWO+3PfHH9eHt0g+isUVOY2dPkjUBXQUp6MyWR34d5c9EHSFDMog1Kr5N0kAs5NRZDgGQWD6I0pO3v6Q7wxVEZTVHp6vXak9UfTacKz7DMwk4fcsGZWdmc1wuKBmGktpaWrOa/+7AvyFqLOdoAhGXOJ5VPWiqEqChEaL8rdU7ecagu2tLb/kmPzTJw5TX7gfc7wB/V6/ZbG76mS7TXwJxIT/gR6wgdq7I8BokGlvJTMKFlUSHVwBwFRfojg97H2cfBMZW+Z5EM/pG/CSEch/JUcNHZYuGzgk5uFhdQh0mwhwbjk2P7/4l9/ivbr0lGPOttvvNPl4eTqrwyCE+INfq7jc//y2salydFUiQ4yARvL/HupQW6ZP/0rxXbiq44J4khj6Ns18pTUXbOBRv6KnwMXmTaIImzpY4d5hQwSds5ZwwOrJDO1OhiIGGIOLGgF9HIXF6PPymrQGrc/cKEVpUMt8M3rxlTShmM+BW8541YTvfp/zfMy+u4b15on71cPnOTEJ7OFcRPni+nc= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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: 13210787 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 52B27C7EE20 for ; Fri, 14 Apr 2023 00:12:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D33E46B007D; Thu, 13 Apr 2023 20:12:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C97F2900002; Thu, 13 Apr 2023 20:12:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A73F76B0080; Thu, 13 Apr 2023 20:12:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 867796B007D for ; Thu, 13 Apr 2023 20:12:08 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 490A9804CD for ; Fri, 14 Apr 2023 00:12:08 +0000 (UTC) X-FDA: 80678069136.22.1944685 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf14.hostedemail.com (Postfix) with ESMTP id 8A507100003 for ; Fri, 14 Apr 2023 00:12:06 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=mW9eVt20; spf=pass (imf14.hostedemail.com: domain of 3VZo4ZAsKCJQy082F92MHB44CC492.0CA96BIL-AA8Jy08.CF4@flex--ackerleytng.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3VZo4ZAsKCJQy082F92MHB44CC492.0CA96BIL-AA8Jy08.CF4@flex--ackerleytng.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681431126; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=W+vIzhvgnDTeM5yLC6D6DMUdvSTbR9axXaSgRhroXf8=; b=D8XcP61W1qlW2aZ+wFzeo36nBfNeaLxSJ3q+yIgSgoHWkhLSnlTgGpz8c8Wp1im1hmtjmW yhq2nE9EZsMNF2d0mrj3ze9rahL0M8xc24Pw7rMrYbZQyiA2jbAsufumYdo5NWH80Ojvm9 4pumL6qf3T7ObeELKb8VIGf0qA+d8Rw= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=mW9eVt20; spf=pass (imf14.hostedemail.com: domain of 3VZo4ZAsKCJQy082F92MHB44CC492.0CA96BIL-AA8Jy08.CF4@flex--ackerleytng.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3VZo4ZAsKCJQy082F92MHB44CC492.0CA96BIL-AA8Jy08.CF4@flex--ackerleytng.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681431126; a=rsa-sha256; cv=none; b=K92eecyirJgWoQmZ//FgolVdfNvZ/XY7+aBmNw6K8CEy8U7ZAr3K/aLj7588DVTr8fVs9k TAZmcTkkeXuoW3tUh1DaAq8tl7JhgzcMPLccl8kMqb41R0wgcsBkfnOmcblMoqBS+LJ+aA lrWZarQ2L2coh4GCuEW2zD4CNIIUMoY= Received: by mail-yb1-f201.google.com with SMTP id 85-20020a250d58000000b00b8f380b2bccso7597087ybn.14 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=NgRXytijMCd6gUpV3j70UU1JdYoqDogH1MEAQjkxU3ON7lg1LWjGf6LO3t6r5ulhA+ LwxjshyM0S90sfVqyzsBcfT3FFyzjOj4MHbIyRB8YZRFlULxFyOxu1OKFtJ6+1CH6OqJ zTbLstR9QHST87EuQ01UI8wMUmQ5uXWYjfFON/igNVMftkGKzMzZlb/uoMFNrTU1IcnC VrRgTlygg7aVJfnBpTsrMq3Usjt/gR+XfbrmMnbQ6aA3Deyos8pxYGgS2QByyNNuMYB/ ArH/NXqTQBBJ9zo/sNDdx4sIxQMS4b6BQrlUyVFEum720QW7bLpLZcXOKc+kq72uiobt ZNjg== X-Gm-Message-State: AAQBX9cSnXWWTmn9biGtc1h10b1euT6giF8jHGERaL1FaxTP7dqY7oOF F/sLMdP32H2KaZ0T8c6/+0ygXxIWPbCOskD5tw== X-Google-Smtp-Source: AKy350b34CBn9n2raOJ+zF+ClQSsfsH/2Lr8AcQiejmlqBPjPibu2aqY4QIJU2kHR7dXtCx3CNEm4OqoAAxVmXTquQ== 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-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 8A507100003 X-Rspam-User: X-Stat-Signature: ckw41gb4c7xignen4zfjri6ppjk5jhk5 X-HE-Tag: 1681431126-523512 X-HE-Meta: U2FsdGVkX1+F1s9KIHc2ygcGyya9W+PF/SpFLlM/w3nxSFaCkgSDzKnyAhRkUhQUVK7vEUO/BtDNY8aLfz19nSe4QacdzQV7sBeTqJTLITjVhgx3wgbClw2rXJUXOdUmv/iDQzbly2mKNeDBiGops0Trln6oCsVoqzDjRQaia5Iq9DOOA6lgXHoEdSEA8B9AhL08SAj0DU9e8ceHrNNo588NOYe2/xH+h7cPvB9vQEX4escMz3tL0ql8oVuN2nH2YoN55Jr5myD+FN7YLo6xeLoZpQ/Kyu+RUz6K4tjOJWgUDUUS7OtnHblYfODm1wI2Sz+4LBNHo33qkC/mrTzXBQ837JI+Ms51xbcb2/68OoKwDLr8JlW0S8juGJMxZ82W1h3FgvzoTillng1jNPrNoa8xlFc4ablPDfRLq27rfN1RhrSfMnvs/MyzS8KOoYdN+MF+8LsJlNH6R7k0vrYNsVodVl5xb15kjlJtK5RKwHVIedGtNgqBGKJMgubkEfHx6mmlii1tDQn2jKpHPMxnYuuU5Yn0JAAv9KFr9Nz1B9WoJGMAZgUiwxJqd5+2bBMkLVDN0p6S3hBPWthyTbT45YfuZHlTre+sSZXx0Dj7CFc+WTxt7bnb6fwDyeF5gH6vB1BZ0AEKde2bKC+zCu5tAOcrIIuJFQI/jVvjj87dimY2CUCX/vHevPeO3ODK4Bwrm152KvQll3UzCduXgCe2X4Tt6neSw66F58Kw09oSfuhSt+A9w2sMW6VKsZQvio6xTt8PAAKWwtULnRxKMK1k1vsIR8PnLYt2MUv2yRo7dHxrcmSgLXPkL/HxzUWAiq4jjrVGwdWmInhdfwZjo9m3NG23u3k3ucHC51QwNK4TancUHV+A7t/dmEQFfj3WDxn6T0OXrmGzr2hjJJVa+B1qtl1AKv6UKlN08OABBXI1YYL0L5/CUeYwL9mFs70r1mRWQuu5Z3hbESQhHWk37Jn mMLY/rRh i42jejmbenEmjiBmchGfBgP/E+HVt+IpSzZ6xN2A96cSGn6ZJZXzwlyGttdNkk++KabURtSoH+XlTZX6Yvf6jh4nyU/qiTfVaKBUkvLA0wj0XGX0OG6MhfG4Oy6wm51o7wecCa6Iol2EbANX5kIIFrl49IKVMKciJEZ8IQ3LthE8WWr2ykLTQJreVGprakgvsPNWlAaQXY9a+Iq6qEJOWrvS6AOuUGlOP6WfLlcwYsYSPGF/8hmkMKAVTtmFMOzrocicY+LVxQZ0U8RoNYsAOs/ZK09sI+zRNi0G4x7Du9maLuX6obfZc1MWwqDJYhtZhjFq26M9sqNg4W0cQkrYM9lTs7MlLrnOajw33LWyHTFC385Ga0ImPnPOFrBZepDYYF2YXHDpTIWVmIOR/IFzffhswwJvncxNlXvMWjieHMaurP22itxYsxYhxXMNPqV+JsoVs6ITMqLTrcfk1pKeY9z5CG/SiNlotB9VhVziis9/cQI9zvKeamX7Qx6YQ5+XZ4m1OZ7Pe7BXKxMAVVEqUN5an/iEQ4dMUazFwUdc7w0T2w/w24+VFH8GnlvnjSiaQOEHq2jliR6rHbxEOcVi6YzVKkAy2YlquzbTpJHc4eqruum9Gc6sHwJRymrl7EKQQtytoBLo4dH8m3hqB56PQ/0Sqjap1hCVCVTGSPPp9BX9Jd9L7HgsU2meMoHGzSYLnPcX6DomJGNuI2ZM= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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: 13210788 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3F775C7EE23 for ; Fri, 14 Apr 2023 00:12:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6A3946B007E; Thu, 13 Apr 2023 20:12:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 60570900002; Thu, 13 Apr 2023 20:12:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 36EBA6B0081; Thu, 13 Apr 2023 20:12:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 1716C6B007E for ; Thu, 13 Apr 2023 20:12:10 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id CBD45404BA for ; Fri, 14 Apr 2023 00:12:09 +0000 (UTC) X-FDA: 80678069178.13.19D4364 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf16.hostedemail.com (Postfix) with ESMTP id 1B00B180016 for ; Fri, 14 Apr 2023 00:12:07 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=IeAvbh5L; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 3V5o4ZAsKCJY02A4HB4OJD66EE6B4.2ECB8DKN-CCAL02A.EH6@flex--ackerleytng.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3V5o4ZAsKCJY02A4HB4OJD66EE6B4.2ECB8DKN-CCAL02A.EH6@flex--ackerleytng.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681431128; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Xv2Sbflv1PdMqZUyau/vGtw69NpiI05I8fyTNzzRBtE=; b=u5xlTpbNVMvJD9eujklRmQZk9x3/d3MXjP7OsS3p2Trm/u9ejpXpOMPywXfmRE/AhLFGOU jhAJHfJ49HzoiAs03P1eiEBJnQyGLL1BAeWkTSMeBlybyB/85IECOifrww6UifmFCPqk/p d0SmBY/C/Ccs0wnPMv3cVT/kZWnP1U4= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=IeAvbh5L; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 3V5o4ZAsKCJY02A4HB4OJD66EE6B4.2ECB8DKN-CCAL02A.EH6@flex--ackerleytng.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3V5o4ZAsKCJY02A4HB4OJD66EE6B4.2ECB8DKN-CCAL02A.EH6@flex--ackerleytng.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681431128; a=rsa-sha256; cv=none; b=8ZzQkqZagU6fWYrBELEgAu2U1/FsBEi6OSLKhsI1+7vEoeeYt5BKl7HgjX9iMhKkOA+7RG eoEIx3kM5BrRuhqaBP2fsNiNLko0usFfjQ+rkck46Jv/nqTwxiKOm4lwa7XGUtvvRsKCWK 6zXgvxw1vKJEauYN9qvoLAfaMz5yud0= Received: by mail-yb1-f201.google.com with SMTP id j193-20020a2523ca000000b00b8f6b82ec94so1100389ybj.18 for ; Thu, 13 Apr 2023 17:12:07 -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=gsSCRKxlE31EJs4I7fKS6UznkaRfkLlZHT38GA72KYJjwfSys6IcCOAib9eZqSSRcX w3f8mdmqPYCFPMhFcUmRDtYv8cPER2mIypSESf3l4mYIo3iL3N8Sa7rICWBFRCCuDGHf Lwe4VawrvCbu/ZD/r1OOTm+K9hZ2/d1BHnVemWuqq0phj9fMIf95xxhg9acKQwOoOoQl PQiCCjfe2VAmabWlpgtVLr/IwUN695k/0nFbMAyHfjxcs9hY5Kx8BbtS6d5pN8KzDt7y HHreWvHz1R+h0IvYJL8lScqRqsutzZBI7va2sWxtOG5yBGhyqc5uFo/GpEl+1bZfQKqb /L3g== X-Gm-Message-State: AAQBX9cBGJcHXvj248YA1Oz6u3XxvCRJTwdqyLkWD1Is+m9itlpfbbE4 P7IN6NGX7ebbkOQwpS7Bzo2PrLtaasBWhjkZyQ== X-Google-Smtp-Source: AKy350bz8+zUcgBBK3sJ0KBBaZXIEou5c04bKvPj9N3prJO/LHXQy58hyZlX1YHGEbHTNZX5qvCYitSezXVlLtbvug== 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-Rspamd-Queue-Id: 1B00B180016 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: deqrzbr75qpe99kuz6qufyo9q3zjh7ko X-HE-Tag: 1681431127-770135 X-HE-Meta: U2FsdGVkX18j/r8Z3Y80rCX1/xJ5aPYOJGzUKGGySwNfH0NnEJ8gTYw+iBedGmfk7d5fH7A4IRbqjbFKcOGDbzHU3HgyqaIThSX+y88TJeGiJ5GakZGKaUO9AJ8oyN735voUkMjah83pNq+p+D3xfMz30jO6Z22yepYDCpYXpfj0YgTm3LILsrKjGVAc3U1Xpk504p/PL+EzcfbyrBCk9nWROkhC6KtXuXE6md3AQt01kPwe8g8A8CYhk+q1Gu0g6DWMdKcEhzFCqk6jy+sExWtmclVQuUHRQ24sojlDcYjT1Y7+XDZaZTUclHrb5rPGVhDl4wV/HC4oAEYiWtvdYWYB9UpaWjYt32OXOepoeQ+M+KZmBBAwBXGwdzyCV+e6Yp9QZWm0ghfTUCwtPCredtMkH7LAzPIBvVtSwJyteaVHenyTMUYJj7RdGRHTUe0ylWuUVQ4nLj+XdMXBSTXI/o1eMcl+vc1vnxD/M8egnjuwbnq8FLs5DKwCOf5Otr9J6dqtyXQzR4IbmrOQ6PwTKrn/CppRRKuy0hiySvF6io+FPXwzdZY/zuB6yiT6cO1u9z33hdkEOeN9ecj1Ndw5zvV4wx+gtzbPupAyKRgF4VBgYSbQrInGS+1ve610XiPOJ6fIG5TwTKbK43cnzzMrvTJlJPVULUE2cuxpiIb0jhQ7muVZCnuOIXcNPPvf3ancmMlR0AcimmpwcZBKup+SwUKMhuABYHD9/eSmHlJI1DC14HN2jSr4B3z67DYcQQKYyurSiyV+q+4vK/6lyDzpn0x00lyZaokJN8JsupqaP5Hqn5gc3UIJaXOtQV1Dzwv+JThSM6Mjlhy+RKY5rofZ2vc+pXs9d+CVCSbMKsjUOW5guhseHe0RRuo+tTs7IrFIb8RqjLMQwjdTQQZoETiQ8d71GqMTbuwDrR1A8K78f70GvT2ZnlOxGYbziawnNc74SCJSJohtXMpYhZeeBsC NdCbBiBE KS8FxY2dV8pVGhZRmO3pIHSAyfC8yZWXnFWBSWyuQcQSeOyr7p2RFDIN8SkV2ew06yH/qObLV44v+fNW3lx4/YmfjzzL6JhZzVaYrjUVpL/aj7FqOscO81wpsZXArUCR8pdo7c/U6Udu4WeudDkA5WZWZyn50O1uA5M1KaGptSGYFH05+EdnLSNUAv65V4/F145rbY9RjdfAheIyC8GaZeJ/nyNgySKcIxm3p05r6SClzI+Egl1R5QX0rfHIYh5jIXFpMGXixZSMifPqtQMrwvpulROwUAUGdQXwjkw0BJzUSAQ6xUjyzxeMrTrMl/ncjzDqJCKrjdzB3Qu9Nr05ESlbcbBdp1QE5KKgKy98weDt5PQf6LR9PPAKFkb6S9L24WgfZWv7+4MCQ4DaVyIFQZQAwoGXT5HVYX2YjTMUuVVAZp5RM+i3QwHNztb793IKXWV+CiHLZAr7/idzBaphRNtnrAY29nYGJRGzaecy1iwfjrTeUBulpg1FZIZ74EVmz8TQz/ujZrl77k+nt+2203rAsnibC1Cw29L8J+BulDzbEhQ7rY+AFTdYt3YS7FnQMvK9eeRQZynQzCoP+yPAfVS4BPVbLn7jiMFViv2zgtEVlG5+5NboDk55Epspx4AWe7F6Tc8WYqPjDrldxekWf2B62DM4SJtaz3brNG8+XxuGpfbB89ZCqNo9zxNxMuSQuS4rNd9tgE31m2dyzu97KG3jw3g== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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: 13210789 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 575C2C77B6E for ; Fri, 14 Apr 2023 00:12:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C4B876B0080; Thu, 13 Apr 2023 20:12:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B85076B0081; Thu, 13 Apr 2023 20:12:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9AFEE6B0082; Thu, 13 Apr 2023 20:12:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 864C66B0080 for ; Thu, 13 Apr 2023 20:12:11 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 5824AAB8AE for ; Fri, 14 Apr 2023 00:12:11 +0000 (UTC) X-FDA: 80678069262.01.F07E181 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf02.hostedemail.com (Postfix) with ESMTP id 99A7780008 for ; Fri, 14 Apr 2023 00:12:09 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=tYf5bMIh; spf=pass (imf02.hostedemail.com: domain of 3WJo4ZAsKCJc13B5IC5PKE77FF7C5.3FDC9ELO-DDBM13B.FI7@flex--ackerleytng.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3WJo4ZAsKCJc13B5IC5PKE77FF7C5.3FDC9ELO-DDBM13B.FI7@flex--ackerleytng.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681431129; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=JIdXLIzbqoaS1yDLNUT0qQstKE8tePtbIkE0dhqmXxw=; b=hKSK4F2ZqEUD2IF506m7QYHrCbDzMxRZBoGzc6nL79CYdAovU/v63cV8lmrKQO7laX/Aw1 og45wcvrx6edhxItHgTkWntJnlNnTObpeoprsyYxqTjDrs0NkRFmcYrHsW1EhxoivIqSOm xeQ8Jseoia1KIeMh7qbsfx97U+eG4vI= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=tYf5bMIh; spf=pass (imf02.hostedemail.com: domain of 3WJo4ZAsKCJc13B5IC5PKE77FF7C5.3FDC9ELO-DDBM13B.FI7@flex--ackerleytng.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3WJo4ZAsKCJc13B5IC5PKE77FF7C5.3FDC9ELO-DDBM13B.FI7@flex--ackerleytng.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681431129; a=rsa-sha256; cv=none; b=LMJTPjWmscMiuSgVNBb7Gf2rlm61Yt9uHW8Y9uwmirn17RaXYMRvoNl8RtkO7OBpJ0UJMP Q8qUiTr/jpLwgMPm3WZnN4iO654h20KtbvpOaivRdHOhy8Yo2f1VYZiPNEksA+bkWhvBlH oHX62oWVgCoApPtaAmtICdpkyGTLO8w= Received: by mail-yb1-f201.google.com with SMTP id l124-20020a252582000000b00b8f5572bcdaso4294139ybl.13 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=CykpWqpb5uGI2ElIKJ+3wihMmWPMa3+d8TEiEULsKxuksw1zqD6NKNgvOUueBaPDL+ QXUB1vvDajp4dTpRskmjGPRVYagaKcBpj0+1oXmTWaCI6cjj0sLta1zDiVBphJygQyOW er4SIqwfAwI18udqUVcHB+JmyqSnX1hSL4fX4fJGFQSjboyTFPWa9akGio2lmVFZ9fuF GtI8lCzrNE5yj+DALdmjp++h61ZS6ScK7O3afUjpQgZyOqRjIw3nC9POvC493bQJy161 UJVFKdpOysod+8/9BStqYDy0TiYkbu5AxiCS/jsG4ZoThcnOphBj7j7DaaqVeffNSiFv I/NA== X-Gm-Message-State: AAQBX9e1mlNzkABy/Lv7pWBYqYfw919b9jM6J6wNgiYH/IoNRSw9mj01 MZD7dJe/0HI5s7u6jGuReikzIAO0KLaw5zethQ== X-Google-Smtp-Source: AKy350aw1qXzddjDywjx3OQ/Kkf8aSm9qQ52ggrXe9lxiXXBS/UAEkiIAH3/1jEr1IF4yNFdix0pirr9uwMpC8d++w== 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-Stat-Signature: 8b8mawn9rg13xketfajmmxch4zaemieg X-Rspam-User: X-Rspamd-Queue-Id: 99A7780008 X-Rspamd-Server: rspam06 X-HE-Tag: 1681431129-697852 X-HE-Meta: U2FsdGVkX18Cu8pAvXzy83idU8MmdCRIe4upp4tscYOiBqiiQ+KvQFlJp57yCmEIJ9U3inJ9gpg0KGU30k8QRQBt2MnkTMJo6YcG/b0YkJYxP/0h5aDV6S3Hab53lwTNqEjdZX0Rw4hjRWUa1isNCB4VMobRtqnI+hTGOZ3bJAGQwjw1+EFJ05M3oYvj/hbh/6Vw7HxaokDCWej4XoFWknfdbcC/7HpSb1zHUEcK8qzwsNe9NlJICTIuhaso6VUnACFLZ6bH4XhHtOSG/EN5tBCpV2v1aQA19ZxE16JQDobHRrUWq9ftfuj8KzHOJu7VHqWI0AXkOencPHI3M5YHtZUD1Zxa6YXhvMsH6zKxh0UkwgVSa2YCBTDgEswOD3WmZOIU+0iCidyoTI+qfU9qodgrfwF7F3EwE5rw5B3VbMmgjuPvb1KBziu58kKNuGB9TgISEgpDJAWNb42CrO/LCRcrywcJp2YbUTmPwM3n6Vhk0oFx52wZ2HJZy1w0I2uYRI/79zlZvprsFeNmeBi1xOrv0QgMHoYRGnacqd4vDyXWIsTZYC6vvKmt7TqMpIyrcS7HYuUElbmY+sJkIm5FCxJoFJ/Ul0mqvcxNQxnFazjuiOyNT1VqV08cEC9bDeDLKTpQs4jjR+K6GTZVGs4KzdqI5D38lIKcq2diFicQTo3nIAuRI+UHe6FjsygzNeWtxs/6xAo2Yjd5C4MxaQRfnf8m59SAMan8yKO35ASFod/uIK6FJyIT8zD43XhPNuZcQ/WPtOdLYOEZQZWtWFr6roMFTH8YWyYTS3aGowISRceCz4Cth6IvLMLpaJ2+PoJuygWPItF24CmGoG2D0iGlqEeznXvbrslOZW0iKp6eZc5h7FM0TmIPRYE9QdSMCQhUzLoosa74fnHqAVYLAX3YItqtXgLWy3Tkw31b6TlMfNHYSmIZssql7kv1+h3bMrPt2H77EDpgqMZSotY6t90 qf9nHsSB 7DoVLhxDTA/lB0BZ7wCpjCNTTnmL2k2kscAru2sMbJPTaQkXwIqQCVZ0QIiArydPCHmOlXnl8thIWmFfPxn8CrK+xFuo/rzxWuEL14+dP1XNtTpqTO+xL+4jW/1+98CJY7rW9EZwdETb2qGKbPJBOwzjsJmdFim0G8ZA1u1BreR8/E+CV4Q5wiOOIp7LZia/8TBGCfufosoyjy1N0jUzu8zJZA/YcFPh/AoAAZZ6tyLHDZsElm8p6Kr4pleVI2Rz5HgeI7eUlnWpc6tyC9jiUAdo2UViiwqV8QtK8pUy5FPHOEjWwi4JcG8EJwpgoQCIXS1XQL585kMJNW8Z52HrLX1qJ3YXw2+5VF3WqU2i4r2UsIp1ImYOrHHIkX9DAq7uML94FxDbniS1SmwjgK/7YXrhE3BtQIJli7Aiu0ebK43tyBxrK4QzL+SY6Srwq8j5556ft9770wODVf6+4rnPafxp3zrNghwFOFrsQ+dcQKKSeV/aPTunHCGQgyHhhz4L1xZs9IzohxxL3rcqiml/Za0tVxnMfgbP6GiL3QCJjRA2qloiqsGtifcaQgX13B3+9K+2rIUwsljKyGDex6e8WTn1WsGrRmHvTz4twdaCZByt38llttEXqutXqqYlxOkPrIAKFXBy48++P53yy5KQ9cTGaDtW8q9bQ65Y8nL4AC4Eu31FoG7uByJgg07Ii+CZbrNTd6bQVnanGOcxqJgWF9ekSkw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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