Skip to contentSkip to conversation

Bluetooth: hci_core: Use ERR_PTR instead of NULL

PATCH In review Khalid Masum 4 participants 6 messages Jul 17, 2022 – Jul 23, 2022

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

PATCH 1/1 Bluetooth: hci_core: Use ERR_PTR instead of NULL

Failure of kzalloc to allocate memory is not reported. Return Error pointer to ENOMEM if memory allocation fails. This will increase readability and will make the function easier to use in future.

---
 drivers/bluetooth/btusb.c | 4 ++--
 net/bluetooth/hci_core.c  | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
drivers/bluetooth/btusb.c +2 -2
index e25fcd49db70..3407762b3b15 100644
@@ -3692,8 +3692,8 @@ static int btusb_probe(struct usb_interface *intf,
 	data->recv_acl = hci_recv_frame;
 
 	hdev = hci_alloc_dev_priv(priv_size);
-	if (!hdev)
-		return -ENOMEM;
+	if (IS_ERR(hdev))
+		return PTR_ERR(hdev);
 
 	hdev->bus = HCI_USB;
 	hci_set_drvdata(hdev, data);
net/bluetooth/hci_core.c +1 -1
index a0f99baafd35..ea50767e02bf 100644
@@ -2419,7 +2419,7 @@ struct hci_dev *hci_alloc_dev_priv(int sizeof_priv)
 
 	hdev = kzalloc(alloc_size, GFP_KERNEL);
 	if (!hdev)
-		return NULL;
+		return ERR_PTR(-ENOMEM);
 
 	hdev->pkt_type  = (HCI_DM1 | HCI_DH1 | HCI_HV1);
 	hdev->esco_type = (ESCO_HV1);
Signature · 1 line
2.36.1
#1 lore raw

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=660435

---Test result---

Test Summary:

CheckPatch                    PASS      1.44 seconds
GitLint                       PASS      0.84 seconds
SubjectPrefix                 PASS      0.65 seconds
BuildKernel                   PASS      31.55 seconds
BuildKernel32                 PASS      27.58 seconds
Incremental Build with patchesPASS      37.85 seconds
TestRunner: Setup             PASS      468.67 seconds
TestRunner: l2cap-tester      PASS      16.39 seconds
TestRunner: bnep-tester       PASS      5.54 seconds
TestRunner: mgmt-tester       PASS      94.94 seconds
TestRunner: rfcomm-tester     PASS      8.95 seconds
TestRunner: sco-tester        PASS      8.67 seconds
TestRunner: smp-tester        PASS      8.79 seconds
TestRunner: userchan-tester   PASS      5.78 seconds

---
Regards,
Linux Bluetooth

#2 lore raw
In reply to Khalid Masum

Hi Khalid,

Khalid Masum <[email protected]> says:

Quoted 6 lines

Failure of kzalloc to allocate memory is not reported. Return Error pointer to ENOMEM if memory allocation fails. This will increase readability and will make the function easier to use in future.

---

[snip]

net/bluetooth/hci_core.c struct hci_dev *hci_alloc_dev_priv(int sizeof_priv) · line 2423

Quoted 10 lines

index a0f99baafd35..ea50767e02bf 100644

net/bluetooth/hci_core.c +1 -1
@@ -2419,7 +2419,7 @@ struct hci_dev *hci_alloc_dev_priv(int sizeof_priv)
 
  	hdev = kzalloc(alloc_size, GFP_KERNEL);
  	if (!hdev)
-		return NULL;
+		return ERR_PTR(-ENOMEM);
 

This will break all callers of hci_alloc_dev(). All callers expect NULL in case of an error, so you will leave them with wrong pointer.

Also, allocation functionS return an error only in case of ENOMEM, so initial code is fine, IMO

Thanks,
--Pavel Skripkin

#3 lore raw
Quoted 18 lines · Pavel Skripkin

Hi Khalid,

Khalid Masum <[email protected]> says:

Quoted 6 lines

Failure of kzalloc to allocate memory is not reported. Return Error pointer to ENOMEM if memory allocation fails. This will increase readability and will make the function easier to use in future.

---

[snip]

Quoted 5 lines

index a0f99baafd35..ea50767e02bf 100644

net/bluetooth/hci_core.c +0 -0
@@ -2419,7 +2419,7 @@ struct hci_dev *hci_alloc_dev_priv(int sizeof_priv)
 
Quoted 8 lines

net/bluetooth/hci_core.c struct hci_dev *hci_alloc_dev_priv(int sizeof_priv) · line 2423

Quoted 5 lines
net/bluetooth/hci_core.c +1 -1
      hdev = kzalloc(alloc_size, GFP_KERNEL);
      if (!hdev)
-             return NULL;
+             return ERR_PTR(-ENOMEM);
 

This will break all callers of hci_alloc_dev(). All callers expect NULL in case of an error, so you will leave them with wrong pointer.

You are right. All callers of hci_alloc_dev() need to be able to handle the error pointer. I shall send a V2 with all the callers of hci_alloc_dev handling the ERR_PTR.

Quoted 3 lines

Also, allocation functionS return an error only in case of ENOMEM, so initial code is fine, IMO

I think it makes the memory allocation error handling look to be a bit different from what we usually do while allocating memory which is, returning an error or an error pointer. Here we are returning a NULL without any context, making it a bit unreadable. So I think returning an error pointer is better. If I am not mistaken, this also complies with the return convention:
https://www.kernel.org/doc/htmldocs/kernel-hacking/convention-returns.html

Quoted 3 lines

Thanks,
--Pavel Skripkin

Thanks,
-- Khalid Masum

#4 lore raw

Hi Khalid,

Quoted 29 lines · Khalid Masum
Quoted 18 lines · Pavel Skripkin

Hi Khalid,

Khalid Masum <[email protected]> says:

Quoted 6 lines

Failure of kzalloc to allocate memory is not reported. Return Error pointer to ENOMEM if memory allocation fails. This will increase readability and will make the function easier to use in future.

---

[snip]

Quoted 5 lines

index a0f99baafd35..ea50767e02bf 100644

net/bluetooth/hci_core.c +0 -0
@@ -2419,7 +2419,7 @@ struct hci_dev *hci_alloc_dev_priv(int sizeof_priv)
 
Quoted 8 lines

net/bluetooth/hci_core.c struct hci_dev *hci_alloc_dev_priv(int sizeof_priv) · line 2423

Quoted 5 lines
net/bluetooth/hci_core.c +1 -1
      hdev = kzalloc(alloc_size, GFP_KERNEL);
      if (!hdev)
-             return NULL;
+             return ERR_PTR(-ENOMEM);
 

This will break all callers of hci_alloc_dev(). All callers expect NULL in case of an error, so you will leave them with wrong pointer.

Quoted 7 lines

You are right. All callers of hci_alloc_dev() need to be able to handle the error pointer. I shall send a V2 with all the callers of hci_alloc_dev handling the ERR_PTR.

Quoted 3 lines

Also, allocation functionS return an error only in case of ENOMEM, so initial code is fine, IMO

If there just a single error like ENOMEM then Id say this is fine, just as it is fine for kzalloc.

Quoted 7 lines

I think it makes the memory allocation error handling look to be a bit different from what we usually do while allocating memory which is, returning an error or an error pointer. Here we are returning a NULL without any context, making it a bit unreadable. So I think returning an error pointer is better. If I am not mistaken, this also complies with the return convention:
https://www.kernel.org/doc/htmldocs/kernel-hacking/convention-returns.html

Not sure if that would apply to code that is basically a wrapper of kzalloc.

Quoted 7 lines
Quoted 3 lines

Thanks,
--Pavel Skripkin

Thanks,
-- Khalid Masum

Signature · 5 lines
Luiz Augusto von Dentz
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#5 lore raw
Quoted 52 lines · Luiz Augusto von Dentz

Hi Khalid,

Quoted 29 lines · Khalid Masum
Quoted 18 lines · Pavel Skripkin

Hi Khalid,

Khalid Masum <[email protected]> says:

Quoted 6 lines

Failure of kzalloc to allocate memory is not reported. Return Error pointer to ENOMEM if memory allocation fails. This will increase readability and will make the function easier to use in future.

---

[snip]

Quoted 5 lines

index a0f99baafd35..ea50767e02bf 100644

net/bluetooth/hci_core.c +0 -0
@@ -2419,7 +2419,7 @@ struct hci_dev *hci_alloc_dev_priv(int sizeof_priv)
 
Quoted 8 lines

net/bluetooth/hci_core.c struct hci_dev *hci_alloc_dev_priv(int sizeof_priv) · line 2423

Quoted 5 lines
net/bluetooth/hci_core.c +1 -1
      hdev = kzalloc(alloc_size, GFP_KERNEL);
      if (!hdev)
-             return NULL;
+             return ERR_PTR(-ENOMEM);
 

This will break all callers of hci_alloc_dev(). All callers expect NULL in case of an error, so you will leave them with wrong pointer.

Quoted 7 lines

You are right. All callers of hci_alloc_dev() need to be able to handle the error pointer. I shall send a V2 with all the callers of hci_alloc_dev handling the ERR_PTR.

Quoted 3 lines

Also, allocation functionS return an error only in case of ENOMEM, so initial code is fine, IMO

If there just a single error like ENOMEM then Id say this is fine, just as it is fine for kzalloc.

Quoted 7 lines

I think it makes the memory allocation error handling look to be a bit different from what we usually do while allocating memory which is, returning an error or an error pointer. Here we are returning a NULL without any context, making it a bit unreadable. So I think returning an error pointer is better. If I am not mistaken, this also complies with the return convention:
https://www.kernel.org/doc/htmldocs/kernel-hacking/convention-returns.html

Quoted 1 line

Not sure if that would apply to code that is basically a wrapper of kzalloc.

I got you.

Quoted 11 lines
Quoted 6 lines
Quoted 2 lines

Thanks,
--Pavel Skripkin

Thanks,
-- Khalid Masum

--
Luiz Augusto von Dentz

Thanks,
-- Khalid Masum