Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp1841840pxf; Sat, 3 Apr 2021 02:07:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwXtCBldcsJ912R6HiUPhQCi4pfwlV4ktgg1gnYfGaF8pDOj0scSCg5J3hl7Bi7/TXjYebM X-Received: by 2002:a5e:8718:: with SMTP id y24mr13764926ioj.85.1617440844430; Sat, 03 Apr 2021 02:07:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1617440844; cv=none; d=google.com; s=arc-20160816; b=VNgguRD63EhhY8+RFyeRNMY806w50IlM69ZUtSPYNGtxCjOz/iOlGmrjH+8zNt7ZKx GAijHVux7vRNu2VtZXI3wnwvgsLVCNXcnCQmYeODHyNTgnk+pd5U3Kw8IGc4RbtH5BkV mp7gHjHFYhN10rl6a8RIRrvutES+ho5b2VgyTkHfNi62dhLV5tEzOcoXh+mThed9Pi9A FL39NW2wFPa30V9taq6XoG2mJrem5L3SidOXs+2f+48zLkHOUaoutHj2Rd9KjrFF9HV4 D+wQCbrDdlwqhiOVtlcb/kZCKDhGG0JSZErJ5jyxAlcxDjBUUHj8mpU2B7o6wcq2cs19 F2nQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=GHdvJGYwSTRvs/+kkZxZ2UvghQjRHwXsjaQRu47DCMY=; b=zhbg9rwZCWN+SfzZJEquPBCzdWmYZBWWizfKm3YaoitK8Ol7+VJu5tTRCziuj//phf fuFcK2SOtegsncA0jBIcXL2l8L4t7lG1/DKPKQBuoojTNT7vwbOLV04hityPv3rYLqtt EPdRtiHCAs/wEtdzOyahwW83CcFCxYEOJKCAE5eYy5uhyYNc5hqXDeLqqH+UhCUNgS57 npUkHoHUDrUVkVdHVyt/oB13Hu2ATodIzRLOcpx4JXNCXma/KfBQsfDQ9hHZgOzvLM4o 3myng6y8BItd4A16za6cX8XfbsQR62p9en+kXd/LnW2tTzQFUF6Kr4ERN6zRJX7MzqgS rXhw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=TjV7Pix6; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z14si9581849ioq.100.2021.04.03.02.06.55; Sat, 03 Apr 2021 02:07:24 -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; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=TjV7Pix6; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236415AbhDCJFE (ORCPT + 99 others); Sat, 3 Apr 2021 05:05:04 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:34159 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235604AbhDCJFD (ORCPT ); Sat, 3 Apr 2021 05:05:03 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1617440700; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=GHdvJGYwSTRvs/+kkZxZ2UvghQjRHwXsjaQRu47DCMY=; b=TjV7Pix6UClayzvp9RTHL3p550R6LqfO7APgZWbbht1V8p0RfoW++XK+OhBwjCIxj9lTVD iHqmwwXw4s39JJwZi8iukhAyvxySvAefJy7FK7/Cmd2upQi5r0E7FyMPIr972YL/N5iGTS 9Hs4FH2qWUk4n+ggZvwHBGqK56vax1c= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-568-l4C8PxvPN9yU2QMMY8PFUA-1; Sat, 03 Apr 2021 05:04:59 -0400 X-MC-Unique: l4C8PxvPN9yU2QMMY8PFUA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 1F122107ACCA; Sat, 3 Apr 2021 09:04:58 +0000 (UTC) Received: from T590 (ovpn-12-28.pek2.redhat.com [10.72.12.28]) by smtp.corp.redhat.com (Postfix) with ESMTPS id CECDD19D61; Sat, 3 Apr 2021 09:04:51 +0000 (UTC) Date: Sat, 3 Apr 2021 17:04:47 +0800 From: Ming Lei To: Christoph Hellwig Cc: Jens Axboe , linux-block@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 1/2] block: shutdown blktrace in case of fatal signal pending Message-ID: References: <20210323081440.81343-1-ming.lei@redhat.com> <20210323081440.81343-2-ming.lei@redhat.com> <20210330165330.GA13829@lst.de> <20210402172730.GA22923@lst.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Apr 03, 2021 at 04:10:16PM +0800, Ming Lei wrote: > On Fri, Apr 02, 2021 at 07:27:30PM +0200, Christoph Hellwig wrote: > > On Wed, Mar 31, 2021 at 08:16:50AM +0800, Ming Lei wrote: > > > On Tue, Mar 30, 2021 at 06:53:30PM +0200, Christoph Hellwig wrote: > > > > On Tue, Mar 23, 2021 at 04:14:39PM +0800, Ming Lei wrote: > > > > > blktrace may allocate lots of memory, if the process is terminated > > > > > by user or OOM, we need to provide one chance to remove the trace > > > > > buffer, otherwise memory leak may be caused. > > > > > > > > > > Fix the issue by shutdown blktrace in case of task exiting in > > > > > blkdev_close(). > > > > > > > > > > Signed-off-by: Ming Lei > > > > > > > > This just seems weird. blktrace has no relationship to open > > > > block device instances. > > > > > > blktrace still needs to open one blkdev, then send its own ioctl > > > commands to block layer. In case of OOM, the allocated memory in > > > these ioctl commands won't be released. > > > > > > Or any other suggestion? > > > > Not much we can do there I think. If we want to autorelease memory > > it needs to be an API that ties the memory allocation to an FD. > > We still may shutdown blktrace if current is the last opener, otherwise > new blktrace can't be started and memory should be leaked forever, and > what do you think of the revised version? This way seems not good enough, another better one is to use file->private_data for such purpose since blkdev fs doesn't use file->privete_data, then we can shutdown blktrace just for the blktrace FD: From 191dff30abfd48c38a78dec78e011a39a3b606ca Mon Sep 17 00:00:00 2001 From: Ming Lei Date: Tue, 23 Mar 2021 10:32:23 +0800 Subject: [PATCH] block: shutdown blktrace in case of task exiting blktrace may allocate lots of memory, if the process is terminated by user or OOM, we need to provide one chance to remove the trace buffer, otherwise memory leak may be caused. Also new blktrace instance can't be started too. Fix the issue by shutdown blktrace in bdev_close() if blktrace was setup on this FD. Signed-off-by: Ming Lei --- block/ioctl.c | 2 ++ fs/block_dev.c | 12 ++++++++++++ include/linux/blktrace_api.h | 11 +++++++++++ 3 files changed, 25 insertions(+) diff --git a/block/ioctl.c b/block/ioctl.c index ff241e663c01..7dad4a546db3 100644 --- a/block/ioctl.c +++ b/block/ioctl.c @@ -611,6 +611,8 @@ long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg) else mode &= ~FMODE_NDELAY; + blkdev_mark_blktrace(file, cmd); + switch (cmd) { /* These need separate implementations for the data structure */ case HDIO_GETGEO: diff --git a/fs/block_dev.c b/fs/block_dev.c index 92ed7d5df677..aaa7d7d1e5a4 100644 --- a/fs/block_dev.c +++ b/fs/block_dev.c @@ -34,6 +34,7 @@ #include #include #include +#include #include "internal.h" struct bdev_inode { @@ -1646,6 +1647,15 @@ EXPORT_SYMBOL(blkdev_put); static int blkdev_close(struct inode * inode, struct file * filp) { struct block_device *bdev = I_BDEV(bdev_file_inode(filp)); + + /* + * The task running blktrace is supposed to shutdown blktrace + * by ioctl. If they forget to shutdown or can't do it because + * of OOM or sort of situation, we shutdown for them. + */ + if (blkdev_has_run_blktrace(filp)) + blk_trace_shutdown(bdev->bd_disk->queue); + blkdev_put(bdev, filp->f_mode); return 0; } @@ -1664,6 +1674,8 @@ static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg) else mode &= ~FMODE_NDELAY; + blkdev_mark_blktrace(file, cmd); + return blkdev_ioctl(bdev, mode, cmd, arg); } diff --git a/include/linux/blktrace_api.h b/include/linux/blktrace_api.h index a083e15df608..754058c1965c 100644 --- a/include/linux/blktrace_api.h +++ b/include/linux/blktrace_api.h @@ -135,4 +135,15 @@ static inline unsigned int blk_rq_trace_nr_sectors(struct request *rq) return blk_rq_is_passthrough(rq) ? 0 : blk_rq_sectors(rq); } +static inline void blkdev_mark_blktrace(struct file *file, unsigned int cmd) +{ + if (cmd == BLKTRACESETUP) + file->private_data = (void *)-1; +} + +static inline bool blkdev_has_run_blktrace(struct file *file) +{ + return file->private_data == (void *)-1; +} + #endif -- 2.29.2 -- Ming