Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp19900415rwd; Wed, 28 Jun 2023 16:25:00 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ48p3xGJ2nox9jwU9F2G5mckFM7nROeC5l43FHjod3dZgpzGqdgFOh+P7AQ1zf12fKtibIr X-Received: by 2002:a05:6a20:4292:b0:125:74e3:d3cc with SMTP id o18-20020a056a20429200b0012574e3d3ccmr16743026pzj.52.1687994700134; Wed, 28 Jun 2023 16:25:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687994700; cv=none; d=google.com; s=arc-20160816; b=OsZZ8h3FzKeLk4nG0BO8dRzhGkX4STAmgMypNu2GoAN5Pbocej++Ol6N78slH4o7Oj o+kcckDttAzYvdC9MhCRwtRAqo6pjJITJW9t7HZMn+Pvf+iKmCvxtBuJUE8AcccuaqE5 arEaOPubG1wD5ucAStv2YIIquizWlGvQ1cSI4C5TSL6IadVz1qla5iGfe5TBQ41aQhcB ag/iOh1U/CSIgEzTRki1z4O0vg/51o530kQDzpiPnzzZE4vRJZTPbDy/bv7m+YcMCPJm qRnOsbLp/GDl9u1V0eE63pY2nZK7W4at1SMrLVw/XspIXGJzC8QrcTaENMUuPM4eStv6 GYNQ== 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:dkim-signature:date; bh=NC7io3hJiGazy/1nLRyLYY/aZGU1OCwphLLIBbRfFVc=; fh=vsF40E3Ve9ZoKkTEiZu7G/WyCu/9wCkGmr1G5IU9IAE=; b=PTDI8G5ETdzyYFC1qhYMe4tFLA13EHVV3rWVFkZYAPdj+h1uSMLV2Jk3iSdJLrlydY h192lITariU3y+FFzLItgzGvH9UZs9wgwk+zD8KofmBRKCvHOU3MwFA35Iy85DahTgrz fMCNa3QpSqyApV/hdEpx89umaGwwGEPOve3WT9ehvyebVPVk6TDLvlWF0fTcLPlIwZ7T 9iZLn8VIH7WwyPZD58aObmED/7aelo9kr1AzYUVJE7O8HBR4O0jxkspZqRwRpY/w0egs FP/NZjIkCJoy95LBfX6L75ktmpLZ/uWTyzXRk7N0/GhzNUMPsCqHXlpA88qvlbJR+uSS yz8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=u44xIlYo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c23-20020a63da17000000b0053f228ce43csi9606539pgh.547.2023.06.28.16.24.47; Wed, 28 Jun 2023 16:25:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=u44xIlYo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231492AbjF1Wzq (ORCPT + 99 others); Wed, 28 Jun 2023 18:55:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53680 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231330AbjF1Wzm (ORCPT ); Wed, 28 Jun 2023 18:55:42 -0400 Received: from out-28.mta0.migadu.com (out-28.mta0.migadu.com [91.218.175.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EBB24272D for ; Wed, 28 Jun 2023 15:55:28 -0700 (PDT) Date: Wed, 28 Jun 2023 18:55:14 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1687992926; 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=NC7io3hJiGazy/1nLRyLYY/aZGU1OCwphLLIBbRfFVc=; b=u44xIlYolPHdvyZbmi/QCzn6vZPbW5FX79/HnVCpNxVXIKZiP2IR1cDolNo/S9hCb9fpLo g3uwlLoc3p7WAk/kI+JJj3gOL9BJVGuHBBfLks+T8IxjbiXUKVfH8na30X+R+dwPSSx5za viuWG+OpYuc9LGzRgkPkz9lUHGnKIVM= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Kent Overstreet To: Jens Axboe Cc: torvalds@linux-foundation.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-bcachefs@vger.kernel.org, Christoph Hellwig , Christian Brauner , Al Viro Subject: Re: [GIT PULL] bcachefs Message-ID: <20230628225514.n3xtlgmjkgapgnrd@moria.home.lan> References: <20230627201524.ool73bps2lre2tsz@moria.home.lan> <20230628040114.oz46icbsjpa4egpp@moria.home.lan> <4b863e62-4406-53e4-f96a-f4d1daf098ab@kernel.dk> <20230628175204.oeek4nnqx7ltlqmg@moria.home.lan> <2e635579-37ba-ddfc-a2ab-e6c080ab4971@kernel.dk> <20230628221342.4j3gr3zscnsu366p@moria.home.lan> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jun 28, 2023 at 04:33:55PM -0600, Jens Axboe wrote: > On 6/28/23 4:13?PM, Kent Overstreet wrote: > > On Wed, Jun 28, 2023 at 03:17:43PM -0600, Jens Axboe wrote: > >> Case in point, just changed my reproducer to use aio instead of > >> io_uring. Here's the full script: > >> > >> #!/bin/bash > >> > >> DEV=/dev/nvme1n1 > >> MNT=/data > >> ITER=0 > >> > >> while true; do > >> echo loop $ITER > >> sudo mount $DEV $MNT > >> fio --name=test --ioengine=aio --iodepth=2 --filename=$MNT/foo --size=1g --buffered=1 --overwrite=0 --numjobs=12 --minimal --rw=randread --output=/dev/null & > >> Y=$(($RANDOM % 3)) > >> X=$(($RANDOM % 10)) > >> VAL="$Y.$X" > >> sleep $VAL > >> ps -e | grep fio > /dev/null 2>&1 > >> while [ $? -eq 0 ]; do > >> killall -9 fio > /dev/null 2>&1 > >> echo will wait > >> wait > /dev/null 2>&1 > >> echo done waiting > >> ps -e | grep "fio " > /dev/null 2>&1 > >> done > >> sudo umount /data > >> if [ $? -ne 0 ]; then > >> break > >> fi > >> ((ITER++)) > >> done > >> > >> and if I run that, fails on the first umount attempt in that loop: > >> > >> axboe@m1max-kvm ~> bash test2.sh > >> loop 0 > >> will wait > >> done waiting > >> umount: /data: target is busy. > >> > >> So yeah, this is _nothing_ new. I really don't think trying to address > >> this in the kernel is the right approach, it'd be a lot saner to harden > >> the xfstest side to deal with the umount a bit more sanely. There are > >> obviously tons of other ways that a mount could get pinned, which isn't > >> too relevant here since the bdev and mount point are basically exclusive > >> to the test being run. But the kill and delayed fput is enough to make > >> that case imho. > > > > Uh, count me very much not in favor of hacking around bugs elsewhere. > > > > Al, do you know if this has been considered before? We've got fput() > > being called from aio completion, which often runs out of a worqueue (if > > not a workqueue, a bottom half of some sort - what happens then, I > > wonder) - so the effect is that it goes on the global list, not the task > > work list. > > > > hence, kill -9ing a process doing aio (or io_uring io, for extra > > reasons) causes umount to fail with -EBUSY. > > > > and since there's no mechanism for userspace to deal with this besides > > sleep and retry, this seems pretty gross. > > But there is, as Christian outlined. I would not call it pretty or > intuitive, but you can in fact make it work just fine and not just for > the deferred fput() case but also in the presence of other kinds of > pins. Of which there are of course many. No, because as I explained that just defers the race until when you next try to use the device, since with lazy umount the device will still be use when umount returns. What you'd want is a lazy, synchronous umount, and AFAIK that doesn't exist. > > I'd be willing to tackle this for aio since I know that code... > > But it's not aio (or io_uring or whatever), it's simply the fact that > doing an fput() from an exiting task (for example) will end up being > done async. And hence waiting for task exits is NOT enough to ensure > that all file references have been released. > > Since there are a variety of other reasons why a mount may be pinned and > fail to umount, perhaps it's worth considering that changing this > behavior won't buy us that much. Especially since it's been around for > more than 10 years: Because it seems that before io_uring the race was quite a bit harder to hit - I only started seeing it when things started switching over to io_uring. generic/388 used to pass reliably for me (pre backpointers), now it doesn't. > commit 4a9d4b024a3102fc083c925c242d98ac27b1c5f6 > Author: Al Viro > Date: Sun Jun 24 09:56:45 2012 +0400 > > switch fput to task_work_add > > though that commit message goes on to read: > > We are guaranteed that __fput() will be done before we return > to userland (or exit). Note that for fput() from a kernel > thread we get an async behaviour; it's almost always OK, but > sometimes you might need to have __fput() completed before > you do anything else. There are two mechanisms for that - > a general barrier (flush_delayed_fput()) and explicit > __fput_sync(). Both should be used with care (as was the > case for fput() from kernel threads all along). See comments > in fs/file_table.c for details. > > where that first sentence isn't true if the task is indeed exiting. I > guess you can say that it is as it doesn't return to userland, but > splitting hairs. Though the commit in question doesn't seem to handle > that case, but assuming that came in with a later fixup. > > It is true if the task_work gets added, as that will get run before > returning to userspace. Yes, AIO seems to very much be the exceptional case that wasn't originally considered. > If a case were to be made that we also guarantee that fput has been done > by the time to task returns to userspace, or exits, And that does seem to be the intent of the original code, no? > then we'd probably want to move that deferred fput list to the > task_struct and ensure that it gets run if the task exits rather than > have a global deferred list. Currently we have: > > > 1) If kthread or in interrupt > 1a) add to global fput list > 2) task_work_add if not. If that fails, goto 1a. > > which would then become: > > 1) If kthread or in interrupt > 1a) add to global fput list > 2) task_work_add if not. If that fails, we know task is existing. add to > per-task defer list to be run at a convenient time before task has > exited. no, it becomes: if we're running in a user task, or if we're doing an operation on behalf of a user task, add to the user task's deferred list: otherwise add to global deferred list.