Hi Arek, On Mon, Oct 9, 2023 at 2:25 AM Arkadiusz Bokowy <a.bokowy@xxxxxxxxxxx> wrote: > > Hi, > > >> +/* Wait for all pending HCI commands to complete. > >> + */ > >> +void hci_cmd_sync_flush(struct hci_dev *hdev) > >> +{ > >> + flush_work(&hdev->cmd_sync_work); > > > > Afaik this will block waiting the work to complete which sounds a > > little dangerous especially if hdev has been locked. > > Yes, this will block wait for all tasks queued on the cmd_synd_work. > Unfortunately, I'm not very familiar (not yet) with BlueZ kernel > component, so I'm not saying that this solution is correct. I hoped > that someone with actual kernel knowledge will review it :) > > Anyway, my simple test case passes with such solution without any lockups. > > Alternatively, I can move this block wait before hdev lock in > hci_le_*_adv_report_evt() functions. > > > Couldn't we just do: > > > > diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c > > index 195aea2198a9..78f0a8fb0a19 100644 > > --- a/net/bluetooth/hci_core.c > > +++ b/net/bluetooth/hci_core.c > > @@ -136,6 +136,7 @@ bool hci_discovery_active(struct hci_dev *hdev) > > struct discovery_state *discov = &hdev->discovery; > > > > switch (discov->state) { > > + case DISCOVERY_STARTING: > > case DISCOVERY_FINDING: > > case DISCOVERY_RESOLVING: > > return true; > > I'm not sure whether it will fix the issue... I've tested it and it does > not pass my test with a delay added to the start_discovery_complete() > function. The problem here is with synchronization. Since the LE meta > event (device found) and start discovery completion might be processed > simultaneously... Also, it will not be true that discovery is active if > the state is "starting", because HCI might return error when enabling > scanning. > > There is other solution to my problem, though. In a real world case > scenario, it's not an issue that the LE meta event coming just after > scan enabled signal will be dropped, because there will be more such > events later. The problem is with btvirt, which does not "broadcasts" LE > meta events when discovering is enabled. So, I can "fix" btvirt instead > of patching the kernel, by repeatedly signaling LE meta events. This > will slightly increase CPU load with btvirt, but will work. What do you > think? Yeah, I think it is more of an issue with btdev then, perhaps we need to add a delay or something to generate the reports, or we keep generating them based on the scan parameters that way it would emulate a little bit better how it works with real controllers, that said keep in mind that we need to cancel all timers properly as well if we go ahead with this change. > Regards, > Arek -- Luiz Augusto von Dentz