diff mbox series

[v3,1/4] Bluetooth: Add bt_skb_sendmsg helper

Message ID 20210903222732.1472560-1-luiz.dentz@gmail.com
State New
Headers show
Series [v3,1/4] Bluetooth: Add bt_skb_sendmsg helper | expand

Commit Message

Luiz Augusto von Dentz Sept. 3, 2021, 10:27 p.m. UTC
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

bt_skb_sendmsg helps takes care of allocation the skb and copying the
the contents of msg over to the skb while checking for possible errors
so it should be safe to call it without holding lock_sock.

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
---
 include/net/bluetooth/bluetooth.h | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

Comments

Luiz Augusto von Dentz Sept. 8, 2021, 5:13 p.m. UTC | #1
Hi Marcel,

On Fri, Sep 3, 2021 at 3:27 PM Luiz Augusto von Dentz
<luiz.dentz@gmail.com> wrote:
>

> From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

>

> bt_skb_sendmsg helps takes care of allocation the skb and copying the

> the contents of msg over to the skb while checking for possible errors

> so it should be safe to call it without holding lock_sock.

>

> Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

> ---

>  include/net/bluetooth/bluetooth.h | 28 ++++++++++++++++++++++++++++

>  1 file changed, 28 insertions(+)

>

> diff --git a/include/net/bluetooth/bluetooth.h b/include/net/bluetooth/bluetooth.h

> index 9125effbf448..aa6f8fe897ce 100644

> --- a/include/net/bluetooth/bluetooth.h

> +++ b/include/net/bluetooth/bluetooth.h

> @@ -420,6 +420,34 @@ static inline struct sk_buff *bt_skb_send_alloc(struct sock *sk,

>         return NULL;

>  }

>

> +/* Shall not be called with lock_sock held */

> +static inline struct sk_buff *bt_skb_sendmsg(struct sock *sk,

> +                                            struct msghdr *msg,

> +                                            size_t len, size_t mtu,

> +                                            size_t headroom, size_t tailroom)

> +{

> +       struct sk_buff *skb;

> +       size_t size = min_t(size_t, len, mtu);

> +       int err;

> +

> +       skb = bt_skb_send_alloc(sk, size + headroom + tailroom,

> +                               msg->msg_flags & MSG_DONTWAIT, &err);

> +       if (!skb)

> +               return ERR_PTR(err);

> +

> +       skb_reserve(skb, headroom);

> +       skb_tailroom_reserve(skb, mtu, tailroom);

> +

> +       if (!copy_from_iter_full(skb_put(skb, size), size, &msg->msg_iter)) {

> +               kfree_skb(skb);

> +               return ERR_PTR(-EFAULT);

> +       }

> +

> +       skb->priority = sk->sk_priority;

> +

> +       return skb;

> +}

> +

>  int bt_to_errno(u16 code);

>

>  void hci_sock_set_flag(struct sock *sk, int nr);

> --

> 2.31.1


Any comments on this set? I did incorporate the skb_tailroom_reserver as well.


-- 
Luiz Augusto von Dentz
Marcel Holtmann Sept. 13, 2021, 8:14 p.m. UTC | #2
Hi Luiz,

> bt_skb_sendmsg helps takes care of allocation the skb and copying the

> the contents of msg over to the skb while checking for possible errors

> so it should be safe to call it without holding lock_sock.

> 

> Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

> ---

> include/net/bluetooth/bluetooth.h | 28 ++++++++++++++++++++++++++++

> 1 file changed, 28 insertions(+)


all 4 patches have been applied to bluetooth-next tree.

Regards

Marcel
diff mbox series

Patch

diff --git a/include/net/bluetooth/bluetooth.h b/include/net/bluetooth/bluetooth.h
index 9125effbf448..aa6f8fe897ce 100644
--- a/include/net/bluetooth/bluetooth.h
+++ b/include/net/bluetooth/bluetooth.h
@@ -420,6 +420,34 @@  static inline struct sk_buff *bt_skb_send_alloc(struct sock *sk,
 	return NULL;
 }
 
+/* Shall not be called with lock_sock held */
+static inline struct sk_buff *bt_skb_sendmsg(struct sock *sk,
+					     struct msghdr *msg,
+					     size_t len, size_t mtu,
+					     size_t headroom, size_t tailroom)
+{
+	struct sk_buff *skb;
+	size_t size = min_t(size_t, len, mtu);
+	int err;
+
+	skb = bt_skb_send_alloc(sk, size + headroom + tailroom,
+				msg->msg_flags & MSG_DONTWAIT, &err);
+	if (!skb)
+		return ERR_PTR(err);
+
+	skb_reserve(skb, headroom);
+	skb_tailroom_reserve(skb, mtu, tailroom);
+
+	if (!copy_from_iter_full(skb_put(skb, size), size, &msg->msg_iter)) {
+		kfree_skb(skb);
+		return ERR_PTR(-EFAULT);
+	}
+
+	skb->priority = sk->sk_priority;
+
+	return skb;
+}
+
 int bt_to_errno(u16 code);
 
 void hci_sock_set_flag(struct sock *sk, int nr);