Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp1210340ybz; Thu, 16 Apr 2020 05:07:50 -0700 (PDT) X-Google-Smtp-Source: APiQypLfKwwblzvsQnQqz4CbgxwoJ8vTXU7aIGKT6scVYnHN+PSYLKz7mBNgBbxsfUlTAnVOks8O X-Received: by 2002:a17:906:f1d2:: with SMTP id gx18mr10005363ejb.24.1587038870425; Thu, 16 Apr 2020 05:07:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587038870; cv=none; d=google.com; s=arc-20160816; b=JIZoeOZjYm516Xacyy0q0OE5cq1portPFGlRZz45SZxgkHtJgv5Ku6di0YV+1cgQQR O+7xjLcsAyfEVc5zkht0//r04RNctIwlrivV2rDRjfIwFB/q6+z0Ub3JX4Gqgrsm8UDQ /7DyPnBduXLuzC0r36bFxuMnvejizsnLIH7brO435FR4OVvSQOGiG/uWs7rf9at4KuJY 0NoJJbZ9r7pJf0yT+NnjsQWLWjy58BzdwdBv4oTTNzsXvJgPWVXcFi3heRDJeVLkahtj 1dK4r3xDioLaqKbOapEaPPedk/5IcgBFpD4tXd3wJ1v2o2LmnkIdTPLgSz22Ls9lpUdI vhUg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=oBPRrpAqNhwB16Ka06nUswvnP5QyFwV/5YNbk2fr8hM=; b=0EKATI0Y0UoBS4KkSk7h5nqj7Jdqa1OrlgmD8Lz18/+toH/5tZ5ITmICwVd6elex9+ e3HrLwQgFpjH08PCiD0swYXL3uBt5ltjc72wjeAcaQmgDlrsDKgKskeVBGKjSpes3aV3 Ur+oEodUFlElbJ3FB31jP/g1zVKIAtTPjQNnyspcEUguokinrhLcCBQK0vn2fewLFJt/ lsPP092h4TVhAzW8/P4j7KO0SZcY8Wqtko4bU3TrhlDg4PKcF+EEzWFKGmRkh6ZWaQ8u E7cCmjq7CAVCi1CPL9xBZ73Ji+2j+UWEUe2zd5iSPVQkAH9SJR55oLFTZZaYdE8KQGgs YBcw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d9si1230222ejj.290.2020.04.16.05.07.25; Thu, 16 Apr 2020 05:07:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2634579AbgDPMCd (ORCPT + 99 others); Thu, 16 Apr 2020 08:02:33 -0400 Received: from mx2.suse.de ([195.135.220.15]:38838 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2634772AbgDPMC1 (ORCPT ); Thu, 16 Apr 2020 08:02:27 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 92035ABF4; Thu, 16 Apr 2020 12:02:24 +0000 (UTC) Received: by quack2.suse.cz (Postfix, from userid 1000) id F342B1E1250; Thu, 16 Apr 2020 14:02:23 +0200 (CEST) Date: Thu, 16 Apr 2020 14:02:23 +0200 From: Jan Kara To: Yufen Yu Cc: Christoph Hellwig , axboe@kernel.dk, tj@kernel.org, jack@suse.cz, bvanassche@acm.org, tytso@mit.edu, gregkh@linuxfoundation.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 3/8] bdi: add a ->dev_name field to struct backing_dev_info Message-ID: <20200416120223.GI23739@quack2.suse.cz> References: <20200416071519.807660-1-hch@lst.de> <20200416071519.807660-4-hch@lst.de> <5bfcd35a-2463-3769-be93-911c4e3c38bb@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <5bfcd35a-2463-3769-be93-911c4e3c38bb@huawei.com> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu 16-04-20 16:34:13, Yufen Yu wrote: > Hi, > > On 2020/4/16 15:15, Christoph Hellwig wrote: > > Cache a copy of the name for the life time of the backing_dev_info > > structure so that we can reference it even after unregistering. > > > > Fixes: 68f23b89067f ("memcg: fix a crash in wb_workfn when a device disappears") > > Reported-by: Yufen Yu > > Signed-off-by: Christoph Hellwig > > --- > > include/linux/backing-dev-defs.h | 1 + > > mm/backing-dev.c | 13 ++++++++++--- > > 2 files changed, 11 insertions(+), 3 deletions(-) > > > > diff --git a/include/linux/backing-dev-defs.h b/include/linux/backing-dev-defs.h > > index 4fc87dee005a..249590bcccf7 100644 > > --- a/include/linux/backing-dev-defs.h > > +++ b/include/linux/backing-dev-defs.h > > @@ -220,6 +220,7 @@ struct backing_dev_info { > > wait_queue_head_t wb_waitq; > > struct device *dev; > > + const char *dev_name; > > struct device *owner; > > struct timer_list laptop_mode_wb_timer; > > diff --git a/mm/backing-dev.c b/mm/backing-dev.c > > index c2c44c89ee5d..4f6c05df72f9 100644 > > --- a/mm/backing-dev.c > > +++ b/mm/backing-dev.c > > @@ -938,9 +938,15 @@ int bdi_register_va(struct backing_dev_info *bdi, const char *fmt, va_list args) > > if (bdi->dev) /* The driver needs to use separate queues per device */ > > return 0; > > - dev = device_create_vargs(bdi_class, NULL, MKDEV(0, 0), bdi, fmt, args); > > - if (IS_ERR(dev)) > > + bdi->dev_name = kvasprintf(GFP_KERNEL, fmt, args); > > + if (!bdi->dev_name) > > + return -ENOMEM; > > + > > + dev = device_create(bdi_class, NULL, MKDEV(0, 0), bdi, bdi->dev_name); > > + if (IS_ERR(dev)) { > > + kfree(bdi->dev_name); > > return PTR_ERR(dev); > > + } > > cgwb_bdi_register(bdi); > > bdi->dev = dev; > > @@ -1034,6 +1040,7 @@ static void release_bdi(struct kref *ref) > > WARN_ON_ONCE(bdi->dev); > > wb_exit(&bdi->wb); > > cgwb_bdi_exit(bdi); > > + kfree(bdi->dev_name); > > kfree(bdi); > > } > > > When driver try to to re-register bdi but without release_bdi(), the old > dev_name will be cover directly by the newer in bdi_register_va(). So, I > am not sure whether it can cause memory leak for bdi->dev_name. Yes, that can indeed happen. E.g. I remember that drivers/scsi/sd.c calls device_add_disk() + del_gendisk() repeatedly for one request_queue and that would result in leaking the name (and possibly cause use-after-free issues). I think dev_name has to be just a static array inside backing_dev_info which gets overwritten on reregistration. The question is how big should be this array... Some grepping shows that 40 bytes should be enough for everybody except fs/vboxsf/super.c which puts 'fc->source' into the name which can be presumably rather large. Anyway, I'd make it 40 and just truncate it case in case it does not fit. bdi_dev_name() is used for informational purposes anyway... Honza -- Jan Kara SUSE Labs, CR