From patchwork Fri Feb 23 23:57:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13570164 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 lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E1EB2C54798 for ; Sat, 24 Feb 2024 01:10:27 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.684998.1065341 (Exim 4.92) (envelope-from ) id 1rdgYO-0005mc-3L; Sat, 24 Feb 2024 01:10:12 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 684998.1065341; Sat, 24 Feb 2024 01:10:12 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rdgYN-0005lI-Rd; Sat, 24 Feb 2024 01:10:11 +0000 Received: by outflank-mailman (input) for mailman id 684998; Fri, 23 Feb 2024 23:57:41 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rdfQC-0005zS-Ux for xen-devel@lists.xenproject.org; Fri, 23 Feb 2024 23:57:40 +0000 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [2607:f8b0:4864:20::102e]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 51dfb343-d2a7-11ee-98f5-efadbce2ee36; Sat, 24 Feb 2024 00:57:38 +0100 (CET) Received: by mail-pj1-x102e.google.com with SMTP id 98e67ed59e1d1-29a378040daso1051030a91.1 for ; Fri, 23 Feb 2024 15:57:38 -0800 (PST) Received: from localhost.localdomain ([2620:10d:c090:400::4:45de]) by smtp.gmail.com with ESMTPSA id si4-20020a17090b528400b002963cab9e2asm2161658pjb.2.2024.02.23.15.57.34 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 23 Feb 2024 15:57:36 -0800 (PST) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 51dfb343-d2a7-11ee-98f5-efadbce2ee36 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708732656; x=1709337456; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=A7v8QcQGKEfLwaJltNJW0mYz5w9KdmkVXr/8REE7aG4=; b=Ua9UR9Nkf7947Ao70UBTRIPhKLlen+kIXRvptVzPvLoTuHUqQVZLd8bXtCwwMMaVe7 /sWEgWotLBNsj47oWGbK31kCqIq5JCYIgDkygRNIWCbqQFLd6Xbnc44dgpl3Lz6LYyln tcBAFtS97aC5TD+MTFrW9F+rOeZbL2WLU7lecXImmAbln6pvvosAd18yWtOhD6r3kmjC fPJiKHO196k6q6cbEMMavAF0o1dMGTZI+Z007iqCvmVvr3or9TRfYVobc+ge/Fui6boG fW3TlTZfxEQpY459mWBzm2MmeUcBYtyYFcKQFCfX8pFnEAW1ztcgNdwoxE/VgXkvmT7a 7z3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708732656; x=1709337456; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=A7v8QcQGKEfLwaJltNJW0mYz5w9KdmkVXr/8REE7aG4=; b=LnnTHZEG6MlJ52DWwAu2qqvnj3L2OVqIYHfWqg6JLRDc6UTdSV46t3GLL84F00Jid6 BbTYCN+SFYKDsPcAYARlHNxzkSFcAEv91ZVu89BHp9IOKlKBk8n8pCCbv8Sccb5QzB3T NjBU2w2P3FVhErTQ9ge9OamUmCJS8OMviOszOhYRrxqsR0Tsn3WnqoVwprmY8I4QdcqD NFmirRT7nGojPYreO31PisusORg++1sQd2NPnEOY88i4AluW4jCu3H/RhXt7rXZWMh07 cQ0/MU59lISZpYYY7rBQmBKCiyZurt7V+YP/ZHLH7thgTR9vrjzX173he444HtmlbQnd zcFA== X-Forwarded-Encrypted: i=1; AJvYcCW/n37LmpjHxmzzrTgwGOKIST1rU8GUraVvF7xrVNZTVBEQyNTcX+T5E7BX1I+KTzYXopH4E8ZqndhuoQvsN/WYyiMq9UxBvKBdeGaFutg= X-Gm-Message-State: AOJu0Yz8BrfmkGVXQepVEPXsB+2aHZ3ZvcLEf4yF8naYDIuOoW3vKiqe 2SM7A1kHw0qCSjfV6PRvl3FPaivWs5Z8vMSyXqDVgFWxyNM7oJQr X-Google-Smtp-Source: AGHT+IHG/DO/OVIktjtp11/4CHtLMBPBSMQzb2KGZX31cgPqxxDf+7H9BLoJUXmr0UjElOG7XOtMJQ== X-Received: by 2002:a17:90b:1050:b0:29a:9ba0:8a5b with SMTP id gq16-20020a17090b105000b0029a9ba08a5bmr411918pjb.5.1708732656476; Fri, 23 Feb 2024 15:57:36 -0800 (PST) From: Alexei Starovoitov To: bpf@vger.kernel.org Cc: daniel@iogearbox.net, andrii@kernel.org, torvalds@linux-foundation.org, brho@google.com, hannes@cmpxchg.org, lstoakes@gmail.com, akpm@linux-foundation.org, urezki@gmail.com, hch@infradead.org, boris.ostrovsky@oracle.com, sstabellini@kernel.org, jgross@suse.com, linux-mm@kvack.org, xen-devel@lists.xenproject.org, kernel-team@fb.com Subject: [PATCH v2 bpf-next 1/3] mm: Enforce VM_IOREMAP flag and range in ioremap_page_range. Date: Fri, 23 Feb 2024 15:57:26 -0800 Message-Id: <20240223235728.13981-2-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-145) In-Reply-To: <20240223235728.13981-1-alexei.starovoitov@gmail.com> References: <20240223235728.13981-1-alexei.starovoitov@gmail.com> MIME-Version: 1.0 From: Alexei Starovoitov There are various users of get_vm_area() + ioremap_page_range() APIs. Enforce that get_vm_area() was requested as VM_IOREMAP type and range passed to ioremap_page_range() matches created vm_area to avoid accidentally ioremap-ing into wrong address range. Signed-off-by: Alexei Starovoitov Reviewed-by: Christoph Hellwig --- mm/vmalloc.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index d12a17fc0c17..f42f98a127d5 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -307,8 +307,21 @@ static int vmap_range_noflush(unsigned long addr, unsigned long end, int ioremap_page_range(unsigned long addr, unsigned long end, phys_addr_t phys_addr, pgprot_t prot) { + struct vm_struct *area; int err; + area = find_vm_area((void *)addr); + if (!area || !(area->flags & VM_IOREMAP)) { + WARN_ONCE(1, "vm_area at addr %lx is not marked as VM_IOREMAP\n", addr); + return -EINVAL; + } + if (addr != (unsigned long)area->addr || + (void *)end != area->addr + get_vm_area_size(area)) { + WARN_ONCE(1, "ioremap request [%lx,%lx) doesn't match vm_area [%lx, %lx)\n", + addr, end, (long)area->addr, + (long)area->addr + get_vm_area_size(area)); + return -ERANGE; + } err = vmap_range_noflush(addr, end, phys_addr, pgprot_nx(prot), ioremap_max_page_shift); flush_cache_vmap(addr, end); From patchwork Fri Feb 23 23:57:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13570165 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 lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4E2ACC54E51 for ; Sat, 24 Feb 2024 01:10:29 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.684999.1065344 (Exim 4.92) (envelope-from ) id 1rdgYO-0005s5-7k; Sat, 24 Feb 2024 01:10:12 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 684999.1065344; Sat, 24 Feb 2024 01:10:12 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rdgYO-0005pG-21; Sat, 24 Feb 2024 01:10:12 +0000 Received: by outflank-mailman (input) for mailman id 684999; Fri, 23 Feb 2024 23:57:42 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rdfQE-0005yy-GZ for xen-devel@lists.xenproject.org; Fri, 23 Feb 2024 23:57:42 +0000 Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [2607:f8b0:4864:20::635]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 5433a7fd-d2a7-11ee-8a57-1f161083a0e0; Sat, 24 Feb 2024 00:57:42 +0100 (CET) Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-1dc418fa351so5821525ad.1 for ; Fri, 23 Feb 2024 15:57:41 -0800 (PST) Received: from localhost.localdomain ([2620:10d:c090:400::4:45de]) by smtp.gmail.com with ESMTPSA id bc7-20020a170902930700b001dc486f0cb2sm3796745plb.208.2024.02.23.15.57.38 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 23 Feb 2024 15:57:39 -0800 (PST) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 5433a7fd-d2a7-11ee-8a57-1f161083a0e0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708732660; x=1709337460; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Wgy7UuZvicma9Y2PX48aaJ5ZDCArBODZAL7mSlN3pk0=; b=YELVKYQWjyHrpXdKoo1gSdbC5UAdJBNpVzVnBN2J70KHFCWih4VpBXw7WQEgl3nmT2 nduA1il6SF2NcgUXhTVhGhn/v81Rkbei1qlP/fG9aMJcOmUiqbYulE5aceVqzvtBGrIj 4dNeVJ7NQXEzoh/qX1a5uGP+J/pdpXXNtIM4i0bNatk62/E2D+BAWwjC9JS+UNAP+S42 tTjrfGSVLHluiVNOANYb+wUCaNe/2WDiOKgao2MwEqahk7RsAORibYqRuhkDu8zBX1/+ NN+CVmW+DJZdy256fPVtU2I8/2mbKGtk8ohPdFd/DQFlc54LLdEiBJsOh+Rcr8MzxXCd OM3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708732660; x=1709337460; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Wgy7UuZvicma9Y2PX48aaJ5ZDCArBODZAL7mSlN3pk0=; b=of7Bl1ruXvOlC7p3+naVW5h3FtUgqb49JWrmXbpCvFfG0lW7pPsJigG5/QT1ehdrqc k0P5UXqPCOrU7PD+DyET44/WVow8g8BjPE2wqvbfarJvu9xjyrjHIDGFs6AaMADROtLt ucwSppxY6TTOyMecVR1TnjyOvjd7tUuztBR8xqHko9sEqD4SGdX9PZgoPG1d0eNvrZW6 PNgKKfBwFd61fHl1/NrSyDSAJCjpBMYRbaWRng7pgNNWCLISF/EMviAdHnik+g9W5+DD HZi2C2wx1fysftAe8N9q/3M0IUi1bU8349XLSiiFmgwxHjfkYGvCCS0vuIDbngSOb3he 3czQ== X-Forwarded-Encrypted: i=1; AJvYcCVmGqN0hB8yCtdvlji6npZkn1nPECxHPVo0gHLxMyeRZogWcw6Qws1tWsHyl+ojZVvS/ylr80kZS0k6F5y0L2iQTV9XA6WTm4JZen4OOOk= X-Gm-Message-State: AOJu0YwMC6sMS/exdGpw5zXGrMAI1oUlytFV3fNP2rHnQC9/JeHswaOx xMt176HU8jFkPx+bvDbbRj1peBfzYpAoiGzd+pypoI/aUaGBHKh6 X-Google-Smtp-Source: AGHT+IGELhXnvr3n524zH97gDxBoJwFKXzefQUh+8dOBuQvkiJz95RKjpkB2JqjALLNdOQ3Mm4DyFA== X-Received: by 2002:a17:902:f645:b0:1dc:b64:13cd with SMTP id m5-20020a170902f64500b001dc0b6413cdmr1551144plg.27.1708732660213; Fri, 23 Feb 2024 15:57:40 -0800 (PST) From: Alexei Starovoitov To: bpf@vger.kernel.org Cc: daniel@iogearbox.net, andrii@kernel.org, torvalds@linux-foundation.org, brho@google.com, hannes@cmpxchg.org, lstoakes@gmail.com, akpm@linux-foundation.org, urezki@gmail.com, hch@infradead.org, boris.ostrovsky@oracle.com, sstabellini@kernel.org, jgross@suse.com, linux-mm@kvack.org, xen-devel@lists.xenproject.org, kernel-team@fb.com Subject: [PATCH v2 bpf-next 2/3] mm, xen: Separate xen use cases from ioremap. Date: Fri, 23 Feb 2024 15:57:27 -0800 Message-Id: <20240223235728.13981-3-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-145) In-Reply-To: <20240223235728.13981-1-alexei.starovoitov@gmail.com> References: <20240223235728.13981-1-alexei.starovoitov@gmail.com> MIME-Version: 1.0 From: Alexei Starovoitov xen grant table and xenbus ring are not ioremap the way arch specific code is using it, so let's add VM_XEN flag to separate them from VM_IOREMAP users. xen will not and should not be calling ioremap_page_range() on that range. /proc/vmallocinfo will print such region as "xen" instead of "ioremap" as well. Signed-off-by: Alexei Starovoitov --- arch/x86/xen/grant-table.c | 2 +- drivers/xen/xenbus/xenbus_client.c | 2 +- include/linux/vmalloc.h | 1 + mm/vmalloc.c | 7 +++++-- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/arch/x86/xen/grant-table.c b/arch/x86/xen/grant-table.c index 1e681bf62561..b816db0349c4 100644 --- a/arch/x86/xen/grant-table.c +++ b/arch/x86/xen/grant-table.c @@ -104,7 +104,7 @@ static int arch_gnttab_valloc(struct gnttab_vm_area *area, unsigned nr_frames) area->ptes = kmalloc_array(nr_frames, sizeof(*area->ptes), GFP_KERNEL); if (area->ptes == NULL) return -ENOMEM; - area->area = get_vm_area(PAGE_SIZE * nr_frames, VM_IOREMAP); + area->area = get_vm_area(PAGE_SIZE * nr_frames, VM_XEN); if (!area->area) goto out_free_ptes; if (apply_to_page_range(&init_mm, (unsigned long)area->area->addr, diff --git a/drivers/xen/xenbus/xenbus_client.c b/drivers/xen/xenbus/xenbus_client.c index 32835b4b9bc5..b9c81a2d578b 100644 --- a/drivers/xen/xenbus/xenbus_client.c +++ b/drivers/xen/xenbus/xenbus_client.c @@ -758,7 +758,7 @@ static int xenbus_map_ring_pv(struct xenbus_device *dev, bool leaked = false; int err = -ENOMEM; - area = get_vm_area(XEN_PAGE_SIZE * nr_grefs, VM_IOREMAP); + area = get_vm_area(XEN_PAGE_SIZE * nr_grefs, VM_XEN); if (!area) return -ENOMEM; if (apply_to_page_range(&init_mm, (unsigned long)area->addr, diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h index c720be70c8dd..223e51c243bc 100644 --- a/include/linux/vmalloc.h +++ b/include/linux/vmalloc.h @@ -28,6 +28,7 @@ struct iov_iter; /* in uio.h */ #define VM_FLUSH_RESET_PERMS 0x00000100 /* reset direct map and flush TLB on unmap, can't be freed in atomic context */ #define VM_MAP_PUT_PAGES 0x00000200 /* put pages and free array in vfree */ #define VM_ALLOW_HUGE_VMAP 0x00000400 /* Allow for huge pages on archs with HAVE_ARCH_HUGE_VMALLOC */ +#define VM_XEN 0x00000800 /* xen use cases */ #if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \ !defined(CONFIG_KASAN_VMALLOC) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index f42f98a127d5..d769a65bddad 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -3822,9 +3822,9 @@ long vread_iter(struct iov_iter *iter, const char *addr, size_t count) if (flags & VMAP_RAM) copied = vmap_ram_vread_iter(iter, addr, n, flags); - else if (!(vm && (vm->flags & VM_IOREMAP))) + else if (!(vm && (vm->flags & (VM_IOREMAP | VM_XEN)))) copied = aligned_vread_iter(iter, addr, n); - else /* IOREMAP area is treated as memory hole */ + else /* IOREMAP|XEN area is treated as memory hole */ copied = zero_iter(iter, n); addr += copied; @@ -4415,6 +4415,9 @@ static int s_show(struct seq_file *m, void *p) if (v->flags & VM_IOREMAP) seq_puts(m, " ioremap"); + if (v->flags & VM_XEN) + seq_puts(m, " xen"); + if (v->flags & VM_ALLOC) seq_puts(m, " vmalloc"); From patchwork Fri Feb 23 23:57:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13570166 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 lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id EF42CC54E4F for ; Sat, 24 Feb 2024 01:10:28 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.685002.1065353 (Exim 4.92) (envelope-from ) id 1rdgYO-00061Q-ML; Sat, 24 Feb 2024 01:10:12 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 685002.1065353; Sat, 24 Feb 2024 01:10:12 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rdgYO-0005zc-EN; Sat, 24 Feb 2024 01:10:12 +0000 Received: by outflank-mailman (input) for mailman id 685002; Fri, 23 Feb 2024 23:57:47 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1rdfQJ-0005zS-Gf for xen-devel@lists.xenproject.org; Fri, 23 Feb 2024 23:57:47 +0000 Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [2607:f8b0:4864:20::636]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 567fba97-d2a7-11ee-98f5-efadbce2ee36; Sat, 24 Feb 2024 00:57:45 +0100 (CET) Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-1dc0d11d1b7so8497715ad.2 for ; Fri, 23 Feb 2024 15:57:45 -0800 (PST) Received: from localhost.localdomain ([2620:10d:c090:400::4:45de]) by smtp.gmail.com with ESMTPSA id d11-20020a170902728b00b001dc23e877c1sm6175189pll.265.2024.02.23.15.57.42 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 23 Feb 2024 15:57:43 -0800 (PST) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 567fba97-d2a7-11ee-98f5-efadbce2ee36 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708732664; x=1709337464; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=4T/JxyeGclar7cIIHLQMvHUD6oGOKffCuXl9nke3dJg=; b=PVL2fwFX95asjvNqbgQ4wubZN/VAg0zoG27fW6SMbPx1rcE70rz2V+qpivYsSlXEyA xY8KU3Q3i1kPlkuFNf+3C4vjS/lm+AErX/bOzj2Fe6eIDYBGJl36vixok6HxomnqQ+r0 t5ZFdwcgwDVt+82A+HvZJWgRdGxV3ADiQv728xmmNCG9jUb+ADloHUaKpBH6Q/4PDjDh RmSrsCW674acpRIjmQljvipaswO2ZUm1f8w7n/3ptc7QKrPdzHZuyvRoT0/XZV9ZD/4t mB/nMU01mll89/elTukKrvud1za2bhIhgNyypTKmMFS8CKGVn05Yyb1fhuxCUnHBODhT nXPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708732664; x=1709337464; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4T/JxyeGclar7cIIHLQMvHUD6oGOKffCuXl9nke3dJg=; b=OAWPTBHY4dKwq7YvW76b0RE/ga+bjyHeutKw6IDziRrBL40jmElMaSgQ5OwA0Dex39 7f/CeR2o5oOyNKy1+6a/ku+e8cUm5oI+V2Vbycc64IVw4zk9uNlmagYXHL2A3ICTSgTr 5dldtCFpSGhaTnXPwssip8QP1CHXb1lvAVnAK5qAn12oMyXZ+jsZ6yiXZk4tGBCslj8B S2TesUcIHKjn7Cu0GS/LnCTgppZwjEHj9eEDEDnIY3hBDvNZ4ZQdFrBOhMX/Zm2nQZOM EoIyDdF1+qnpn+p86XsHu5Gqfz7V0n0Ji2W8wzk0iqr7TUIlpoOyEOVKWsnCw04eirqi XoWA== X-Forwarded-Encrypted: i=1; AJvYcCVXjDdd5hQYBGin0C7nUSCWsggiSGKps3jy+PLOM5xVCk0OqIfszD9a7MxxbtZbLVedn2NHD9z5DF5XlNLNIonQcNGRAv//ynPA6KRKcbs= X-Gm-Message-State: AOJu0YyaSoqO9tCiMwaXg3RDKjZW2pu+lK+a8cjCk9+a6UN+J2VBkHGk Zq37xYs0njE1unJweFf/6TyKKosJZj1vprzXD189vTU9BbPkYGQQ8w+sK1wQ X-Google-Smtp-Source: AGHT+IGrC0E10ADJh+Q28iXnJRIwpr5YwuUUqZaBfkmopbBhR99u1mcgLWatOYemqm7CQuFVwzUw8A== X-Received: by 2002:a17:902:c40d:b0:1d9:d341:b150 with SMTP id k13-20020a170902c40d00b001d9d341b150mr1582992plk.40.1708732664121; Fri, 23 Feb 2024 15:57:44 -0800 (PST) From: Alexei Starovoitov To: bpf@vger.kernel.org Cc: daniel@iogearbox.net, andrii@kernel.org, torvalds@linux-foundation.org, brho@google.com, hannes@cmpxchg.org, lstoakes@gmail.com, akpm@linux-foundation.org, urezki@gmail.com, hch@infradead.org, boris.ostrovsky@oracle.com, sstabellini@kernel.org, jgross@suse.com, linux-mm@kvack.org, xen-devel@lists.xenproject.org, kernel-team@fb.com Subject: [PATCH v2 bpf-next 3/3] mm: Introduce VM_SPARSE kind and vm_area_[un]map_pages(). Date: Fri, 23 Feb 2024 15:57:28 -0800 Message-Id: <20240223235728.13981-4-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-145) In-Reply-To: <20240223235728.13981-1-alexei.starovoitov@gmail.com> References: <20240223235728.13981-1-alexei.starovoitov@gmail.com> MIME-Version: 1.0 From: Alexei Starovoitov vmap/vmalloc APIs are used to map a set of pages into contiguous kernel virtual space. get_vm_area() with appropriate flag is used to request an area of kernel address range. It'se used for vmalloc, vmap, ioremap, xen use cases. - vmalloc use case dominates the usage. Such vm areas have VM_ALLOC flag. - the areas created by vmap() function should be tagged with VM_MAP. - ioremap areas are tagged with VM_IOREMAP. - xen use cases are VM_XEN. BPF would like to extend the vmap API to implement a lazily-populated sparse, yet contiguous kernel virtual space. Introduce VM_SPARSE vm_area flag and vm_area_map_pages(area, start_addr, count, pages) API to map a set of pages within a given area. It has the same sanity checks as vmap() does. It also checks that get_vm_area() was created with VM_SPARSE flag which identifies such areas in /proc/vmallocinfo and returns zero pages on read through /proc/kcore. The next commits will introduce bpf_arena which is a sparsely populated shared memory region between bpf program and user space process. It will map privately-managed pages into a sparse vm area with the following steps: area = get_vm_area(area_size, VM_SPARSE); // at bpf prog verification time vm_area_map_pages(area, kaddr, 1, page); // on demand // it will return an error if kaddr is out of range vm_area_unmap_pages(area, kaddr, 1); free_vm_area(area); // after bpf prog is unloaded Signed-off-by: Alexei Starovoitov --- include/linux/vmalloc.h | 4 +++ mm/vmalloc.c | 55 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h index 223e51c243bc..416bc7b0b4db 100644 --- a/include/linux/vmalloc.h +++ b/include/linux/vmalloc.h @@ -29,6 +29,7 @@ struct iov_iter; /* in uio.h */ #define VM_MAP_PUT_PAGES 0x00000200 /* put pages and free array in vfree */ #define VM_ALLOW_HUGE_VMAP 0x00000400 /* Allow for huge pages on archs with HAVE_ARCH_HUGE_VMALLOC */ #define VM_XEN 0x00000800 /* xen use cases */ +#define VM_SPARSE 0x00001000 /* sparse vm_area. not all pages are present. */ #if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \ !defined(CONFIG_KASAN_VMALLOC) @@ -233,6 +234,9 @@ static inline bool is_vm_area_hugepages(const void *addr) } #ifdef CONFIG_MMU +int vm_area_map_pages(struct vm_struct *area, unsigned long addr, unsigned int count, + struct page **pages); +int vm_area_unmap_pages(struct vm_struct *area, unsigned long addr, unsigned int count); void vunmap_range(unsigned long addr, unsigned long end); static inline void set_vm_flush_reset_perms(void *addr) { diff --git a/mm/vmalloc.c b/mm/vmalloc.c index d769a65bddad..a05dfbbacb78 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -648,6 +648,54 @@ static int vmap_pages_range(unsigned long addr, unsigned long end, return err; } +/** + * vm_area_map_pages - map pages inside given vm_area + * @area: vm_area + * @addr: start address inside vm_area + * @count: number of pages + * @pages: pages to map (always PAGE_SIZE pages) + */ +int vm_area_map_pages(struct vm_struct *area, unsigned long addr, unsigned int count, + struct page **pages) +{ + unsigned long size = ((unsigned long)count) * PAGE_SIZE; + unsigned long end = addr + size; + + might_sleep(); + if (WARN_ON_ONCE(area->flags & VM_FLUSH_RESET_PERMS)) + return -EINVAL; + if (WARN_ON_ONCE(area->flags & VM_NO_GUARD)) + return -EINVAL; + if (WARN_ON_ONCE(!(area->flags & VM_SPARSE))) + return -EINVAL; + if (count > totalram_pages()) + return -E2BIG; + if (addr < (unsigned long)area->addr || (void *)end > area->addr + area->size) + return -ERANGE; + + return vmap_pages_range(addr, end, PAGE_KERNEL, pages, PAGE_SHIFT); +} + +/** + * vm_area_unmap_pages - unmap pages inside given vm_area + * @area: vm_area + * @addr: start address inside vm_area + * @count: number of pages to unmap + */ +int vm_area_unmap_pages(struct vm_struct *area, unsigned long addr, unsigned int count) +{ + unsigned long size = ((unsigned long)count) * PAGE_SIZE; + unsigned long end = addr + size; + + if (WARN_ON_ONCE(!(area->flags & VM_SPARSE))) + return -EINVAL; + if (addr < (unsigned long)area->addr || (void *)end > area->addr + area->size) + return -ERANGE; + + vunmap_range(addr, end); + return 0; +} + int is_vmalloc_or_module_addr(const void *x) { /* @@ -3822,9 +3870,9 @@ long vread_iter(struct iov_iter *iter, const char *addr, size_t count) if (flags & VMAP_RAM) copied = vmap_ram_vread_iter(iter, addr, n, flags); - else if (!(vm && (vm->flags & (VM_IOREMAP | VM_XEN)))) + else if (!(vm && (vm->flags & (VM_IOREMAP | VM_XEN | VM_SPARSE)))) copied = aligned_vread_iter(iter, addr, n); - else /* IOREMAP|XEN area is treated as memory hole */ + else /* IOREMAP|XEN|SPARSE area is treated as memory hole */ copied = zero_iter(iter, n); addr += copied; @@ -4418,6 +4466,9 @@ static int s_show(struct seq_file *m, void *p) if (v->flags & VM_XEN) seq_puts(m, " xen"); + if (v->flags & VM_SPARSE) + seq_puts(m, " sparse"); + if (v->flags & VM_ALLOC) seq_puts(m, " vmalloc");