Skip to contentSkip to conversation

net: bluetooth: hci_sock: Use 'const void *' instead of 'void *' for 2nd parameter of hci_test_bit()

PATCH Superseded Chen Gang S 5 participants 11 messages Feb 2, 2015 – Feb 5, 2015

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 net: bluetooth: hci_sock: Use 'const void *' instead of 'void *' for 2nd parameter of hci_test_bit()

hci_test_bit() does not modify 2nd parameter, so it is better to let it be constant, or may cause build warning. The related warning (with allmodconfig under xtensa):

net/bluetooth/hci_sock.c: In function 'hci_sock_sendmsg':
net/bluetooth/hci_sock.c:955:8: warning: passing argument 2 of 'hci_test_bit' discards 'const' qualifier from pointer target type [-Wdiscarded-array-qualifiers]

          &hci_sec_filter.ocf_mask[ogf])) &&
          ^
  net/bluetooth/hci_sock.c:49:19: note: expected 'void *' but argument is of type 'const __u32 (*)[4] {aka const unsigned int (*)[4]}'
   static inline int hci_test_bit(int nr, void *addr)
                     ^
---
 net/bluetooth/hci_sock.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/bluetooth/hci_sock.c +1 -1
index 80c5a79..858b53a 100644
@@ -46,7 +46,7 @@ struct hci_pinfo {
 	unsigned short    channel;
 };
 
-static inline int hci_test_bit(int nr, void *addr)
+static inline int hci_test_bit(int nr, const void *addr)
 {
 	return *((__u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31));
 }
Signature · 1 line
1.9.3
#1 lore raw
Quoted 3 lines · Chen Gang S

hci_test_bit() does not modify 2nd parameter, so it is better to let it be constant, or may cause build warning. The related warning (with allmodconfig under xtensa):

[]

net/bluetooth/hci_sock.c

Quoted 1 line
net/bluetooth/hci_sock.c +0 -0

[]

net/bluetooth/hci_sock.c struct hci_pinfo { · line 52

Quoted 9 lines
net/bluetooth/hci_sock.c +1 -1
@@ -46,7 +46,7 @@ struct hci_pinfo {
 	unsigned short    channel;
 };
 
-static inline int hci_test_bit(int nr, void *addr)
+static inline int hci_test_bit(int nr, const void *addr)
 {
 	return *((__u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31));
 }

It's probably better to use const __u32 * here too, but the real thing I wonder is whether or not there's an issue with one of the 2 uses of this function.

One of them passes a unsigned long *, the other a u32 *.

$ git grep -w hci_test_bit
net/bluetooth/hci_sock.c:static inline int hci_test_bit(int nr, void *addr)
net/bluetooth/hci_sock.c:       if (!hci_test_bit(flt_event, &flt->event_mask))
net/bluetooth/hci_sock.c:                    !hci_test_bit(ocf & HCI_FLT_OCF_BITS,
net/bluetooth/hci_sock.c-                                  &hci_sec_filter.ocf_mask[ogf])) &&

hci_sec_filter.ocf_mask is __u32
but flt->event_mask is unsigned long.

Any possible issue here on 64-bit systems?

---

$ git grep -A4 "struct hci_filter {"
include/net/bluetooth/hci_sock.h:struct hci_filter {
include/net/bluetooth/hci_sock.h-       unsigned long type_mask;
include/net/bluetooth/hci_sock.h-       unsigned long event_mask[2];
include/net/bluetooth/hci_sock.h-       __le16 opcode;
include/net/bluetooth/hci_sock.h-};

---

static bool is_filtered_packet(struct sock *sk, struct sk_buff *skb)

