From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-171.mta0.migadu.com (out-171.mta0.migadu.com [91.218.175.171]) (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 631AA26ED45 for ; Tue, 7 Apr 2026 09:41:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.171 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775554880; cv=none; b=PFqlQWbRlg+TsM3E2nWHfDiMehWT2HeXzBEBFPexJMc2n+ZOEFzVyv/+T8qCvqA0jDj4G8zMIfsNZIPnf9ed7ECmi4Zy/MsM08F8j6LD1a0csd96uyXDIb5uO+fn/vmBsQS7df6abjgqV+9zIAzNl5A8N7M0jm9aTUqwWlixak4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775554880; c=relaxed/simple; bh=KgpkJhlN1z1QlMZqVAxt43YkPZ+vTw1jPiMOtNbAUsA=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=L8z5ie6RDZxyYHEQpftUuKZMVfB1N3PeMaEzy6Od1ybs7+H1VMH9vNfcRFVGAeMpTj8DzIowBxozrP72htyPSZnMD4WUT8VlVEwfLjpYbZviDcw2t30nVLgdEurK3vG1OdBMlZJ4EAZZAzT3MTfU2pLMIVnaKUY5ibPKcJ8VhkE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=tlxrsy9P; arc=none smtp.client-ip=91.218.175.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="tlxrsy9P" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1775554876; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=5joibMANCykBiLgKj2c6T79xsF0pUdONomREytFFN/E=; b=tlxrsy9PvC8ry2AFC2uiBSAP+k5vcwW+UxYKkBy6y2cobEAVGlO3Tgnojx0JglNk8ioIKP H3tj64pbA+nwEkosOdHh0+c1QPtiXtnPmHNpIHUyrJFf5+0T85KugehlJ8TSzbsDPofQQL rrSqNWfrzCDa+2FGnn6OXlF7DJMY7x8= From: Qingfang Deng To: Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Qingfang Deng , Sebastian Andrzej Siewior , Kees Cook , Kuniyuki Iwashima , linux-ppp@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH net-next] ppp: consolidate refcount decrements Date: Tue, 7 Apr 2026 17:40:56 +0800 Message-ID: <20260407094058.257246-1-qingfang.deng@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT ppp_destroy_{channel,interface} are always called after refcount_dec_and_test(). To reduce boilerplate code, consolidate the decrements by moving them into the two functions. To reflect this change in semantics, rename the functions to ppp_release_*. Signed-off-by: Qingfang Deng --- drivers/net/ppp/ppp_generic.c | 61 ++++++++++++++++------------------- 1 file changed, 28 insertions(+), 33 deletions(-) diff --git a/drivers/net/ppp/ppp_generic.c b/drivers/net/ppp/ppp_generic.c index cb29a6968c63..b097d1b38ac9 100644 --- a/drivers/net/ppp/ppp_generic.c +++ b/drivers/net/ppp/ppp_generic.c @@ -286,12 +286,12 @@ static struct compressor *find_compressor(int type); static void ppp_get_stats(struct ppp *ppp, struct ppp_stats *st); static int ppp_create_interface(struct net *net, struct file *file, int *unit); static void init_ppp_file(struct ppp_file *pf, int kind); -static void ppp_destroy_interface(struct ppp *ppp); +static void ppp_release_interface(struct ppp *ppp); static struct ppp *ppp_find_unit(struct ppp_net *pn, int unit); static struct channel *ppp_find_channel(struct ppp_net *pn, int unit); static int ppp_connect_channel(struct channel *pch, int unit); static int ppp_disconnect_channel(struct channel *pch); -static void ppp_destroy_channel(struct channel *pch); +static void ppp_release_channel(struct channel *pch); static int unit_get(struct idr *p, void *ptr, int min); static int unit_set(struct idr *p, void *ptr, int n); static void unit_put(struct idr *p, int n); @@ -407,22 +407,18 @@ static int ppp_release(struct inode *unused, struct file *file) if (pf) { file->private_data = NULL; - if (pf->kind == INTERFACE) { + switch (pf->kind) { + case INTERFACE: ppp = PF_TO_PPP(pf); rtnl_lock(); if (file == ppp->owner) unregister_netdevice(ppp->dev); rtnl_unlock(); - } - if (refcount_dec_and_test(&pf->refcnt)) { - switch (pf->kind) { - case INTERFACE: - ppp_destroy_interface(PF_TO_PPP(pf)); - break; - case CHANNEL: - ppp_destroy_channel(PF_TO_CHANNEL(pf)); - break; - } + ppp_release_interface(ppp); + break; + case CHANNEL: + ppp_release_channel(PF_TO_CHANNEL(pf)); + break; } } return 0; @@ -675,8 +671,7 @@ static int ppp_bridge_channels(struct channel *pch, struct channel *pchb) synchronize_rcu(); if (pchb) - if (refcount_dec_and_test(&pchb->file.refcnt)) - ppp_destroy_channel(pchb); + ppp_release_channel(pchb); return -EALREADY; } @@ -708,11 +703,9 @@ static int ppp_unbridge_channels(struct channel *pch) synchronize_rcu(); if (pchbb == pch) - if (refcount_dec_and_test(&pch->file.refcnt)) - ppp_destroy_channel(pch); + ppp_release_channel(pch); - if (refcount_dec_and_test(&pchb->file.refcnt)) - ppp_destroy_channel(pchb); + ppp_release_channel(pchb); return 0; } @@ -786,8 +779,7 @@ static long ppp_ioctl(struct file *file, unsigned int cmd, unsigned long arg) break; err = ppp_bridge_channels(pch, pchb); /* Drop earlier refcount now bridge establishment is complete */ - if (refcount_dec_and_test(&pchb->file.refcnt)) - ppp_destroy_channel(pchb); + ppp_release_channel(pchb); break; case PPPIOCUNBRIDGECHAN: @@ -1584,8 +1576,7 @@ static void ppp_dev_priv_destructor(struct net_device *dev) struct ppp *ppp; ppp = netdev_priv(dev); - if (refcount_dec_and_test(&ppp->file.refcnt)) - ppp_destroy_interface(ppp); + ppp_release_interface(ppp); } static int ppp_fill_forward_path(struct net_device_path_ctx *ctx, @@ -3022,8 +3013,7 @@ ppp_unregister_channel(struct ppp_channel *chan) pch->file.dead = 1; wake_up_interruptible(&pch->file.rwait); - if (refcount_dec_and_test(&pch->file.refcnt)) - ppp_destroy_channel(pch); + ppp_release_channel(pch); } /* @@ -3404,12 +3394,14 @@ init_ppp_file(struct ppp_file *pf, int kind) } /* - * Free the memory used by a ppp unit. This is only called once - * there are no channels connected to the unit and no file structs - * that reference the unit. + * Drop a reference to a ppp unit and free its memory if the refcount reaches + * zero. */ -static void ppp_destroy_interface(struct ppp *ppp) +static void ppp_release_interface(struct ppp *ppp) { + if (!refcount_dec_and_test(&ppp->file.refcnt)) + return; + atomic_dec(&ppp_unit_count); if (!ppp->file.dead || ppp->n_channels) { @@ -3561,18 +3553,21 @@ ppp_disconnect_channel(struct channel *pch) wake_up_interruptible(&ppp->file.rwait); ppp_unlock(ppp); synchronize_net(); - if (refcount_dec_and_test(&ppp->file.refcnt)) - ppp_destroy_interface(ppp); + ppp_release_interface(ppp); err = 0; } return err; } /* - * Free up the resources used by a ppp channel. + * Drop a reference to a ppp channel and free its memory if the refcount reaches + * zero. */ -static void ppp_destroy_channel(struct channel *pch) +static void ppp_release_channel(struct channel *pch) { + if (!refcount_dec_and_test(&pch->file.refcnt)) + return; + put_net_track(pch->chan_net, &pch->ns_tracker); pch->chan_net = NULL; -- 2.43.0