From nobody Tue Mar 25 09:19:41 2025 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 4ZMPWf2qgwz5sG47; Tue, 25 Mar 2025 09:19:42 +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 "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4ZMPWf12nLz429Z; Tue, 25 Mar 2025 09:19:42 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1742894382; 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=T7KFnJUYwQnP7aaTm4+PKEl90349Ml04jOVT+WEk+hI=; b=ifDnCS39CSV1l1zetTUlLqG2kn6NbMHqMdV498ypMO6hbk3C3Eb+yYWKGl1YuwihSzfJqj aLBN/pgai/lsLW0H0qwV3rXl0lKEHYbkQqNauIvhO91sNDKC1HkJizMGnQQzsjkc6y125A oIK+HetMW5IhrrMm+qabe9S/RlFDbySZGuAAs6SuIjVaD7Lt4zcRrv2s+C+vJFNZXq/mIe mQdMpYV2P2RXhXozdnHE5+zs9P/bM689oWedFGOCUMvpg8mdCH4uAPUcchy7IPQlHuXz50 tolUrhquw15XiFwpIa4UnoMFv6/NNz+Q6fcvENmgfiF5xNVJrtP/Gqo7UX0Xxg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1742894382; a=rsa-sha256; cv=none; b=inI6Vj9coTp4xfV3IYN0kK8q9eWP6CKBdP+6LuHNS+GFeJbAD7FoERow3dl5/UVuytunAS qdZokQ6OB6moAhTy+3Ghc5yf6KKvkHrjVcnGcFh1PfztGnkBJ+VcsN3IFyacyffzfqT2gQ 6Gm9FSx42mIVK9cz64edi3mvNdnfz2rJ2XvxWxYkS7tVCUbC2YpOFQH/L4BjmsuieB7wxt nh7QIEsFdE/cxO3CoXGYr8oVEnstyDD7lKUtPD/89sZkFuuA2ytWATq+KhMvaDMvXnBYGL at5RKUjJ/tYnVGWIm+9F+4Rel6ChYONZ3Eqg8lshOEuBT88T1iqeyDNFvFqHSg== 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=1742894382; 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=T7KFnJUYwQnP7aaTm4+PKEl90349Ml04jOVT+WEk+hI=; b=EHo3+wKQ0KNqljTDUIxwvpOZMSjdnqro2ij3rJ84NAosxdzMytmeEDMsfdALLH3TacbJeQ dEN496nCrwfuOf7WWVyVQiLnRnbUTyaLeVrvT/JoTLes1kixS8X5sYafcWkB6/FLFudUjr X2je0KoIsEpLm3qeGSXoPeJnMPtbF7n2XxzaL6JH8YjFPm+f/PpDcr2WYjJiQs7gC0xfEx 4yOJQA+PHvewOfq7HoCm8ScaW9EhV6CFpAMKMjbGICL3lHPByU6L52FGdC2jnfo4rC1/fu Uy2RDzHT7JRhRBxffZbXRF/ZmnmSLSWd87TgNbl7S0s7Lf3bXiGRdWlJAnE2Pg== 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 4ZMPWf0M9VzbLG; Tue, 25 Mar 2025 09:19:42 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 52P9JfN7032705; Tue, 25 Mar 2025 09:19:41 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 52P9JfOA032702; Tue, 25 Mar 2025 09:19:41 GMT (envelope-from git) Date: Tue, 25 Mar 2025 09:19:41 GMT Message-Id: <202503250919.52P9JfOA032702@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Olivier Certner Subject: git: 718d1928f874 - main - LinuxKPI: make linux_alloc_pages() honor __GFP_NORETRY 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: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: olce X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 718d1928f8748fe4429c011296f94f194d63c695 Auto-Submitted: auto-generated The branch main has been updated by olce: URL: https://cgit.FreeBSD.org/src/commit/?id=718d1928f8748fe4429c011296f94f194d63c695 commit 718d1928f8748fe4429c011296f94f194d63c695 Author: Mathieu AuthorDate: 2024-11-14 00:24:02 +0000 Commit: Olivier Certner CommitDate: 2025-03-25 08:41:44 +0000 LinuxKPI: make linux_alloc_pages() honor __GFP_NORETRY This is to fix slowdowns with drm-kmod that get worse over time as physical memory become more fragmented (and probably also depending on other factors). Based on information posted in this bug report: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=277476 By default, linux_alloc_pages() retries failed allocations by calling vm_page_reclaim_contig() to attempt to free contiguous physical memory pages. vm_page_reclaim_contig() does not always succeed and calling it can be very slow even when it fails. When physical memory is very fragmented, vm_page_reclaim_contig() can end up being called (and failing) after every allocation attempt. This could cause very noticeable graphical desktop hangs (which could last seconds). The drm-kmod code in question attempts to allocate multiple contiguous pages at once but does not actually require them to be contiguous. It can fallback to doing multiple smaller allocations when larger allocations fail. It passes alloc_pages() the __GFP_NORETRY flag in this case. This patch makes linux_alloc_pages() fail early (without retrying) when this flag is passed. [olce: The problem this patch fixes is longer and longer GUI freezes as a machine's memory gets filled and becomes fragmented, when using amdgpu from DRM kmod 5.15 and DRM kmod 6.1 (DRM kmod 5.10 is unaffected; newer Linux kernel introduced an "optimization" by which a pool of pages is filled preferentially with contiguous pages, which triggered the problem for us). The original commit message above evokes freezes lasting seconds, but I occasionally witnessed some lasting tens of minutes, rendering a machine completely useless. The patch has been reviewed for its potential impacts to other LinuxKPI parts and our existing DRM kmods' code. In particular, there is no other user of __GFP_NORETRY/GFP_NORETRY with Linux's alloc_pages*() functions in our tree or DRM kmod ports. It has also been tested extensively, by me for months against 14-STABLE and sporadically on -CURRENT on a RX580, and by several others as reported below and as is visible in more details in the quoted bugzilla PR and in the initial drm-kmod issue at https://github.com/freebsd/drm-kmod/issues/302, on a variety of other AMD GPUs (several RX580, RX570, Radeon Pro WX5100, Green Sardine 5600G, Ryzen 9 4900H with embedded Renoir).] PR: 277476 Reported by: Josef 'Jeff' Sipek Reviewed by: olce Tested by: many (olce, Pierre Pronchery, Evgenii Khramtsov, chaplina, rk) MFC after: 2 weeks Relnotes: yes Sponsored by: The FreeBSD Foundation (review and part of testing) --- sys/compat/linuxkpi/common/include/linux/gfp.h | 4 ++-- sys/compat/linuxkpi/common/src/linux_page.c | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/sys/compat/linuxkpi/common/include/linux/gfp.h b/sys/compat/linuxkpi/common/include/linux/gfp.h index bd8fa1a18372..35dbe3e2a436 100644 --- a/sys/compat/linuxkpi/common/include/linux/gfp.h +++ b/sys/compat/linuxkpi/common/include/linux/gfp.h @@ -43,7 +43,6 @@ #define __GFP_NOWARN 0 #define __GFP_HIGHMEM 0 #define __GFP_ZERO M_ZERO -#define __GFP_NORETRY 0 #define __GFP_NOMEMALLOC 0 #define __GFP_RECLAIM 0 #define __GFP_RECLAIMABLE 0 @@ -57,7 +56,8 @@ #define __GFP_KSWAPD_RECLAIM 0 #define __GFP_WAIT M_WAITOK #define __GFP_DMA32 (1U << 24) /* LinuxKPI only */ -#define __GFP_BITS_SHIFT 25 +#define __GFP_NORETRY (1U << 25) /* LinuxKPI only */ +#define __GFP_BITS_SHIFT 26 #define __GFP_BITS_MASK ((1 << __GFP_BITS_SHIFT) - 1) #define __GFP_NOFAIL M_WAITOK diff --git a/sys/compat/linuxkpi/common/src/linux_page.c b/sys/compat/linuxkpi/common/src/linux_page.c index bece8c954bfd..b5a0d34a6ad7 100644 --- a/sys/compat/linuxkpi/common/src/linux_page.c +++ b/sys/compat/linuxkpi/common/src/linux_page.c @@ -117,7 +117,8 @@ linux_alloc_pages(gfp_t flags, unsigned int order) page = vm_page_alloc_noobj_contig(req, npages, 0, pmax, PAGE_SIZE, 0, VM_MEMATTR_DEFAULT); if (page == NULL) { - if (flags & M_WAITOK) { + if ((flags & (M_WAITOK | __GFP_NORETRY)) == + M_WAITOK) { int err = vm_page_reclaim_contig(req, npages, 0, pmax, PAGE_SIZE, 0); if (err == ENOMEM)