From patchwork Thu Feb 29 23:43:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13577758 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 35A9CC54798 for ; Thu, 29 Feb 2024 23:43:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BAAA06B00B3; Thu, 29 Feb 2024 18:43:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B32386B00B4; Thu, 29 Feb 2024 18:43:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9ABE56B00B5; Thu, 29 Feb 2024 18:43:37 -0500 (EST) 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 869486B00B3 for ; Thu, 29 Feb 2024 18:43:37 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 600981C0F7C for ; Thu, 29 Feb 2024 23:43:37 +0000 (UTC) X-FDA: 81846470874.14.84A544A Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) by imf24.hostedemail.com (Postfix) with ESMTP id 9AB7E180012 for ; Thu, 29 Feb 2024 23:43:35 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=VsVfDCbZ; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf24.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.210.170 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709250215; a=rsa-sha256; cv=none; b=YfqAnhOIolZ1qgzbz/HfcEEVXmn834BVRjbo82D3ds+ASUl4RQQVBSkOEnP6ot9BD7+Shd q7rATdU+PEslHp7GGYIwZpp2tgKlHLHfyoXXXx5Orr5OuNB+R7aSMPOe+s5XA5jQ4lKFO0 A3LsLxX06/RFrqBsnwnMh8sjutxUlsw= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=VsVfDCbZ; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf24.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.210.170 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1709250215; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=GGmuzG0BuzfpsyKgKDkTT6Enikl3D6AjMcJQdJLpWcI=; b=ZYgGZ/Fat74JrtD6AAxsPvVqF+KBsadu/XeeXUL7jDAGSc5c7h4pgrwxaksBkuk4eb8hDz wEMvjlAkH5Lc9uqilPSJ3FnQ6nfAyaCH3xhY6e+ywEH6ss/Zi0f4ngRjwIOz8tD5JE8+iF Xy6Zy9Gk6iUjPgnoWuJrYBNe5SnN+zQ= Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-6e57a3bf411so940755b3a.0 for ; Thu, 29 Feb 2024 15:43:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709250214; x=1709855014; darn=kvack.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=GGmuzG0BuzfpsyKgKDkTT6Enikl3D6AjMcJQdJLpWcI=; b=VsVfDCbZurLOqHVhRwAg7bnuniokJg8wDNHIajb9LexO4oC1Fb8ZyYGjhR/xvqcb5d 5zcqPKYsYfkoUNpQzLjnA+IHW8FBtZsTzzwr1gJ0fzSfZgOzFpGp75I97tjgONYXpbXU sl14rbXYy7xZwXxiJ4xjAThAoTAcoiJmwqkUS10Iscz2ZDppPuOk6QAotCVD/JF8HdEu K0U31ue2JZjkS1b5317vNuLbIiyQk2HnhSdFXPrR2mivaezVi+hYsylo9tK6F8tBp26S /PKq9rL/0AP+NVSqvlTecD69rVYvXgCCKYVOAFgGf5ER54wpf6vjQJvOmg9m1MyApYf2 QbkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709250214; x=1709855014; 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=GGmuzG0BuzfpsyKgKDkTT6Enikl3D6AjMcJQdJLpWcI=; b=vaZ1H8AA0FG2hUyB/jGBU8ePRtyc3rqWJOrwgveSHthmn09Tj/eIgcf6uTtuPvMKMV BWGFOyFpDFyfjlgd+SiVcbU+3EwtNpRtz0rN2Y9zJFUEs6cdTdXwTspvn973ic/d9uAg gRDMYqw8RI5o3fc4tzKqfFQFyUv+sdlet9vAdupfD8HaGGSsl9W8j9UVxd4uiK7PIs4w ES1MnhW2qUPJ0NA/12V0GDO39GIjzneuO3QV8qJmQ+EUADgSym5kLC0JO4D1JJwGT36d HruSbvmnpIjkvstxwzloGEdSqNctlhO3KJ9U3So6/Mcuue7Dow2uP7bSERTTC5BLyVrK 63Ag== X-Forwarded-Encrypted: i=1; AJvYcCUl+B754ON3p3zBxX6ijGZHWEQg3pjxg4PVE6NIYCerMkHwpJzUoqVYHRG/JEJMyo/2aQLUkHFs/GkF6gsD4qsQVzw= X-Gm-Message-State: AOJu0YzpxWIeaxfWQcZOFnZmXiNqWLxgNeJBaNfI+X6sjKtRAlzyZokU 4Z68g7LnCZJpveP9MenejMtBR1zgv+tz3OEapoM6yPPpAIs5gqI2 X-Google-Smtp-Source: AGHT+IHI1rET3Z0/K8864m6GkWZwYbKsdcM+bbhi3ZQPOLm9p/XFzMkvrTJzDnJ+bGJeFJuwnOye6Q== X-Received: by 2002:a05:6a00:92a2:b0:6e4:84db:e30e with SMTP id jw34-20020a056a0092a200b006e484dbe30emr186581pfb.32.1709250214399; Thu, 29 Feb 2024 15:43:34 -0800 (PST) Received: from localhost.localdomain ([2620:10d:c090:400::5:8f17]) by smtp.gmail.com with ESMTPSA id r8-20020aa79ec8000000b006e50cedb59bsm1850608pfq.16.2024.02.29.15.43.32 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 29 Feb 2024 15:43:33 -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 v3 bpf-next 3/3] mm: Introduce VM_SPARSE kind and vm_area_[un]map_pages(). Date: Thu, 29 Feb 2024 15:43:16 -0800 Message-Id: <20240229234316.44409-4-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-145) In-Reply-To: <20240229234316.44409-1-alexei.starovoitov@gmail.com> References: <20240229234316.44409-1-alexei.starovoitov@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 9AB7E180012 X-Stat-Signature: x3z8r3wuyybeo4s8te845q7wdsmxrtha X-HE-Tag: 1709250215-937003 X-HE-Meta: U2FsdGVkX1/l5IzBrEpcrfZSb2+T7qmwUpp4W5p1wlNGU+WvtRjthBJ9A1xPimefhxGwuN7Zcnh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S8ZejDZDHtuAC9XYjmkC9L9QWGOu9 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: List-Subscribe: List-Unsubscribe: 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 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: // request virtual memory region during bpf prog verification area = get_vm_area(area_size, VM_SPARSE); // on demand vm_area_map_pages(area, kaddr, kend, pages); vm_area_unmap_pages(area, kaddr, kend); // after bpf program is detached and unloaded free_vm_area(area); Signed-off-by: Alexei Starovoitov --- include/linux/vmalloc.h | 5 ++++ mm/vmalloc.c | 59 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h index 71075ece0ed2..dfbcfb9f9a08 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 grant table and xenbus 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,10 @@ static inline bool is_vm_area_hugepages(const void *addr) } #ifdef CONFIG_MMU +int vm_area_map_pages(struct vm_struct *area, unsigned long start, + unsigned long end, struct page **pages); +void vm_area_unmap_pages(struct vm_struct *area, unsigned long start, + unsigned long end); 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 d53ece3f38ee..dae98b1f78a8 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -648,6 +648,58 @@ static int vmap_pages_range(unsigned long addr, unsigned long end, return err; } +static int check_sparse_vm_area(struct vm_struct *area, unsigned long start, + unsigned long end) +{ + 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 ((end - start) >> PAGE_SHIFT > totalram_pages()) + return -E2BIG; + if (start < (unsigned long)area->addr || + (void *)end > area->addr + get_vm_area_size(area)) + return -ERANGE; + return 0; +} + +/** + * vm_area_map_pages - map pages inside given sparse vm_area + * @area: vm_area + * @start: start address inside vm_area + * @end: end address inside vm_area + * @pages: pages to map (always PAGE_SIZE pages) + */ +int vm_area_map_pages(struct vm_struct *area, unsigned long start, + unsigned long end, struct page **pages) +{ + int err; + + err = check_sparse_vm_area(area, start, end); + if (err) + return err; + + return vmap_pages_range(start, end, PAGE_KERNEL, pages, PAGE_SHIFT); +} + +/** + * vm_area_unmap_pages - unmap pages inside given sparse vm_area + * @area: vm_area + * @start: start address inside vm_area + * @end: end address inside vm_area + */ +void vm_area_unmap_pages(struct vm_struct *area, unsigned long start, + unsigned long end) +{ + if (check_sparse_vm_area(area, start, end)) + return; + + vunmap_range(start, end); +} + int is_vmalloc_or_module_addr(const void *x) { /* @@ -3822,9 +3874,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 +4470,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");