From nobody Wed Oct 25 16:56:41 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4SFw7Y5yYPz4xl4S; Wed, 25 Oct 2023 16:56:41 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4SFw7Y5Fpgz4bxc; Wed, 25 Oct 2023 16:56:41 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1698253001; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=k7ScKYwi3hhgl7PcfyE2gmHffyr25Rwr2ZeNoGpj9IA=; b=NuZr5fTF88ivlWk8sXkEFrJGDkpRNi+Bgg4xwvGnc2+2Iiz1ueyNsGlJfcyhlIknR4s1h2 pNnkKVhcfcEMOWawmC1Vw9mZzOw1GPRR4OMwaAoSbBCmBagLcoZwYvwt59yi6iQeDTQsfp R7o9j4Pc/xUV5Fu39B8XqMgkdPWju8bdh6dcsOLHqV4xu9hlD77ptIc7rDkLLgzQfmM6P9 tEjvoEnRl9q23iReQyGksAaUFHdw+sAgeaNNz8ayvTGinyGpqvTPRViaL0mdXe5kmiYndA oSOgqOZ5FWjG1k9fEWRMQR670rbxWDB6LeiZc4F7K87jAk5ytLcc2YCko7TidA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1698253001; a=rsa-sha256; cv=none; b=iVwxSdo5eAcIpFwtVGK2Xz7vg/my1/iEz5Wwq/kjZMD+rcBCqABfeOOa/Oj+ASfyxUNcgL zPamMoG2xIA7Ijncia4SmNmPacg+xO64etDqKt67lyHj6sGX8vbOw6fHvXjC72/0Daw5+C vZNaJE31MHjW3CmKLOHXixTOYVPqjiGB7B2+2/rNkPGp56Oj4ymcWOKlCp9YtmD65pM8lW y5ZIwtaXLMKVh8+onL5PQBfh1jwamSKMWNAUwhud77xWPhf3R+fg66qLvG+SrdLi83stV5 JLkH6VoKY7KTQhTfO2pD3AqBjLElGTg+K1AiLtkAlp/AoNL36RaGCLQ6atfpkQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1698253001; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=k7ScKYwi3hhgl7PcfyE2gmHffyr25Rwr2ZeNoGpj9IA=; b=ODyqKOiiZ770jF3OxtBP9ZYdh5meDCzQj89fSL5hUgEqEmbeZRaQ6FNh2tOTmHA4c5f1o0 N6K7HRSCJayiDjyGifMeugaWr04gvXyRcaARxz+STl+qUFYJdBBNPkzEHPPiwbkQk+YWWV 85azoXZXrPjrQYfbq6KNtBE6kcRt1eEYDCPmokHL54NTdZfuxiEssJbnHGb5tDTERGs7Vv IQGVGDAfT2ey6f5FcwrLF8TmL824RCWjbSjTxfKIyF6a5azqn5UCr64eWedHvwum6OKqxA TS0tYDESFzpGpT0oe581VjmDfeP3mjtQzp8p8H+FFuzWavh3Njb4T9vTJtBk+w== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4SFw7Y48V9z1Brr; Wed, 25 Oct 2023 16:56:41 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 39PGufSi021919; Wed, 25 Oct 2023 16:56:41 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 39PGufSW021916; Wed, 25 Oct 2023 16:56:41 GMT (envelope-from git) Date: Wed, 25 Oct 2023 16:56:41 GMT Message-Id: <202310251656.39PGufSW021916@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mark Johnston Subject: git: 87dbb943df73 - releng/14.0 - linuxkpi: Handle direct-mapped addresses in linux_free_kmem() List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/releng/14.0 X-Git-Reftype: branch X-Git-Commit: 87dbb943df73022dd98487c123aeb125da11c4af Auto-Submitted: auto-generated The branch releng/14.0 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=87dbb943df73022dd98487c123aeb125da11c4af commit 87dbb943df73022dd98487c123aeb125da11c4af Author: Mark Johnston AuthorDate: 2023-10-17 14:26:18 +0000 Commit: Mark Johnston CommitDate: 2023-10-25 16:53:01 +0000 linuxkpi: Handle direct-mapped addresses in linux_free_kmem() See the analysis in PR 271333. It is possible for driver code to allocate a page, store its address as returned by page_address(), then call free_page() on that address. On most systems that'll result in the LinuxKPI calling kmem_free() with a direct-mapped address, which is not legal. Fix the problem by making linux_free_kmem() check the address to see whether it's direct-mapped or not, and handling it appropriately. Approved by: re (gjb) PR: 271333, 274515 Reviewed by: hselasky, bz Tested by: trasz MFC after: 1 week Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D40028 (cherry picked from commit 6223d0b67af923f53d962a9bf594dc37004dffe8) (cherry picked from commit 4862eb8604d503b52e7c3aa7ff32155b75a1ff93) --- sys/compat/linuxkpi/common/src/linux_page.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/sys/compat/linuxkpi/common/src/linux_page.c b/sys/compat/linuxkpi/common/src/linux_page.c index ce9ad34464bd..21e338acb089 100644 --- a/sys/compat/linuxkpi/common/src/linux_page.c +++ b/sys/compat/linuxkpi/common/src/linux_page.c @@ -145,6 +145,14 @@ linux_alloc_pages(gfp_t flags, unsigned int order) return (page); } +static void +_linux_free_kmem(vm_offset_t addr, unsigned int order) +{ + size_t size = ((size_t)PAGE_SIZE) << order; + + kmem_free((void *)addr, size); +} + void linux_free_pages(struct page *page, unsigned int order) { @@ -163,7 +171,7 @@ linux_free_pages(struct page *page, unsigned int order) vaddr = (vm_offset_t)page_address(page); - linux_free_kmem(vaddr, order); + _linux_free_kmem(vaddr, order); } } @@ -185,9 +193,17 @@ linux_alloc_kmem(gfp_t flags, unsigned int order) void linux_free_kmem(vm_offset_t addr, unsigned int order) { - size_t size = ((size_t)PAGE_SIZE) << order; + KASSERT((addr & PAGE_MASK) == 0, + ("%s: addr %p is not page aligned", __func__, (void *)addr)); - kmem_free((void *)addr, size); + if (addr >= VM_MIN_KERNEL_ADDRESS && addr < VM_MAX_KERNEL_ADDRESS) { + _linux_free_kmem(addr, order); + } else { + vm_page_t page; + + page = PHYS_TO_VM_PAGE(DMAP_TO_PHYS(addr)); + linux_free_pages(page, order); + } } static int