Linux bcachefs list
 help / color / mirror / Atom feed
From: Jens Axboe <axboe@kernel.dk>
To: Kent Overstreet <kent.overstreet@linux.dev>,
	Jann Horn <jannh@google.com>
Cc: linux-bcachefs@vger.kernel.org
Subject: Re: [PATCH] bcachefs: dio write: Take ref on mm_struct when using asynchronously
Date: Wed, 27 Nov 2024 15:21:31 -0700	[thread overview]
Message-ID: <6ada0aa7-a260-4c0b-b80c-6360b2ea878f@kernel.dk> (raw)
In-Reply-To: <xrjd4otiojkszczc2u2buwbnb6sw2z7u2p66evivsgxrpkidxq@55yc6lbcnhh2>

On 11/27/24 3:11 PM, Kent Overstreet wrote:
> diff --git a/fs/bcachefs/fs-io-direct.c b/fs/bcachefs/fs-io-direct.c
> index 2089c36b5866..8da0577411e8 100644
> --- a/fs/bcachefs/fs-io-direct.c
> +++ b/fs/bcachefs/fs-io-direct.c
> @@ -226,6 +226,7 @@ struct dio_write {
>  	struct mm_struct		*mm;
>  	const struct iovec		*iov;
>  	unsigned			loop:1,
> +					have_mm_ref:1,
>  					extending:1,
>  					sync:1,
>  					flush:1;
> @@ -390,6 +391,9 @@ static __always_inline long bch2_dio_write_done(struct dio_write *dio)
>  
>  	kfree(dio->iov);
>  
> +	if (dio->have_mm_ref)
> +		mmdrop(dio->mm);
> +
>  	ret = dio->op.error ?: ((long) dio->written << 9);
>  	bio_put(&dio->op.wbio.bio);
>  
> @@ -529,9 +533,24 @@ static __always_inline long bch2_dio_write_loop(struct dio_write *dio)
>  
>  		if (unlikely(dio->iter.count) &&
>  		    !dio->sync &&
> -		    !dio->loop &&
> -		    bch2_dio_write_copy_iov(dio))
> -			dio->sync = sync = true;
> +		    !dio->loop) {
> +			/*
> +			 * Rest of write will be submitted asynchronously -
> +			 * unless copying the iov fails:
> +			 */
> +			if (likely(!bch2_dio_write_copy_iov(dio))) {
> +				/*
> +				 * aio guarantees that mm_struct outlives the
> +				 * request, but io_uring does not
> +				 */
> +				if (dio->mm) {
> +					mmgrab(dio->mm);
> +					dio->have_mm_ref = true;
> +				}
> +			} else {
> +				dio->sync = sync = true;
> +			}
> +		}
>  
>  		dio->loop = true;
>  		closure_call(&dio->op.cl, bch2_write, NULL, NULL);
> @@ -559,15 +578,25 @@ static __always_inline long bch2_dio_write_loop(struct dio_write *dio)
>  
>  static noinline __cold void bch2_dio_write_continue(struct dio_write *dio)
>  {
> -	struct mm_struct *mm = dio->mm;
> +	struct mm_struct *mm = dio->have_mm_ref ? dio->mm: NULL;
>  
>  	bio_reset(&dio->op.wbio.bio, NULL, REQ_OP_WRITE);
>  
> -	if (mm)
> +	if (mm) {
> +		if (unlikely(!mmget_not_zero(mm))) {
> +			/* process exited */
> +			dio->op.error = -ESRCH;
> +			bch2_dio_write_done(dio);
> +			return;
> +		}
> +
>  		kthread_use_mm(mm);
> +	}
>  	bch2_dio_write_loop(dio);
> -	if (mm)
> +	if (mm) {
> +		mmput(mm);
>  		kthread_unuse_mm(mm);
> +	}
>  }
>  
>  static void bch2_dio_write_loop_async(struct bch_write_op *op)

Based on a quick look, this one looks good to me.

-- 
Jens Axboe

  reply	other threads:[~2024-11-27 22:21 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-27 21:36 [PATCH] bcachefs: dio write: Take ref on mm_struct when using asynchronously Kent Overstreet
2024-11-27 21:47 ` Jens Axboe
2024-11-27 21:48 ` Jann Horn
2024-11-27 22:11   ` Kent Overstreet
2024-11-27 22:21     ` Jens Axboe [this message]
2024-11-27 23:11     ` Jann Horn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6ada0aa7-a260-4c0b-b80c-6360b2ea878f@kernel.dk \
    --to=axboe@kernel.dk \
    --cc=jannh@google.com \
    --cc=kent.overstreet@linux.dev \
    --cc=linux-bcachefs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox