Skip to contentSkip to conversation

blk-softirq vs smp_call_function_single_async()

DISCUSSION Peter Zijlstra 3 participants 8 messages Jun 8, 2020 – Jun 9, 2020 Block layer

lore
Share & tools
lore
#0 lore raw

Hi Jens,

I've been going through smp_call_function_single_async() users and stumbled upon blk-softirq.c, which has:

static int raise_blk_irq(int cpu, struct request *rq)

{
	if (cpu_online(cpu)) {
		call_single_data_t *data = &rq->csd;

		data->func = trigger_softirq;
		data->info = rq;
		data->flags = 0;

		smp_call_function_single_async(cpu, data);
		return 0;
	}

	return 1;
}

What, if anything, guarantees rq->csd is not already in use at that time?

The purpose of that CSD is to make the BLOCK_SOFTIRQ go, but there's plenty of other ways to tickle that, afaict. So if that races vs someone else, and that completes whatever was needed, then can't we get to raise_blk_irq() again, even though the csd is still enqueued?

Worse; it has: data->flags = 0; so our early exit will not happen, even when it should.

#1 lore raw
Quoted 21 lines · Peter Zijlstra

Hi Jens,

I've been going through smp_call_function_single_async() users and stumbled upon blk-softirq.c, which has:

static int raise_blk_irq(int cpu, struct request *rq)

{
	if (cpu_online(cpu)) {
		call_single_data_t *data = &rq->csd;

		data->func = trigger_softirq;
		data->info = rq;
		data->flags = 0;

		smp_call_function_single_async(cpu, data);
		return 0;
	}

	return 1;
}
Quoted 2 lines

What, if anything, guarantees rq->csd is not already in use at that time?

A request can only be completed once.

#2 lore raw
Quoted 19 lines · Christoph Hellwig
Quoted 18 lines · Peter Zijlstra

Hi Jens,

I've been going through smp_call_function_single_async() users and stumbled upon blk-softirq.c, which has:

static int raise_blk_irq(int cpu, struct request *rq)

{
	if (cpu_online(cpu)) {
		call_single_data_t *data = &rq->csd;

		data->func = trigger_softirq;
		data->info = rq;
		data->flags = 0;

		smp_call_function_single_async(cpu, data);
		return 0;
	}
Quoted 7 lines
Quoted 5 lines
	return 1;
}

What, if anything, guarantees rq->csd is not already in use at that time?

A request can only be completed once.

Sure, but that doesn't help.

CPU0 CPU1

raise_blk_irq() BLOCK_SOFTIRQ
IPI -> CPU1

				// picks up thing from CPU0
				req->complete(req);


	<big hole where CSD is active and request completed>

				<IPI>
				  trigger_softirq()

What happens to a struct request after completion, is it free()d or reused? If reused, how do we guarantee CSD completion before free()ing?

#3 lore raw
Quoted 19 lines · Peter Zijlstra
Quoted 1 line

A request can only be completed once.

Sure, but that doesn't help.

CPU0 CPU1

raise_blk_irq() BLOCK_SOFTIRQ
IPI -> CPU1

				// picks up thing from CPU0
				req->complete(req);


	<big hole where CSD is active and request completed>

				<IPI>
				  trigger_softirq()
Quoted 3 lines

What happens to a struct request after completion, is it free()d or reused? If reused, how do we guarantee CSD completion before free()ing?

The request is freed in the block layer sense by __blk_mq_free_request (which doesn't actually free the memory, so it eventually gets reused).

__blk_mq_free_request is called from blk_mq_end_request which is called from the drivers ->complete_rq method, which is called from the block softirq.

What is the method to guaranteed CSD completion?

#4 lore raw
Quoted 24 lines · Christoph Hellwig
Quoted 19 lines · Peter Zijlstra
Quoted 1 line

A request can only be completed once.

Sure, but that doesn't help.

CPU0 CPU1

raise_blk_irq() BLOCK_SOFTIRQ
IPI -> CPU1

				// picks up thing from CPU0
				req->complete(req);


	<big hole where CSD is active and request completed>

				<IPI>
				  trigger_softirq()
Quoted 3 lines

What happens to a struct request after completion, is it free()d or reused? If reused, how do we guarantee CSD completion before free()ing?

Quoted 8 lines

The request is freed in the block layer sense by __blk_mq_free_request (which doesn't actually free the memory, so it eventually gets reused).

__blk_mq_free_request is called from blk_mq_end_request which is called from the drivers ->complete_rq method, which is called from the block softirq.

What is the method to guaranteed CSD completion?

There isn't one, it was meant to be used with static allocations.

Frederic proposed changing all these to irq_work, and I think I'll go do that. First dinner though.

#5 lore raw
Quoted 4 lines · Peter Zijlstra

There isn't one, it was meant to be used with static allocations.

Frederic proposed changing all these to irq_work, and I think I'll go do that. First dinner though.

The irq_work API looks reasonable. What are the tradeoffs for smp_call_single_async vs irq_work?

maintainer
#7 lore raw
In reply to Peter Zijlstra
Quoted 32 lines · Peter Zijlstra
Quoted 31 lines · Christoph Hellwig
Quoted 19 lines · Peter Zijlstra
Quoted 1 line

A request can only be completed once.

Sure, but that doesn't help.

CPU0 CPU1

raise_blk_irq() BLOCK_SOFTIRQ
IPI -> CPU1

				// picks up thing from CPU0
				req->complete(req);


	<big hole where CSD is active and request completed>

				<IPI>
				  trigger_softirq()
Quoted 3 lines

What happens to a struct request after completion, is it free()d or reused? If reused, how do we guarantee CSD completion before free()ing?

The request is freed in the block layer sense by __blk_mq_free_request (which doesn't actually free the memory, so it eventually gets reused).

__blk_mq_free_request is called from blk_mq_end_request which is called from the drivers ->complete_rq method, which is called from the block softirq.

Quoted 6 lines
Quoted 1 line

What is the method to guaranteed CSD completion?

There isn't one, it was meant to be used with static allocations.

Frederic proposed changing all these to irq_work, and I think I'll go do that. First dinner though.

That sounds good to me, and will also shrink struct request a bit, which is always nice.

Signature · 1 line
Jens Axboe
#6 lore raw
In reply to Christoph Hellwig
Quoted 5 lines · Christoph Hellwig
Quoted 4 lines · Peter Zijlstra

There isn't one, it was meant to be used with static allocations.

Frederic proposed changing all these to irq_work, and I think I'll go do that. First dinner though.

OK, after having looked at this more, I think my initial analysis is actually wrong and this code should work as-is.

The thing that I missed yesterday is that we only add the request to the blk_cpu_done list in the IPI, this means that the race I described earlier is not in fact possible.

The IPI must happen for progress to be made.

And the same is true for blk_mq_force_complete_rq(), which also uses this csd.

Quoted 2 lines

The irq_work API looks reasonable. What are the tradeoffs for smp_call_single_async vs irq_work?

To still answer your question; irq_work_queue*() has an atomic op extra that allows for more convenient semantics -- but is in your case strictly superfluous.

Still, Jens' point about irq_work being smaller stands, and I think more users could benefit from something intermediate. Let me continue with the cleanups / audit and see what comes out at the end.

Thanks!