commit:     961b92133cc9353908d0d58d5bc8ba6d2959d9d6
Author:     Sam James <sam <AT> gentoo <DOT> org>
AuthorDate: Tue Dec 16 04:54:03 2025 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Tue Dec 16 04:56:32 2025 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=961b9213

sci-libs/openblas: fix memory allocation issues

* Backport upstream fix to not spin in blas_memory_alloc on ENOMEM
  (bug #967251). Combined with the high defaults for NUM_PARALLEL and
  NUM_THREADS set by the ebuild, flexiblas configure would hang.

* Choose lower values for NUM_PARALLEL, NUM_THREADS on 32-bit arches
  (bug #967251).

* Document the current issue w/ overcommit (bug #967026).

Thank you to Anon Emuss for the detailed bug report and analysis.

Bug: https://bugs.gentoo.org/967026
Closes: https://bugs.gentoo.org/967251
Tested-by: Eli Schwartz <eschwartz <AT> gentoo.org>
Signed-off-by: Sam James <sam <AT> gentoo.org>

 .../files/openblas-0.3.30-enomem-check.patch       | 46 ++++++++++++++++++++++
 ...-0.3.30-r4.ebuild => openblas-0.3.30-r5.ebuild} | 26 +++++++++++-
 2 files changed, 71 insertions(+), 1 deletion(-)

diff --git a/sci-libs/openblas/files/openblas-0.3.30-enomem-check.patch 
b/sci-libs/openblas/files/openblas-0.3.30-enomem-check.patch
new file mode 100644
index 000000000000..bf2e1d4035a8
--- /dev/null
+++ b/sci-libs/openblas/files/openblas-0.3.30-enomem-check.patch
@@ -0,0 +1,46 @@
+https://bugs.gentoo.org/967251
+https://github.com/OpenMathLib/OpenBLAS/issues/5289
+https://github.com/OpenMathLib/OpenBLAS/pull/5303
+
+From 31ef2cbbb353c1331a22ed4f909a6ffb5cc45b45 Mon Sep 17 00:00:00 2001
+From: Martin Kroeker <[email protected]>
+Date: Fri, 13 Jun 2025 14:11:03 +0200
+Subject: [PATCH] Exit if memory allocation keeps failing, instead of looping
+ forever
+
+---
+ driver/others/memory.c | 13 +++++++++++--
+ 1 file changed, 11 insertions(+), 2 deletions(-)
+
+diff --git a/driver/others/memory.c b/driver/others/memory.c
+index c53e798bc1..c8415f348e 100644
+--- a/driver/others/memory.c
++++ b/driver/others/memory.c
+@@ -2922,6 +2922,7 @@ void *blas_memory_alloc(int procpos){
+   blas_unlock(&memory[position].lock);
+ #endif
+   if (!memory[position].addr) {
++    int failcount = 0;
+     do {
+ #ifdef DEBUG
+       printf("Allocation Start : %lx\n", base_address);
+@@ -2973,8 +2974,16 @@ void *blas_memory_alloc(int procpos){
+ #ifdef DEBUG
+       printf("  Success -> %08lx\n", map_address);
+ #endif
+-      if (((BLASLONG) map_address) == -1) base_address = 0UL;
+-
++      if (((BLASLONG) map_address) == -1) {
++            base_address = 0UL;
++            failcount++;
++            if (failcount >10) {
++                    fprintf(stderr, "OpenBLAS error: Memory allocation still 
failed after 10 retries, giving up.\n");
++                    exit(1);
++            }
++      } else {
++            failcount = 0;
++      }
+       if (base_address) base_address += BUFFER_SIZE + FIXED_PAGESIZE;
+ 
+     } while ((BLASLONG)map_address == -1);
+

diff --git a/sci-libs/openblas/openblas-0.3.30-r4.ebuild 
b/sci-libs/openblas/openblas-0.3.30-r5.ebuild
similarity index 88%
rename from sci-libs/openblas/openblas-0.3.30-r4.ebuild
rename to sci-libs/openblas/openblas-0.3.30-r5.ebuild
index c6c72fa54cb9..ec3f26c2ff3a 100644
--- a/sci-libs/openblas/openblas-0.3.30-r4.ebuild
+++ b/sci-libs/openblas/openblas-0.3.30-r5.ebuild
@@ -36,6 +36,8 @@ PATCHES=(
        "${FILESDIR}/${P}-lunar-lake.patch"
        # https://github.com/OpenMathLib/OpenBLAS/pull/5360
        "${FILESDIR}/${P}-arm-assembly.patch"
+       # https://github.com/OpenMathLib/OpenBLAS/pull/5303
+       "${FILESDIR}/${P}-enomem-check.patch"
 )
 
 pkg_pretend() {
@@ -109,7 +111,29 @@ src_configure() {
                export DYNAMIC_ARCH=1 NO_AFFINITY=1 TARGET=GENERIC
        fi
 
-       export NUM_PARALLEL=${OPENBLAS_NPARALLEL:-8} 
NUM_THREADS=${OPENBLAS_NTHREAD:-64}
+       case $(tc-get-ptr-size) in
+               4)
+                       # NUM_BUFFERS = MAX(50, (2*NUM_PARALLEL*NUM_THREADS)
+                       # BUFFER_SIZE = (16 << 20) (on x86)
+                       # NUM_BUFFERS * BUFFER_SIZE is allocated and must be
+                       # <4GiB on 32-bit arches (bug #967251).
+                       #
+                       # Scale down to 2*8*(16 << 20) = 256MiB for 32-bit
+                       # arches. This avoids spinning in blas_memory_alloc
+                       # which doesn't handle ENOMEM.
+                       export NUM_PARALLEL=${OPENBLAS_NPARALLEL:-2}
+                       export NUM_THREADS=${OPENBLAS_NTHREAD:-8}
+                       ;;
+               8)
+                       # XXX: The current values here rely on overcommit
+                       # for most systems (bug #967026).
+                       export NUM_PARALLEL=${OPENBLAS_NPARALLEL:-8}
+                       export NUM_THREADS=${OPENBLAS_NTHREAD:-64}
+                       ;;
+               *)
+                       die "Unexpected tc-get-ptr-size. Please file a bug."
+                       ;;
+       esac
 
        # Allow setting OPENBLAS_TARGET to override auto detection in case the
        # toolchain is not enough to detect.

Reply via email to