mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] Drivers: vmbus: Check for channel allocation before looking up relids
@ 2023-02-14 11:27 Mohammed Gamal
  2023-02-17  6:25 ` Dexuan Cui
  0 siblings, 1 reply; 6+ messages in thread
From: Mohammed Gamal @ 2023-02-14 11:27 UTC (permalink / raw)
  To: decui, linux-hyperv
  Cc: linux-kernel, mikelley, parri.andrea, haiyangz, wei.liu,
	vkuznets, xxiong, Mohammed Gamal

relid2channel() assumes vmbus channel array to be allocated when called.
However, in cases such as kdump/kexec, not all relids will be reset by the host.
When the second kernel boots and if the guest receives a vmbus interrupt during
vmbus driver initialization before vmbus_connect() is called, before it finishes,
or if it fails, the vmbus interrupt service routine is called which in turn calls
relid2channel() and can cause a null pointer dereference.

So Make relid2channel() check if vmbus channels is allocated first, and if not
print a warning and return NULL to the caller.

Fixes: 8b6a877c060e ("Drivers: hv: vmbus: Replace the per-CPU channel lists with a global array of channels")

Signed-off-by: Mohammed Gamal <mgamal@redhat.com>
---
Changes from v1:
	* Corrected and detailed description in commit message
	* Added detailed warning message
---
 drivers/hv/connection.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/hv/connection.c b/drivers/hv/connection.c
index 9dc27e5d367a..82876e84f719 100644
--- a/drivers/hv/connection.c
+++ b/drivers/hv/connection.c
@@ -409,6 +409,10 @@ void vmbus_disconnect(void)
  */
 struct vmbus_channel *relid2channel(u32 relid)
 {
+	if (vmbus_connection.channels == NULL) {
+		WARN(1, "Requested relid=%u, but channel mapping not allocated!\n", relid);
+		return NULL;
+	}
 	if (WARN_ON(relid >= MAX_CHANNEL_RELIDS))
 		return NULL;
 	return READ_ONCE(vmbus_connection.channels[relid]);
-- 
2.38.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: [PATCH v2] Drivers: vmbus: Check for channel allocation before looking up relids
  2023-02-14 11:27 [PATCH v2] Drivers: vmbus: Check for channel allocation before looking up relids Mohammed Gamal
@ 2023-02-17  6:25 ` Dexuan Cui
  2023-02-17  9:42   ` Mohammed Gamal
  2023-02-17  9:51   ` Vitaly Kuznetsov
  0 siblings, 2 replies; 6+ messages in thread
From: Dexuan Cui @ 2023-02-17  6:25 UTC (permalink / raw)
  To: Mohammed Gamal, linux-hyperv
  Cc: linux-kernel, Michael Kelley (LINUX),
	parri.andrea, Haiyang Zhang, wei.liu, vkuznets, xxiong

> From: Mohammed Gamal <mgamal@redhat.com>
> Sent: Tuesday, February 14, 2023 3:28 AM
> ...
> So Make relid2channel() check if vmbus channels is allocated first, and if not
> print a warning and return NULL to the caller.
Can we change the above to:

Print a warning and error out in relid2channel() for a channel id that's invalid
in the second kernel.
 
