Skip to contentSkip to conversation

Drivers: hv: vmbus: annotate struct vmbus_gpadl with __counted_by_ptr

PATCH In review Bill Wendling 4 participants 7 messages Sep 23 – Sep 26

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Drivers: hv: vmbus: annotate struct vmbus_gpadl with __counted_by_ptr

Add the "__counted_by_ptr" attribute to the buffer field of "struct vmbus_gpadl". This allows compilers (GCC and Clang) to perform compile-time and runtime bounds-checking when KASAN is enabled, preventing potential out-of-bounds accesses to the GPADL buffer.

The fields "buffer" and "size" of "struct vmbus_gpadl" are assigned exactly once, during GPADL establishment inside "__vmbus_establish_gpadl()" in "drivers/hv/channel.c".

To ensure that the count field ("size") is initialized before the pointer field ("buffer") is assigned, we reorder the assignments in "__vmbus_establish_gpadl()" so that "gpadl->size" is written before "gpadl->buffer".

---
 drivers/hv/channel.c   | 2 +-
 include/linux/hyperv.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/hv/channel.c +1 -1
index 7e4cc6f55237..7042de2dd481 100644
@@ -548,8 +548,8 @@ static int __vmbus_establish_gpadl(struct vmbus_channel *channel,
 
 	/* At this point, we received the gpadl created msg */
 	gpadl->gpadl_handle = gpadlmsg->gpadl;
-	gpadl->buffer = kbuffer;
 	gpadl->size = size;
+	gpadl->buffer = kbuffer;
 
 
 cleanup:
include/linux/hyperv.h +1 -1
index 9e109d91aa14..408748a05440 100644
@@ -780,7 +780,7 @@ struct vmbus_device {
 struct vmbus_gpadl {
 	u32 gpadl_handle;
 	u32 size;
-	void *buffer;
+	void *buffer __counted_by_ptr(size);
 	bool decrypted;
 };
 
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 27 lines · Bill Wendling

Add the "__counted_by_ptr" attribute to the buffer field of "struct vmbus_gpadl". This allows compilers (GCC and Clang) to perform compile-time and runtime bounds-checking when KASAN is enabled, preventing potential out-of-bounds accesses to the GPADL buffer.

The fields "buffer" and "size" of "struct vmbus_gpadl" are assigned exactly once, during GPADL establishment inside "__vmbus_establish_gpadl()" in "drivers/hv/channel.c".

To ensure that the count field ("size") is initialized before the pointer field ("buffer") is assigned, we reorder the assignments in "__vmbus_establish_gpadl()" so that "gpadl->size" is written before "gpadl->buffer".

---
  drivers/hv/channel.c   | 2 +-
  include/linux/hyperv.h | 2 +-
  2 files changed, 2 insertions(+), 2 deletions(-)
drivers/hv/channel.c +0 -0
index 7e4cc6f55237..7042de2dd481 100644

drivers/hv/channel.c static int __vmbus_establish_gpadl(struct vmbus_channel *channel, · line 552

Quoted 6 lines
drivers/hv/channel.c +1 -1
@@ -548,8 +548,8 @@ static int __vmbus_establish_gpadl(struct vmbus_channel *channel,
 
  	/* At this point, we received the gpadl created msg */
  	gpadl->gpadl_handle = gpadlmsg->gpadl;
-	gpadl->buffer = kbuffer;
  	gpadl->size = size;
+	gpadl->buffer = kbuffer;

I think in some cases these subtle changes are worth a short but informative comment saying that the _counter_ must be initialized before the first reference to the pointer.

We can see this as part of the learning curve people have to go through before the counted_by annotations and their requirements are more widely known and understood. But I guess this can be handled on a case-by-case basis.

Regardless:

Thanks
-Gustavo

Quoted 16 lines
drivers/hv/channel.c +0 -0
  cleanup:
include/linux/hyperv.h +1 -1
index 9e109d91aa14..408748a05440 100644
@@ -780,7 +780,7 @@ struct vmbus_device {
  struct vmbus_gpadl {
  	u32 gpadl_handle;
  	u32 size;
-	void *buffer;
+	void *buffer __counted_by_ptr(size);
  	bool decrypted;
  };
 
#2 lore raw
Quoted 25 lines · Gustavo A. R. Silva
Quoted 22 lines · Bill Wendling

Add the "__counted_by_ptr" attribute to the buffer field of "struct vmbus_gpadl". This allows compilers (GCC and Clang) to perform compile-time and runtime bounds-checking when KASAN is enabled, preventing potential out-of-bounds accesses to the GPADL buffer.

The fields "buffer" and "size" of "struct vmbus_gpadl" are assigned exactly once, during GPADL establishment inside "__vmbus_establish_gpadl()" in "drivers/hv/channel.c".

To ensure that the count field ("size") is initialized before the pointer field ("buffer") is assigned, we reorder the assignments in "__vmbus_establish_gpadl()" so that "gpadl->size" is written before "gpadl->buffer".

---
  drivers/hv/channel.c   | 2 +-
  include/linux/hyperv.h | 2 +-
  2 files changed, 2 insertions(+), 2 deletions(-)
Quoted 14 lines

drivers/hv/channel.c static int __vmbus_establish_gpadl(struct vmbus_channel *channel, · line 551

Quoted 10 lines
drivers/hv/channel.c +1 -1
index 7e4cc6f55237..7042de2dd481 100644
@@ -548,8 +548,8 @@ static int __vmbus_establish_gpadl(struct vmbus_channel *channel,
  	/* At this point, we received the gpadl created msg */
  	gpadl->gpadl_handle = gpadlmsg->gpadl;
-	gpadl->buffer = kbuffer;
  	gpadl->size = size;
+	gpadl->buffer = kbuffer;

I think in some cases these subtle changes are worth a short but informative comment saying that the _counter_ must be initialized before the first reference to the pointer.

But that's only for dereferencing it... there's no ordering requirement here at all (and I think swapping order is needless churn). Neither order is correct: only having them both set before dereferencing "buffer" is required.

Signature · 1 line
Kees Cook
#3 lore raw
Quoted 41 lines · Kees Cook
Quoted 25 lines · Gustavo A. R. Silva
Quoted 22 lines · Bill Wendling

Add the "__counted_by_ptr" attribute to the buffer field of "struct vmbus_gpadl". This allows compilers (GCC and Clang) to perform compile-time and runtime bounds-checking when KASAN is enabled, preventing potential out-of-bounds accesses to the GPADL buffer.

The fields "buffer" and "size" of "struct vmbus_gpadl" are assigned exactly once, during GPADL establishment inside "__vmbus_establish_gpadl()" in "drivers/hv/channel.c".

To ensure that the count field ("size") is initialized before the pointer field ("buffer") is assigned, we reorder the assignments in "__vmbus_establish_gpadl()" so that "gpadl->size" is written before "gpadl->buffer".

---
   drivers/hv/channel.c   | 2 +-
   include/linux/hyperv.h | 2 +-
   2 files changed, 2 insertions(+), 2 deletions(-)
Quoted 14 lines

drivers/hv/channel.c static int __vmbus_establish_gpadl(struct vmbus_channel *channel, · line 551

Quoted 10 lines
drivers/hv/channel.c +1 -1
index 7e4cc6f55237..7042de2dd481 100644
@@ -548,8 +548,8 @@ static int __vmbus_establish_gpadl(struct vmbus_channel *channel,
   	/* At this point, we received the gpadl created msg */
   	gpadl->gpadl_handle = gpadlmsg->gpadl;
-	gpadl->buffer = kbuffer;
   	gpadl->size = size;
+	gpadl->buffer = kbuffer;

I think in some cases these subtle changes are worth a short but informative comment saying that the _counter_ must be initialized before the first reference to the pointer.

Quoted 1 line

But that's only for dereferencing it... there's no ordering requirement

Ah yes, I got a bit carried away by the change itself.

Quoted 3 lines

here at all (and I think swapping order is needless churn). Neither order is correct: only having them both set before dereferencing "buffer" is required.

Yes; maybe this is something the LLM should learn.

Thanks
-Gustavo

#5 lore raw
In reply to Bill Wendling

From: Bill Wendling <[email protected]> Sent: Tuesday, September 22, 2026 10:53 PM

Quoted 5 lines

Add the "__counted_by_ptr" attribute to the buffer field of "struct vmbus_gpadl". This allows compilers (GCC and Clang) to perform compile-time and runtime bounds-checking when KASAN is enabled, preventing potential out-of-bounds accesses to the GPADL buffer.

It turns out this "buffer" field is never dereferenced. The value is saved here only for the purpose of passing into set_memory_encrypted() as called by vmbus_teardown_gpadl(). The memory *is* referenced, but through a wraparound mapping created by vmap() from the struct page's underlying the buffer. The mapping is created in hv_ringbuffer_init().

Given that, is this patch still appropriate?

Michael

Quoted 49 lines

The fields "buffer" and "size" of "struct vmbus_gpadl" are assigned exactly once, during GPADL establishment inside "__vmbus_establish_gpadl()" in "drivers/hv/channel.c".

To ensure that the count field ("size") is initialized before the pointer field ("buffer") is assigned, we reorder the assignments in "__vmbus_establish_gpadl()" so that "gpadl->size" is written before "gpadl->buffer".

---
 drivers/hv/channel.c   | 2 +-
 include/linux/hyperv.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/hv/channel.c +1 -1
index 7e4cc6f55237..7042de2dd481 100644
@@ -548,8 +548,8 @@ static int __vmbus_establish_gpadl(struct vmbus_channel
 *channel,
 
 	/* At this point, we received the gpadl created msg */
 	gpadl->gpadl_handle = gpadlmsg->gpadl;
-	gpadl->buffer = kbuffer;
 	gpadl->size = size;
+	gpadl->buffer = kbuffer;
 
 

cleanup:

include/linux/hyperv.h +1 -2
index 9e109d91aa14..408748a05440 100644
@@ -780,7 +780,7 @@ struct vmbus_device {
 struct vmbus_gpadl {
 	u32 gpadl_handle;
 	u32 size;
-	void *buffer;
+	void *buffer __counted_by_ptr(size);
 	bool decrypted;
 };
 
--

2.55.0.1082.g2b9226bbc0-goog

#6 lore raw
Quoted 8 lines · Michael Kelley

From: Bill Wendling <[email protected]> Sent: Tuesday, September 22, 2026 10:53 PM

Quoted 5 lines

Add the "__counted_by_ptr" attribute to the buffer field of "struct vmbus_gpadl". This allows compilers (GCC and Clang) to perform compile-time and runtime bounds-checking when KASAN is enabled, preventing potential out-of-bounds accesses to the GPADL buffer.

Quoted 8 lines

It turns out this "buffer" field is never dereferenced. The value is saved here only for the purpose of passing into set_memory_encrypted() as called by vmbus_teardown_gpadl(). The memory *is* referenced, but through a wraparound mapping created by vmap() from the struct page's underlying the buffer. The mapping is created in hv_ringbuffer_init().

Given that, is this patch still appropriate?

Hi Michael,

Huh...so it is. The buffer is converted to 'unsigned long' (*sigh*) and then passed into arch-specific functions, which do what you mentioned above. Given that please feel free to ignore this patch. Apologies for the extra emails...

-bw

Quoted 49 lines
Quoted 48 lines

The fields "buffer" and "size" of "struct vmbus_gpadl" are assigned exactly once, during GPADL establishment inside "__vmbus_establish_gpadl()" in "drivers/hv/channel.c".

To ensure that the count field ("size") is initialized before the pointer field ("buffer") is assigned, we reorder the assignments in "__vmbus_establish_gpadl()" so that "gpadl->size" is written before "gpadl->buffer".

---
 drivers/hv/channel.c   | 2 +-
 include/linux/hyperv.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/hv/channel.c +1 -1
index 7e4cc6f55237..7042de2dd481 100644
@@ -548,8 +548,8 @@ static int __vmbus_establish_gpadl(struct vmbus_channel
 *channel,
 
      /* At this point, we received the gpadl created msg */
      gpadl->gpadl_handle = gpadlmsg->gpadl;
-     gpadl->buffer = kbuffer;
      gpadl->size = size;
+     gpadl->buffer = kbuffer;
 
 

cleanup:

include/linux/hyperv.h +1 -2
index 9e109d91aa14..408748a05440 100644
@@ -780,7 +780,7 @@ struct vmbus_device {
 struct vmbus_gpadl {
      u32 gpadl_handle;
      u32 size;
-     void *buffer;
+     void *buffer __counted_by_ptr(size);
      bool decrypted;
 };
 
--

2.55.0.1082.g2b9226bbc0-goog

#4 lore raw
In reply to Gustavo A. R. Silva
Quoted 46 lines · Gustavo A. R. Silva
Quoted 41 lines · Kees Cook
Quoted 25 lines · Gustavo A. R. Silva
Quoted 22 lines · Bill Wendling

Add the "__counted_by_ptr" attribute to the buffer field of "struct vmbus_gpadl". This allows compilers (GCC and Clang) to perform compile-time and runtime bounds-checking when KASAN is enabled, preventing potential out-of-bounds accesses to the GPADL buffer.

The fields "buffer" and "size" of "struct vmbus_gpadl" are assigned exactly once, during GPADL establishment inside "__vmbus_establish_gpadl()" in "drivers/hv/channel.c".

To ensure that the count field ("size") is initialized before the pointer field ("buffer") is assigned, we reorder the assignments in "__vmbus_establish_gpadl()" so that "gpadl->size" is written before "gpadl->buffer".

---
   drivers/hv/channel.c   | 2 +-
   include/linux/hyperv.h | 2 +-
   2 files changed, 2 insertions(+), 2 deletions(-)
Quoted 14 lines

drivers/hv/channel.c static int __vmbus_establish_gpadl(struct vmbus_channel *channel, · line 551

Quoted 10 lines
drivers/hv/channel.c +1 -1
index 7e4cc6f55237..7042de2dd481 100644
@@ -548,8 +548,8 @@ static int __vmbus_establish_gpadl(struct vmbus_channel *channel,
    /* At this point, we received the gpadl created msg */
    gpadl->gpadl_handle = gpadlmsg->gpadl;
-   gpadl->buffer = kbuffer;
    gpadl->size = size;
+   gpadl->buffer = kbuffer;

I think in some cases these subtle changes are worth a short but informative comment saying that the _counter_ must be initialized before the first reference to the pointer.

Quoted 1 line

But that's only for dereferencing it... there's no ordering requirement

Ah yes, I got a bit carried away by the change itself.

Quoted 7 lines
Quoted 3 lines

here at all (and I think swapping order is needless churn). Neither order is correct: only having them both set before dereferencing "buffer" is required.

Yes; maybe this is something the LLM should learn.

Thanks
-Gustavo

I've explicitly told the LLM exactly this, but it either forgets or ignores it. *sigh*

-bw