From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) (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 218B23AE187 for ; Tue, 11 Aug 2026 17:21:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=96.67.55.147 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786468876; cv=none; b=LA+Z8D2cveotvOTN0UVTLc8ryxrXhHkVZ8xIZAhs1zD+cbetF7RSYGi7AndgJDmt6do2XFXOadQnN0vcssQJ+1bVEXVk77Ym0IeUPudDFIio2vgof9GN52m86u3RkC6FUf1fSE54ULAy/XXSDgChl/UiiWoExbK3yDjdapEWbHE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786468876; c=relaxed/simple; bh=H+FxSyqPvIqlpH0OMrc10j5JJIIChtislu9M75P92QY=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type; b=ePE6xAoLC4D4ujLRN2Z2tCUS1iAHBpbGbg3BLqZp7RvlgInTpSCeLh3I/3MnITyBkZV6vrqcEhbiUC+BV6ndtjW17RyrVS/gGOAvgFv/p1XYeO7Gnpd5ZA2xs3y3o6ddBDx0sNNY0xBVneU3nj0DaxASN2V4N2LZaKyud4WjJ/M= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=surriel.com; spf=pass smtp.mailfrom=surriel.com; dkim=pass (2048-bit key) header.d=surriel.com header.i=@surriel.com header.b=QIAi/En5; arc=none smtp.client-ip=96.67.55.147 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=surriel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=surriel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=surriel.com header.i=@surriel.com header.b="QIAi/En5" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=surriel.com ; s=mail; h=Content-Transfer-Encoding:Content-Type:MIME-Version:Message-ID: Subject:Cc:To:From:Date:Sender:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=vdlN0I4eOAZv2Qy49qE60ejxX7ioV1x7Kb2lltJcp7E=; b=QIAi/En5UGoWdHRQsETDOCk6cO UmC77yL5DmC652os/5Os+ily2xRQzsxW3rOwiym6mtLSOKWiorbXACGAMgQMf/JiUcuM/qflpuhQS dhQxAKG15weXMrWjRRzELbWxZfxbeLwmjYvoBxeMK82KzTL+ZKOSTwYfFoKtBVsnFxYbTndsdDaaF dAY20Sj0UHcdPgY8uDgnKJC2OzVZ0ifW0cGB+7y6MeGQeI21b5Vk/ANE5EvKaoiZ4VsoMv1hNUzYr z6d8Vswjei0eEFVzs7ZuRjU/5zoL5g0HC73p9SbLaAacQp3Hh+vQf1Q02/oveJy+6blyKDqkeDHCd jeDPqMaA==; Received: from [2601:18c:8100:a0e0:5a47:caff:fe78:8708] (helo=fangorn) by shelob.surriel.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.97.1) (envelope-from ) id 1wtq9e-000000003gk-0Lbo; Tue, 11 Aug 2026 13:20:46 -0400 Date: Tue, 11 Aug 2026 13:20:45 -0400 From: Rik van Riel To: David Hildenbrand Cc: Andrew Morton , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH v3] mm/cma: don't release CMA pages still in use Message-ID: <20260811132045.5f114772@fangorn> X-Mailer: Claws Mail 4.4.0 (GTK 3.24.52; x86_64-redhat-linux-gnu) 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-Transfer-Encoding: 7bit When a driver calls dma_free_contiguous() before quiescing DMA, the page still has a reference from the device. put_page_testzero() there returns false, WARN fires, but the code proceeds to free_contig_frozen_range() putting a live page onto buddy and clearing the bitmap. A subsequent allocation can hand the same PFN to a new owner while the original holder still references it. A concurrent put_page() that drops the last reference between the testzero loop and free_contig_frozen_range() can double-queue the page via page->lru, corrupting buddy lists. Avoid the corruption by not freeing a CMA region if any of the pages inside are still in use. Make it explicit in the warning that the driver allowed a leak. Fixes: 9bda131c6093 ("mm: cma: add cma_alloc_frozen{_compound}()") Link: https://lore.kernel.org/linux-mm/20260809210608.06b5ccb9@fangorn/ [v1] Link: https://lore.kernel.org/linux-mm/20260810122737.030f8452@fangorn/ [v2] Cc: stable@vger.kernel.org Signed-off-by: Rik van Riel --- v3: - simplify things by simply leaking the CMA range, drivers should not call cma_release() while the space is still in use (David Hildenbrand) mm/cma.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/mm/cma.c b/mm/cma.c index a13ce4999b39..c1425f25c39b 100644 --- a/mm/cma.c +++ b/mm/cma.c @@ -1018,7 +1018,7 @@ bool cma_release(struct cma *cma, const struct page *pages, unsigned long count) { struct cma_memrange *cmr; - unsigned long ret = 0; + unsigned long leaked = 0; unsigned long i, pfn; cmr = find_cma_memrange(cma, pages, count); @@ -1027,9 +1027,12 @@ bool cma_release(struct cma *cma, const struct page *pages, pfn = page_to_pfn(pages); for (i = 0; i < count; i++, pfn++) - ret += !put_page_testzero(pfn_to_page(pfn)); + leaked += !put_page_testzero(pfn_to_page(pfn)); - WARN(ret, "%lu pages are still in use!\n", ret); + if (leaked) { + WARN(1, "%lu pages are still in use, not freeing CMA region!\n", leaked); + return true; + } __cma_release_frozen(cma, cmr, pages, count); -- 2.55.0