> --- a/drivers/hv/connection.c
> +++ b/drivers/hv/connection.c
> @@ -409,6 +409,10 @@ void vmbus_disconnect(void)
>   */
>  struct vmbus_channel *relid2channel(u32 relid)
>  {
> +	if (vmbus_connection.channels == NULL) {
> +		WARN(1, "Requested relid=%u, but channel mapping not
> allocated!\n", relid);

WARN() may be too noisy. I suggest we use pr_warn() instead.

Can we make the line a little shorter:
        pr_warn("relid2channel: invalid channel id %u\n", relid);

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] Drivers: vmbus: Check for channel allocation before looking up relids
  2023-02-17  6:25 ` Dexuan Cui
@ 2023-02-17  9:42   ` Mohammed Gamal
  2023-02-17 18:59     ` Dexuan Cui
  2023-02-17  9:51   ` Vitaly Kuznetsov
  1 sibling, 1 reply; 6+ messages in thread
From: Mohammed Gamal @ 2023-02-17  9:42 UTC (permalink / raw)
  To: Dexuan Cui
  Cc: linux-hyperv, linux-kernel, Michael Kelley (LINUX),
	parri.andrea, Haiyang Zhang, wei.liu, vkuznets, xxiong

On Fri, Feb 17, 2023 at 8:25 AM Dexuan Cui <decui@microsoft.com> wrote:
>
> > From: Mohammed Gamal <mgamal@redhat.com>
> > Sent: Tuesday, February 14, 2023 3:28 AM
> > ...
> > So Make relid2channel() check if vmbus channels is allocated first, and if not
> > print a warning and return NULL to the caller.
> Can we change the above to:
>
> Print a warning and error out in relid2channel() for a channel id that's invalid
> in the second kernel.
Will do!

>
> > --- a/drivers/hv/connection.c
> > +++ b/drivers/hv/connection.c
> > @@ -409,6 +409,10 @@ void vmbus_disconnect(void)
> >   */
> >  struct vmbus_channel *relid2channel(u32 relid)
> >  {
> > +     if (vmbus_connection.channels == NULL) {
> > +             WARN(1, "Requested relid=%u, but channel mapping not
> > allocated!\n", relid);
>
> WARN() may be too noisy. I suggest we use pr_warn() instead.
>
Makes sense. Will use pr_warn() instead.

> Can we make the line a little shorter:
>         pr_warn("relid2channel: invalid channel id %u\n", relid);
>
I think this message could be a bit misleading. The problem here is
not that the relid
is invalid, but that the relid-to-channel mapping hasn't been
allocated by the second
kernel yet. An invalid relid could simply be the case where relid >=
MAX_CHANNEL_RELIDS.

May be something like:
     pr_warn("relid2channel: No channels mapped for relid %d\n, relid");
would be clearer?


^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: [PATCH v2] Drivers: vmbus: Check for channel allocation before looking up relids
  2023-02-17  6:25 ` Dexuan Cui
  2023-02-17  9:42   ` Mohammed Gamal
@ 2023-02-17  9:51   ` Vitaly Kuznetsov
  2023-02-17 19:00     ` Dexuan Cui
  1 sibling, 1 reply; 6+ messages in thread
From: Vitaly Kuznetsov @ 2023-02-17  9:51 UTC (permalink / raw)
  To: Dexuan Cui, Mohammed Gamal, linux-hyperv
  Cc: linux-kernel, Michael Kelley (LINUX),
	parri.andrea, Haiyang Zhang, wei.liu, xxiong

Dexuan Cui <decui@microsoft.com> writes:

>> From: Mohammed Gamal <mgamal@redhat.com>
>> Sent: Tuesday, February 14, 2023 3:28 AM
>> ...
>> So Make relid2channel() check if vmbus channels is allocated first, and if not
>> print a warning and return NULL to the caller.
> Can we change the above to:
>
> Print a warning and error out in relid2channel() for a channel id that's invalid
> in the second kernel.
>  
>> --- a/drivers/hv/connection.c
>> +++ b/drivers/hv/connection.c
>> @@ -409,6 +409,10 @@ void vmbus_disconnect(void)
>>   */
>>  struct vmbus_channel *relid2channel(u32 relid)
>>  {
>> +	if (vmbus_connection.channels == NULL) {
>> +		WARN(1, "Requested relid=%u, but channel mapping not
>> allocated!\n", relid);
>
> WARN() may be too noisy. I suggest we use pr_warn() instead.
>
> Can we make the line a little shorter:
>         pr_warn("relid2channel: invalid channel id %u\n", relid);
>

I'd even suggest to make it pr_warn_once() to be
safe. In theory, vmbus_chan_sched() call site looks like it can create a
lot of noise.

-- 
Vitaly


^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: [PATCH v2] Drivers: vmbus: Check for channel allocation before looking up relids
  2023-02-17  9:42   ` Mohammed Gamal
@ 2023-02-17 18:59     ` Dexuan Cui
  0 siblings, 0 replies; 6+ messages in thread
From: Dexuan Cui @ 2023-02-17 18:59 UTC (permalink / raw)
  To: Mohammed Gamal
  Cc: linux-hyperv, linux-kernel, Michael Kelley (LINUX),
	parri.andrea, Haiyang Zhang, wei.liu, vkuznets, xxiong

> From: Mohammed Gamal <mgamal@redhat.com>
> Sent: Friday, February 17, 2023 1:43 AM
> > > @@ -409,6 +409,10 @@ void vmbus_disconnect(void)
> > >   */
> > >  struct vmbus_channel *relid2channel(u32 relid)
> > >  {
> > > +     if (vmbus_connection.channels == NULL) {
> > > +             WARN(1, "Requested relid=%u, but channel mapping not
> > > allocated!\n", relid);
> >
> > WARN() may be too noisy. I suggest we use pr_warn() instead.
> >
> Makes sense. Will use pr_warn() instead.

As Vitaly suggested, pr_warn_once() looks better.
 
> > Can we make the line a little shorter:
> >         pr_warn("relid2channel: invalid channel id %u\n", relid);
> >
> I think this message could be a bit misleading. The problem here is
> not that the relid
> is invalid, but that the relid-to-channel mapping hasn't been
> allocated by the second
> kernel yet. An invalid relid could simply be the case where relid >=
> MAX_CHANNEL_RELIDS.
> 
> May be something like:
>      pr_warn("relid2channel: No channels mapped for relid %d\n, relid");
> would be clearer?

IMO the 'relid' is invalid in the second kernel because the second
kernel has not called vmbus_request_offers() yet, but your new message
is ok to me. I just wanted to avoid a too long line of >80 characters :-)

^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: [PATCH v2] Drivers: vmbus: Check for channel allocation before looking up relids
  2023-02-17  9:51   ` Vitaly Kuznetsov
@ 2023-02-17 19:00     ` Dexuan Cui
  0 siblings, 0 replies; 6+ messages in thread
From: Dexuan Cui @ 2023-02-17 19:00 UTC (permalink / raw)
  To: Vitaly Kuznetsov, Mohammed Gamal, linux-hyperv
  Cc: linux-kernel, Michael Kelley (LINUX),
	parri.andrea, Haiyang Zhang, wei.liu, xxiong

> From: Vitaly Kuznetsov <vkuznets@redhat.com>
> Sent: Friday, February 17, 2023 1:52 AM
> 
> I'd even suggest to make it pr_warn_once() to be
> safe. In theory, vmbus_chan_sched() call site looks like it can create a
> lot of noise.
 
Good point.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-02-17 19:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-14 11:27 [PATCH v2] Drivers: vmbus: Check for channel allocation before looking up relids Mohammed Gamal
2023-02-17  6:25 ` Dexuan Cui
2023-02-17  9:42   ` Mohammed Gamal
2023-02-17 18:59     ` Dexuan Cui
2023-02-17  9:51   ` Vitaly Kuznetsov
2023-02-17 19:00     ` Dexuan Cui

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®