From nobody Tue Apr 8 13:41:15 2025 X-Original-To: dev-commits-src-branches@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 4ZX6g02J7qz5sQJP; Tue, 08 Apr 2025 13:41:16 +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 4ZX6fz61zlz3DtW; Tue, 08 Apr 2025 13:41:15 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1744119675; 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=zY+dW3+QSiVos8MCRjSbxBJmmA7x4PIPaHVObY5FozA=; b=PQDfCmShuomMptkS/89DsttVh7ACC5Zh6DSEp3nFu4J+dJSGh/euPon0hqtK/4Vuga2iMY 4dHNaJPrZXLP/VaVWS7QjjMSRpF/GYhzCkmqAw/H1A1CqHzm07qzwwPWHzR9OE6BGhPAhF 5g7PBkbgNEG9FzvBayAmwjggHzbC/ru/jYOXfIntMeDxkcbK2BH/yAVDa2zAR+635gS0/d r203b8IoPiMEuFHek8qI/diyi9mQ4Cfv1PlBw7awg7UBreA3CZ4GkJUjhcdVLeoFs5k6CI zmh4J822n7ZQPWNNmxc6KCoWwl1LDoJFP9znYys5Ae+6UQb6gSYG+g49hyJ3dQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1744119675; a=rsa-sha256; cv=none; b=Q3WVz+XKDWkt9qXpLR5QDBwKiIg8EimdCSQ+Wl/vudrnKJMFMNOuF18AMbtFa/igejyi8H /BUhWigaImXiHL0k1+tjixEt9jAWA7rbsd3slizXmpjFnIwc3DtdEEbh71pXL1vrlD70ug ZHd89pEK7Up0Ot6pz2GSNIP2LT4BVru9tSvurIGdNN513nlsfVPGIukOObsUjMp5dj2iPX G1gGk20S6+vyRLnP5D7tq38KgfEkDNObf9dq5K4GY1D327h4G4wR19WisVZ2DH2Zr/3o4R 3kjMZARccfPx82bWqmPp9HuA9pCNG6gRczX4HKuXqpmcDGo6HTjCpTuHAL7e9A== 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=1744119675; 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=zY+dW3+QSiVos8MCRjSbxBJmmA7x4PIPaHVObY5FozA=; b=PSXNZD+qSKxLxTkGvhiOJksSq0s2ujMP+6MTn1+KNCHmLhpNVMh/V3QUDYWPG+BJkcDO9J HbF4NCTUam2mS7wBVKiZDBsczoXEj/fjct1i+ntCBWWa/uG7A77n5T970Wl0pPOFyk9yJb Zkr0eFFzG1KBJI/W6OLeP/v4bDaMGryinp3d8rHliEHajajRn+Si4XHRNRkwp+P/Cuh/D6 349p3zMGT/2ouiqUEDGMIQ5y2hMU4cr45bnLm1RoWf5hWlCwGgrYoVHTFMJo9qtcXo91Kj ElSxG30ym7cAe0HQx1euOOzMdmRw5WZ9DTaGPzlvWkoDlKzn5nHIm5tu7N9c6A== 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 4ZX6fz5bkhzbD4; Tue, 08 Apr 2025 13:41:15 +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 538DfFBr034639; Tue, 8 Apr 2025 13:41:15 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 538DfF46034636; Tue, 8 Apr 2025 13:41:15 GMT (envelope-from git) Date: Tue, 8 Apr 2025 13:41:15 GMT Message-Id: <202504081341.538DfF46034636@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Olivier Certner Subject: git: 831e6fb0baf6 - stable/14 - LinuxKPI: make linux_alloc_pages() honor __GFP_NORETRY List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-branches@freebsd.org Sender: owner-dev-commits-src-branches@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/stable/14 X-Git-Reftype: branch X-Git-Commit: 831e6fb0baf67c2421abb50b6a14da9e71c183bb Auto-Submitted: auto-generated The branch stable/14 has been updated by olce: URL: https://cgit.FreeBSD.org/src/commit/?id=831e6fb0baf67c2421abb50b6a14da9e71c183bb commit 831e6fb0baf67c2421abb50b6a14da9e71c183bb Author: Mathieu AuthorDate: 2024-11-14 00:24:02 +0000 Commit: Olivier Certner CommitDate: 2025-04-08 13:38:29 +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) (cherry picked from commit 718d1928f8748fe4429c011296f94f194d63c695) --- 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 e285f8591a3c..a9455a028640 100644 --- a/sys/compat/linuxkpi/common/include/linux/gfp.h +++ b/sys/compat/linuxkpi/common/include/linux/gfp.h @@ -44,7 +44,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 @@ -58,7 +57,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 ead2f24cf5df..cc7683e3b572 100644 --- a/sys/compat/linuxkpi/common/src/linux_page.c +++ b/sys/compat/linuxkpi/common/src/linux_page.c @@ -118,7 +118,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) { if (!vm_page_reclaim_contig(req, npages, 0, pmax, PAGE_SIZE, 0)) { vm_wait(NULL);