From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 667E626E711 for ; Thu, 12 Mar 2026 13:42:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773322944; cv=none; b=hx9EWgbl6y1ijwRSmpU84TBC9rYo+Y6ydzN/IVFPwH/D9175wmzOimrsJl9MuIErDe8zRiZ0oGmNvbf2JvtWeXHvriuGcmJySB2ptuovgXuwATOmO8nc/siYXQuJhVXoZKufKScAMi0U3TMuAftBk0Gz/lWtBOzg2xRlD+RLzUg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773322944; c=relaxed/simple; bh=eTmkpSmT2S+/I17rer4Asjeia4g7cvefyQ7YglIs0XM=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=dFh0JkomqJdQ4e0Hlrq60FM1x/AtFe+H7TkUdhYO33qNIjKm3ON1EtjqNghxwmQcXwRdsd9ijkNUqeIuq4PFIRmyb+2/G/K4QNwsQXFb60Y5ugK/CL2XNYgOCOOIjwDdQjtZxfq40mh7Q95JoqWH5noK+cF7sA6p5TSJFFN4+yw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=LIsdol+r; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="LIsdol+r" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 114DEC4CEF7; Thu, 12 Mar 2026 13:42:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773322944; bh=eTmkpSmT2S+/I17rer4Asjeia4g7cvefyQ7YglIs0XM=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=LIsdol+rX5bsYIBDrjwtqkR7A1ECQGNtXwdiaQMKjNTsq9eYyPayAE1gaNbbBTfFj Cj6JBzQ7z7ljJDyipOf5RJoDEAxpNMtYVCANiT9tzUe84Yz3rrS0h2k6SIFOHM85+t z5UrftzPDkPuCMt9PqS4bjM1qdIdUYZqy2998tt1/XKe1ODKe3mNa2d2wEzbe9MXL9 AydUPq5LPXxMRw5eHzhNV1Ji0OTTGZch665qw5b8+4NDU+gpLEAw+oihmeHyVeDP4K +XBu5/G9eZa/Fc4TPKnxHe/IA2lB2k+7kCxhFPrHEf+gW3fcZ7tbfffcOmRlGRvCjg q71TQps115pkw== Date: Thu, 12 Mar 2026 15:42:17 +0200 From: Mike Rapoport To: Hubert Mazur Cc: Andrew Morton , Greg Kroah-Hartman , Stanislaw Kardach , Michal Krawczyk , Slawomir Rosek , Ryan Neph , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v1 1/1] mm: fix race condition in the memory management Message-ID: References: <20260312131438.361746-1-hmazur@google.com> <20260312131438.361746-2-hmazur@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260312131438.361746-2-hmazur@google.com> On Thu, Mar 12, 2026 at 01:14:38PM +0000, Hubert Mazur wrote: > Subject: mm: fix race condition in the memory management The prefix should be mm/execmem: And this is not exactly a fix. > When 'ARCH_HAS_EXECMEM_ROX' is being enabled the memory management > system will use caching techniques to optimize the allocations. The > logic tries to find the appropriate memory block based on requested > size. This can fail if current allocations is not sufficient hence > kernel allocates a new block large enough in regards to the request. > After the allocation is done, the new block is being added to the > free_areas tree and then - traverses the tree with hope to find the > matching piecie of memory. The operations of allocating new memory and > traversing the tree are not protected by mutex and thus there is a > chance that some other process will "steal" this shiny new block. It's a Does it actually happen in some environment or it's a theoretical issue? > classic race condition for resources. Fix this accordingly by moving a > new block of memory to busy fragments instead of free and return the > pointer to memory. This simplifies the allocation logic since we don't > firstly extend the free areas just to take it a bit later. In case the > new memory allocation is required - do it and return to the caller. It's hard to parse a single huge paragraph. > Signed-off-by: Hubert Mazur > --- > mm/execmem.c | 36 +++++++++++++++++------------------- > 1 file changed, 17 insertions(+), 19 deletions(-) > > diff --git a/mm/execmem.c b/mm/execmem.c > index 810a4ba9c924..8aa44d19ec73 100644 > --- a/mm/execmem.c > +++ b/mm/execmem.c > @@ -307,32 +302,35 @@ static int execmem_cache_populate(struct execmem_range *range, size_t size) > if (err) > goto err_free_mem; > > - err = execmem_cache_add(p, alloc_size, GFP_KERNEL); > + /* Set new allocation as an already busy fragment */ > + addr = (unsigned long)p; > + MA_STATE(mas, busy_areas, addr - 1, addr + 1); > + mas_set_range(&mas, addr, addr+size - 1); > + > + mutex_lock(&execmem_cache.mutex); > + err = mas_store_gfp(&mas, (void *)addr, GFP_KERNEL); > + mutex_unlock(&execmem_cache.mutex); > + > if (err) > goto err_reset_direct_map; > > - return 0; > + return p; This is wrong. The caller asked for 'size' and got ALIGN(size, PMD_SIZE) instead. > > err_reset_direct_map: > execmem_set_direct_map_valid(vm, true); > err_free_mem: > vfree(p); > - return err; > + return NULL; > } -- Sincerely yours, Mike.