From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CECD0C2D0C2 for ; Fri, 3 Jan 2020 17:15:22 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 76DA320866 for ; Fri, 3 Jan 2020 17:15:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 76DA320866 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=mit.edu Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id CAAF08E0005; Fri, 3 Jan 2020 12:15:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C5B378E0003; Fri, 3 Jan 2020 12:15:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B4ADE8E0005; Fri, 3 Jan 2020 12:15:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0026.hostedemail.com [216.40.44.26]) by kanga.kvack.org (Postfix) with ESMTP id 99AD78E0003 for ; Fri, 3 Jan 2020 12:15:21 -0500 (EST) Received: from smtpin13.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with SMTP id 35E95824999B for ; Fri, 3 Jan 2020 17:15:21 +0000 (UTC) X-FDA: 76336974042.13.head78_68ccec479c219 X-HE-Tag: head78_68ccec479c219 X-Filterd-Recvd-Size: 4058 Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11]) by imf26.hostedemail.com (Postfix) with ESMTP for ; Fri, 3 Jan 2020 17:15:19 +0000 (UTC) Received: from callcc.thunk.org (guestnat-104-133-0-111.corp.google.com [104.133.0.111] (may be forged)) (authenticated bits=0) (User authenticated as tytso@ATHENA.MIT.EDU) by outgoing.mit.edu (8.14.7/8.12.4) with ESMTP id 003HFHUx023035 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 3 Jan 2020 12:15:18 -0500 Received: by callcc.thunk.org (Postfix, from userid 15806) id 7E9C84200AF; Fri, 3 Jan 2020 12:15:17 -0500 (EST) Date: Fri, 3 Jan 2020 12:15:17 -0500 From: "Theodore Y. Ts'o" To: Linux Filesystem Development List , linux-mm@kvack.org, Andrew Morton Subject: Re: [PATCH -v2] memcg: fix a crash in wb_workfn when a device disappears Message-ID: <20200103171517.GA4253@mit.edu> References: <20191227194829.150110-1-tytso@mit.edu> <20191228005211.163952-1-tytso@mit.edu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20191228005211.163952-1-tytso@mit.edu> X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Fri, Dec 27, 2019 at 07:52:11PM -0500, Theodore Ts'o wrote: > Without memcg, there is a one-to-one mapping between the bdi and > bdi_writeback structures. In this world, things are fairly > straightforward; the first thing bdi_unregister() does is to shutdown > the bdi_writeback structure (or wb), and part of that writeback > ensures that no other work queued against the wb, and that the wb is > fully drained. > > With memcg, however, there is a one-to-many relationship between the > bdi and bdi_writeback structures; that is, there are multiple wb > objects which can all point to a single bdi. There is a refcount > which prevents the bdi object from being released (and hence, > unregistered). So in theory, the bdi_unregister() *should* only get > called once its refcount goes to zero (bdi_put will drop the refcount, > and when it is zero, release_bdi gets called, which calls > bdi_unregister). > > Unfortunately, del_gendisk() in block/gen_hd.c never got the memo > about the Brave New memcg World, and calls bdi_unregister directly. > It does this without informing the file system, or the memcg code, or > anything else. This causes the root wb associated with the bdi to be > unregistered, but none of the memcg-specific wb's are shutdown. So when > one of these wb's are woken up to do delayed work, they try to > dereference their wb->bdi->dev to fetch the device name, but > unfortunately bdi->dev is now NULL, thanks to the bdi_unregister() > called by del_gendisk(). As a result, *boom*. > > Fortunately, it looks like the rest of the writeback path is perfectly > happy with bdi->dev and bdi->owner being NULL, so the simplest fix is > to create a bdi_dev_name() function which can handle bdi->dev being > NULL. This also allows us to bulletproof the writeback tracepoints to > prevent them from dereferencing a NULL pointer and crashing the kernel > if one is tracing with memcg's enabled, and an iSCSI device dies or a > USB storage stick is pulled. > > Previous-Version-Link: https://lore.kernel.org/r/20191227194829.150110-1-tytso@mit.edu > Google-Bug-Id: 145475544 > Tested: fs smoke test > Signed-off-by: Theodore Ts'o > --- > > Notes: > v2: add #include for linux/device.h > > fs/fs-writeback.c | 2 +- > include/linux/backing-dev.h | 10 +++++++++ > include/trace/events/writeback.h | 37 +++++++++++++++----------------- > mm/backing-dev.c | 1 + > 4 files changed, 29 insertions(+), 21 deletions(-) Ping? Any comments? Any objections if I carry this patch[1] in the ext4 tree? Or would it be better for Andrew to carry it in the linux-mm tree? [1] https://lore.kernel.org/k/20191227203117.152399-1-tytso@mit.edu - Ted