From nobody Tue Oct 17 15:55:31 2023 X-Original-To: dev-commits-src-main@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 4S8z8g6ZJjz4xgQn; Tue, 17 Oct 2023 15:55:31 +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 4S8z8g659jz4DwH; Tue, 17 Oct 2023 15:55:31 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1697558131; 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=odmS3avjerTaBzlrWdql1GWlDG4JEAoPmAiFpymuYhY=; b=A4WATrV99spBHLrpDPyOmelsoA0/bd7J/+GOZdAiTXS0Db+ZMt0OLKInRWw6CJ3tPLMLDf NmZxyi8VMJN1ox3GTACDdsXiKtQFXd4nfPlKJmGOgcs67y5ooVXae+HA3T3l6qFoESLz7e YoFBv3ucF7JTGUVsdD9PW0f9E+yktg4ORvJicad/beJwosKaIela5VnT4RV6DVZg1wlKGt 3QFwkAd0Y14+yt4GZNSCqqJmlk1Ke00ZnqMfetQq0k59KRnSVh0KyBWh60/Pd6VoxieBTr RFQWEPG4W0InUiEqPDonIgUM6yntTF+RIf4Zjq4Dg4qPuDrspjClbFoXcrfw8w== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1697558131; a=rsa-sha256; cv=none; b=raL7plVSWIl4ezd4iEIGFRQI7ZGylrfFP9nYBIx0R6oZtANNrC56ravvIAf1XOylw9mfzG 9zPfbTuPjDUUBSW+IDy72Q+knKOIWoFnz4e5SdqeTHhRnnEGTseCrLpu2dlRULUmLc+BZn 7uVqHAxMV4gmVZo3nCm6HEh3IzO0Pp22bVuqnJE0Q8qiaTeWGjtBgrqmwkSoySe9NNOQ40 ZUI6ccXoAeIor4AkWumjfVX8m8ATmTAN2LszoBnLGZn2qPoyEfDyGIEWnFMs794b+dJmrH GTOBuY4a5Ppi15Dbtb1wVeyMOaItIlyhnPhE2S3+dhhMn/oPWAbmMiMnPA3+mg== 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=1697558131; 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=odmS3avjerTaBzlrWdql1GWlDG4JEAoPmAiFpymuYhY=; b=JTx787vPs3ZbWVJJsfrNPQ68ddc/AtYty4cMwqLLzIoDw2CCA8UgS0kDZxiI4P4tZXeKnW ulKsD5W0utcaheibxFYtpZ0xpxqJ1pW8iKoGxwCf5FHzlqFj9TW+hpxQdip7qj/HjY5cB9 QxcV1Sl0sFnfPIm//Ry99jkxD+AR2+onKYy6G+NcgDNmUBEaVF/yYM/90wjZ+TkDNntSt/ 3VS5dd4B5rHlbOK/xrtftIxiYyBJweQij22o6lMFpWTsLGMUetpekpRGtw1UxQtQyocB/b u6fd5JUD5tUtKMDP3ZUb52eTcqYXeIVdqFTy3vEgOgiThtKYn9dOG5xPKze+Tw== 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 4S8z8g4vfKzVdt; Tue, 17 Oct 2023 15:55:31 +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 39HFtVOV029021; Tue, 17 Oct 2023 15:55:31 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 39HFtVnn029019; Tue, 17 Oct 2023 15:55:31 GMT (envelope-from git) Date: Tue, 17 Oct 2023 15:55:31 GMT Message-Id: <202310171555.39HFtVnn029019@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: 6223d0b67af9 - main - linuxkpi: Handle direct-mapped addresses in linux_free_kmem() List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@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/main X-Git-Reftype: branch X-Git-Commit: 6223d0b67af923f53d962a9bf594dc37004dffe8 Auto-Submitted: auto-generated The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=6223d0b67af923f53d962a9bf594dc37004dffe8 commit 6223d0b67af923f53d962a9bf594dc37004dffe8 Author: Mark Johnston AuthorDate: 2023-10-17 14:26:18 +0000 Commit: Mark Johnston CommitDate: 2023-10-17 15:19:06 +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. 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 --- 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