From patchwork Fri Sep 3 22:27:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Augusto von Dentz X-Patchwork-Id: 507080 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F3D6CC433F5 for ; Fri, 3 Sep 2021 22:27:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C084660C51 for ; Fri, 3 Sep 2021 22:27:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233168AbhICW2h (ORCPT ); Fri, 3 Sep 2021 18:28:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35856 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229875AbhICW2h (ORCPT ); Fri, 3 Sep 2021 18:28:37 -0400 Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AF262C061575 for ; Fri, 3 Sep 2021 15:27:36 -0700 (PDT) Received: by mail-pl1-x633.google.com with SMTP id n18so370955plp.7 for ; Fri, 03 Sep 2021 15:27:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=5IpJP9B7ZdVTX0IhB74qBI/8P/SQ3SVQ67PrRn8dkQE=; b=p10hlOr6qEjmH0FU7i/A4ISvelnl7CFXhq6omjySab7b5A5U8vI9cpI82kS7xrmEhH PF6E7PClUB2d1Np6dWh794fHz2f6Hwqxra3eIv8xz2RLdDb7PbGxsXol0qV8APOs0MBV 4JQDl0nID1GyoTpW2S9mcPSiaE9reelufJmSZVvowSjvUIMkKRMvXBiXsM7SaQKAOBng MXM2YrdyTQyiAVv4ET5Geb5KePAdUGVDHmEQ4XjMjO8u3WVh2i1q7BunXldMTLpe6fCC gb6qTsjOMMW387Na0P60Nv+EBV2xcMfGC0/p7F8fJVWUzkVslQbANJ0D5lRWY7rDuDxa JPHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=5IpJP9B7ZdVTX0IhB74qBI/8P/SQ3SVQ67PrRn8dkQE=; b=VvC5eP6+3NutzI+TEO99LkX2oICwihyX9Gz9vfPcXnOqveTSfyaGyyX+XDlSLyMr46 Fc3CntCJL0ZXIXZSDX8Cju1bVqNIAFEAYm26g1Qx7GIPtqkfbXJ2uTC6HY+UFmbAtWvV Kh3hMwjiXFHzEYhTC3wzUvDp2rKQBDoEKismUG6vXleTVAHKTkUgdDN/gN2fglJFAUBP WllCfvO7OGZJopSrJWiL8kNWZGZWwegTNyDlxje3s850unCwHztDFqHdnr9zfNbdruk9 sscuOtenp4diyYcW4J4A9aWS2owQ9W7CGRGDeLrnt4tvSptRJWg7wGvKeK3Fy1zeI5Jh w2Wg== X-Gm-Message-State: AOAM530aqQU98zBe/0SuTz8CSga3qGxSw6qAQ7OqmXt6PrU5e+9XvZiP 1qqMTPXhWywyuhWk1w34fKTPgVBYXuk= X-Google-Smtp-Source: ABdhPJzB9hC4t+bglI5xsSFnqyz1b3nOSV3bsgBHl3hmyJiI0RSGMdcHbo+uIjFPcjTZBx9rxXpSNQ== X-Received: by 2002:a17:90b:e0d:: with SMTP id ge13mr1064579pjb.53.1630708055597; Fri, 03 Sep 2021 15:27:35 -0700 (PDT) Received: from localhost.localdomain (c-71-56-157-77.hsd1.or.comcast.net. [71.56.157.77]) by smtp.gmail.com with ESMTPSA id x4sm225796pjq.20.2021.09.03.15.27.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Sep 2021 15:27:35 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH v3 1/4] Bluetooth: Add bt_skb_sendmsg helper Date: Fri, 3 Sep 2021 15:27:29 -0700 Message-Id: <20210903222732.1472560-1-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Luiz Augusto von Dentz 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 --- 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); From patchwork Fri Sep 3 22:27:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Augusto von Dentz X-Patchwork-Id: 506634 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 650E1C433EF for ; Fri, 3 Sep 2021 22:27:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3906660C51 for ; Fri, 3 Sep 2021 22:27:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233620AbhICW2h (ORCPT ); Fri, 3 Sep 2021 18:28:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233033AbhICW2h (ORCPT ); Fri, 3 Sep 2021 18:28:37 -0400 Received: from mail-pg1-x529.google.com (mail-pg1-x529.google.com [IPv6:2607:f8b0:4864:20::529]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 26A69C061575 for ; Fri, 3 Sep 2021 15:27:37 -0700 (PDT) Received: by mail-pg1-x529.google.com with SMTP id c17so487378pgc.0 for ; Fri, 03 Sep 2021 15:27:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=mljDYYlLDcj/0CkTyYH9Brnr1HPRQ48Nwhm/HTTbv9s=; b=UoFPSQVSt5FfCbDjJxMQXcwsfN0aZbp++dQcOOp9PcRNmoMm/CBVH+h8kBiNh0IiBA VmLt88993ZWffJdM+QnpmqER1oS+gqaCyGJYjn+sdFACIkNHZSJwnaLuVCVBHG71tbEu ThabQ7HzrxP/2cC8NVx7AsDeblTbp4IlJN34LzUEmMNrB1SCjptGYOIGWHQRntFNe6UK bcAl9vYn0coxikEBiY3Bg9PnRXnOzbgQYrCK+aa+FH6JOdfgPmhRitZjJTCbWdtArnij DSG7wF9yvdIzU+RI8M45omKnWM44rHaMTCZQdFC8S/u0jBgOp6OJDq5jKwalb6RQ+H3c KPGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=mljDYYlLDcj/0CkTyYH9Brnr1HPRQ48Nwhm/HTTbv9s=; b=JhrvUmwTyLy/WQdplnWJyiWHvvTDHUN/cJTCZjRtH1lW1AP4KcoMtFOm670C6Bmgyl MNFtuNmsb1yF82MO7gFQU7l0lAU01A67kFVfHo86i9QhBibkx0Xdh1KUWd/7/wRlm5za PU20fNE4TqGicLYhtAz/tf2heQ6cAe0WT7nwMxigfkO3f5OU2y3qjAArxwTW9Am9I+y0 /ocur+ge90C3+6ZdsHO8YBKPNDw/v40evdAQLF7CSGHBavX9griB70LYrLz7/BcSyUgD qTHscZjsVodxngBp3uHQNN5239AjTHjCKSYJ3L7uyLj5WeUB5O9qnYXVTcrHta600iJl XYSQ== X-Gm-Message-State: AOAM531zwE39K4D5QW644QJX3J4wvfFdiZX60+GNYZ27zdqKz8wIqJ14 FjrZ+ySZGMjOOZ4ln7YdXjib34L3fa0= X-Google-Smtp-Source: ABdhPJwnzRsO6gY1qtDMVRA0x6ma3WbMXnIJh6SrXqgsk5dIp7PqTZ7aRv1ouvJFtCKXnFQTyMzyRw== X-Received: by 2002:a63:a55a:: with SMTP id r26mr1034150pgu.13.1630708056180; Fri, 03 Sep 2021 15:27:36 -0700 (PDT) Received: from localhost.localdomain (c-71-56-157-77.hsd1.or.comcast.net. [71.56.157.77]) by smtp.gmail.com with ESMTPSA id x4sm225796pjq.20.2021.09.03.15.27.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Sep 2021 15:27:35 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH v3 2/4] Bluetooth: Add bt_skb_sendmmsg helper Date: Fri, 3 Sep 2021 15:27:30 -0700 Message-Id: <20210903222732.1472560-2-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210903222732.1472560-1-luiz.dentz@gmail.com> References: <20210903222732.1472560-1-luiz.dentz@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Luiz Augusto von Dentz This works similarly to bt_skb_sendmsg but can split the msg into multiple skb fragments which is useful for stream sockets. Signed-off-by: Luiz Augusto von Dentz --- include/net/bluetooth/bluetooth.h | 38 +++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/include/net/bluetooth/bluetooth.h b/include/net/bluetooth/bluetooth.h index aa6f8fe897ce..82a83b3b77b8 100644 --- a/include/net/bluetooth/bluetooth.h +++ b/include/net/bluetooth/bluetooth.h @@ -448,6 +448,44 @@ static inline struct sk_buff *bt_skb_sendmsg(struct sock *sk, return skb; } +/* Similar to bt_skb_sendmsg but can split the msg into multiple fragments + * accourding to the MTU. + */ +static inline struct sk_buff *bt_skb_sendmmsg(struct sock *sk, + struct msghdr *msg, + size_t len, size_t mtu, + size_t headroom, size_t tailroom) +{ + struct sk_buff *skb, **frag; + + skb = bt_skb_sendmsg(sk, msg, len, mtu, headroom, tailroom); + if (IS_ERR_OR_NULL(skb)) + return skb; + + len -= skb->len; + if (!len) + return skb; + + /* Add remaining data over MTU as continuation fragments */ + frag = &skb_shinfo(skb)->frag_list; + while (len) { + struct sk_buff *tmp; + + tmp = bt_skb_sendmsg(sk, msg, len, mtu, headroom, tailroom); + if (IS_ERR_OR_NULL(tmp)) { + kfree_skb(skb); + return tmp; + } + + len -= tmp->len; + + *frag = tmp; + frag = &(*frag)->next; + } + + return skb; +} + int bt_to_errno(u16 code); void hci_sock_set_flag(struct sock *sk, int nr); From patchwork Fri Sep 3 22:27:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Augusto von Dentz X-Patchwork-Id: 507079 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E70C7C4332F for ; Fri, 3 Sep 2021 22:27:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C9D73610A1 for ; Fri, 3 Sep 2021 22:27:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234285AbhICW2i (ORCPT ); Fri, 3 Sep 2021 18:28:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233033AbhICW2i (ORCPT ); Fri, 3 Sep 2021 18:28:38 -0400 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AA6B1C061575 for ; Fri, 3 Sep 2021 15:27:37 -0700 (PDT) Received: by mail-pj1-x1030.google.com with SMTP id mj9-20020a17090b368900b001965618d019so458597pjb.4 for ; Fri, 03 Sep 2021 15:27:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=bUG5RFoU/MlbNuIj3tMKfHg8hf0P6016JQmHbmqnbzw=; b=nCIHcKI1aUHii/T75cabONlKCoCS8f2CKkSsdyOUfH5KiIcc/d6apTnqS3MzJ7+xJb rm6KqOHLz5L+sV1IFdyfSwSKN1GyRgWX6R6RKz+GuNqRFgW/O8GTuMC3UjYDgwB6vKgM X4uGq8bu536VUuERsKRwJ2xsZn0KvxdLddVFz4F9RfSoKwfZgjFrrQpAL9bmhH7O0DLL NEvYyMkIGV5ZDxkGN9pMDUZ0zk7qyeOpZLRM0DoAZrkiMn3s13RdsI8u/DpZlpIDh4Bf hbvT7gjccLCrmLPBTjHLbvcNhbmwRraO9TqXrAstJFV1s/G+T2sVCoNfNZHfGlU9MTUv 2lOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=bUG5RFoU/MlbNuIj3tMKfHg8hf0P6016JQmHbmqnbzw=; b=tYeFaQhEsNZvck8yMYSUCmf/jnzl5ayMBMmnFi0B2l0dPQMi0c36YxPAnr8RUop7/L Df71l15kqN9OvUROTz/JriT4bKellxsT27ty+q5k8LUKhWsSC/Un/4H0TtoqZuRk+WsZ kUlzdxHHcZbLnfwaUfb+z5N8NS2mmoeiXL5CKY7bLMYCDR5wVzgmm5HUcu4JdjRVHDxJ OPumv2WVnQ2IhxTjTtKnZfRDJQ+fWAfvlr9xGPdZQoNdadDJRlEboKhesPfMFT63DH5k RNIGOtJZMI20PP4sMonk/V5gITLUkF4VapkvWNO9L5Czg0XOo3xLYGICgFKT+ZoaeWj3 piwA== X-Gm-Message-State: AOAM5309mKALSHrVJBf+Vw2p8gUwh3qdJNdNdYgaYJXr/UAgSacaIyzo 97WOE9tbKO/n5ev7MvXzGWOp6PEvc/0= X-Google-Smtp-Source: ABdhPJxCrl3vBIaK7HmyVY6qkxZb0gU0GltTOH+Etx0fgtPR4DQyBaYteyuriPv8duEomHRuBsjDaQ== X-Received: by 2002:a17:902:e0c3:b0:134:fde4:63c2 with SMTP id e3-20020a170902e0c300b00134fde463c2mr913440pla.43.1630708056960; Fri, 03 Sep 2021 15:27:36 -0700 (PDT) Received: from localhost.localdomain (c-71-56-157-77.hsd1.or.comcast.net. [71.56.157.77]) by smtp.gmail.com with ESMTPSA id x4sm225796pjq.20.2021.09.03.15.27.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Sep 2021 15:27:36 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH v3 3/4] Bluetooth: SCO: Replace use of memcpy_from_msg with bt_skb_sendmsg Date: Fri, 3 Sep 2021 15:27:31 -0700 Message-Id: <20210903222732.1472560-3-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210903222732.1472560-1-luiz.dentz@gmail.com> References: <20210903222732.1472560-1-luiz.dentz@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Luiz Augusto von Dentz This makes use of bt_skb_sendmsg instead of allocating a different buffer to be used with memcpy_from_msg which cause one extra copy. Signed-off-by: Luiz Augusto von Dentz --- net/bluetooth/sco.c | 34 +++++++++++----------------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/net/bluetooth/sco.c b/net/bluetooth/sco.c index b62c91c627e2..901097c1f484 100644 --- a/net/bluetooth/sco.c +++ b/net/bluetooth/sco.c @@ -280,27 +280,19 @@ static int sco_connect(struct hci_dev *hdev, struct sock *sk) return err; } -static int sco_send_frame(struct sock *sk, void *buf, int len, - unsigned int msg_flags) +static int sco_send_frame(struct sock *sk, struct sk_buff *skb) { struct sco_conn *conn = sco_pi(sk)->conn; - struct sk_buff *skb; - int err; /* Check outgoing MTU */ - if (len > conn->mtu) + if (skb->len > conn->mtu) return -EINVAL; - BT_DBG("sk %p len %d", sk, len); + BT_DBG("sk %p len %d", sk, skb->len); - skb = bt_skb_send_alloc(sk, len, msg_flags & MSG_DONTWAIT, &err); - if (!skb) - return err; - - memcpy(skb_put(skb, len), buf, len); hci_send_sco(conn->hcon, skb); - return len; + return skb->len; } static void sco_recv_frame(struct sco_conn *conn, struct sk_buff *skb) @@ -722,7 +714,7 @@ static int sco_sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) { struct sock *sk = sock->sk; - void *buf; + struct sk_buff *skb; int err; BT_DBG("sock %p, sk %p", sock, sk); @@ -734,24 +726,20 @@ static int sco_sock_sendmsg(struct socket *sock, struct msghdr *msg, if (msg->msg_flags & MSG_OOB) return -EOPNOTSUPP; - buf = kmalloc(len, GFP_KERNEL); - if (!buf) - return -ENOMEM; - - if (memcpy_from_msg(buf, msg, len)) { - kfree(buf); - return -EFAULT; - } + skb = bt_skb_sendmsg(sk, msg, len, len, 0, 0); + if (IS_ERR_OR_NULL(skb)) + return PTR_ERR(skb); lock_sock(sk); if (sk->sk_state == BT_CONNECTED) - err = sco_send_frame(sk, buf, len, msg->msg_flags); + err = sco_send_frame(sk, skb); else err = -ENOTCONN; release_sock(sk); - kfree(buf); + if (err) + kfree_skb(skb); return err; } From patchwork Fri Sep 3 22:27:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Augusto von Dentz X-Patchwork-Id: 506633 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 31F25C433EF for ; Fri, 3 Sep 2021 22:27:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 12DF260C51 for ; Fri, 3 Sep 2021 22:27:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234673AbhICW2j (ORCPT ); Fri, 3 Sep 2021 18:28:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35870 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233033AbhICW2j (ORCPT ); Fri, 3 Sep 2021 18:28:39 -0400 Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B922AC061575 for ; Fri, 3 Sep 2021 15:27:38 -0700 (PDT) Received: by mail-pj1-x1029.google.com with SMTP id n13-20020a17090a4e0d00b0017946980d8dso455699pjh.5 for ; Fri, 03 Sep 2021 15:27:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=cLcSl8nMUeEmOer03hxjSfU3oPUHGSQDxIvzfp/OHI0=; b=jc59hr3Z8Eif/3ubpcV+GLSd+Vi/XpmV7vL/8X7XFil8tzQIGnVZWFj3BeybSakOsF QtyYUYSwRCfTSYyuV/61oC+9Q41PoSL1xQL+XaDNNJe89B89ycDr81QMkYlzI997r+Ri dwPhaV0vGA27xbdYDUi42XN+LrOhYSzZp2dDitCFaS6Zdnfad22Na0OZMTFtIthpneaa 8RLp0eI5pqknfUEd/0apMfajtjSFyqesN+jitgGkayRsHwkz+/lxEVWWiJ2i3U8VfaVk 3Xjwn/atYvur8MvM16epSkCzggGQvrtBZ6J12+XDfVtA/m/4EUtGd3J0qeui+OzsCVx3 ZtUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=cLcSl8nMUeEmOer03hxjSfU3oPUHGSQDxIvzfp/OHI0=; b=JbfCvyZthpW00E+97l8qWAAdUQbTsyiv5gMawzOEGAGtKeO5ae0lF/13jcN7EiHecU xND4LmikJ2Gk2CE6RSl5V+nt2q4bCI0XW5EboTYmkeBTd6lcKEFu8HdjpzvU5ccATNGX jE8fwdYgeuYZ+lgNYXGdWH1S+FW/VDG+zbIL4yZ5VEOsuFvpeH0+Gd548DOsVEJ6uvyV Aud7wMcC/D9cs90Dga5+jl6VE+5yOnmrcDcTUNVGys++opi/w5pC+Px642/yQe5QH+1B km8AWX4o2f2FK3EvZeX/3fGN49eOLNvdtAKiv5zozBtteEUA6H1GhWq87lF9wnmKJGYF VW7A== X-Gm-Message-State: AOAM533ZUg1kYMo3SZWR8ExyQSCgP/prEwcmXVg4PUJIlel3+7yLO9gA 0x7v91JX4iIAKig7n7YYYGjZj2RX3jE= X-Google-Smtp-Source: ABdhPJxHk3cT1Aj9Z7b7dTbQkr/0UH7CkQJTdNHl3IyWBVcWM6y+iye1Eq64N56/raB1nCqsxj45nQ== X-Received: by 2002:a17:90a:ce8e:: with SMTP id g14mr1108561pju.241.1630708058019; Fri, 03 Sep 2021 15:27:38 -0700 (PDT) Received: from localhost.localdomain (c-71-56-157-77.hsd1.or.comcast.net. [71.56.157.77]) by smtp.gmail.com with ESMTPSA id x4sm225796pjq.20.2021.09.03.15.27.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Sep 2021 15:27:37 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH v3 4/4] Bluetooth: RFCOMM: Replace use of memcpy_from_msg with bt_skb_sendmmsg Date: Fri, 3 Sep 2021 15:27:32 -0700 Message-Id: <20210903222732.1472560-4-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210903222732.1472560-1-luiz.dentz@gmail.com> References: <20210903222732.1472560-1-luiz.dentz@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Luiz Augusto von Dentz This makes use of bt_skb_sendmmsg instead using memcpy_from_msg which is not considered safe to be used when lock_sock is held. Also make rfcomm_dlc_send handle skb with fragments and queue them all atomically. Signed-off-by: Luiz Augusto von Dentz --- net/bluetooth/rfcomm/core.c | 50 +++++++++++++++++++++++++++++++------ net/bluetooth/rfcomm/sock.c | 46 ++++++++-------------------------- 2 files changed, 53 insertions(+), 43 deletions(-) diff --git a/net/bluetooth/rfcomm/core.c b/net/bluetooth/rfcomm/core.c index f2bacb464ccf..7324764384b6 100644 --- a/net/bluetooth/rfcomm/core.c +++ b/net/bluetooth/rfcomm/core.c @@ -549,22 +549,58 @@ struct rfcomm_dlc *rfcomm_dlc_exists(bdaddr_t *src, bdaddr_t *dst, u8 channel) return dlc; } +static int rfcomm_dlc_send_frag(struct rfcomm_dlc *d, struct sk_buff *frag) +{ + int len = frag->len; + + BT_DBG("dlc %p mtu %d len %d", d, d->mtu, len); + + if (len > d->mtu) + return -EINVAL; + + rfcomm_make_uih(frag, d->addr); + __skb_queue_tail(&d->tx_queue, frag); + + return len; +} + int rfcomm_dlc_send(struct rfcomm_dlc *d, struct sk_buff *skb) { - int len = skb->len; + unsigned long flags; + struct sk_buff *frag, *next; + int len; if (d->state != BT_CONNECTED) return -ENOTCONN; - BT_DBG("dlc %p mtu %d len %d", d, d->mtu, len); + frag = skb_shinfo(skb)->frag_list; + skb_shinfo(skb)->frag_list = NULL; - if (len > d->mtu) - return -EINVAL; + /* Queue all fragments atomically. */ + spin_lock_irqsave(&d->tx_queue.lock, flags); - rfcomm_make_uih(skb, d->addr); - skb_queue_tail(&d->tx_queue, skb); + len = rfcomm_dlc_send_frag(d, skb); + if (len < 0 || !frag) + goto unlock; + + for (; frag; frag = next) { + int ret; + + next = frag->next; + + ret = rfcomm_dlc_send_frag(d, frag); + if (ret < 0) { + kfree_skb(frag); + goto unlock; + } + + len += ret; + } + +unlock: + spin_unlock_irqrestore(&d->tx_queue.lock, flags); - if (!test_bit(RFCOMM_TX_THROTTLED, &d->flags)) + if (len > 0 && !test_bit(RFCOMM_TX_THROTTLED, &d->flags)) rfcomm_schedule(); return len; } diff --git a/net/bluetooth/rfcomm/sock.c b/net/bluetooth/rfcomm/sock.c index 2c95bb58f901..5938af3e9936 100644 --- a/net/bluetooth/rfcomm/sock.c +++ b/net/bluetooth/rfcomm/sock.c @@ -575,46 +575,20 @@ static int rfcomm_sock_sendmsg(struct socket *sock, struct msghdr *msg, lock_sock(sk); sent = bt_sock_wait_ready(sk, msg->msg_flags); - if (sent) - goto done; - - while (len) { - size_t size = min_t(size_t, len, d->mtu); - int err; - - skb = sock_alloc_send_skb(sk, size + RFCOMM_SKB_RESERVE, - msg->msg_flags & MSG_DONTWAIT, &err); - if (!skb) { - if (sent == 0) - sent = err; - break; - } - skb_reserve(skb, RFCOMM_SKB_HEAD_RESERVE); - - err = memcpy_from_msg(skb_put(skb, size), msg, size); - if (err) { - kfree_skb(skb); - if (sent == 0) - sent = err; - break; - } - skb->priority = sk->sk_priority; + release_sock(sk); - err = rfcomm_dlc_send(d, skb); - if (err < 0) { - kfree_skb(skb); - if (sent == 0) - sent = err; - break; - } + if (sent) + return sent; - sent += size; - len -= size; - } + skb = bt_skb_sendmmsg(sk, msg, len, d->mtu, RFCOMM_SKB_HEAD_RESERVE, + RFCOMM_SKB_TAIL_RESERVE); + if (IS_ERR_OR_NULL(skb)) + return PTR_ERR(skb); -done: - release_sock(sk); + sent = rfcomm_dlc_send(d, skb); + if (sent < 0) + kfree_skb(skb); return sent; }