Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp1978971rwb; Fri, 19 Aug 2022 12:50:27 -0700 (PDT) X-Google-Smtp-Source: AA6agR6BL+rqeKUqokj2fKfpsnZowXA40jPtCEvE6Tg8Ph5V5MqEqTok+upH7nsVFDL+1jm1BkS5 X-Received: by 2002:a63:5658:0:b0:42a:1663:b965 with SMTP id g24-20020a635658000000b0042a1663b965mr6147542pgm.486.1660938626784; Fri, 19 Aug 2022 12:50:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660938626; cv=none; d=google.com; s=arc-20160816; b=QzORrPfeKpAEieyKHSZqHU1PhQvjrCGdPoDSi8uQVDnOX100XvM8DN0+n4tGRoJ5M6 Di8JLo6HVKhRheWGqHp76BhH6g7aA7glVIT3LvVk7dbdnsDPdPnNAos7P8dPFq+DOtvE PEVHkohtsjcAOv12/YJ6pzqCPRWfAtSUh8lZ815J13lqER3zFxyRi74alyG6gF8vYRDE UdOrDENAVZ6xzm1zeanjo4CevocTG76SqblcLtCqHB80t0Bp/1gpJTKQ4Saa7ZjK6HYj JAcDxDNk/LLLyYWIqTZ5nzha1fo2otakfU9uT1FEtU5kFsFiPpmZcf4hkukpCUH0f7Wk CBgA== 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=wwAlKeSDycJak5rtJF1j/sIWnqEeKZKFE+bHQtcBO2E=; b=i6URJP/tE76lJjFwOunuqEpPRIONT3pCuO07XZxpVmYy+UEObHp9slP9L97YNGNDM1 zcoJZlvnHfJx8k9zfTQfN8UTpvA1KHld5K2sLk/5y1Dlin6nB8LuolyKGJI224STlZc9 l3fgg6p+FmXyPHbgfWzB0Fy25DDHymyMVpoaFu4REEum5oKb8q0GpAHj2WRe9migC3Ge F9m8giiWRrSumQkkDBnQlYE00ypYJ0NBp+vZFoRth2W99REwVgIpVJqy57CYu8dgfeqF 1AWn/KnP8sR09NKxQ968R4ruRTHzplM6d15EUkhQZsrZVs9Ala80eJbZKcifTOhoxh2t lotg== 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 j38-20020a63fc26000000b0040d7ac04fafsi5168951pgi.36.2022.08.19.12.50.15; Fri, 19 Aug 2022 12:50:26 -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 S1351532AbiHSTrp (ORCPT + 99 others); Fri, 19 Aug 2022 15:47:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49238 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351628AbiHSTrk (ORCPT ); Fri, 19 Aug 2022 15:47:40 -0400 Received: from mail.ispras.ru (mail.ispras.ru [83.149.199.84]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 32EFE11418A; Fri, 19 Aug 2022 12:47:39 -0700 (PDT) Received: from stanislav-HLY-WX9XX.. (unknown [46.172.22.155]) by mail.ispras.ru (Postfix) with ESMTPSA id 51FF740737D5; Fri, 19 Aug 2022 19:47:36 +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: Fri, 19 Aug 2022 22:47:27 +0300 Message-Id: <20220819194727.18911-2-goriainov@ispras.ru> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220819194727.18911-1-goriainov@ispras.ru> References: <20220819194727.18911-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 3cbf7530a163d048a6376cd22fecb9cdcb23b192 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