From patchwork Fri Sep 12 13:10:02 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 120146 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 716C73857B90 for ; Fri, 12 Sep 2025 13:18:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 716C73857B90 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=PSxBA8x+ X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id AAE813858031 for ; Fri, 12 Sep 2025 13:10:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AAE813858031 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org AAE813858031 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1757682626; cv=none; b=fwGUZIpcgd1pLmjDknMOJ9Epcikqtz3adtxNqchy/YDQFjSbiHV6TRRC+aVkZEtYh+5cx9Vf/pS/db5ZcbaOcbz3DfhbvsSWblqt9EPOqHBY57nGwbi9iqKQlK6gGsGr15FeqoMOPttPLhGIHUBOLJ8NM+MBy+6TXOADIT8PaCA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1757682626; c=relaxed/simple; bh=KiAsk8s4+/pHZFH9rSfxgGGf1flJnqMreC+Y7QEbdUo=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=TFcF8vXeLJ3L7RmNpIj2lLr0vdtyHu+6tCjKHMih8Wz6Px6UEiY8p2bsA7CQ8a+xZDc6robtVanJigQUxGHtChlLl2uQo4uF9myIZASogsLVZUtWZnH0vGh8ka+wfVlsoVS8dSmQNxiXYGmtNI6XtoWCcb+gZsZPvzOAVfTw3ZE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AAE813858031 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1757682626; 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=y9vPuv70fR3dSktmCph+RxBwRZQK9/ZWdUCvLkrO9iA=; b=PSxBA8x+YA0ybljHgYlb9uZC3hn0acqdpQEJwlW2GlAJbX9GR8NUWz2PvPbru6YyLZDZXr HhdMxmlT9PH94O4D7ItNnyh3Ija9awa8X4REJD8MaK9SICnoetbh0k3UgdHqboOQtdBMNz 1bCoZV/viY5sdt0m5eyQm/1w/365HdA= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-679-tDK7xnzzOmKBmpq5Bu4dQA-1; Fri, 12 Sep 2025 09:10:25 -0400 X-MC-Unique: tDK7xnzzOmKBmpq5Bu4dQA-1 X-Mimecast-MFC-AGG-ID: tDK7xnzzOmKBmpq5Bu4dQA_1757682624 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D69AD180034C; Fri, 12 Sep 2025 13:10:23 +0000 (UTC) Received: from zen.kayari.org (unknown [10.42.28.196]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 801B91944CCA; Fri, 12 Sep 2025 13:10:22 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix memory leak in PSTL TBB backend [PR117276] Date: Fri, 12 Sep 2025 14:10:02 +0100 Message-ID: <20250912131020.1974294-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: osd3zZxYj22UjQ71lcPXnvRtyNvcVLl3_-ii_MpHjqM_1757682624 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_HOSTKARMA_W, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_PASS, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org Backport of upstream patch: https://github.com/uxlfoundation/oneDPL/pull/1589 libstdc++-v3/ChangeLog: PR libstdc++/117276 * include/pstl/parallel_backend_tbb.h (__func_task::finalize): Make deallocation unconditional. --- Tested x86_64-linux. Pushed to trunk. Backports needed. libstdc++-v3/include/pstl/parallel_backend_tbb.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/libstdc++-v3/include/pstl/parallel_backend_tbb.h b/libstdc++-v3/include/pstl/parallel_backend_tbb.h index bb6fa8f18e83..ff5236f39071 100644 --- a/libstdc++-v3/include/pstl/parallel_backend_tbb.h +++ b/libstdc++-v3/include/pstl/parallel_backend_tbb.h @@ -521,7 +521,7 @@ class __root_task friend class __func_task<_Func>; }; -#else // TBB_INTERFACE_VERSION <= 12000 +#else // TBB_INTERFACE_VERSION > 12000 class __task : public tbb::detail::d1::task { protected: @@ -656,10 +656,16 @@ class __func_task : public __task _PSTL_ASSERT(__parent != nullptr); _PSTL_ASSERT(__parent->_M_refcount.load(std::memory_order_relaxed) > 0); - if (--__parent->_M_refcount == 0) + + auto __refcount = --__parent->_M_refcount; + + // Placing the deallocation after the refcount decrement allows another thread to proceed with tree + // folding concurrently with this task cleanup. + __alloc.deallocate(this, *__ed); + + if (__refcount == 0) { _PSTL_ASSERT(__next == nullptr); - __alloc.deallocate(this, *__ed); return __parent; }