Re: [RFC PATCH 4/4] PCI: hv: Fix synchronization between channel callback and hv_compose_msi_msg()

From: Andrea Parri
Date: Fri Apr 01 2022 - 12:45:16 EST


> > @@ -1662,6 +1662,55 @@ static u32 hv_compose_msi_req_v3(
> > return sizeof(*int_pkt);
> > }
> >
> > +/* As in vmbus_request_addr() but without the requestor lock */
> > +static u64 __hv_pci_request_addr(struct vmbus_channel *channel, u64 trans_id)
> > +{
> > + struct vmbus_requestor *rqstor = &channel->requestor;
> > + u64 req_addr;
> > +
> > + if (trans_id >= rqstor->size ||
> > + !test_bit(trans_id, rqstor->req_bitmap))
> > + return VMBUS_RQST_ERROR;
> > +
> > + req_addr = rqstor->req_arr[trans_id];
> > + rqstor->req_arr[trans_id] = rqstor->next_request_id;
> > + rqstor->next_request_id = trans_id;
> > +
> > + bitmap_clear(rqstor->req_bitmap, trans_id, 1);
> > +
> > + return req_addr;
> > +}
> > +
> > +/*
> > + * Clear/remove @trans_id from @channel's requestor, provided the memory
> > + * address stored at @trans_id equals @rqst_addr.
> > + */
> > +static void hv_pci_request_addr_match(struct vmbus_channel *channel,
> > + u64 trans_id, u64 rqst_addr)
> > +{
> > + struct vmbus_requestor *rqstor = &channel->requestor;
> > + unsigned long flags;
> > + u64 req_addr;
> > +
> > + spin_lock_irqsave(&rqstor->req_lock, flags);
> > +
> > + if (trans_id >= rqstor->size ||
> > + !test_bit(trans_id, rqstor->req_bitmap)) {
> > + spin_unlock_irqrestore(&rqstor->req_lock, flags);
> > + return;
> > + }
> > +
> > + req_addr = rqstor->req_arr[trans_id];
> > + if (req_addr == rqst_addr) {
> > + rqstor->req_arr[trans_id] = rqstor->next_request_id;
> > + rqstor->next_request_id = trans_id;
> > +
> > + bitmap_clear(rqstor->req_bitmap, trans_id, 1);
> > + }
> > +
> > + spin_unlock_irqrestore(&rqstor->req_lock, flags);
> > +}
> > +
>
> Even though these two new functions are used only in the Hyper-V
> vPCI driver, it seems like they should go in drivers/hv/channel.c
> along with vmbus_next_request_id() and vmbus_request_addr().
> And maybe vmbus_request_addr(), which gets the spin lock,
> could be implemented to call the new version above that
> assumes the spin lock is already held. Also, the new function
> that requires matching on the rqst_addr might also be folded
> into common code via an optional rqst_addr argument.

Noted.


> > @@ -2747,17 +2808,27 @@ static void hv_pci_onchannelcallback(void *context)
> > switch (desc->type) {
> > case VM_PKT_COMP:
> >
> > - req_addr = chan->request_addr_callback(chan, req_id);
> > + spin_lock_irqsave(&rqstor->req_lock, flags);
>
> Obtaining the lock (and releasing it below) might be better abstracted into
> a lock_requestor() and unlock_requestor() pair that are implemented in
> drivers/hv/channel.c along with the other related functions.

Seems like these helpers should go 'inline' in <linux/hyper.h>, let me
do it...

Thanks,
Andrea