Received: by 2002:ab2:60d1:0:b0:1f7:5705:b850 with SMTP id i17csp970973lqm; Thu, 2 May 2024 00:58:13 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXrNR3d7cBFaOtWt2m9tRT3/8QBiFOXjLwQcZnUnGMEwl7uTI8oM61wseYQo9Ib3aLGR43PGKF6S+k/p6hEV+fgdu8pjUH3R5zbpjTTIA== X-Google-Smtp-Source: AGHT+IE6rLXjESz7lmockO2a47kVYjNk8OCe59/NYq33qsKmszX7zLOHXjEGPqmWG0G9SFj0O5n7 X-Received: by 2002:a05:620a:581a:b0:790:88f9:5adc with SMTP id wm26-20020a05620a581a00b0079088f95adcmr1825292qkn.37.1714636693477; Thu, 02 May 2024 00:58:13 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714636693; cv=pass; d=google.com; s=arc-20160816; b=mbgvOOIMxDUf7MlUlS3Npj16Drk1gIxRmVHNB/G2bql7J1/Bz+Q9JXXJ318w0Y6Ddj oRS/JD4ypH6vxEszik/yBpQ0OSY0IFta4v8SkKSODaN4g0BF+QY+6zIGtyzY24P0uIfH MfUYvYSVRv1kWYU1J22EYOLLzlIc6BPb0eWaRJxn6gJNp3No6aHOX0ktVrxa0OYoZlL6 4kHkhHR1zq37ay1kMmEVmX47/Cvmg3+N/FPhibwwP0ONtsC0WIm8DJuCAHjinBymMazY V8fvz6FgL7u5hneycv0uHIzPdq4LOB8GV+dRqnn81zBVjobFypVNkEcxs+RFdVTA4uH0 HC/g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=O2K7W10bUuhGIOY6LbVf5zRLSo3TolQftZm0XAlBBVE=; fh=+1+ozPDlJ+ammBwKJ2RtWWxuuvdgB7fSCY+kMIBr7/Y=; b=VQwWqAKHZy0OM1e+0Dm3eqNBOI5JyFE/35LsVKBJvnh5c0p5uxdrhhotLMyqDG1tqx z2zL2DvzEt2qdO/vvdIyn23y+6f3cTk5BItUK4FcTLPIHW1nAK74QjM+0bS8ABeHkYg9 rgHsAMEesX3QQFUu/1dNCaGzwVqgGl2euZ4WJwFvchlyc7zIpvfSdjqb2dJHyBkRzH/S 3LLIylqjeZj2Y+149lZ9hWSveq5+tZGznYt4qYbQxYMNrubK728J7c00Kp2h5yfbHSQZ DQr2rs1aGGdrgiTEksiJ1MVNsDZjkkxAeml3EAQQed3V/9nQ18JxOJZqPlku/B8AO1GH /POA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=bB1+ngLp; arc=pass (i=1 spf=pass spfdomain=linaro.org dkim=pass dkdomain=linaro.org dmarc=pass fromdomain=linaro.org); spf=pass (google.com: domain of linux-kernel+bounces-166081-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-166081-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id v10-20020a05620a0a8a00b00790f78edb58si389410qkg.657.2024.05.02.00.58.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 May 2024 00:58:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-166081-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=bB1+ngLp; arc=pass (i=1 spf=pass spfdomain=linaro.org dkim=pass dkdomain=linaro.org dmarc=pass fromdomain=linaro.org); spf=pass (google.com: domain of linux-kernel+bounces-166081-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-166081-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 278DE1C2124E for ; Thu, 2 May 2024 07:58:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D769E502A1; Thu, 2 May 2024 07:56:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="bB1+ngLp" Received: from mail-lj1-f178.google.com (mail-lj1-f178.google.com [209.85.208.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AD6BB405FB for ; Thu, 2 May 2024 07:56:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714636611; cv=none; b=PX04ACoaS9hVIN3LBAvUFJgaD+rdpJREGBBSY8zdRJhPvqzOMDA6co92oZ2W4EwPLUv11ZAd5LGLDuxxVnG7qdpUBR29GbRuTO10NFBLMta5JMI7wwNKzELPAY9EibADf6+qO4+iPex5/IPMMzukqSErggEIJruc2m37DLyAWvg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714636611; c=relaxed/simple; bh=7yHUReQVpBbuF59WgVxqcgBxLTriwnNY9V4A/86EQqc=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=lDqmUH1sewrhcC1ixDjKQnH2hnxubPUvin8zztHySGNfu6XI6oOw2FIQHlE6zMjs9U+4A8DqwubOR53SQTPaOprnSE5nuQAOo9nXNK9EW4zVT8VSDlF6K+DcHVRVYLdjgZyUuIg63f6mUpVv2BO9S2PnU0A+bNUY+gNex12XPBs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=bB1+ngLp; arc=none smtp.client-ip=209.85.208.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-lj1-f178.google.com with SMTP id 38308e7fff4ca-2e0a2870bceso51241161fa.2 for ; Thu, 02 May 2024 00:56:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1714636608; x=1715241408; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=O2K7W10bUuhGIOY6LbVf5zRLSo3TolQftZm0XAlBBVE=; b=bB1+ngLpOKsPIRqtatn9z6eQOKUuH9HVr/KcdiVq2B7vZlxpt4Fqun7SlolesZ8EKm B3ca1BFa1E8AWUTzmaIwLdBTAQSXmalLTbq1qUYNxyxiKsDUXLnqHOdYPlgVdujbfRDq W/r3F5ryoOUc61zYz8rwGUT78EYJ2PKW0bX5uqWeezqEWxH1BJt7o4ufcFBwXX9VKvne 9RvVftDfTxt3XmDMrUjh6tyNmeIxD2A6dRtTEVUGpLea1+e2aDpx8ZsbPwSzBVnytqYl H4oB0vlrTYgRMaNZIAMogRMPrPd7VOwzMBTEaQG8LUNciIOY+haPxQ//DerOsV47gkbR Byag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714636608; x=1715241408; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=O2K7W10bUuhGIOY6LbVf5zRLSo3TolQftZm0XAlBBVE=; b=dvG5gtekYPnehnLFR7FvxKLqJKiH/cPb7TvA1cEszm/mepCceFLYMVBQUPrnt1LFMt ZLRFHPrCKVit7I6u0MgJjWVLQCXwd6oaGKM9YrpHIbO39RF6AcDcRA3VqDaMtv+b7FR/ B1LFhwyBeJLIYI5f5kj/XLHpgyZx4levXxzzRTQIcuwyqi/cIm5OvJGOxe0l3IjpuSqM wgoSf0TPPAg++FqiE2ZGruAeou31KcMkdqA2+VHymy04NbO+t0UI9Aw9MZ9xG5S9bdWY CD9bJ7pmcW7mHX02aqxJKP26tzw4PWA8xFIgEFgyJLzsDnenWwCCvWXDX2wdSibGQiJV N0jQ== X-Forwarded-Encrypted: i=1; AJvYcCU5TZo9fS9McAF6BI/C7mV/DBhPjAtvwSpQ6z8Zm4oklm+g/emQ2psXiBkDAe1dYwbO+ZPTkg0oq6WjdEzmJbR+cpwwnEJQGiazAKjy X-Gm-Message-State: AOJu0YxZr5L1M6vtb483UJM+rJV8kC92IhWIvsVZ55KidyxTd+ul5j79 hJqvS4w8u3GbnYuuiy0ziroeGpVPrF+4pa6rCOzMqzDQfD3LyEUReww7EBmdL/E= X-Received: by 2002:a2e:3201:0:b0:2df:6cb8:c92f with SMTP id y1-20020a2e3201000000b002df6cb8c92fmr2508931ljy.23.1714636607767; Thu, 02 May 2024 00:56:47 -0700 (PDT) Received: from localhost ([102.222.70.76]) by smtp.gmail.com with ESMTPSA id z11-20020a2e884b000000b002d816c0500asm85707ljj.118.2024.05.02.00.56.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 May 2024 00:56:47 -0700 (PDT) Date: Thu, 2 May 2024 10:56:43 +0300 From: Dan Carpenter To: duoming@zju.edu.cn Cc: linux-hams@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, pabeni@redhat.com, kuba@kernel.org, edumazet@google.com, davem@davemloft.net, jreuter@yaina.de, lars@oddbit.com, Miroslav Skoric Subject: Re: [PATCH net] ax25: Fix refcount leak issues of ax25_dev Message-ID: <8767454a-2d5a-4c6d-b887-440047c9bc5b@moroto.mountain> References: <20240501060218.32898-1-duoming@zju.edu.cn> <7fcfdc9a-e3f3-49a1-9373-39b5ad745799@moroto.mountain> <1402dfc8.20a4b.18f37963e87.Coremail.duoming@zju.edu.cn> 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: <1402dfc8.20a4b.18f37963e87.Coremail.duoming@zju.edu.cn> On Thu, May 02, 2024 at 12:35:44PM +0800, duoming@zju.edu.cn wrote: > On Wed, 1 May 2024 20:43:37 +0300 Dan Carpenter wrote: > > I'm always happy to take credit for stuff but the Reported by should go > > to Lars and Miroslav. > > > > Reported-by: Lars Kellogg-Stedman > > Reported-by: Miroslav Skoric > > This patch is not related with the problem raised by Lars Kellogg-Stedman > and Miroslav Skoric, it only solves the reference counting leak issues of > ax25_dev in ax25_addr_ax25dev() and ax25_dev_device_down(). So I think > there is no need to change the "Reported by" label. > Ah... I was really hoping it was related to the other bugs. Okay, what about we separate this into different patches one for each bug? The changes to ax25_addr_ax25dev() and ax25_dev_free() are obvious and could go in as-is but as two separate patches. The changes to ax25_dev_device_up/down() are more subtle. The ax25_dev_list stuff is frustrating. It would be so much easier if it were a normal list and you could just do: /* * Remove any packet forwarding that points to this device. */ list_for_each_entry(s, ax25_dev_list, list) { if (s->forward == dev) s->forward = NULL; } list_for_each_entry(s, ax25_dev_list, list) { if (s == ax25_dev) { list_del(s); free_net = true; break; } } spin_unlock_bh(&ax25_dev_lock); dev->ax25_ptr = NULL; if (free_net) netdev_put(dev, &ax25_dev->dev_tracker); ax25_dev_put(ax25_dev); } Why do we call netdev_put() on that one path? Btw, here is an untested conversion to lists... regards, dan carpenter diff --git a/include/net/ax25.h b/include/net/ax25.h index 0d939e5aee4e..c2a85fd3f5ea 100644 --- a/include/net/ax25.h +++ b/include/net/ax25.h @@ -216,7 +216,7 @@ typedef struct { struct ctl_table; typedef struct ax25_dev { - struct ax25_dev *next; + struct list_head list; struct net_device *dev; netdevice_tracker dev_tracker; @@ -330,7 +330,6 @@ int ax25_addr_size(const ax25_digi *); void ax25_digi_invert(const ax25_digi *, ax25_digi *); /* ax25_dev.c */ -extern ax25_dev *ax25_dev_list; extern spinlock_t ax25_dev_lock; #if IS_ENABLED(CONFIG_AX25) diff --git a/net/ax25/ax25_dev.c b/net/ax25/ax25_dev.c index 282ec581c072..b632af38f1e1 100644 --- a/net/ax25/ax25_dev.c +++ b/net/ax25/ax25_dev.c @@ -22,11 +22,12 @@ #include #include #include +#include #include #include #include -ax25_dev *ax25_dev_list; +static struct list_head ax25_dev_list; DEFINE_SPINLOCK(ax25_dev_lock); ax25_dev *ax25_addr_ax25dev(ax25_address *addr) @@ -34,11 +35,12 @@ ax25_dev *ax25_addr_ax25dev(ax25_address *addr) ax25_dev *ax25_dev, *res = NULL; spin_lock_bh(&ax25_dev_lock); - for (ax25_dev = ax25_dev_list; ax25_dev != NULL; ax25_dev = ax25_dev->next) + list_for_each_entry(ax25_dev, &ax25_dev_list, list) { if (ax25cmp(addr, (const ax25_address *)ax25_dev->dev->dev_addr) == 0) { res = ax25_dev; ax25_dev_hold(ax25_dev); } + } spin_unlock_bh(&ax25_dev_lock); return res; @@ -52,6 +54,10 @@ void ax25_dev_device_up(struct net_device *dev) { ax25_dev *ax25_dev; + // FIXME: do call this in probe or something + if (!ax25_dev_list.next) + INIT_LIST_HEAD(&ax25_dev_list); + ax25_dev = kzalloc(sizeof(*ax25_dev), GFP_KERNEL); if (!ax25_dev) { printk(KERN_ERR "AX.25: ax25_dev_device_up - out of memory\n"); @@ -85,8 +91,7 @@ void ax25_dev_device_up(struct net_device *dev) #endif spin_lock_bh(&ax25_dev_lock); - ax25_dev->next = ax25_dev_list; - ax25_dev_list = ax25_dev; + list_add(&ax25_dev->list, &ax25_dev_list); spin_unlock_bh(&ax25_dev_lock); ax25_dev_hold(ax25_dev); @@ -111,23 +116,18 @@ void ax25_dev_device_down(struct net_device *dev) /* * Remove any packet forwarding that points to this device. */ - for (s = ax25_dev_list; s != NULL; s = s->next) + list_for_each_entry(s, &ax25_dev_list, list) { if (s->forward == dev) s->forward = NULL; - - if ((s = ax25_dev_list) == ax25_dev) { - ax25_dev_list = s->next; - goto unlock_put; } - while (s != NULL && s->next != NULL) { - if (s->next == ax25_dev) { - s->next = ax25_dev->next; + list_for_each_entry(s, &ax25_dev_list, list) { + if (s == ax25_dev) { + list_del(&s->list); goto unlock_put; } - - s = s->next; } + spin_unlock_bh(&ax25_dev_lock); dev->ax25_ptr = NULL; ax25_dev_put(ax25_dev); @@ -200,16 +200,13 @@ struct net_device *ax25_fwd_dev(struct net_device *dev) */ void __exit ax25_dev_free(void) { - ax25_dev *s, *ax25_dev; + ax25_dev *s, *n; spin_lock_bh(&ax25_dev_lock); - ax25_dev = ax25_dev_list; - while (ax25_dev != NULL) { - s = ax25_dev; - netdev_put(ax25_dev->dev, &ax25_dev->dev_tracker); - ax25_dev = ax25_dev->next; + list_for_each_entry_safe(s, n, &ax25_dev_list, list) { + netdev_put(s->dev, &s->dev_tracker); + list_del(&s->list); kfree(s); } - ax25_dev_list = NULL; spin_unlock_bh(&ax25_dev_lock); }