From nobody Sat Apr 4 07:47:56 2026 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 60F12229B38 for ; Fri, 20 Mar 2026 07:57:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773993459; cv=none; b=L1GPDqTLnb/zv0FH7FWsTB5eb84jUMMODpW8MkmDP1kj0t1xdeOhRWpGPHkWa58IrxFvuIQTm1ZZhKCJcPfpoKuazZi1ifn2j5SuZ+3IfWBM03cOD/ExUdMpifGSCs6F+N3nolWEiW9Ne15O+gLtkvfyUXFkHn2x0l+0NJ/gnGE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773993459; c=relaxed/simple; bh=sj5Epb5EiLreGKH8w64k1d2M8yNh3m9xLPZPmgm4Ryg=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=Art8/8TbM0ni8oKZYg1SPw0o+PjxKUbk8J3vW5wHrGuBzdockEn7ko0pp5n23/FiyXqE1mJNLxkHegBdRmBQs0qsI9noy8c6MW4+2eKrGB7nngM6fE+cAQ7bJ2tqhiTXSWeXSujnUTnSEgaKSd/F1faJWwYwhacMn2HMUXlVvyk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--hmazur.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=YMQ8rGun; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--hmazur.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="YMQ8rGun" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-486fa07f2bbso1467245e9.2 for ; Fri, 20 Mar 2026 00:57:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773993457; x=1774598257; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=sH3eNdN5a36zAeWkkdWtNUuBUS2xJvF1FweZBpquDLE=; b=YMQ8rGunbhX/V2KGl2m/oLo2DYZTmdOSE4Kj5FyML9RIfOj8R4+swESQzm9MEQleRp 2Anb0aMaVriarVa7m56EvQWD1+aIRON0jKIEcgRIEkopkD6FxsluvKHhxuhAaUYuMWdK IwdQMgGNsrkPbI/tVoqmHan1vVJnNn1coj1XPYVWiFAdC9DRabKZARjXyLL32YZvlK/R HSXY8etcLdtF7Teiu6v699yMPl5vKnihh1BtBnlYbVLaTHhY6FeWrgQDQYnhhDrGjtY1 gy5igEm16BBRJjLw99V5ojYbwJUFZlJfdEV5vFsb8DQK8tbEsMBEDFyHQRy4pXHDBV/i CJUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773993457; x=1774598257; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=sH3eNdN5a36zAeWkkdWtNUuBUS2xJvF1FweZBpquDLE=; b=BUcfKQc6J5Cp5j2N+8X5iSBQAis2F+a1NoK28G+rklfUBagjsdERCYK0e0k9RbxG/0 HV5lzdsBRYSIAb2COeyjqEDYXsB29spzbMR0Ig8ddFDKIGxpcMczLpdkmLNJtBw0iZfF EwO7MJjnzdKbGc3MEeHdzjLg2im5NF7xyT44Z4NMoGniSEP1iH7nbhK2FGpvc6nFs7Si n0RDbf21DiftjQncdyUSmZBGImn4F9zHIn59db622X1CofCQo46yFO+GNJiQsm5tFcgS RxeWIQZwH7ovQ/CywcpxhJlhWPmDi94tRTFhfoT+bQIKKRUr1woaYVpyqYCQ2I7/iFJO r+3Q== X-Forwarded-Encrypted: i=1; AJvYcCU8tscI9OBYMG84l7TYT9uH/uTpyLGmhz7QM235n+eRDT2e/MEBH87RqX7MzXRxX9oBdSC/5MsVqGtxM3U=@vger.kernel.org X-Gm-Message-State: AOJu0YzHelu1+wVaGJ3l+lrcJBD4/qkU+xcs63CEfhps+Q/YTg6zxCXc 5M3qDy+fHv3UTBskA+ynMULL3XvcZxgVy7L4xEx87lMXHAXlgxctecyH0hTYHnECTMdeNeIl2b+ 5vAEEYw== X-Received: from wmlu26.prod.google.com ([2002:a05:600c:211a:b0:486:f89b:7f11]) (user=hmazur job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4a13:b0:487:1e7:8e7 with SMTP id 5b1f17b1804b1-48701e70a2fmr3341125e9.33.1773993456420; Fri, 20 Mar 2026 00:57:36 -0700 (PDT) Date: Fri, 20 Mar 2026 07:57:23 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.53.0.959.g497ff81fa9-goog Message-ID: <20260320075723.779985-1-hmazur@google.com> Subject: [PATCH v4] mm/execmem: Make the populate and alloc atomic From: Hubert Mazur To: Andrew Morton , Mike Rapoport Cc: Greg Kroah-Hartman , Stanislaw Kardach , Michal Krawczyk , Slawomir Rosek , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Hubert Mazur Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When a block of memory is requested from the execmem manager it tries to find a suitable fragment by traversing the free_areas. In case there is no such block, a new memory area is added to the free_areas and then allocated to the caller by traversing the free_area tree again. The above operations of allocation and tree traversal are not atomic hence another request may consume this newly allocated memory block which results in the allocation failure for the original request. Such occurrence can be spotted on devices running the 6.18 kernel during the parallel modules loading. To mitigate such resource races execute the cache population and allocation operations under one mutex lock. Signed-off-by: Hubert Mazur Reviewed-by: Mike Rapoport (Microsoft) --- Changes in v4: - Fixed typos in the source code comments - Extended the commit message with rationale behind introducing the change Changes in v3: - Addressed the maintainer comments regarding style issues - Removed unnecessary conditional statement Link to v3: https://lore.kernel.org/all/20260319085907.3510446-1-hmazur@google.com/ Changes in v2: The __execmem_cache_alloc_locked function (lockless version of __execmem_cache_alloc) is introduced and called after execmem_cache_add_locked from the __execmem_cache_populate_alloc function (renamed from execmem_cache_populate). Both calls are guarded now with a single mutex. Link to v2: https://lore.kernel.org/all/20260317125020.1293472-2-hmazur@google.com/ Changes in v1: Allocate new memory fragment and assign it directly to the busy_areas inside execmem_cache_populate function. Link to v1: https://lore.kernel.org/all/20260312131438.361746-1-hmazur@google.com/T/#t mm/execmem.c | 55 +++++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/mm/execmem.c b/mm/execmem.c index 810a4ba9c924..084a207e4278 100644 --- a/mm/execmem.c +++ b/mm/execmem.c @@ -203,13 +203,6 @@ static int execmem_cache_add_locked(void *ptr, size_t = size, gfp_t gfp_mask) return mas_store_gfp(&mas, (void *)lower, gfp_mask); } -static int execmem_cache_add(void *ptr, size_t size, gfp_t gfp_mask) -{ - guard(mutex)(&execmem_cache.mutex); - - return execmem_cache_add_locked(ptr, size, gfp_mask); -} - static bool within_range(struct execmem_range *range, struct ma_state *mas, size_t size) { @@ -225,18 +218,16 @@ static bool within_range(struct execmem_range *range,= struct ma_state *mas, return false; } -static void *__execmem_cache_alloc(struct execmem_range *range, size_t siz= e) +static void *execmem_cache_alloc_locked(struct execmem_range *range, size_= t size) { struct maple_tree *free_areas =3D &execmem_cache.free_areas; struct maple_tree *busy_areas =3D &execmem_cache.busy_areas; MA_STATE(mas_free, free_areas, 0, ULONG_MAX); MA_STATE(mas_busy, busy_areas, 0, ULONG_MAX); - struct mutex *mutex =3D &execmem_cache.mutex; unsigned long addr, last, area_size =3D 0; void *area, *ptr =3D NULL; int err; - mutex_lock(mutex); mas_for_each(&mas_free, area, ULONG_MAX) { area_size =3D mas_range_len(&mas_free); @@ -245,7 +236,7 @@ static void *__execmem_cache_alloc(struct execmem_range= *range, size_t size) } if (area_size < size) - goto out_unlock; + return NULL; addr =3D mas_free.index; last =3D mas_free.last; @@ -254,7 +245,7 @@ static void *__execmem_cache_alloc(struct execmem_range= *range, size_t size) mas_set_range(&mas_busy, addr, addr + size - 1); err =3D mas_store_gfp(&mas_busy, (void *)addr, GFP_KERNEL); if (err) - goto out_unlock; + return NULL; mas_store_gfp(&mas_free, NULL, GFP_KERNEL); if (area_size > size) { @@ -268,19 +259,25 @@ static void *__execmem_cache_alloc(struct execmem_ran= ge *range, size_t size) err =3D mas_store_gfp(&mas_free, ptr, GFP_KERNEL); if (err) { mas_store_gfp(&mas_busy, NULL, GFP_KERNEL); - goto out_unlock; + return NULL; } } ptr =3D (void *)addr; -out_unlock: - mutex_unlock(mutex); return ptr; } -static int execmem_cache_populate(struct execmem_range *range, size_t size) +static void *__execmem_cache_alloc(struct execmem_range *range, size_t siz= e) +{ + guard(mutex)(&execmem_cache.mutex); + + return execmem_cache_alloc_locked(range, size); +} + +static void *execmem_cache_populate_alloc(struct execmem_range *range, siz= e_t size) { unsigned long vm_flags =3D VM_ALLOW_HUGE_VMAP; + struct mutex *mutex =3D &execmem_cache.mutex; struct vm_struct *vm; size_t alloc_size; int err =3D -ENOMEM; @@ -294,7 +291,7 @@ static int execmem_cache_populate(struct execmem_range = *range, size_t size) } if (!p) - return err; + return NULL; vm =3D find_vm_area(p); if (!vm) @@ -307,33 +304,39 @@ static int execmem_cache_populate(struct execmem_rang= e *range, size_t size) if (err) goto err_free_mem; - err =3D execmem_cache_add(p, alloc_size, GFP_KERNEL); + /* + * New memory blocks must be allocated and added to the cache + * as an atomic operation, otherwise they may be consumed + * by a parallel call to the execmem_cache_alloc function. + */ + mutex_lock(mutex); + err =3D execmem_cache_add_locked(p, alloc_size, GFP_KERNEL); if (err) goto err_reset_direct_map; - return 0; + p =3D execmem_cache_alloc_locked(range, size); + + mutex_unlock(mutex); + + return p; err_reset_direct_map: + mutex_unlock(mutex); execmem_set_direct_map_valid(vm, true); err_free_mem: vfree(p); - return err; + return NULL; } static void *execmem_cache_alloc(struct execmem_range *range, size_t size) { void *p; - int err; p =3D __execmem_cache_alloc(range, size); if (p) return p; - err =3D execmem_cache_populate(range, size); - if (err) - return NULL; - - return __execmem_cache_alloc(range, size); + return execmem_cache_populate_alloc(range, size); } static inline bool is_pending_free(void *ptr) -- 2.53.0.959.g497ff81fa9-goog