From patchwork Fri Jan 31 00:18:05 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Vishal Moola (Oracle)" X-Patchwork-Id: 13955034 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 9A428C0218A for ; Fri, 31 Jan 2025 00:19:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 325242800DD; Thu, 30 Jan 2025 19:19:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2AF0D2800DC; Thu, 30 Jan 2025 19:19:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 128852800DD; Thu, 30 Jan 2025 19:19:31 -0500 (EST) 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 E09AD2800DC for ; Thu, 30 Jan 2025 19:19:30 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 12CF31C7155 for ; Fri, 31 Jan 2025 00:19:30 +0000 (UTC) X-FDA: 83065838100.08.033FEE1 Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) by imf08.hostedemail.com (Postfix) with ESMTP id 3F0C216000A for ; Fri, 31 Jan 2025 00:19:28 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=j+gp1gr4; spf=pass (imf08.hostedemail.com: domain of vishal.moola@gmail.com designates 209.85.216.42 as permitted sender) smtp.mailfrom=vishal.moola@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738282768; 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=TyKozpykEAjFCoENnU5v7/kSmkHMXuR/AybReX8XXa8=; b=Lo5AsTXyonUHAN/RQ0atnDaC1fET0P+k8lCYHUHXrC24ETTDoiVgdKnRJgGAHedPqTfriR wVTXTQegv8EEjzkWSQsAitLnc7yZ2wRaGmlGCdPbJ6W93sfpziRQruo2f+4+xRK5JZmjqX rtmCTV4dc9zZhIVToNQdGM41OtH/nP0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738282768; a=rsa-sha256; cv=none; b=WT7Pojj+itmFKbfZ/BQDZ4aIy55inNAE4hRAU5wU3lahpJ99hhPwKQyNhDavAXe+UGgPUB FFMXGLHcctzouFgAaGMBLx/QyL2rz1hXngMGe/xPfi+u6wQn3mPCjHvTdz5hRdeM68qCuV CkWiO1kw37K2YO+b3TaZ1HNxQ9E+cpY= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=j+gp1gr4; spf=pass (imf08.hostedemail.com: domain of vishal.moola@gmail.com designates 209.85.216.42 as permitted sender) smtp.mailfrom=vishal.moola@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-2ee50ffcf14so4013247a91.0 for ; Thu, 30 Jan 2025 16:19:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1738282767; x=1738887567; 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=TyKozpykEAjFCoENnU5v7/kSmkHMXuR/AybReX8XXa8=; b=j+gp1gr4U+hGcPwmLwl07IbToLk7XIE9GWAqCcfSAyN4gZZsqXs61ONWWMkxi7eKME ywby1oIyUr6qc3Oq32JUvcD6+5nebOuUrxLzPvUFN94iz0oA7XDQnrwq5ZjXieJbH3YU kzr2CizxnHeEJXXNbz7XCnKjDlAWuwHH6LmMipf2H1e8B7HeZTiVjS7L2H/By0kfa82D 2KSOe3f+Wa4EdG2ZhmUkhYQiGWZKTMt47ePj94wzodBLvt/aC99s9RlhZwjA22MyUQ0w alLtVYSsheHPPtOuDPDbuqUGC+UADQuBEIeajwd0+QnYpEEjGty89O4+qZ9DLEotu68x y3ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738282767; x=1738887567; 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=TyKozpykEAjFCoENnU5v7/kSmkHMXuR/AybReX8XXa8=; b=QBFtoFzf9wp4FeM/hJnlXtFbm9CKDxIpGuNl9ikBllyXpSRJzlxcJgtmol0SYutmO9 39dUqmTEXvlpt0Sa6ID6pQQ4memjNFnz8bPENHcEDmtDygR8xwxjTwrcnkwTAZylzqa+ I/3dHSadkuJbylOspe/kz7r5s7XKlynIuL06fVZGDBeKzEQt/Iq4hbap2NVMxgkd21fT Dj+FrC2gBn3nsjpIsYzWbqc2v/wuxT2ybaT6KNW/IYDwP/l1ubNjciVS8N4FeXTQLHlc oTRy3yEMrD8UhFdAoczeZF+48IcJWzzUgKI1cwgvE2adCGpxCaTRgBho7A0LM0hm8PXl rJDQ== X-Gm-Message-State: AOJu0YwKki1fZ6fuSbFxHLUFmRzpiYJYKLHH4WHn2Od715olYQqGRRQ2 yo9MCC3+tZuvWww+HiFOCJNkKvMnMMzpiKOvGlfJ7VAGv2iRdaHP X-Gm-Gg: ASbGnct57kapgidK8zHpeA+xTOhCTw3zgvapepxYmzgKmiVwpbVoY4rzYN6FXcp8GKU E6a7FATs6WsyVCNKiRKd+5MGVyGcWX9JFbBWfYEDOZwG6bo9xZG5meLP5JR9k1bMvc0qnylR2gV cOUuY0YeGOOXud6kugFwY3PInmVFMBq1sz3rDpHSzhNLkBMhH1ucj/CVww6I9r+P/iAC6dQbqe8 8hkUiocZPXpDurQkeyG31xWV4WCCHPnu0j+9GLju+GIU59i7aZUI2sLZFE3hjotKMaG2dKDA1OR wccCLZePGuMTUd7kpQtgrpY5xAae4Qc+lONXApXeXLbmG7ejTw== X-Google-Smtp-Source: AGHT+IGTcJ0wEj7nw/VAr9mFtCvAOb9swQCbE3NNKbAN9EVVCz2Q0TQQOwiAKxPFXeABEt6KtjxpUQ== X-Received: by 2002:a05:6a00:1386:b0:725:41c4:dbc7 with SMTP id d2e1a72fcca58-72fe2d1f669mr9530361b3a.4.1738282766749; Thu, 30 Jan 2025 16:19:26 -0800 (PST) Received: from fedora.. (c-67-164-59-41.hsd1.ca.comcast.net. [67.164.59.41]) by smtp.googlemail.com with ESMTPSA id d2e1a72fcca58-72fe6429218sm2148234b3a.66.2025.01.30.16.19.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Jan 2025 16:19:26 -0800 (PST) From: "Vishal Moola (Oracle)" To: akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, hch@infradead.org, urezki@gmail.com, intel-gfx@lists.freedesktop.org, "Vishal Moola (Oracle)" Subject: [PATCH 1/2] mm/vmalloc: Introduce vmap_file() Date: Thu, 30 Jan 2025 16:18:05 -0800 Message-ID: <20250131001806.92349-2-vishal.moola@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250131001806.92349-1-vishal.moola@gmail.com> References: <20250131001806.92349-1-vishal.moola@gmail.com> MIME-Version: 1.0 X-Stat-Signature: jg4gmbqnzd8ycs4nptyy9wuf19e3guhw X-Rspam-User: X-Rspamd-Queue-Id: 3F0C216000A X-Rspamd-Server: rspam03 X-HE-Tag: 1738282768-592176 X-HE-Meta: U2FsdGVkX1/s1zuMzX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N+gde/o1qhvLbsd2Ux5W6BKOXaoAuvrmXLrpk4ZWvXJVE9kj/YdeB1L5a/HyOLJvg2xAiauuf2YSZH1ZA6IaYfKUbHz2F7IRuOmWxzfVOBrlPycf4n7n5ew3eyNxDhQ0BoOfEHuHaJxWHaESfmP0XSDIbwJAba426uTE10dhp8EsxwaF8fSptdB896JagquvJ9nc87MywPjM7R1PLxFTt2KhIw8qmet/8u00v3RK+reDZ73Dcn6PFylawr1LmAi++Bpd3V+3kGCNThmbWbvcKNmUrFMqq5symR9QeMVnAYau7MFEVYX/Ktk1GuZXXdNBWA+wIi7EVpdn5dBXwVH//xTUp6wAF4ySkkJXh/R7iVjDU9lYBq9tLKL1+t6Am1YGCCtTbT21CxUis2Odi20ZjTDA= 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: vmap_file() is effectively an in-kernel equivalent to calling mmap() on a file. A user can pass in a file mapping, and vmap_file() will map the specified portion of that file directly to kernel virtual space. Signed-off-by: Vishal Moola (Oracle) --- include/linux/vmalloc.h | 2 + mm/vmalloc.c | 97 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h index 31e9ffd936e3..d5420985865f 100644 --- a/include/linux/vmalloc.h +++ b/include/linux/vmalloc.h @@ -192,6 +192,8 @@ extern void vfree_atomic(const void *addr); extern void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot); +void *vmap_file(struct address_space *mapping, loff_t start, loff_t end, + unsigned long flags, pgprot_t prot); void *vmap_pfn(unsigned long *pfns, unsigned int count, pgprot_t prot); extern void vunmap(const void *addr); diff --git a/mm/vmalloc.c b/mm/vmalloc.c index a6e7acebe9ad..4b1e31a8aad9 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -3475,6 +3475,103 @@ void *vmap(struct page **pages, unsigned int count, } EXPORT_SYMBOL(vmap); +/** + * vmap_file - map all folios in a file to virtually contiguous space. + * @mapping: The address space to map. + * @start: The starting byte. + * @end: The final byte to map. + * @flags: vm_area->flags. + * @prot: page protection for the mapping. + * + * Maps a file into contiguous kernel virtual space. The caller is expected + * to ensure that the folios caching the file are present and uptodate. The + * folios must remain so until the file is unmapped. + * + * If @start or @end are not PAGE_ALIGNED, vmap_file() will round + * @start down and @end up to encompass the entire range. The + * address returned is always PAGE_ALIGNED. + * + * Return: the address of the area or %NULL on failure. + */ +void *vmap_file(struct address_space *mapping, loff_t start, loff_t end, + unsigned long flags, pgprot_t prot) +{ + struct vm_struct *area; + struct folio *folio; + unsigned long addr; + pgoff_t first = start >> PAGE_SHIFT; + pgoff_t last = end >> PAGE_SHIFT; + XA_STATE(xas, &mapping->i_pages, first); + + unsigned long size = (last - first + 1) << PAGE_SHIFT; + + if (WARN_ON_ONCE(flags & VM_FLUSH_RESET_PERMS)) + return NULL; + + /* + * Your top guard is someone else's bottom guard. Not having a top + * guard compromises someone else's mappings too. + */ + if (WARN_ON_ONCE(flags & VM_NO_GUARD)) + flags &= ~VM_NO_GUARD; + + area = get_vm_area_caller(size, flags, __builtin_return_address(0)); + if (!area) + return NULL; + + addr = (unsigned long) area->addr; + + rcu_read_lock(); + xas_for_each(&xas, folio, last) { + int err; + bool pmd_bound; + + if (xas_retry(&xas, folio)) + continue; + if (!folio || xa_is_value(folio) || + !folio_test_uptodate(folio)) + goto out; + + /* We need to check if this folio will cross the pmd boundary. + * If it does, we drop the rcu lock to allow for a new page + * table allocation. + */ + + pmd_bound = (addr == (unsigned long) area->addr) || + (IS_ALIGNED(addr, PMD_SIZE)) || + ((addr & PMD_MASK) != + ((addr + folio_size(folio)) & PMD_MASK)); + + if (pmd_bound) { + xas_pause(&xas); + rcu_read_unlock(); + } + + err = vmap_range_noflush(addr, addr + folio_size(folio), + folio_pfn(folio) << PAGE_SHIFT, prot, + PAGE_SHIFT); + + if (pmd_bound) + rcu_read_lock(); + + if (err) { + vunmap(area->addr); + area->addr = NULL; + goto out; + } + + addr += folio_size(folio); + } + +out: + rcu_read_unlock(); + flush_cache_vmap((unsigned long)area->addr, + (unsigned long)area->addr + size); + + return area->addr; +} +EXPORT_SYMBOL(vmap_file); + #ifdef CONFIG_VMAP_PFN struct vmap_pfn_data { unsigned long *pfns;