Re: [PATCH] ext4: bio_alloc never fails

From: Theodore Y. Ts'o
Date: Wed Oct 30 2019 - 11:04:53 EST


On Wed, Oct 30, 2019 at 03:43:10PM +0530, Ritesh Harjani wrote:
>
>
> On 10/30/19 9:56 AM, Gao Xiang wrote:
> > Similar to [1] [2], it seems a trivial cleanup since
> > bio_alloc can handle memory allocation as mentioned in
> > fs/direct-io.c (also see fs/block_dev.c, fs/buffer.c, ..)
> >
>
> AFAIU, the reason is that, bio_alloc with __GFP_DIRECT_RECLAIM
> flags guarantees bio allocation under some given restrictions,
> as stated in fs/direct-io.c
> So here it is ok to not check for NULL value from bio_alloc.
>
> I think we can update above info too in your commit msg.

Please also add a short comment in the code itself, so it's clear why
it's OK to skip the error check, and reference the comments for
bio_alloc_bioset(). This is the fairly subtle bit which makes this
change not obvious:

* When @bs is not NULL, if %__GFP_DIRECT_RECLAIM is set then bio_alloc will
* always be able to allocate a bio. This is due to the mempool guarantees.
* To make this work, callers must never allocate more than 1 bio at a time
* from this pool. Callers that need to allocate more than 1 bio must always
* submit the previously allocated bio for IO before attempting to allocate
* a new one. Failure to do so can cause deadlocks under memory pressure.
*
* Note that when running under generic_make_request() (i.e. any block
* driver), bios are not submitted until after you return - see the code in
* generic_make_request() that converts recursion into iteration, to prevent
* stack overflows.
*
* This would normally mean allocating multiple bios under
* generic_make_request() would be susceptible to deadlocks, but we have
* deadlock avoidance code that resubmits any blocked bios from a rescuer
* thread.

Otherwise, someone else may not understand why it's safe to not check
the error return then submit cleanup patch to add the error checking
back. :-)

- Ted