Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp519519rwb; Thu, 18 Aug 2022 07:45:35 -0700 (PDT) X-Google-Smtp-Source: AA6agR4Vmkx0iwnGD8GfdwFFiQn/Jt6jAx4Fasplvvr+reMnmndVQdSHA1z2H2FSyEqJkG53ApMT X-Received: by 2002:a05:6402:1681:b0:43d:2e92:63d with SMTP id a1-20020a056402168100b0043d2e92063dmr2604317edv.253.1660833934739; Thu, 18 Aug 2022 07:45:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660833934; cv=none; d=google.com; s=arc-20160816; b=UNFYBJac3AUCPzD1E6Guda6+qPz/PQtb2oXBPL9BQQo7C9dKhkMPGbfPcInFaAb3YR rlv8i5ef8293nK/iDcGF4xANGolJT15Qm2siN3i9iMgC4PlUokzmCXhUZ2D1J8dWRvBe By/Ohih8xuYD0tNijdpKT7HimsHv/isTb9VmHEa2DWF+5bZ6jiMRksKUzLDD5EwrYzVH s52a/lvyXSERz9OI9chHXdmle4NXs5yWA0/QrwYViy9qSECcBl2KC7TLXvA+0sJO/NtY 5sGI9JeS/74yIdmBxr1ZbaNFjV0DMjKc+PuLh3bgW1fVkEfuQ5lSwZj27ob7RZJeMDCv oT6A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=qWc0uEXmnioSgzG6heFza7jeHJT02CXmff/ZMhHLPuw=; b=ItUQk6xkJOBrIK0rIhzrgFBoAA/bgK2E1kzLvIU/DHTXmbFBrotakzCREyr6pL/37C l8F02YIU5RPzRWZQtJeO5RNSK1EQ9GJgmr8daAzvCwO0G/9+07gCNRetUOGL9tIvrs+e XQ6ZWnFRM9pmkPunr751GN+DCEA/sA8DXKhL49X/uD+UnD5Lydx3z3juBCAh6H4GG7/s cF6IlQD1edvf/CbkSSupq5AJJMZ7pHf3SGRpCOKuyly41qCorsfZHZ9+dfgMw5GNC8N5 Z/Cgi8O9Ycw5+StVB39kBuWTieMpYKXk9tism39VsSA9ajO6xOIXFjDPdA2GnG7rPer8 1Ozg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ispras.ru Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p14-20020a056402500e00b00445e8e96ae8si1121954eda.151.2022.08.18.07.45.08; Thu, 18 Aug 2022 07:45:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ispras.ru Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245735AbiHROOV (ORCPT + 99 others); Thu, 18 Aug 2022 10:14:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35426 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245722AbiHROOQ (ORCPT ); Thu, 18 Aug 2022 10:14:16 -0400 Received: from mail.ispras.ru (mail.ispras.ru [83.149.199.84]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D7C3179622; Thu, 18 Aug 2022 07:14:14 -0700 (PDT) Received: from stanislav-HLY-WX9XX.. (unknown [46.172.22.148]) by mail.ispras.ru (Postfix) with ESMTPSA id DD25140737D5; Thu, 18 Aug 2022 14:14:10 +0000 (UTC) From: Stanislav Goriainov To: stable@vger.kernel.org, Greg Kroah-Hartman Cc: Stanislav Goriainov , "David S. Miller" , Jakub Kicinski , "Matthew Wilcox (Oracle)" , Manivannan Sadhasivam , Xiaolong Huang , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, ldv-project@linuxtesting.org Subject: [PATCH 5.10 1/1] qrtr: Convert qrtr_ports from IDR to XArray Date: Thu, 18 Aug 2022 17:14:01 +0300 Message-Id: <20220818141401.4971-2-goriainov@ispras.ru> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220818141401.4971-1-goriainov@ispras.ru> References: <20220818141401.4971-1-goriainov@ispras.ru> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: "Matthew Wilcox (Oracle)" commit 3403fb9adea5f5d8f9337d77ba1b31e6536ac7f1 upstream. The XArray interface is easier for this driver to use. Also fixes a bug reported by the improper use of GFP_ATOMIC. Signed-off-by: Matthew Wilcox (Oracle) Reviewed-by: Manivannan Sadhasivam Signed-off-by: David S. Miller Signed-off-by: Stanislav Goriainov --- net/qrtr/qrtr.c | 42 ++++++++++++++---------------------------- 1 file changed, 14 insertions(+), 28 deletions(-) diff --git a/net/qrtr/qrtr.c b/net/qrtr/qrtr.c index 56cffbfa000b..13448ca5aeff 100644 --- a/net/qrtr/qrtr.c +++ b/net/qrtr/qrtr.c @@ -20,6 +20,8 @@ /* auto-bind range */ #define QRTR_MIN_EPH_SOCKET 0x4000 #define QRTR_MAX_EPH_SOCKET 0x7fff +#define QRTR_EPH_PORT_RANGE \ + XA_LIMIT(QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET) /** * struct qrtr_hdr_v1 - (I|R)PCrouter packet header version 1 @@ -106,8 +108,7 @@ static LIST_HEAD(qrtr_all_nodes); static DEFINE_MUTEX(qrtr_node_lock); /* local port allocation management */ -static DEFINE_IDR(qrtr_ports); -static DEFINE_MUTEX(qrtr_port_lock); +static DEFINE_XARRAY_ALLOC(qrtr_ports); /** * struct qrtr_node - endpoint node @@ -635,7 +636,7 @@ static struct qrtr_sock *qrtr_port_lookup(int port) port = 0; rcu_read_lock(); - ipc = idr_find(&qrtr_ports, port); + ipc = xa_load(&qrtr_ports, port); if (ipc) sock_hold(&ipc->sk); rcu_read_unlock(); @@ -677,9 +678,7 @@ static void qrtr_port_remove(struct qrtr_sock *ipc) __sock_put(&ipc->sk); - mutex_lock(&qrtr_port_lock); - idr_remove(&qrtr_ports, port); - mutex_unlock(&qrtr_port_lock); + xa_erase(&qrtr_ports, port); /* Ensure that if qrtr_port_lookup() did enter the RCU read section we * wait for it to up increment the refcount */ @@ -698,29 +697,20 @@ static void qrtr_port_remove(struct qrtr_sock *ipc) */ static int qrtr_port_assign(struct qrtr_sock *ipc, int *port) { - u32 min_port; int rc; - mutex_lock(&qrtr_port_lock); if (!*port) { - min_port = QRTR_MIN_EPH_SOCKET; - rc = idr_alloc_u32(&qrtr_ports, ipc, &min_port, QRTR_MAX_EPH_SOCKET, GFP_ATOMIC); - if (!rc) - *port = min_port; + rc = xa_alloc(&qrtr_ports, port, ipc, QRTR_EPH_PORT_RANGE, + GFP_KERNEL); } else if (*port < QRTR_MIN_EPH_SOCKET && !capable(CAP_NET_ADMIN)) { rc = -EACCES; } else if (*port == QRTR_PORT_CTRL) { - min_port = 0; - rc = idr_alloc_u32(&qrtr_ports, ipc, &min_port, 0, GFP_ATOMIC); + rc = xa_insert(&qrtr_ports, 0, ipc, GFP_KERNEL); } else { - min_port = *port; - rc = idr_alloc_u32(&qrtr_ports, ipc, &min_port, *port, GFP_ATOMIC); - if (!rc) - *port = min_port; + rc = xa_insert(&qrtr_ports, *port, ipc, GFP_KERNEL); } - mutex_unlock(&qrtr_port_lock); - if (rc == -ENOSPC) + if (rc == -EBUSY) return -EADDRINUSE; else if (rc < 0) return rc; @@ -734,20 +724,16 @@ static int qrtr_port_assign(struct qrtr_sock *ipc, int *port) static void qrtr_reset_ports(void) { struct qrtr_sock *ipc; - int id; - - mutex_lock(&qrtr_port_lock); - idr_for_each_entry(&qrtr_ports, ipc, id) { - /* Don't reset control port */ - if (id == 0) - continue; + unsigned long index; + rcu_read_lock(); + xa_for_each_start(&qrtr_ports, index, ipc, 1) { sock_hold(&ipc->sk); ipc->sk.sk_err = ENETRESET; ipc->sk.sk_error_report(&ipc->sk); sock_put(&ipc->sk); } - mutex_unlock(&qrtr_port_lock); + rcu_read_unlock(); } /* Bind socket to address. -- 2.34.1