{
	struct hci_filter *flt;
[...]
	if (!hci_test_bit(flt_event, &flt->event_mask))
		return true;
#2 lore raw
Quoted 29 lines · Joe Perches
Quoted 3 lines · Chen Gang S

hci_test_bit() does not modify 2nd parameter, so it is better to let it be constant, or may cause build warning. The related warning (with allmodconfig under xtensa):

[]

net/bluetooth/hci_sock.c

Quoted 1 line
net/bluetooth/hci_sock.c +0 -0

[]

net/bluetooth/hci_sock.c struct hci_pinfo { · line 52

Quoted 9 lines
net/bluetooth/hci_sock.c +1 -1
@@ -46,7 +46,7 @@ struct hci_pinfo {
 	unsigned short    channel;
 };
 
-static inline int hci_test_bit(int nr, void *addr)
+static inline int hci_test_bit(int nr, const void *addr)
 {
 	return *((__u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31));
 }

It's probably better to use const __u32 * here too, but the real thing I wonder is whether or not there's an issue with one of the 2 uses of this function.

One of them passes a unsigned long *, the other a u32 *.

$ git grep -w hci_test_bit
net/bluetooth/hci_sock.c:static inline int hci_test_bit(int nr, void *addr)
net/bluetooth/hci_sock.c:       if (!hci_test_bit(flt_event, &flt->event_mask))
net/bluetooth/hci_sock.c:                    !hci_test_bit(ocf & HCI_FLT_OCF_BITS,
net/bluetooth/hci_sock.c-                                  &hci_sec_filter.ocf_mask[ogf])) &&
Quoted 5 lines

hci_sec_filter.ocf_mask is __u32
but flt->event_mask is unsigned long.

Any possible issue here on 64-bit systems?

For me, it can not cause issue on 64-bit systems. hci_test_bit() treats 'addr' as "__u32 *", and has to use the pointer to do something.

Thanks.

Signature · 3 lines
Chen Gang

Open, share, and attitude like air, water, and life which God blessed
#3 lore raw
Quoted 30 lines · Chen Gang S
Quoted 29 lines · Joe Perches
Quoted 3 lines · Chen Gang S

hci_test_bit() does not modify 2nd parameter, so it is better to let it be constant, or may cause build warning. The related warning (with allmodconfig under xtensa):

[]

net/bluetooth/hci_sock.c

Quoted 1 line
net/bluetooth/hci_sock.c +0 -0

[]

net/bluetooth/hci_sock.c struct hci_pinfo { · line 52

Quoted 9 lines
net/bluetooth/hci_sock.c +1 -1
@@ -46,7 +46,7 @@ struct hci_pinfo {
 	unsigned short    channel;
 };
 
-static inline int hci_test_bit(int nr, void *addr)
+static inline int hci_test_bit(int nr, const void *addr)
 {
 	return *((__u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31));
 }

It's probably better to use const __u32 * here too, but the real thing I wonder is whether or not there's an issue with one of the 2 uses of this function.

One of them passes a unsigned long *, the other a u32 *.

$ git grep -w hci_test_bit
net/bluetooth/hci_sock.c:static inline int hci_test_bit(int nr, void *addr)
net/bluetooth/hci_sock.c:       if (!hci_test_bit(flt_event, &flt->event_mask))
net/bluetooth/hci_sock.c:                    !hci_test_bit(ocf & HCI_FLT_OCF_BITS,
net/bluetooth/hci_sock.c-                                  &hci_sec_filter.ocf_mask[ogf])) &&
Quoted 9 lines
Quoted 5 lines

hci_sec_filter.ocf_mask is __u32
but flt->event_mask is unsigned long.

Any possible issue here on 64-bit systems?

For me, it can not cause issue on 64-bit systems. hci_test_bit() treats 'addr' as "__u32 *", and has to use the pointer to do something.

'event_mask' is intended to type cast to "__u32 *" within 'hci_sock.c'. So for me, "const __u32 *" is better than "const void *" for 2nd parameter of hci_test_bit().

If what I said above is correct, and also if necessary, I shall patch v3 for it.

Thanks.

Signature · 3 lines
Chen Gang

Open, share, and attitude like air, water, and life which God blessed
#5 lore raw
In reply to Chen Gang S

RnJvbTogQ2hlbiBHYW5nIFMNCj4gLXN0YXRpYyBpbmxpbmUgaW50IGhjaV90ZXN0X2JpdChpbnQg bnIsIHZvaWQgKmFkZHIpDQo+ICtzdGF0aWMgaW5saW5lIGludCBoY2lfdGVzdF9iaXQoaW50IG5y LCBjb25zdCB2b2lkICphZGRyKQ0KPiAgew0KPiAgCXJldHVybiAqKChfX3UzMiAqKSBhZGRyICsg KG5yID4+IDUpKSAmICgoX191MzIpIDEgPDwgKG5yICYgMzEpKTsNCj4gIH0NCg0KSXMgdGhlcmUg YSAnc3RhbmRhcmQnIGZ1bmN0aW9uIGx1cmtpbmcgdGhhdCB3aWxsIGRvIHRoZSBhYm92ZS4NCk9u IHg4NiB0aGUgY3B1cyAnYml0IHRlc3QnIGluc3RydWN0aW9uIHdpbGwgaGFuZGxlIGJpdCBudW1i ZXJzDQpncmVhdGVyIHRoYW4gdGhlIHdvcmQgc2l6ZSAtIHNvIGl0IGNhbiBiZSBhIHNpbmdsZSBp bnN0cnVjdGlvbi4NCg0KCURhdmlkDQoNCg==

#4 lore raw
In reply to Chen Gang S

From: Gang S

Quoted 4 lines · Chen Gang S
Quoted 3 lines · Joe Perches
Quoted 2 lines · Chen Gang S
Quoted 1 line

hci_test_bit() does not modify 2nd parameter, so it is better to let =

it

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 2 lines

be constant, or may cause build warning. The related warning (with allmodconfig under xtensa):

[]

net/bluetooth/hci_sock.c

Quoted 1 line
net/bluetooth/hci_sock.c +0 -0

[]

net/bluetooth/hci_sock.c struct hci_pinfo { · line 52

Quoted 9 lines
net/bluetooth/hci_sock.c +1 -1
@@ -46,7 +46,7 @@ struct hci_pinfo {
 	unsigned short    channel;
 };
 
-static inline int hci_test_bit(int nr, void *addr)
+static inline int hci_test_bit(int nr, const void *addr)
 {
 	return *((__u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31));
 }

It's probably better to use const __u32 * here too, but the real thing I wonder is whether or not there's an issue with one of the 2 uses of this function.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

One of them passes a unsigned long *, the other a u32 *.

$ git grep -w hci_test_bit
net/bluetooth/hci_sock.c:static inline int hci_test_bit(int nr, void *=

addr)

Quoted 1 line
Quoted 1 line
Quoted 1 line

net/bluetooth/hci_sock.c: if (!hci_test_bit(flt_event, &flt->eve=

nt_mask))

Quoted 1 line
Quoted 1 line
Quoted 1 line

net/bluetooth/hci_sock.c: !hci_test_bit(ocf & HCI_F=

LT_OCF_BITS,

Quoted 1 line
Quoted 1 line
Quoted 1 line

net/bluetooth/hci_sock.c- &hci_sec_fi=

lter.ocf_mask[ogf])) &&

Quoted 10 lines
Quoted 10 lines
Quoted 6 lines

hci_sec_filter.ocf_mask is __u32
but flt->event_mask is unsigned long.

Any possible issue here on 64-bit systems?

For me, it can not cause issue on 64-bit systems. hci_test_bit() treats 'addr' as "__u32 *", and has to use the pointer to do something.

Quoted 7 lines

=20
'event_mask' is intended to type cast to "__u32 *" within 'hci_sock.c'. So for me, "const __u32 *" is better than "const void *" for 2nd parameter of hci_test_bit().
=20
If what I said above is correct, and also if necessary, I shall patch v3 for it.

How are the bits set in the first place?
If the array is ever indexed as long [] then the code above is unlikely to be testing the correct bits.

David

#6 lore raw
In reply to David Laight

Hello.

Quoted 9 lines · David Laight

net/bluetooth/hci_sock.c struct hci_pinfo { · line 52

Quoted 5 lines
net/bluetooth/hci_sock.c +1 -1
-static inline int hci_test_bit(int nr, void *addr)
+static inline int hci_test_bit(int nr, const void *addr)
  {
  	return *((__u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31));
  }

Is there a 'standard' function lurking that will do the above. On x86 the cpus 'bit test' instruction will handle bit numbers greater than the word size - so it can be a single instruction.

Of course, there's test_bit().

Quoted 1 line

David

WBR, Sergei

maintainer
#7 lore raw

Hi Sergei,

Quoted 11 lines
Quoted 9 lines

net/bluetooth/hci_sock.c struct hci_pinfo { · line 52

Quoted 5 lines
net/bluetooth/hci_sock.c +1 -1
-static inline int hci_test_bit(int nr, void *addr)
+static inline int hci_test_bit(int nr, const void *addr)
 {
 	return *((__u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31));
 }

Is there a 'standard' function lurking that will do the above. On x86 the cpus 'bit test' instruction will handle bit numbers greater than the word size - so it can be a single instruction.

Of course, there's test_bit().

we did leave hci_test_bit in the code since there are some userspace facing API that we can not change. Remember that the origin of this code is from 2.4.6 kernel.

So we can only change this if you can ensure not to break the userspace API. So might want to write unit tests to ensure working HCI filter before even considering touching this.

Regards

Marcel

#8 lore raw
Quoted 12 lines · Marcel Holtmann

Hi Sergei,

Quoted 10 lines
Quoted 9 lines

net/bluetooth/hci_sock.c struct hci_pinfo { · line 52

Quoted 5 lines
net/bluetooth/hci_sock.c +1 -1
-static inline int hci_test_bit(int nr, void *addr)
+static inline int hci_test_bit(int nr, const void *addr)
 {
 	return *((__u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31));
 }

Is there a 'standard' function lurking that will do the above. On x86 the cpus 'bit test' instruction will handle bit numbers greater than the word size - so it can be a single instruction.

Quoted 6 lines
Quoted 1 line

Of course, there's test_bit().

we did leave hci_test_bit in the code since there are some userspace facing API that we can not change. Remember that the origin of this code is from 2.4.6 kernel.

So we can only change this if you can ensure not to break the userspace API. So might want to write unit tests to ensure working HCI filter before even considering touching this.

For me, we have to remain hci_test_bit(), it is for "__u32 *" (which we can not change). The common test_bit() is for "unsigned long *", in this case, I guess it may cause issue under 64-bit environments.

Thanks.

Signature · 3 lines
Chen Gang

Open, share, and attitude like air, water, and life which God blessed
#9 lore raw

From: Chen Gang S [mailto:[email protected]]

Quoted 9 lines · Marcel Holtmann
Quoted 8 lines

Hi Sergei,

Quoted 6 lines
Quoted 5 lines
Quoted 5 lines
net/bluetooth/hci_sock.c +1 -1
-static inline int hci_test_bit(int nr, void *addr)
+static inline int hci_test_bit(int nr, const void *addr)
 {
 	return *((__u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31));
 }
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Is there a 'standard' function lurking that will do the above. On x86 the cpus 'bit test' instruction will handle bit numbers greater than the word size - so it can be a single instruction.

Of course, there's test_bit().

we did leave hci_test_bit in the code since there are some userspace fa=

cing

Quoted 4 lines
Quoted 4 lines

API that we can not change. Remember that the origin of this code is from 2.4.6 kernel.

So we can only change this if you can ensure not to break the userspace=

API.

Quoted 1 line
Quoted 1 line

So might want to write unit tests to ensure working HCI filter before e=

ven

Quoted 6 lines
Quoted 2 lines

considering touching this.

=20
For me, we have to remain hci_test_bit(), it is for "__u32 *" (which we can not change). The common test_bit() is for "unsigned long *", in this case, I guess it may cause issue under 64-bit environments.

Except that half the time you are passing a 'long *' and you haven't explained why this isn't broken on 64bit architectures.

Note that on LE systems the size of the accesses used to access a dense bit array don't matter. This is not true of BE systems.

David

#10 lore raw
Quoted 25 lines · David Laight

From: Chen Gang S [mailto:[email protected]]

Quoted 24 lines · Marcel Holtmann
Quoted 22 lines

Hi Sergei,

Quoted 11 lines
Quoted 9 lines

net/bluetooth/hci_sock.c struct hci_pinfo { · line 52

Quoted 5 lines
net/bluetooth/hci_sock.c +1 -1
-static inline int hci_test_bit(int nr, void *addr)
+static inline int hci_test_bit(int nr, const void *addr)
 {
 	return *((__u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31));
 }

Is there a 'standard' function lurking that will do the above. On x86 the cpus 'bit test' instruction will handle bit numbers greater than the word size - so it can be a single instruction.

Of course, there's test_bit().

we did leave hci_test_bit in the code since there are some userspace facing API that we can not change. Remember that the origin of this code is from 2.4.6 kernel.

So we can only change this if you can ensure not to break the userspace API. So might want to write unit tests to ensure working HCI filter before even considering touching this.

Quoted 7 lines
Quoted 3 lines

For me, we have to remain hci_test_bit(), it is for "__u32 *" (which we can not change). The common test_bit() is for "unsigned long *", in this case, I guess it may cause issue under 64-bit environments.

Except that half the time you are passing a 'long *' and you haven't explained why this isn't broken on 64bit architectures.

Maybe we are misunderstanding with each other (excuse me for my pool English). What I want to say is:

- hci_test_bit() is OK (current implementation can not cause issue for 64-bit machine).

- But if we use test_bit(), I guess it will cause issue for 64-bit machine.

Quoted 3 lines

Note that on LE systems the size of the accesses used to access a dense bit array don't matter. This is not true of BE systems.

Yes, what you said above sounds reasonable to me, too.

Thanks.

Signature · 3 lines
Chen Gang

Open, share, and attitude like air, water, and life which God blessed