From patchwork Wed Sep 1 00:26:18 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: 505145 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 CDF45C4320A for ; Wed, 1 Sep 2021 00:26:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AD45B60F6B for ; Wed, 1 Sep 2021 00:26:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241442AbhIAA1U (ORCPT ); Tue, 31 Aug 2021 20:27:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33226 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231249AbhIAA1T (ORCPT ); Tue, 31 Aug 2021 20:27:19 -0400 Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 10B6DC061760 for ; Tue, 31 Aug 2021 17:26:24 -0700 (PDT) Received: by mail-pf1-x434.google.com with SMTP id x16so700892pfh.2 for ; Tue, 31 Aug 2021 17:26:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=oV91TlQ/uyvI2F8HVAFusfi8G+Q5pnpGFBPBrBkciLA=; b=mZJY/ocaN6AjdA65d6pe50IWkT7WSpv6+//ptgBwH1ImMCTUwQI7rkrsNs/qcPHN03 /1GjAUv5HR3+QHWJgycHtW7Fu8AFYfs040FpFE/9U5ieoEySafctP2e4c8YSQP6cajZz z0cd6+40fmIhJrC/RCLTWxsISRx509G/JZQDGbywMFUP9aDM0hisrBQva0xzU3RXAXOi RWdr2a2KkpZOJicFdVK5BupulPkvADDmrmfUSMjAwM3BBwMc7GLAy4L+9hXnka7vLREg 0bxYGQdWdbGR30xM2oAJWZELy7R7r1d67CDNRHLQR1piYqfSaPbTlRbGf7q4kPXo7ycl SaqA== 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=oV91TlQ/uyvI2F8HVAFusfi8G+Q5pnpGFBPBrBkciLA=; b=mxMNQQs+FgklpXi+l6ZzTmENpxgty3dFy9ZkZOhD/W/+GRdoZkKhvanjPYVPWnNKQI Sm6GvpSuDHniQVJigjKta3FvRoQinnASkh9TrcKV4Ex58ctg86nGz3hvNoGtrxb//YqH +lkgkNbnbFNb590AOslfzNxdhbWP02U0nwwNLvAjw51kA+BeeLkZNdzY213ZItF3s6Dp 11OYANPKWmJbCQDPwyXETjBaSPGqxAjzI5i+AuxpuCURWUeiqszHO4o+TUancebM0w+a 0ObjXHmekXPomp+zlQvF4J86vIj1HZFLi5Cgn1+x0MWflriFxgWHOI5MjqNh8XlcUmLT 5bmQ== X-Gm-Message-State: AOAM531Fziu39YcLXiZD+udd7BEL7aDy0pP4ggAnuKkCgXLzp8VPsY9O 2NLY3p43qiPUvGtb/+kooOamyhaZYeQ= X-Google-Smtp-Source: ABdhPJyRRAAM2nPMR+M03bYmQ/1vLnWW5vUPK1BgP6v8pgnoQycoSKentTK/CZfvPrcl4RqioQHDjA== X-Received: by 2002:a62:cf86:0:b029:3e0:7cef:9e03 with SMTP id b128-20020a62cf860000b02903e07cef9e03mr31566411pfg.0.1630455982143; Tue, 31 Aug 2021 17:26:22 -0700 (PDT) Received: from lvondent-mobl4.intel.com (c-71-56-157-77.hsd1.or.comcast.net. [71.56.157.77]) by smtp.gmail.com with ESMTPSA id gm5sm4054655pjb.32.2021.08.31.17.26.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Aug 2021 17:26:21 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH 1/4] Bluetooth: Add bt_skb_sendmsg helper Date: Tue, 31 Aug 2021 17:26:18 -0700 Message-Id: <20210901002621.414016-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 | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/include/net/bluetooth/bluetooth.h b/include/net/bluetooth/bluetooth.h index 9125effbf448..f858efcf9f40 100644 --- a/include/net/bluetooth/bluetooth.h +++ b/include/net/bluetooth/bluetooth.h @@ -420,6 +420,32 @@ 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 header, + size_t footer) +{ + struct sk_buff *skb; + int err; + + skb = bt_skb_send_alloc(sk, len + header + footer, + msg->msg_flags & MSG_DONTWAIT, &err); + if (!skb) + return ERR_PTR(err); + + skb_reserve(skb, header); + + if (memcpy_from_msg(skb_put(skb, len), msg, len)) { + 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 Wed Sep 1 00:26:19 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: 505828 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 5FFFDC432BE for ; Wed, 1 Sep 2021 00:26:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 388CC6102A for ; Wed, 1 Sep 2021 00:26:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241441AbhIAA1U (ORCPT ); Tue, 31 Aug 2021 20:27:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230333AbhIAA1T (ORCPT ); Tue, 31 Aug 2021 20:27:19 -0400 Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8F077C061575 for ; Tue, 31 Aug 2021 17:26:23 -0700 (PDT) Received: by mail-pg1-x52b.google.com with SMTP id k24so963961pgh.8 for ; Tue, 31 Aug 2021 17:26:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=aCRHB/ssiwjKRr5Y83BliYp7RmTBRWH6WSIPulVy6og=; b=guPz0kEPpXMb22FucYINhDnnZWTX7ruXm+jkAdxoBxnSxzHhqTUCGWZfoikyOnsbDJ xlKABaIQHuRVl3Jf1TmJyWzUwHIXAUdjxoeOEZrX30sRiONUP1EqypOgrGREtB0rFT5U ftXPOOVP5mHEpoif5It2n/QNUuy6QK03hRfeLQIYDAlXcFmzLnzWg4uV70lFh8vO+7q7 09qTfS3m+j1Sx+96pjlij+iZhN73g5lQ249jhS+zXgwIEmCuP05pxeHcQTXsGnsFKCF4 CrCtUkr2a+MNNbBUW8mRzPxBUofsk1qgufdPf5RHKadKiAUBSRD/kmgGUlt9U/f3enGE UyZg== 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=aCRHB/ssiwjKRr5Y83BliYp7RmTBRWH6WSIPulVy6og=; b=P+RvQyGkrKOUOhOcnUJyhvdqoBMPxDYP+ddusPMNJvbkeWF2LN7usdBwcauxlT3+eb /FwoILMP10orb3WH7PZ9VoHPSNZ4mVEfnkIi9MTrpN+/B+wUYpRYaj5vMRPDx7OG63KJ dDL0j2nzGfvQneRoTysHNtw0tTsxsT7qjwQoDdWr6K+pjlvQO0lpuzyFqy2AlhqqGQEY FwXt3dk5fS6aj0RPvyde0C6qQcJoeGKf8R2uim3jjwEDPnb/n3GF6pAcvIuxgNVGJqsm pT2WOBo/zPrkswOfLKsIaqFmV6ArzCs69cR4ADGel2ctOwClgOKAhnVZAND8NV+K7rF9 0X2A== X-Gm-Message-State: AOAM533tgnOQRzsiyGaQv4NJxB0L5JTryFX+2LLEknjkuEHSebFNJnvK 81NpjnLRxW3EhZhHf2CGkBJMk1WgwVs= X-Google-Smtp-Source: ABdhPJxrDDFlR9Y4cvNeK0kvZRWsshSiFPfwUXx5o0JaPIuZqq0JSpMrmw53tfrS+phb21z7NZuX0A== X-Received: by 2002:a62:d104:0:b0:3ee:46b:d4ed with SMTP id z4-20020a62d104000000b003ee046bd4edmr30595255pfg.38.1630455982813; Tue, 31 Aug 2021 17:26:22 -0700 (PDT) Received: from lvondent-mobl4.intel.com (c-71-56-157-77.hsd1.or.comcast.net. [71.56.157.77]) by smtp.gmail.com with ESMTPSA id gm5sm4054655pjb.32.2021.08.31.17.26.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Aug 2021 17:26:22 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH 2/4] Bluetooth: Add bt_skb_sendmmsg helper Date: Tue, 31 Aug 2021 17:26:19 -0700 Message-Id: <20210901002621.414016-2-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210901002621.414016-1-luiz.dentz@gmail.com> References: <20210901002621.414016-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 | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/include/net/bluetooth/bluetooth.h b/include/net/bluetooth/bluetooth.h index f858efcf9f40..96743e6e7a0a 100644 --- a/include/net/bluetooth/bluetooth.h +++ b/include/net/bluetooth/bluetooth.h @@ -446,6 +446,41 @@ 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 header, size_t footer) +{ + struct sk_buff *skb, **frag; + size_t size = min_t(size_t, len, mtu); + + skb = bt_skb_sendmsg(sk, msg, size, header, footer); + if (IS_ERR_OR_NULL(skb)) + return skb; + + len -= size; + if (!len) + return skb; + + /* Add remaining data over MTU as continuation fragments */ + frag = &skb_shinfo(skb)->frag_list; + while (len) { + *frag = bt_skb_sendmsg(sk, msg, size, header, footer); + if (IS_ERR_OR_NULL(*frag)) { + kfree_skb(skb); + return *frag; + } + + len -= (*frag)->len; + frag = &(*frag)->next; + } + + return skb; +} + int bt_to_errno(u16 code); void hci_sock_set_flag(struct sock *sk, int nr); From patchwork Wed Sep 1 00:26:20 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: 505827 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 79F71C43214 for ; Wed, 1 Sep 2021 00:26:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5694D6103D for ; Wed, 1 Sep 2021 00:26:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241446AbhIAA1V (ORCPT ); Tue, 31 Aug 2021 20:27:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33232 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241398AbhIAA1U (ORCPT ); Tue, 31 Aug 2021 20:27:20 -0400 Received: from mail-pg1-x535.google.com (mail-pg1-x535.google.com [IPv6:2607:f8b0:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 47CC5C061575 for ; Tue, 31 Aug 2021 17:26:24 -0700 (PDT) Received: by mail-pg1-x535.google.com with SMTP id g184so975468pgc.6 for ; Tue, 31 Aug 2021 17:26:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=oyY+Qj1v1ce0ewZFAidlMwWq7Ceg2ohLUWvByrJdMb8=; b=VxniagMmuRoe0vPC7fMMyZrC7D8p0oR66J9dHFuS5Gddk0/CTSjKxdClDpcDVcd/pf 2/rzUJGy0v/D4OEFKY6SqDGTcvk5OF9HPLVrN2JBisAWvvZJLdjeYZUpb+dTbeZpo0X0 7/kClKWcK6jttbU6O/jXTPhjx1j5toe8IHcFcrYJXTjtPzqydgRkHi8I3UGyMfc8y8a7 Ik3CcP8pyJupBTy7k0leemoSj95y1TuUTzhZPLtKwlzwA6UgnNPGWVOtjhajjI9jRtz1 dljauwkVNtn55SfZnXpg2EdHm4mUWvpFevHyi2zskJ0AfM5Y0FX+083ckLBk+YBkCTa8 rXfA== 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=oyY+Qj1v1ce0ewZFAidlMwWq7Ceg2ohLUWvByrJdMb8=; b=T/d3CQQb3HtreTy8yh1dR+SjltxdbXst/BUd9aICVGH8v+bl0XmP8BmM0pMP7dvkiR SUfEdsRRR/Kj2kT3IqHCfjeetYz/ZoXw221u8aW0c137eQUE7clYtnVGX2BK1oiJ9ofZ Z30YykAFuTwV61jhNs1uJlytTEbXZCdY7E2sDrP4pJ70/50oLviFAeVMYFGYr+x5XSlv VAJk0akEt+fMCUWN/YKeSjvYJq+Vue61kaHsmBSX7qBxm8Q+vppwOK06zRCSt+W7Q9Xs ic2i0twDObHjnVtIPTXdaEk9LMGKO8BVfoMeWkRk4qg2FxDmg7PPr7+ePO1FKnxYb1pA S4dg== X-Gm-Message-State: AOAM532u7iWWgYAuaQxCmmaBOMv2SrinKsDaIf7gGG52py9weQnoaQrU vMYaoWc3B8oJHs+Mztkl29c9wSbJh7o= X-Google-Smtp-Source: ABdhPJyx4MjbNAAH/vmu2a76JUWM3l9+XT6zSusOhZKUjyuYWK23YDN9BqpatQL9pm94ZRkk63o+dQ== X-Received: by 2002:a62:1ec7:0:b0:3eb:2693:90e2 with SMTP id e190-20020a621ec7000000b003eb269390e2mr31234147pfe.52.1630455983512; Tue, 31 Aug 2021 17:26:23 -0700 (PDT) Received: from lvondent-mobl4.intel.com (c-71-56-157-77.hsd1.or.comcast.net. [71.56.157.77]) by smtp.gmail.com with ESMTPSA id gm5sm4054655pjb.32.2021.08.31.17.26.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Aug 2021 17:26:23 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH 3/4] Bluetooth: SCO: Replace use of memcpy_from_msg with bt_skb_sendmsg Date: Tue, 31 Aug 2021 17:26:20 -0700 Message-Id: <20210901002621.414016-3-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210901002621.414016-1-luiz.dentz@gmail.com> References: <20210901002621.414016-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..60fb4dc73bc2 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, 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 Wed Sep 1 00:26:21 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: 505144 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 DEE0CC43216 for ; Wed, 1 Sep 2021 00:26:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BCA4B61041 for ; Wed, 1 Sep 2021 00:26:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241448AbhIAA1V (ORCPT ); Tue, 31 Aug 2021 20:27:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33234 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231249AbhIAA1V (ORCPT ); Tue, 31 Aug 2021 20:27:21 -0400 Received: from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 35F51C061575 for ; Tue, 31 Aug 2021 17:26:25 -0700 (PDT) Received: by mail-pf1-x42d.google.com with SMTP id u6so733907pfi.0 for ; Tue, 31 Aug 2021 17:26:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=e+JOgnu5aYfmjmTYg8pc1RJeuZkSzP18EUONRnYx5AM=; b=PEOeSwSggZvIC93e1GatwXAwzW9IwQfF7+4eNzlkjQYiQj5BMKOC3oM/rqHCkJukAX 49CbZzK5g94+jXqY7ocvGJzI3Pk0sPq0f22imTIOokxG83I0BAG1FH5wJIoPlcweHtJN bEJSCxEALVPdfwOio43B0ZplsF0pySalyyoBq5NwRZzWcZX301vRezhHPrdcCNmgcPeD 2Uq9GyU9WaJIraCD/nRpQ11h+HMGZjRY9Fs9e4gSOaTn32BRNur/6kgtXLmwV5pzMPWK HIiqKqKq7s71Dn+NFutGs81I+4XvyLe23vPpVKgGJiEQtuBnJ06EpND4e0wFJD0YA4Pw 0S5w== 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=e+JOgnu5aYfmjmTYg8pc1RJeuZkSzP18EUONRnYx5AM=; b=OJrgaSXtRsK3pZOYElq8DrWvKN8LWwvhKXI1+guY0cETtazNlPSawGil4D0nU1VbXK QerguynWoHJVGCEVmtM+8uHle7Qsqlum7zEHhpcXQZ4mXbZ5ULS80xtNQukWRF758r4K uRmBvOIP461DfR/lnaZQh93b4qcDOhGgUvHn/NvWQ06/87PxtZNEG66h3D/ZajOVaZcC NxMSckqth/8MlICgbzWv3TgmnTpiB+JNHY2dEGYbvTsjkCBqxictoo3p6iQMSXlvtuUi /9fGJHGgOdauO91926usaf/PYwt6Iv3mf7nuJ+JrhuPcmYQhIWGf7bvGIlLaxQHVIPsW e7Cg== X-Gm-Message-State: AOAM532yUONBKnL1u+vliCDODi5hkY9lAe9jl14ceeCKgsG5oBlavs2y 3OtzgJqqXsJ1K6wsRopEWIEXp5obkls= X-Google-Smtp-Source: ABdhPJzDdC5iom0Dn2rEXLov7Cd0fnPGPq432pCDWuL5i31YX2lTwAbw9Asb8IixHPIUX9yVe3iaAA== X-Received: by 2002:aa7:83d8:0:b0:3ef:990f:5525 with SMTP id j24-20020aa783d8000000b003ef990f5525mr31053819pfn.29.1630455984451; Tue, 31 Aug 2021 17:26:24 -0700 (PDT) Received: from lvondent-mobl4.intel.com (c-71-56-157-77.hsd1.or.comcast.net. [71.56.157.77]) by smtp.gmail.com with ESMTPSA id gm5sm4054655pjb.32.2021.08.31.17.26.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Aug 2021 17:26:23 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH 4/4] Bluetooth: RFCOMM: Replace use of memcpy_from_msg with bt_skb_sendmmsg Date: Tue, 31 Aug 2021 17:26:21 -0700 Message-Id: <20210901002621.414016-4-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210901002621.414016-1-luiz.dentz@gmail.com> References: <20210901002621.414016-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 | 44 +++++++++++++++++++++++++++++------ net/bluetooth/rfcomm/sock.c | 46 ++++++++----------------------------- 2 files changed, 47 insertions(+), 43 deletions(-) diff --git a/net/bluetooth/rfcomm/core.c b/net/bluetooth/rfcomm/core.c index f2bacb464ccf..361a60b4a670 100644 --- a/net/bluetooth/rfcomm/core.c +++ b/net/bluetooth/rfcomm/core.c @@ -549,22 +549,52 @@ 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; + struct sk_buff *frag; + int len; if (d->state != BT_CONNECTED) return -ENOTCONN; - BT_DBG("dlc %p mtu %d len %d", d, d->mtu, len); + /* Queue all fragments atomically. */ + spin_lock_bh(&d->tx_queue.lock); - if (len > d->mtu) - return -EINVAL; + len = rfcomm_dlc_send_frag(d, skb); + if (len < 0) + goto unlock; - rfcomm_make_uih(skb, d->addr); - skb_queue_tail(&d->tx_queue, skb); + skb_walk_frags(skb, frag) { + int ret; + + ret = rfcomm_dlc_send_frag(d, frag); + if (ret < 0) + break; + + len += ret; + } + + skb_shinfo(skb)->frag_list = NULL; + +unlock: + spin_unlock_bh(&d->tx_queue.lock); - 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; }