This partially fixes a problem described here
I fixes a bug when device removal happens immediatelly after bio_endio.
Note that rmmod is still not safe. These rmmod bugs seem to be ubiquitous.
Free dm_io structure before calling bio_endio
There is a possible race:
/* scheduling happens */
close the device
remove the device
Thread 2, when removing the device, sees nonempty md->io_pool (because the
io hasn't been freed by thread 1 yet) and may crash with BUG in mempool_free.
Thread 1 may also crash, when freeing into a nonexisting mempool.
To fix this bug, we must make sure that bio_endio is the last call and
the md structure is not accessed after bio_endio was signaled.
There is another bio_endio in process_barrier, but it is called from the thread
and the thread is destroyed prior to freeing the mempools, so this call is
not affected by the bug.
Similar bug exists for module unloads (the module may be unloaded immediatelly
after bio_endio), but it can't be fixed without redesigning module refcounts.