From owner-dev-commits-src-branches@freebsd.org Thu Apr 8 16:22:29 2021 Return-Path: Delivered-To: dev-commits-src-branches@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id ACC215B816A; Thu, 8 Apr 2021 16:22:29 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4FGRPK4Zdmz3jNN; Thu, 8 Apr 2021 16:22:29 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 901405916; Thu, 8 Apr 2021 16:22:29 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 138GMTqh003664; Thu, 8 Apr 2021 16:22:29 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 138GMTOx003663; Thu, 8 Apr 2021 16:22:29 GMT (envelope-from git) Date: Thu, 8 Apr 2021 16:22:29 GMT Message-Id: <202104081622.138GMTOx003663@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Tai-hwa Liang Subject: git: f87d56d4bf5c - stable/13 - net: fixing a memory leak in if_deregister_com_alloc() MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: avatar X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: f87d56d4bf5cb4dfdbcdd04ad386a41556fc170b Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-branches@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commits to the stable branches of the FreeBSD src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 08 Apr 2021 16:22:29 -0000 The branch stable/13 has been updated by avatar: URL: https://cgit.FreeBSD.org/src/commit/?id=f87d56d4bf5cb4dfdbcdd04ad386a41556fc170b commit f87d56d4bf5cb4dfdbcdd04ad386a41556fc170b Author: Tai-hwa Liang AuthorDate: 2021-03-06 14:36:35 +0000 Commit: Tai-hwa Liang CommitDate: 2021-04-08 16:21:33 +0000 net: fixing a memory leak in if_deregister_com_alloc() Drain the callbacks upon if_deregister_com_alloc() such that the if_com_free[type] won't be nullified before if_destroy(). Taking fwip(4) as an example, before this fix, kldunload if_fwip will go through the following: 1. fwip_detach() 2. if_free() -> schedule if_destroy() through NET_EPOCH_CALL 3. fwip_detach() returns 4. firewire_modevent(MOD_UNLOAD) -> if_deregister_com_alloc() 5. kernel complains about: Warning: memory type fw_com leaked memory on destroy (1 allocations, 64 bytes leaked). 6. EPOCH runs if_destroy() -> if_free_internal()i By this time, if_com_free[if_alloctype] is NULL since it's already nullified by if_deregister_com_alloc(); hence, firewire_free() won't have a chance to release the allocated fw_com. Reviewed by: hselasky, glebius MFC after: 2 weeks (cherry picked from commit 092f3f081265c68cd8de0234ba8e46560ccc061e) --- sys/net/if.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/sys/net/if.c b/sys/net/if.c index 948be6876b65..776fcf2fc78d 100644 --- a/sys/net/if.c +++ b/sys/net/if.c @@ -4045,6 +4045,14 @@ if_deregister_com_alloc(u_char type) ("if_deregister_com_alloc: %d not registered", type)); KASSERT(if_com_free[type] != NULL, ("if_deregister_com_alloc: %d free not registered", type)); + + /* + * Ensure all pending EPOCH(9) callbacks have been executed. This + * fixes issues about late invocation of if_destroy(), which leads + * to memory leak from if_com_alloc[type] allocated if_l2com. + */ + epoch_drain_callbacks(net_epoch_preempt); + if_com_alloc[type] = NULL; if_com_free[type] = NULL; }