From patchwork Tue Jun 8 04:02:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Ryazanov X-Patchwork-Id: 12305299 X-Patchwork-Delegate: johannes@sipsolutions.net 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.8 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,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 44C11C47082 for ; Tue, 8 Jun 2021 04:04:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2CD1B6120F for ; Tue, 8 Jun 2021 04:04:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231149AbhFHEF7 (ORCPT ); Tue, 8 Jun 2021 00:05:59 -0400 Received: from mail-lf1-f45.google.com ([209.85.167.45]:47070 "EHLO mail-lf1-f45.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230364AbhFHEF6 (ORCPT ); Tue, 8 Jun 2021 00:05:58 -0400 Received: by mail-lf1-f45.google.com with SMTP id m21so14134681lfg.13; Mon, 07 Jun 2021 21:03:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NgZj1BbnXzcQI0hCaHfsE+MOSxkyGzszbyYkCnu9Th0=; b=nXlktMmHRd95mu5YWER2k1hcldsPTe2ffqVG/L36UXe+ruX1EwUejfS5H8V5lpcyF+ HiNWFv6Rd/VN2gUS1M6DF4rQcmhWZ5SE6c42SgbvGuVmwYgG6rLImhoUlR3aiYyFJynZ fY1hCSgpgWzvNhEW79f/AYWLFnWOtsRUE0IWGfK7YUOq7qGF+uK4E+LFRK+j5oUkp5fy IrtjzKW4a4Bs4Zm3mv+LWkFTy4x6XrUjqWUgy9GlRguCo47MCfTweso9cfTUOwBxvynH klxDU8ppebi+7EfNmovg9fL57NtL7GlKy+hpZROyToqbY92sO1H0ddLryHSWtuFtcR1J oQ5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NgZj1BbnXzcQI0hCaHfsE+MOSxkyGzszbyYkCnu9Th0=; b=WmrmQJNY/EaVIDkJmSpe4N0zocgySXLNMDigqPuqNQCytQ1/VsoYUekaX0TGFfg8tH hpn+UPKn+WRYBQBz+4VdNkQw7I70NmGD01TWN2VUS23je55+/7yienLDiYh+T4k1DPnO AnpSM+0GqF2roF7BEzp6IAo55roHyBPM7m9oT+Q7VGSkLsy3kYOkI+BtfPtlGlHQGewP WFC+S9UXG/jMYDEVJsaklfKgEnxVaUA/hSvuEo7cb05Gt1Hf66rRqnexZYrH/1wXchn0 V1cauwon1hdN69zQclLpIBPt7ac9pj9/h/osJ/OazjdskEIdXRNkXL8sM8Hj9MapQHqI lMcA== X-Gm-Message-State: AOAM5336qEGnHCkESFrOrdyE6JTqr8FSJcpShu3xi0dZ4xWgnetl9qo/ 6NUmzmClDtCMGyFh7jyb+iE= X-Google-Smtp-Source: ABdhPJwQnluFnuEaRCH3dKtbCOx9lTqD75x17SNW3YD7bJE/SCQBUC1sW7hQDpmE2gpbn1EzGjEXpQ== X-Received: by 2002:a05:6512:2096:: with SMTP id t22mr14410816lfr.272.1623124970017; Mon, 07 Jun 2021 21:02:50 -0700 (PDT) Received: from rsa-laptop.internal.lan ([217.25.229.52]) by smtp.gmail.com with ESMTPSA id l23sm1729096lfj.26.2021.06.07.21.02.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Jun 2021 21:02:49 -0700 (PDT) From: Sergey Ryazanov To: Loic Poulain , "David S. Miller" , Jakub Kicinski Cc: netdev@vger.kernel.org, linux-wireless@vger.kernel.org Subject: [PATCH 01/10] wwan_hwsim: WWAN device simulator Date: Tue, 8 Jun 2021 07:02:32 +0300 Message-Id: <20210608040241.10658-2-ryazanov.s.a@gmail.com> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210608040241.10658-1-ryazanov.s.a@gmail.com> References: <20210608040241.10658-1-ryazanov.s.a@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org This driver simulates a set of WWAN device with a set of AT control ports. It can be used to test WWAN kernel framework as well as user space tools. Signed-off-by: Sergey Ryazanov --- drivers/net/wwan/Kconfig | 10 ++ drivers/net/wwan/Makefile | 2 + drivers/net/wwan/wwan_hwsim.c | 318 ++++++++++++++++++++++++++++++++++ 3 files changed, 330 insertions(+) create mode 100644 drivers/net/wwan/wwan_hwsim.c diff --git a/drivers/net/wwan/Kconfig b/drivers/net/wwan/Kconfig index 7ad1920120bc..ec0b194a373c 100644 --- a/drivers/net/wwan/Kconfig +++ b/drivers/net/wwan/Kconfig @@ -20,6 +20,16 @@ config WWAN_CORE To compile this driver as a module, choose M here: the module will be called wwan. +config WWAN_HWSIM + tristate "Simulated WWAN device" + depends on WWAN_CORE + help + This driver is a developer testing tool that can be used to test WWAN + framework. + + To compile this driver as a module, choose M here: the module will be + called wwan_hwsim. If unsure, say N. + config MHI_WWAN_CTRL tristate "MHI WWAN control driver for QCOM-based PCIe modems" select WWAN_CORE diff --git a/drivers/net/wwan/Makefile b/drivers/net/wwan/Makefile index 556cd90958ca..f33f77ca1021 100644 --- a/drivers/net/wwan/Makefile +++ b/drivers/net/wwan/Makefile @@ -6,4 +6,6 @@ obj-$(CONFIG_WWAN_CORE) += wwan.o wwan-objs += wwan_core.o +obj-$(CONFIG_WWAN_HWSIM) += wwan_hwsim.o + obj-$(CONFIG_MHI_WWAN_CTRL) += mhi_wwan_ctrl.o diff --git a/drivers/net/wwan/wwan_hwsim.c b/drivers/net/wwan/wwan_hwsim.c new file mode 100644 index 000000000000..96d25d7e5bb8 --- /dev/null +++ b/drivers/net/wwan/wwan_hwsim.c @@ -0,0 +1,318 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * WWAN device simulator for WWAN framework testing. + * + * Copyright (c) 2021, Sergey Ryazanov + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include +#include +#include +#include +#include +#include + +static int wwan_hwsim_devsnum = 2; +module_param_named(devices, wwan_hwsim_devsnum, int, 0444); +MODULE_PARM_DESC(devices, "Number of simulated devices"); + +static struct class *wwan_hwsim_class; + +static DEFINE_SPINLOCK(wwan_hwsim_devs_lock); +static LIST_HEAD(wwan_hwsim_devs); +static unsigned int wwan_hwsim_dev_idx; + +struct wwan_hwsim_dev { + struct list_head list; + unsigned int id; + struct device dev; + spinlock_t ports_lock; /* Serialize ports creation/deletion */ + unsigned int port_idx; + struct list_head ports; +}; + +struct wwan_hwsim_port { + struct list_head list; + unsigned int id; + struct wwan_hwsim_dev *dev; + struct wwan_port *wwan; + enum { /* AT command parser state */ + AT_PARSER_WAIT_A, + AT_PARSER_WAIT_T, + AT_PARSER_WAIT_TERM, + AT_PARSER_SKIP_LINE, + } pstate; +}; + +static int wwan_hwsim_port_start(struct wwan_port *wport) +{ + struct wwan_hwsim_port *port = wwan_port_get_drvdata(wport); + + port->pstate = AT_PARSER_WAIT_A; + + return 0; +} + +static void wwan_hwsim_port_stop(struct wwan_port *wport) +{ +} + +/* Implements a minimalistic AT commands parser that echo input back and + * reply with 'OK' to each input command. See AT command protocol details in the + * ITU-T V.250 recomendations document. + * + * Be aware that this processor is not fully V.250 compliant. + */ +static int wwan_hwsim_port_tx(struct wwan_port *wport, struct sk_buff *in) +{ + struct wwan_hwsim_port *port = wwan_port_get_drvdata(wport); + struct sk_buff *out; + int i, n, s; + + /* Estimate a max possible number of commands by counting the number of + * termination chars (S3 param, CR by default). And then allocate the + * output buffer that will be enough to fit the echo and result codes of + * all commands. + */ + for (i = 0, n = 0; i < in->len; ++i) + if (in->data[i] == '\r') + n++; + n = in->len + n * (2 + 2 + 2); /* Output buffer size */ + out = alloc_skb(n, GFP_KERNEL); + if (!out) + return -ENOMEM; + + for (i = 0, s = 0; i < in->len; ++i) { + char c = in->data[i]; + + if (port->pstate == AT_PARSER_WAIT_A) { + if (c == 'A' || c == 'a') + port->pstate = AT_PARSER_WAIT_T; + else if (c != '\n') /* Ignore formating char */ + port->pstate = AT_PARSER_SKIP_LINE; + } else if (port->pstate == AT_PARSER_WAIT_T) { + if (c == 'T' || c == 't') + port->pstate = AT_PARSER_WAIT_TERM; + else + port->pstate = AT_PARSER_SKIP_LINE; + } else if (port->pstate == AT_PARSER_WAIT_TERM) { + if (c != '\r') + continue; + /* Consume the trailing formatting char as well */ + if ((i + 1) < in->len && in->data[i + 1] == '\n') + i++; + n = i - s + 1; + memcpy(skb_put(out, n), &in->data[s], n);/* Echo */ + memcpy(skb_put(out, 6), "\r\nOK\r\n", 6); + s = i + 1; + port->pstate = AT_PARSER_WAIT_A; + } else if (port->pstate == AT_PARSER_SKIP_LINE) { + if (c != '\r') + continue; + port->pstate = AT_PARSER_WAIT_A; + } + } + + if (i > s) { + /* Echo the processed portion of a not yet completed command */ + n = i - s; + memcpy(skb_put(out, n), &in->data[s], n); + } + + consume_skb(in); + + wwan_port_rx(wport, out); + + return 0; +} + +static const struct wwan_port_ops wwan_hwsim_port_ops = { + .start = wwan_hwsim_port_start, + .stop = wwan_hwsim_port_stop, + .tx = wwan_hwsim_port_tx, +}; + +static struct wwan_hwsim_port *wwan_hwsim_port_new(struct wwan_hwsim_dev *dev) +{ + struct wwan_hwsim_port *port; + int err; + + port = kzalloc(sizeof(*port), GFP_KERNEL); + if (!port) + return ERR_PTR(-ENOMEM); + + port->dev = dev; + + spin_lock(&dev->ports_lock); + port->id = dev->port_idx++; + spin_unlock(&dev->ports_lock); + + port->wwan = wwan_create_port(&dev->dev, WWAN_PORT_AT, + &wwan_hwsim_port_ops, + port); + if (IS_ERR(port->wwan)) { + err = PTR_ERR(port->wwan); + goto err_free_port; + } + + return port; + +err_free_port: + kfree(port); + + return ERR_PTR(err); +} + +static void wwan_hwsim_port_del(struct wwan_hwsim_port *port) +{ + wwan_remove_port(port->wwan); + kfree(port); +} + +static void wwan_hwsim_dev_release(struct device *sysdev) +{ + struct wwan_hwsim_dev *dev = container_of(sysdev, typeof(*dev), dev); + + kfree(dev); +} + +static struct wwan_hwsim_dev *wwan_hwsim_dev_new(void) +{ + struct wwan_hwsim_dev *dev; + int err; + + dev = kzalloc(sizeof(*dev), GFP_KERNEL); + if (!dev) + return ERR_PTR(-ENOMEM); + + spin_lock(&wwan_hwsim_devs_lock); + dev->id = wwan_hwsim_dev_idx++; + spin_unlock(&wwan_hwsim_devs_lock); + + dev->dev.release = wwan_hwsim_dev_release; + dev->dev.class = wwan_hwsim_class; + dev_set_name(&dev->dev, "hwsim%u", dev->id); + + spin_lock_init(&dev->ports_lock); + INIT_LIST_HEAD(&dev->ports); + + err = device_register(&dev->dev); + if (err) + goto err_free_dev; + + return dev; + +err_free_dev: + kfree(dev); + + return ERR_PTR(err); +} + +static void wwan_hwsim_dev_del(struct wwan_hwsim_dev *dev) +{ + spin_lock(&dev->ports_lock); + while (!list_empty(&dev->ports)) { + struct wwan_hwsim_port *port; + + port = list_first_entry(&dev->ports, struct wwan_hwsim_port, + list); + list_del(&port->list); + spin_unlock(&dev->ports_lock); + wwan_hwsim_port_del(port); + spin_lock(&dev->ports_lock); + } + spin_unlock(&dev->ports_lock); + + device_unregister(&dev->dev); + /* Memory will be freed in the device release callback */ +} + +static int __init wwan_hwsim_init_devs(void) +{ + struct wwan_hwsim_dev *dev; + int i, j; + + for (i = 0; i < wwan_hwsim_devsnum; ++i) { + dev = wwan_hwsim_dev_new(); + if (IS_ERR(dev)) + return PTR_ERR(dev); + + spin_lock(&wwan_hwsim_devs_lock); + list_add_tail(&dev->list, &wwan_hwsim_devs); + spin_unlock(&wwan_hwsim_devs_lock); + + /* Create a couple of ports per each device to accelerate + * the simulator readiness time. + */ + for (j = 0; j < 2; ++j) { + struct wwan_hwsim_port *port; + + port = wwan_hwsim_port_new(dev); + if (IS_ERR(port)) + return PTR_ERR(port); + + spin_lock(&dev->ports_lock); + list_add_tail(&port->list, &dev->ports); + spin_unlock(&dev->ports_lock); + } + } + + return 0; +} + +static void wwan_hwsim_free_devs(void) +{ + struct wwan_hwsim_dev *dev; + + spin_lock(&wwan_hwsim_devs_lock); + while (!list_empty(&wwan_hwsim_devs)) { + dev = list_first_entry(&wwan_hwsim_devs, struct wwan_hwsim_dev, + list); + list_del(&dev->list); + spin_unlock(&wwan_hwsim_devs_lock); + wwan_hwsim_dev_del(dev); + spin_lock(&wwan_hwsim_devs_lock); + } + spin_unlock(&wwan_hwsim_devs_lock); +} + +static int __init wwan_hwsim_init(void) +{ + int err; + + if (wwan_hwsim_devsnum < 0 || wwan_hwsim_devsnum > 128) + return -EINVAL; + + wwan_hwsim_class = class_create(THIS_MODULE, "wwan_hwsim"); + if (IS_ERR(wwan_hwsim_class)) + return PTR_ERR(wwan_hwsim_class); + + err = wwan_hwsim_init_devs(); + if (err) + goto err_clean_devs; + + return 0; + +err_clean_devs: + wwan_hwsim_free_devs(); + class_destroy(wwan_hwsim_class); + + return err; +} + +static void __exit wwan_hwsim_exit(void) +{ + wwan_hwsim_free_devs(); + class_destroy(wwan_hwsim_class); +} + +module_init(wwan_hwsim_init); +module_exit(wwan_hwsim_exit); + +MODULE_AUTHOR("Sergey Ryazanov"); +MODULE_DESCRIPTION("Device simulator for WWAN framework"); +MODULE_LICENSE("GPL"); From patchwork Tue Jun 8 04:02:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Ryazanov X-Patchwork-Id: 12305283 X-Patchwork-Delegate: johannes@sipsolutions.net 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=-12.9 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,UNWANTED_LANGUAGE_BODY, 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 947B4C47082 for ; Tue, 8 Jun 2021 04:03:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 73CCA6120F for ; Tue, 8 Jun 2021 04:03:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229782AbhFHEE6 (ORCPT ); Tue, 8 Jun 2021 00:04:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34208 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229455AbhFHEE6 (ORCPT ); Tue, 8 Jun 2021 00:04:58 -0400 Received: from mail-lf1-x133.google.com (mail-lf1-x133.google.com [IPv6:2a00:1450:4864:20::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C5DA6C061787; Mon, 7 Jun 2021 21:02:52 -0700 (PDT) Received: by mail-lf1-x133.google.com with SMTP id a1so20133781lfr.12; Mon, 07 Jun 2021 21:02:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=OHFz2XiGKzjMgGqDHw8Ln6pO4vzSXzZficuVDOKNoOo=; b=f26c1mSujkUX/ImJQ8SNUPBjZ5RnTP34UeX+xJK66/pAGejZI1PoCE/118wNiB7RFE Wd/ASO216UgvyczSNRZ+knpFOAXErUGW5UunOQla+i6ct2bOB2mF31u64LAeIibdQSOX C1ygTZl3bQSEDh7DZQRzTm5a0BCOTYhZHdL6YbvC0JKBaq951YfcekTWe8d8SqGUSymb XSia+eC+FinqIGjxMV6fJlV2R6OnNc4+9G+IrsJgFAa+CK4yWA/VUYO9ciqhk21CS2YA N0BJi7RNKKKGwfpgWZPp1Tpq6ijddtLnkDEPKB5FjtoKZOBwsXtWer//aN3ctX+H5pw/ Jdxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=OHFz2XiGKzjMgGqDHw8Ln6pO4vzSXzZficuVDOKNoOo=; b=d3VFQirgCiw3sahAkArvouIcX3n3OJOGghhFv6diJs77Cd3Pa4awAsZRoYjX4Hon8a cvLZ6XzgzX1UdMqHzAk67AXcQ/z8XoGFMPjHKOkISu7V6xBcih9asq5fLww8qyUS8xEM 4lUrz995Gd15iHgH1ZGUNp0mOLDU7u1zbOMyWytrW6vFOKwcsg5QXTI3kZatS7dQaFTp Sq5c/YRS9TyglwomGgZDc35OB37iqLl4siOsPB5ukxVnRLhfcqEvVzu8T7moESMpbeBn kIody0XbEItMKuwnj53TY1mf8FhSwhtdAhfgGwYSfiRz4joK4/p055q1hhYwFK6d7bwc JVXA== X-Gm-Message-State: AOAM533fdM77YIj2GgAcDG/VpU//tO+SkH8/1AAVo1Ytgdu7gdKTFXEn aK3qYwvt0U8HTbV6z+TLAezqQLgj3uc= X-Google-Smtp-Source: ABdhPJwQJ8edDU0q6xED+wYi1hRWxoziQkwDjPeRu6yHOo+Lc/PEErEU3MdhJoHzJxqqd2hjNtfjOQ== X-Received: by 2002:a05:6512:32a5:: with SMTP id q5mr13439043lfe.171.1623124971147; Mon, 07 Jun 2021 21:02:51 -0700 (PDT) Received: from rsa-laptop.internal.lan ([217.25.229.52]) by smtp.gmail.com with ESMTPSA id l23sm1729096lfj.26.2021.06.07.21.02.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Jun 2021 21:02:50 -0700 (PDT) From: Sergey Ryazanov To: Loic Poulain , "David S. Miller" , Jakub Kicinski Cc: netdev@vger.kernel.org, linux-wireless@vger.kernel.org Subject: [PATCH 02/10] wwan_hwsim: add debugfs management interface Date: Tue, 8 Jun 2021 07:02:33 +0300 Message-Id: <20210608040241.10658-3-ryazanov.s.a@gmail.com> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210608040241.10658-1-ryazanov.s.a@gmail.com> References: <20210608040241.10658-1-ryazanov.s.a@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org wwan_hwsim creates and removes simulated control ports on module loading and unloading. It would be helpful to be able to create/remove devices and ports at run-time to trigger wwan port (un-)register actions without module reloading. Some simulator objects (e.g. ports) do not have the underling device and it is not possible to fully manage the simulator via sysfs. wwan_hsim intend for developers, so implement it as a self-contained debugfs based management interface. Signed-off-by: Sergey Ryazanov --- drivers/net/wwan/wwan_hwsim.c | 186 +++++++++++++++++++++++++++++++++- 1 file changed, 184 insertions(+), 2 deletions(-) diff --git a/drivers/net/wwan/wwan_hwsim.c b/drivers/net/wwan/wwan_hwsim.c index 96d25d7e5bb8..472cae544a2b 100644 --- a/drivers/net/wwan/wwan_hwsim.c +++ b/drivers/net/wwan/wwan_hwsim.c @@ -15,6 +15,8 @@ #include #include #include +#include +#include static int wwan_hwsim_devsnum = 2; module_param_named(devices, wwan_hwsim_devsnum, int, 0444); @@ -22,6 +24,9 @@ MODULE_PARM_DESC(devices, "Number of simulated devices"); static struct class *wwan_hwsim_class; +static struct dentry *wwan_hwsim_debugfs_topdir; +static struct dentry *wwan_hwsim_debugfs_devcreate; + static DEFINE_SPINLOCK(wwan_hwsim_devs_lock); static LIST_HEAD(wwan_hwsim_devs); static unsigned int wwan_hwsim_dev_idx; @@ -30,6 +35,9 @@ struct wwan_hwsim_dev { struct list_head list; unsigned int id; struct device dev; + struct work_struct del_work; + struct dentry *debugfs_topdir; + struct dentry *debugfs_portcreate; spinlock_t ports_lock; /* Serialize ports creation/deletion */ unsigned int port_idx; struct list_head ports; @@ -40,6 +48,8 @@ struct wwan_hwsim_port { unsigned int id; struct wwan_hwsim_dev *dev; struct wwan_port *wwan; + struct work_struct del_work; + struct dentry *debugfs_topdir; enum { /* AT command parser state */ AT_PARSER_WAIT_A, AT_PARSER_WAIT_T, @@ -48,6 +58,12 @@ struct wwan_hwsim_port { } pstate; }; +static const struct file_operations wwan_hwsim_debugfs_portdestroy_fops; +static const struct file_operations wwan_hwsim_debugfs_portcreate_fops; +static const struct file_operations wwan_hwsim_debugfs_devdestroy_fops; +static void wwan_hwsim_port_del_work(struct work_struct *work); +static void wwan_hwsim_dev_del_work(struct work_struct *work); + static int wwan_hwsim_port_start(struct wwan_port *wport) { struct wwan_hwsim_port *port = wwan_port_get_drvdata(wport); @@ -139,6 +155,7 @@ static const struct wwan_port_ops wwan_hwsim_port_ops = { static struct wwan_hwsim_port *wwan_hwsim_port_new(struct wwan_hwsim_dev *dev) { struct wwan_hwsim_port *port; + char name[0x10]; int err; port = kzalloc(sizeof(*port), GFP_KERNEL); @@ -159,6 +176,13 @@ static struct wwan_hwsim_port *wwan_hwsim_port_new(struct wwan_hwsim_dev *dev) goto err_free_port; } + INIT_WORK(&port->del_work, wwan_hwsim_port_del_work); + + snprintf(name, sizeof(name), "port%u", port->id); + port->debugfs_topdir = debugfs_create_dir(name, dev->debugfs_topdir); + debugfs_create_file("destroy", 0200, port->debugfs_topdir, port, + &wwan_hwsim_debugfs_portdestroy_fops); + return port; err_free_port: @@ -169,10 +193,34 @@ static struct wwan_hwsim_port *wwan_hwsim_port_new(struct wwan_hwsim_dev *dev) static void wwan_hwsim_port_del(struct wwan_hwsim_port *port) { + debugfs_remove(port->debugfs_topdir); + + /* Make sure that there is no pending deletion work */ + if (current_work() != &port->del_work) + cancel_work_sync(&port->del_work); + wwan_remove_port(port->wwan); kfree(port); } +static void wwan_hwsim_port_del_work(struct work_struct *work) +{ + struct wwan_hwsim_port *port = + container_of(work, typeof(*port), del_work); + struct wwan_hwsim_dev *dev = port->dev; + + spin_lock(&dev->ports_lock); + if (list_empty(&port->list)) { + /* Someone else deleting port at the moment */ + spin_unlock(&dev->ports_lock); + return; + } + list_del_init(&port->list); + spin_unlock(&dev->ports_lock); + + wwan_hwsim_port_del(port); +} + static void wwan_hwsim_dev_release(struct device *sysdev) { struct wwan_hwsim_dev *dev = container_of(sysdev, typeof(*dev), dev); @@ -204,6 +252,17 @@ static struct wwan_hwsim_dev *wwan_hwsim_dev_new(void) if (err) goto err_free_dev; + INIT_WORK(&dev->del_work, wwan_hwsim_dev_del_work); + + dev->debugfs_topdir = debugfs_create_dir(dev_name(&dev->dev), + wwan_hwsim_debugfs_topdir); + debugfs_create_file("destroy", 0200, dev->debugfs_topdir, dev, + &wwan_hwsim_debugfs_devdestroy_fops); + dev->debugfs_portcreate = + debugfs_create_file("portcreate", 0200, + dev->debugfs_topdir, dev, + &wwan_hwsim_debugfs_portcreate_fops); + return dev; err_free_dev: @@ -214,23 +273,136 @@ static struct wwan_hwsim_dev *wwan_hwsim_dev_new(void) static void wwan_hwsim_dev_del(struct wwan_hwsim_dev *dev) { + debugfs_remove(dev->debugfs_portcreate); /* Avoid new ports */ + spin_lock(&dev->ports_lock); while (!list_empty(&dev->ports)) { struct wwan_hwsim_port *port; port = list_first_entry(&dev->ports, struct wwan_hwsim_port, list); - list_del(&port->list); + list_del_init(&port->list); spin_unlock(&dev->ports_lock); wwan_hwsim_port_del(port); spin_lock(&dev->ports_lock); } spin_unlock(&dev->ports_lock); + debugfs_remove(dev->debugfs_topdir); + + /* Make sure that there is no pending deletion work */ + if (current_work() != &dev->del_work) + cancel_work_sync(&dev->del_work); + device_unregister(&dev->dev); /* Memory will be freed in the device release callback */ } +static void wwan_hwsim_dev_del_work(struct work_struct *work) +{ + struct wwan_hwsim_dev *dev = container_of(work, typeof(*dev), del_work); + + spin_lock(&wwan_hwsim_devs_lock); + if (list_empty(&dev->list)) { + /* Someone else deleting device at the moment */ + spin_unlock(&wwan_hwsim_devs_lock); + return; + } + list_del_init(&dev->list); + spin_unlock(&wwan_hwsim_devs_lock); + + wwan_hwsim_dev_del(dev); +} + +static ssize_t wwan_hwsim_debugfs_portdestroy_write(struct file *file, + const char __user *usrbuf, + size_t count, loff_t *ppos) +{ + struct wwan_hwsim_port *port = file->private_data; + + /* We can not delete port here since it will cause a deadlock due to + * waiting this callback to finish in the debugfs_remove() call. So, + * use workqueue. + */ + schedule_work(&port->del_work); + + return count; +} + +static const struct file_operations wwan_hwsim_debugfs_portdestroy_fops = { + .write = wwan_hwsim_debugfs_portdestroy_write, + .open = simple_open, + .llseek = noop_llseek, +}; + +static ssize_t wwan_hwsim_debugfs_portcreate_write(struct file *file, + const char __user *usrbuf, + size_t count, loff_t *ppos) +{ + struct wwan_hwsim_dev *dev = file->private_data; + struct wwan_hwsim_port *port; + + port = wwan_hwsim_port_new(dev); + if (IS_ERR(port)) + return PTR_ERR(port); + + spin_lock(&dev->ports_lock); + list_add_tail(&port->list, &dev->ports); + spin_unlock(&dev->ports_lock); + + return count; +} + +static const struct file_operations wwan_hwsim_debugfs_portcreate_fops = { + .write = wwan_hwsim_debugfs_portcreate_write, + .open = simple_open, + .llseek = noop_llseek, +}; + +static ssize_t wwan_hwsim_debugfs_devdestroy_write(struct file *file, + const char __user *usrbuf, + size_t count, loff_t *ppos) +{ + struct wwan_hwsim_dev *dev = file->private_data; + + /* We can not delete device here since it will cause a deadlock due to + * waiting this callback to finish in the debugfs_remove() call. So, + * use workqueue. + */ + schedule_work(&dev->del_work); + + return count; +} + +static const struct file_operations wwan_hwsim_debugfs_devdestroy_fops = { + .write = wwan_hwsim_debugfs_devdestroy_write, + .open = simple_open, + .llseek = noop_llseek, +}; + +static ssize_t wwan_hwsim_debugfs_devcreate_write(struct file *file, + const char __user *usrbuf, + size_t count, loff_t *ppos) +{ + struct wwan_hwsim_dev *dev; + + dev = wwan_hwsim_dev_new(); + if (IS_ERR(dev)) + return PTR_ERR(dev); + + spin_lock(&wwan_hwsim_devs_lock); + list_add_tail(&dev->list, &wwan_hwsim_devs); + spin_unlock(&wwan_hwsim_devs_lock); + + return count; +} + +static const struct file_operations wwan_hwsim_debugfs_devcreate_fops = { + .write = wwan_hwsim_debugfs_devcreate_write, + .open = simple_open, + .llseek = noop_llseek, +}; + static int __init wwan_hwsim_init_devs(void) { struct wwan_hwsim_dev *dev; @@ -272,7 +444,7 @@ static void wwan_hwsim_free_devs(void) while (!list_empty(&wwan_hwsim_devs)) { dev = list_first_entry(&wwan_hwsim_devs, struct wwan_hwsim_dev, list); - list_del(&dev->list); + list_del_init(&dev->list); spin_unlock(&wwan_hwsim_devs_lock); wwan_hwsim_dev_del(dev); spin_lock(&wwan_hwsim_devs_lock); @@ -291,6 +463,12 @@ static int __init wwan_hwsim_init(void) if (IS_ERR(wwan_hwsim_class)) return PTR_ERR(wwan_hwsim_class); + wwan_hwsim_debugfs_topdir = debugfs_create_dir("wwan_hwsim", NULL); + wwan_hwsim_debugfs_devcreate = + debugfs_create_file("devcreate", 0200, + wwan_hwsim_debugfs_topdir, NULL, + &wwan_hwsim_debugfs_devcreate_fops); + err = wwan_hwsim_init_devs(); if (err) goto err_clean_devs; @@ -299,6 +477,7 @@ static int __init wwan_hwsim_init(void) err_clean_devs: wwan_hwsim_free_devs(); + debugfs_remove(wwan_hwsim_debugfs_topdir); class_destroy(wwan_hwsim_class); return err; @@ -306,7 +485,10 @@ static int __init wwan_hwsim_init(void) static void __exit wwan_hwsim_exit(void) { + debugfs_remove(wwan_hwsim_debugfs_devcreate); /* Avoid new devs */ wwan_hwsim_free_devs(); + flush_scheduled_work(); /* Wait deletion works completion */ + debugfs_remove(wwan_hwsim_debugfs_topdir); class_destroy(wwan_hwsim_class); } From patchwork Tue Jun 8 04:02:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Ryazanov X-Patchwork-Id: 12305297 X-Patchwork-Delegate: johannes@sipsolutions.net 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.8 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,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 A5117C47082 for ; Tue, 8 Jun 2021 04:04:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8F9DC6120F for ; Tue, 8 Jun 2021 04:04:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230503AbhFHEFz (ORCPT ); Tue, 8 Jun 2021 00:05:55 -0400 Received: from mail-lf1-f45.google.com ([209.85.167.45]:37659 "EHLO mail-lf1-f45.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230364AbhFHEFz (ORCPT ); Tue, 8 Jun 2021 00:05:55 -0400 Received: by mail-lf1-f45.google.com with SMTP id f11so29739329lfq.4; Mon, 07 Jun 2021 21:03:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=2T7mIKspOgOa6iTte+bN4zWzDmpXAFYCXaoc2hPlut8=; b=kTFV8DhKMJsh1Yus1Ow1Db/nNszmSRADk8rAAwc/E2XYFMMmZqRQ2AfF5e/VO8mwJY GZkRjgfKv37q2f8BSobmwrTbWCHdIxBeB5dgUaKPMtQUqTUqO1RmtNI0QcRlguGz5EmW Fd2GF+x0YI6miZQf+gCqc+AUQWhJwCJU6pmaW76DrRbTfhCAfSIP+R/1iiS6zHLq67km PocKRfirjesACDsRD9bQnwZgJ/q8mRhBHv7akoOZMk0mH1HZgKgZ1iFg9VCGx4ITJHEW 8QKtPlZToaW92W4Yt8HRKcyaGJKCv10I7V92lustFmRVI0QifJFXLDZ8UvnmWBdSBQG9 N7rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2T7mIKspOgOa6iTte+bN4zWzDmpXAFYCXaoc2hPlut8=; b=O0k166V0nKBSxAfWdtoKp9bYWC93ufOALJVEq35iKxAo5sbhmy2sp98z/hq2Sy2OYO fhsQTljUBsT8oxK6e4aYMEmfYTZHFnK9F0+gL5yonal84WSmlFnd5Mt4INhs0+6hlaxq chn9wmmuly0btwr3byrafSbYyHxzSpP04WOzCd9+0dzbB/SmCG0Z+5+VZ+5bDp/SWeqV CMa/TnvKhBGyDsvHx69ReG1JyWIWLBLEv7ndOjg/QiPhNnMw4SYXGOCbJ6atuF3ncB+8 5f9Ke0LXkkXV2+fpIP6Ymy4Cm/PkMRRkaH91zFwWg766A4waU1fKm6FQE6un7MfcfEJL AgKQ== X-Gm-Message-State: AOAM5314uqq5er7AGRgHGCcVNfVlJmtXwEaIprwcQj3Ybx7SrDx4MBq5 CkKSQIw5QIhmq0m0161qVizNKPjeFHg= X-Google-Smtp-Source: ABdhPJzXt8hdW4gKAPUGP3uhN1h4UA2IIXI01v30gewJbphkSxI1315OBuOEUukvter7mU7Bi+3xBg== X-Received: by 2002:ac2:4c0a:: with SMTP id t10mr13415850lfq.401.1623124972121; Mon, 07 Jun 2021 21:02:52 -0700 (PDT) Received: from rsa-laptop.internal.lan ([217.25.229.52]) by smtp.gmail.com with ESMTPSA id l23sm1729096lfj.26.2021.06.07.21.02.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Jun 2021 21:02:51 -0700 (PDT) From: Sergey Ryazanov To: Loic Poulain , "David S. Miller" , Jakub Kicinski Cc: netdev@vger.kernel.org, linux-wireless@vger.kernel.org Subject: [PATCH 03/10] net: wwan: make WWAN_PORT_MAX meaning less surprised Date: Tue, 8 Jun 2021 07:02:34 +0300 Message-Id: <20210608040241.10658-4-ryazanov.s.a@gmail.com> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210608040241.10658-1-ryazanov.s.a@gmail.com> References: <20210608040241.10658-1-ryazanov.s.a@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org It is quite unusual when some value can not be equal to a defined range max value. Also most subsystems defines FOO_TYPE_MAX as a maximum valid value. So turn the WAN_PORT_MAX meaning from the number of supported port types to the maximum valid port type. Signed-off-by: Sergey Ryazanov Reviewed-by: Loic Poulain --- drivers/net/wwan/wwan_core.c | 2 +- include/linux/wwan.h | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/drivers/net/wwan/wwan_core.c b/drivers/net/wwan/wwan_core.c index 6e8f19c71a9e..632ff86398ac 100644 --- a/drivers/net/wwan/wwan_core.c +++ b/drivers/net/wwan/wwan_core.c @@ -250,7 +250,7 @@ struct wwan_port *wwan_create_port(struct device *parent, struct wwan_port *port; int minor, err = -ENOMEM; - if (type >= WWAN_PORT_MAX || !ops) + if (type > WWAN_PORT_MAX || !ops) return ERR_PTR(-EINVAL); /* A port is always a child of a WWAN device, retrieve (allocate or diff --git a/include/linux/wwan.h b/include/linux/wwan.h index 7216c114d758..fa33cc16d931 100644 --- a/include/linux/wwan.h +++ b/include/linux/wwan.h @@ -15,8 +15,10 @@ * @WWAN_PORT_QMI: Qcom modem/MSM interface for modem control * @WWAN_PORT_QCDM: Qcom Modem diagnostic interface * @WWAN_PORT_FIREHOSE: XML based command protocol - * @WWAN_PORT_UNKNOWN: Unknown port type - * @WWAN_PORT_MAX: Number of supported port types + * + * @WWAN_PORT_MAX: Highest supported port types + * @WWAN_PORT_UNKNOWN: Special value to indicate an unknown port type + * @__WWAN_PORT_MAX: Internal use */ enum wwan_port_type { WWAN_PORT_AT, @@ -24,8 +26,12 @@ enum wwan_port_type { WWAN_PORT_QMI, WWAN_PORT_QCDM, WWAN_PORT_FIREHOSE, + + /* Add new port types above this line */ + + __WWAN_PORT_MAX, + WWAN_PORT_MAX = __WWAN_PORT_MAX - 1, WWAN_PORT_UNKNOWN, - WWAN_PORT_MAX = WWAN_PORT_UNKNOWN, }; struct wwan_port; From patchwork Tue Jun 8 04:02:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Ryazanov X-Patchwork-Id: 12305293 X-Patchwork-Delegate: johannes@sipsolutions.net 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.8 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,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 358F4C47082 for ; Tue, 8 Jun 2021 04:03:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 151B261249 for ; Tue, 8 Jun 2021 04:03:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230416AbhFHEFq (ORCPT ); Tue, 8 Jun 2021 00:05:46 -0400 Received: from mail-lj1-f178.google.com ([209.85.208.178]:37467 "EHLO mail-lj1-f178.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229457AbhFHEFq (ORCPT ); Tue, 8 Jun 2021 00:05:46 -0400 Received: by mail-lj1-f178.google.com with SMTP id e2so25129057ljk.4; Mon, 07 Jun 2021 21:03:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=pPIdebRJBNO3bofmM1cHoD9YdsleLfcIV4SSBMyfI6g=; b=rg/D2oX5HGQDJEupPTB3i6Eoyd9mDK4JGLBxP4Z6oOUYj0HnOcD/eQQb+m0yJvor4Y FK+3nT5vcPqC7sYCpOa9IOv0sXQy8pAZ+qsMaKR9Oh6S5bzd5laWK0pfBQJFrHVXedF9 KTT3ZIzsgrF1KXmg0p0L+98WQ3/gKh+xL6+4Ci4XjlyvX+efocMrtbJNZyD+ElhThpB7 V7VZw+SfzqKsu/AOAQANYuzFU7bGedhrXZyr4qk9NaME6ymJQ0THGoShkNuhnmxEmG7n HDOb0ICRNNmiGlvC3dqZFMHhGWkGLiEa7f4FwoqKfRDVKVnLZED9NE6ovcls0Yt/SCCA fVfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=pPIdebRJBNO3bofmM1cHoD9YdsleLfcIV4SSBMyfI6g=; b=JX7C0uvwvQ2yirb1Rhm/hTpJ9rg93WTyGUa57Px/2dd6rDOCAe2aLqhlZ6T5OPE7zc WpT5glWgGjm9DBqTFUZ7S7NrRHP0kqdYAgYo3ThCDZHlglgFXMWyVPk6z7zdud34Q60a cW3fZXC0z6JiVYdkCQtvXy3VrtgwprHjw//GQ34v9AKXhJK41tpSZx05omr8wMqrqF8O J4T3BcbXhJocDf+2l3lGoE2Rf/jf6xpSOM8/jhA1KU6XNSJ5bYv3/2RRtKR85kZjncmZ ddVlijbKMXUV0Q+X5TGYUiOLJeFC09eRJRcNrKyi+rxX90evRfJrlgz8SrOGgzOQ0aFC J11g== X-Gm-Message-State: AOAM533jAE6+p8QSZQWCaCTQp193b//6zTRpQefwLRX7JdshBfQ6RAS3 8uBi+eO0Lg5YoDp7wTiGrNw= X-Google-Smtp-Source: ABdhPJxgXAaYKkR7I9XHwAi+zyAKhcNbXXSRMFzBworIkFYk/aAPJ0jL7PAZDHBHXsLqDjC2yq33ZA== X-Received: by 2002:a2e:b4b0:: with SMTP id q16mr16790107ljm.434.1623124973054; Mon, 07 Jun 2021 21:02:53 -0700 (PDT) Received: from rsa-laptop.internal.lan ([217.25.229.52]) by smtp.gmail.com with ESMTPSA id l23sm1729096lfj.26.2021.06.07.21.02.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Jun 2021 21:02:52 -0700 (PDT) From: Sergey Ryazanov To: Loic Poulain , "David S. Miller" , Jakub Kicinski Cc: netdev@vger.kernel.org, linux-wireless@vger.kernel.org Subject: [PATCH 04/10] net: wwan: core: init port type string array using enum values Date: Tue, 8 Jun 2021 07:02:35 +0300 Message-Id: <20210608040241.10658-5-ryazanov.s.a@gmail.com> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210608040241.10658-1-ryazanov.s.a@gmail.com> References: <20210608040241.10658-1-ryazanov.s.a@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org This array is indexed by port type. Make it self-descriptive by using the port type enum values as indices in the array initializer. Signed-off-by: Sergey Ryazanov Reviewed-by: Loic Poulain --- drivers/net/wwan/wwan_core.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/drivers/net/wwan/wwan_core.c b/drivers/net/wwan/wwan_core.c index 632ff86398ac..97d77b06d222 100644 --- a/drivers/net/wwan/wwan_core.c +++ b/drivers/net/wwan/wwan_core.c @@ -184,13 +184,12 @@ static void wwan_remove_dev(struct wwan_device *wwandev) /* ------- WWAN port management ------- */ -/* Keep aligned with wwan_port_type enum */ -static const char * const wwan_port_type_str[] = { - "AT", - "MBIM", - "QMI", - "QCDM", - "FIREHOSE" +static const char * const wwan_port_type_str[WWAN_PORT_MAX + 1] = { + [WWAN_PORT_AT] = "AT", + [WWAN_PORT_MBIM] = "MBIM", + [WWAN_PORT_QMI] = "QMI", + [WWAN_PORT_QCDM] = "QCDM", + [WWAN_PORT_FIREHOSE] = "FIREHOSE", }; static ssize_t type_show(struct device *dev, struct device_attribute *attr, From patchwork Tue Jun 8 04:02:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Ryazanov X-Patchwork-Id: 12305295 X-Patchwork-Delegate: johannes@sipsolutions.net 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.8 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,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 B8090C4743E for ; Tue, 8 Jun 2021 04:03:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A17916120F for ; Tue, 8 Jun 2021 04:03:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230452AbhFHEFr (ORCPT ); Tue, 8 Jun 2021 00:05:47 -0400 Received: from mail-lf1-f51.google.com ([209.85.167.51]:45989 "EHLO mail-lf1-f51.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230364AbhFHEFr (ORCPT ); Tue, 8 Jun 2021 00:05:47 -0400 Received: by mail-lf1-f51.google.com with SMTP id a1so20133886lfr.12; Mon, 07 Jun 2021 21:03:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=1uFD2O0vKXy2Tb2HNN6E4vBvc6S9cY96zFOzzHgNrTM=; b=ekFCy4rTrKxhkCHRrv+Kw1A0n6rHg7Q0KKykF1lDJFFGciPhqnC6b3DD8Fgb+Tx05j jcSRzqSd1ysrJHvXBeBbXBXDwk+xilym3oUs0/8/BEFaXs4FRFGGIMl6o5bNvGXRCWav R7H0EAF/K7lV5kbXx9l8hCNA5zcMdYHvyJS0vnk5JBiN6fMlsEwK3+44hYBKKgmYZ7ta iuyLQ0dT6jLD7+86LLH+lrFFPGmVPJEWRlpaevFMX1dS46oWzIDgdXk/C3tmKn7NGKWn 2YcyyQL4TKH/rcDLbeWGudtNfsZ31ryErzwhagwywKXV2xpXyMfuehanT0n3xi76IAdr nXog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=1uFD2O0vKXy2Tb2HNN6E4vBvc6S9cY96zFOzzHgNrTM=; b=Fq5o+oE7CM2X+O+z/UQ/1VMv8YFuRpKy/fSgTkNLJT5OmTEwISC+brC09dwAZC8hXK y6BAKBFvnK0eYDUqn9ouKwkPQJQ5kvKjHoPe5gNiE5KCMSwlw6Tb+GImZ70Fy8figPTd yc+e9ruOebWtsvEghjW/rmD9UI9e7aEqTQPQ3ivAwqjKsf/U7rkhhxqNmm6go+DjCh+H uyPeO/auUMmRyxaDSzp2pcdI2bh7DY8nWKu+ETjqhg2J87PsacqQzGhsDRV33wWhSxxm uMSOpalDw8UYumNOZwe7PTVRdMr4PLNTB9IaXfydLuHPeJ7yu79UpakThMyCzOK+Doi+ DT4g== X-Gm-Message-State: AOAM532fftXA4DIDEcqOwhOQE6lCYXNh0xrbQPxxjkF+kH16u5twEom5 j2eDESKDGpU8akv25JZQqhc= X-Google-Smtp-Source: ABdhPJxocvEw+Mi3KJ9OSKPUilp799hP5e1cKMh5Y/I0m68CnqNuyvtmyHN7V924o7St9JnOz7eTXg== X-Received: by 2002:a19:4f1a:: with SMTP id d26mr14467136lfb.265.1623124973967; Mon, 07 Jun 2021 21:02:53 -0700 (PDT) Received: from rsa-laptop.internal.lan ([217.25.229.52]) by smtp.gmail.com with ESMTPSA id l23sm1729096lfj.26.2021.06.07.21.02.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Jun 2021 21:02:53 -0700 (PDT) From: Sergey Ryazanov To: Loic Poulain , "David S. Miller" , Jakub Kicinski Cc: netdev@vger.kernel.org, linux-wireless@vger.kernel.org Subject: [PATCH 05/10] net: wwan: core: spell port device name in lowercase Date: Tue, 8 Jun 2021 07:02:36 +0300 Message-Id: <20210608040241.10658-6-ryazanov.s.a@gmail.com> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210608040241.10658-1-ryazanov.s.a@gmail.com> References: <20210608040241.10658-1-ryazanov.s.a@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org Usually a device name is spelled in lowercase, let us follow this practice in the WWAN subsystem as well. The bottom line is that such name is easier to type. To keep the device type attribute contents more natural (i.e., spell abbreviations in uppercase), while making the device name lowercase, turn the port type strings array to an array of structure that contains both the port type name and the device name suffix. Signed-off-by: Sergey Ryazanov Reviewed-by: Loic Poulain --- drivers/net/wwan/wwan_core.c | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/drivers/net/wwan/wwan_core.c b/drivers/net/wwan/wwan_core.c index 97d77b06d222..ba4392d71b80 100644 --- a/drivers/net/wwan/wwan_core.c +++ b/drivers/net/wwan/wwan_core.c @@ -184,12 +184,30 @@ static void wwan_remove_dev(struct wwan_device *wwandev) /* ------- WWAN port management ------- */ -static const char * const wwan_port_type_str[WWAN_PORT_MAX + 1] = { - [WWAN_PORT_AT] = "AT", - [WWAN_PORT_MBIM] = "MBIM", - [WWAN_PORT_QMI] = "QMI", - [WWAN_PORT_QCDM] = "QCDM", - [WWAN_PORT_FIREHOSE] = "FIREHOSE", +static const struct { + const char * const name; /* Port type name */ + const char * const devsuf; /* Port devce name suffix */ +} wwan_port_types[WWAN_PORT_MAX + 1] = { + [WWAN_PORT_AT] = { + .name = "AT", + .devsuf = "at", + }, + [WWAN_PORT_MBIM] = { + .name = "MBIM", + .devsuf = "mbim", + }, + [WWAN_PORT_QMI] = { + .name = "QMI", + .devsuf = "qmi", + }, + [WWAN_PORT_QCDM] = { + .name = "QCDM", + .devsuf = "qcdm", + }, + [WWAN_PORT_FIREHOSE] = { + .name = "FIREHOSE", + .devsuf = "firehose", + }, }; static ssize_t type_show(struct device *dev, struct device_attribute *attr, @@ -197,7 +215,7 @@ static ssize_t type_show(struct device *dev, struct device_attribute *attr, { struct wwan_port *port = to_wwan_port(dev); - return sprintf(buf, "%s\n", wwan_port_type_str[port->type]); + return sprintf(buf, "%s\n", wwan_port_types[port->type].name); } static DEVICE_ATTR_RO(type); @@ -285,7 +303,7 @@ struct wwan_port *wwan_create_port(struct device *parent, /* create unique name based on wwan device id, port index and type */ dev_set_name(&port->dev, "wwan%up%u%s", wwandev->id, atomic_inc_return(&wwandev->port_id), - wwan_port_type_str[port->type]); + wwan_port_types[port->type].devsuf); err = device_register(&port->dev); if (err) From patchwork Tue Jun 8 04:02:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Ryazanov X-Patchwork-Id: 12305287 X-Patchwork-Delegate: johannes@sipsolutions.net 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.8 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,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 B1A9AC47082 for ; Tue, 8 Jun 2021 04:03:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 98BD860FE3 for ; Tue, 8 Jun 2021 04:03:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230267AbhFHEFF (ORCPT ); Tue, 8 Jun 2021 00:05:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34218 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230223AbhFHEFF (ORCPT ); Tue, 8 Jun 2021 00:05:05 -0400 Received: from mail-lj1-x22f.google.com (mail-lj1-x22f.google.com [IPv6:2a00:1450:4864:20::22f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 868F6C061789; Mon, 7 Jun 2021 21:02:56 -0700 (PDT) Received: by mail-lj1-x22f.google.com with SMTP id s22so4688017ljg.5; Mon, 07 Jun 2021 21:02:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=lVnV9bzdy6uuIzlfqAADzTp6JOA579xDrvynPNNeP4g=; b=G3CeIgUYoBiBPFOc2fzqb5S8GPmuhTnsNMc+bJOQ9hQ27dg4qluBT2HOH62hO3BHb6 K2yq0hvG4kb4OsvHCeCGZ3chkFfhv2g1p+zrdq0BdWdfznFfjsy4Ckl++glp38mUASaX guKX7VYqIQFGEvFN+4pv7dfbhxxfxprPBLs8rLOVW8ZIcGNSqoGSUvdgY6EOupTs36RZ FE+lx2/mrT3ZUytfkHpHmY6nIrsi9FW2EMLGWP7eWDtALSoUcAg4O5T/zlMq+sDMle5e UqHHoQ/XYLd1nNnAdPCMvdn48zhcHpMwOVD4O1NIjmxh7gaADMzoZEEjEasJRlocijEj ApSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=lVnV9bzdy6uuIzlfqAADzTp6JOA579xDrvynPNNeP4g=; b=LisP9HE0yNYcZOWRt62OoJmdA8J8ZW28zYhXUh6aoZUMiWRokT4saad3W2jaDxg6e/ NPwhbMBo7RUINOuHtqQCD0FBQtugXHaGeG+7453HkCCZGRJ91/krJCYMrKsUz2Vb9lH7 N0cFznDZ150en2adF6XqJEYhZveX7pkJyP+l5xd5zXPdesmjeAKMcO6YIAGaw9f0zNM5 CViJ6FGycO6Og+UV7gc/OP33TLHRiIg/2ThRfZ2F4WjsLINQ56MrrTop0Rnd1bkRzeBk GnAFoT3+hAphKCEGwClEdIRWobdVQI5CR5dukqbij1IiD3MNyCQWMlXqwKHWVHlO9A4U 3I+w== X-Gm-Message-State: AOAM531tn9C1kJWNX9TwYTN6LdyAvzuUFaDYNG8WRi8UvXJJgOY/lZA0 d5oQ0IFOgTqlieMY+KAcMjc= X-Google-Smtp-Source: ABdhPJxfZhVrT7KLAx9VFuVzf/hRu22kqRSq+2lmO/jC10K3bPeeJp4+qQMrwMuEgouyRND6fsnZAg== X-Received: by 2002:a05:651c:324:: with SMTP id b4mr16941663ljp.166.1623124974910; Mon, 07 Jun 2021 21:02:54 -0700 (PDT) Received: from rsa-laptop.internal.lan ([217.25.229.52]) by smtp.gmail.com with ESMTPSA id l23sm1729096lfj.26.2021.06.07.21.02.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Jun 2021 21:02:54 -0700 (PDT) From: Sergey Ryazanov To: Loic Poulain , "David S. Miller" , Jakub Kicinski Cc: netdev@vger.kernel.org, linux-wireless@vger.kernel.org Subject: [PATCH 06/10] net: wwan: core: make port names more user-friendly Date: Tue, 8 Jun 2021 07:02:37 +0300 Message-Id: <20210608040241.10658-7-ryazanov.s.a@gmail.com> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210608040241.10658-1-ryazanov.s.a@gmail.com> References: <20210608040241.10658-1-ryazanov.s.a@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org At the moment, the port name is allocated based on the parent device name, port id and the port type. Where the port id specifies nothing but the ports registration order and is only used to make the port name unique. Most likely, to configure a WWAN device, the user will look for a port of a specific type (e.g. AT port or MBIM port, etc.). The current naming scheme can make it difficult to find a port of a specific type. Consider a WWAN device that has 3 ports: AT port, MBIM port, and another one AT port. With the global port index, the port names will be: * wwan0p1at * wwan0p2mbim * wwan0p3at To find the MBIM port, user should know in advance the device ports composition (i.e. the user should know that the MBIM port is the 2nd one) or carefully examine the whole ports list. It is not unusual for USB modems to have a different composition, even if they are build on a same chipset. Moreover, some modems able to change the ports composition based on the user's configuration. All this makes port names fully unpredictable. To make naming more user-friendly, remove the global port id and enumerate ports by its type. E.g.: * wwan0p1at -> wwan0at0 * wwan0p2mbim -> wwan0mbim0 * wwan0p3at -> wwan0at1 With this naming scheme, the first AT port name will always be wwanXat0, the first MBIM port name will always be wwanXmbim0, etc. Signed-off-by: Sergey Ryazanov --- drivers/net/wwan/wwan_core.c | 67 ++++++++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 6 deletions(-) diff --git a/drivers/net/wwan/wwan_core.c b/drivers/net/wwan/wwan_core.c index ba4392d71b80..2844b17a724c 100644 --- a/drivers/net/wwan/wwan_core.c +++ b/drivers/net/wwan/wwan_core.c @@ -33,12 +33,10 @@ static int wwan_major; * * @id: WWAN device unique ID. * @dev: Underlying device. - * @port_id: Current available port ID to pick. */ struct wwan_device { unsigned int id; struct device dev; - atomic_t port_id; }; /** @@ -258,6 +256,56 @@ static struct wwan_port *wwan_port_get_by_minor(unsigned int minor) return to_wwan_port(dev); } +/* Allocate and set unique name based on passed format + * + * Name allocation approach is highly inspired by the __dev_alloc_name() + * function. + * + * To avoid names collision, the caller must prevent the new port device + * registration as well as concurrent invocation of this function. + */ +static int __wwan_port_dev_assign_name(struct wwan_port *port, const char *fmt) +{ + struct wwan_device *wwandev = to_wwan_dev(port->dev.parent); + const unsigned int max_ports = PAGE_SIZE * 8; + struct class_dev_iter iter; + unsigned long *idmap; + struct device *dev; + char buf[0x20]; + int id; + + idmap = (unsigned long *)get_zeroed_page(GFP_KERNEL); + if (!idmap) + return -ENOMEM; + + /* Collect ids of same name format ports */ + class_dev_iter_init(&iter, wwan_class, NULL, &wwan_port_dev_type); + while ((dev = class_dev_iter_next(&iter))) { + if (dev->parent != &wwandev->dev) + continue; + if (sscanf(dev_name(dev), fmt, &id) != 1) + continue; + if (id < 0 || id >= max_ports) + continue; + set_bit(id, idmap); + } + class_dev_iter_exit(&iter); + + /* Allocate unique id */ + id = find_first_zero_bit(idmap, max_ports); + free_page((unsigned long)idmap); + + snprintf(buf, sizeof(buf), fmt, id); /* Name generation */ + + dev = device_find_child_by_name(&wwandev->dev, buf); + if (dev) { + put_device(dev); + return -ENFILE; + } + + return dev_set_name(&port->dev, buf); +} + struct wwan_port *wwan_create_port(struct device *parent, enum wwan_port_type type, const struct wwan_port_ops *ops, @@ -266,6 +314,7 @@ struct wwan_port *wwan_create_port(struct device *parent, struct wwan_device *wwandev; struct wwan_port *port; int minor, err = -ENOMEM; + char namefmt[0x20]; if (type > WWAN_PORT_MAX || !ops) return ERR_PTR(-EINVAL); @@ -300,12 +349,18 @@ struct wwan_port *wwan_create_port(struct device *parent, port->dev.devt = MKDEV(wwan_major, minor); dev_set_drvdata(&port->dev, drvdata); - /* create unique name based on wwan device id, port index and type */ - dev_set_name(&port->dev, "wwan%up%u%s", wwandev->id, - atomic_inc_return(&wwandev->port_id), - wwan_port_types[port->type].devsuf); + /* allocate unique name based on wwan device id, port type and number */ + snprintf(namefmt, sizeof(namefmt), "wwan%u%s%%d", wwandev->id, + wwan_port_types[port->type].devsuf); + /* Serialize ports registration */ + mutex_lock(&wwan_register_lock); + + __wwan_port_dev_assign_name(port, namefmt); err = device_register(&port->dev); + + mutex_unlock(&wwan_register_lock); + if (err) goto error_put_device; From patchwork Tue Jun 8 04:02:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Ryazanov X-Patchwork-Id: 12305301 X-Patchwork-Delegate: johannes@sipsolutions.net 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.8 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,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 3A250C4743F for ; Tue, 8 Jun 2021 04:04:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 24E7A60FE3 for ; Tue, 8 Jun 2021 04:04:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230364AbhFHEGA (ORCPT ); Tue, 8 Jun 2021 00:06:00 -0400 Received: from mail-lf1-f44.google.com ([209.85.167.44]:38411 "EHLO mail-lf1-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230481AbhFHEF7 (ORCPT ); Tue, 8 Jun 2021 00:05:59 -0400 Received: by mail-lf1-f44.google.com with SMTP id r5so29919923lfr.5; Mon, 07 Jun 2021 21:03:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=P49wBkaDkamht0wn8kZscQTKx/D9qYCGtYJXiHNCtDE=; b=lcSN+Umc9GgwrvCbBZZQImUXPmJlL17VaxfHR4K0545GcFAqJqCZFOvx5NhwvpxPlB 7wLHuRgmXgHYuK13V9kBt8gBORJrfbb6fVD02PAUi3MgFbSYcATr2uIVlcE7EOB5nSSq sKV/838A7Cr8au9l4K7i0rUGCKizvqcwRrERvZ1mC0vSSQ4MSWeB8zqywwooOLUWRzG/ jgHqEsMUhO6Wqwf4VB8T98nD8pMAfsjWsPcV+jl16MRWBBa45HcwhZH3jKUwOxHHseRr 5AEZHXTL+4U4l/K5cwBsPoOD9J7EIGUIFd3r1dfUtvSKL7EQLZv3iR3sGFLIN7OTPDqO dWKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=P49wBkaDkamht0wn8kZscQTKx/D9qYCGtYJXiHNCtDE=; b=ZcR1iTarjLrpqlIHp5Vzqo36MkQDaV7YeuqR2hakRloOzM0YBsFTZEzAEO9jY43QBm AZYrvgZesLHzY9AEJH0/GQjaQaBzhgtkD+g3Y3Ns5IwOi61FYqnh1tXbGWEnA+auVnnE htlZVOgB3XCIPnEbgl368CgevCOyT8pHgA6V/HxWlUPM/Zy29AgxDU7RtDU9ZkbZ9FjU aKzOdyzaQeKypMTH7fGsQOf41Voi5oh77WQevIOw2dvIM8GGNJW41IFiJuFeRJ12OEvQ 6Bg9oFbVddREzGdlvDefhVsIH0Mnl6tEW/n338X8JQoDB7u936TUwKTEFwlJwnQd6Q7n UR9Q== X-Gm-Message-State: AOAM530iuzEQSiVp97v4Q8x1QR1aq68/dzKJiYRJr3kYnWbZSeTueYVf r/2HAUp9WgXfQpeSAenUwko= X-Google-Smtp-Source: ABdhPJxliUFhZSLoP6zAle7JYsWSmZOfOptpPgsAFeL4YxUS0+fQmNmXUUije4EP8sSgCfUucEw7YQ== X-Received: by 2002:a05:6512:2186:: with SMTP id b6mr13480688lft.490.1623124975933; Mon, 07 Jun 2021 21:02:55 -0700 (PDT) Received: from rsa-laptop.internal.lan ([217.25.229.52]) by smtp.gmail.com with ESMTPSA id l23sm1729096lfj.26.2021.06.07.21.02.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Jun 2021 21:02:55 -0700 (PDT) From: Sergey Ryazanov To: Loic Poulain , "David S. Miller" , Jakub Kicinski Cc: netdev@vger.kernel.org, linux-wireless@vger.kernel.org Subject: [PATCH 07/10] net: wwan: core: expand ports number limit Date: Tue, 8 Jun 2021 07:02:38 +0300 Message-Id: <20210608040241.10658-8-ryazanov.s.a@gmail.com> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210608040241.10658-1-ryazanov.s.a@gmail.com> References: <20210608040241.10658-1-ryazanov.s.a@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org Currently, we limit the total ports number to 256. It is quite common for PBX or SMS gateway to be equipped with a lot of modems. In now days, a modem could have 2-4 control ports or even more, what only accelerates the ports exhausing rate. To avoid facing the port number limitation issue reports, increase the limit up the maximum number of minors (i.e. up to 1 << MINORBITS). Signed-off-by: Sergey Ryazanov Reviewed-by: Loic Poulain --- drivers/net/wwan/wwan_core.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/drivers/net/wwan/wwan_core.c b/drivers/net/wwan/wwan_core.c index 2844b17a724c..9346b2661eb3 100644 --- a/drivers/net/wwan/wwan_core.c +++ b/drivers/net/wwan/wwan_core.c @@ -14,7 +14,8 @@ #include #include -#define WWAN_MAX_MINORS 256 /* 256 minors allowed with register_chrdev() */ +/* Maximum number of minors in use */ +#define WWAN_MAX_MINORS (1 << MINORBITS) static DEFINE_MUTEX(wwan_register_lock); /* WWAN device create|remove lock */ static DEFINE_IDA(minors); /* minors for WWAN port chardevs */ @@ -634,7 +635,8 @@ static int __init wwan_init(void) return PTR_ERR(wwan_class); /* chrdev used for wwan ports */ - wwan_major = register_chrdev(0, "wwan_port", &wwan_port_fops); + wwan_major = __register_chrdev(0, 0, WWAN_MAX_MINORS, "wwan_port", + &wwan_port_fops); if (wwan_major < 0) { class_destroy(wwan_class); return wwan_major; @@ -645,7 +647,7 @@ static int __init wwan_init(void) static void __exit wwan_exit(void) { - unregister_chrdev(wwan_major, "wwan_port"); + __unregister_chrdev(wwan_major, 0, WWAN_MAX_MINORS, "wwan_port"); class_destroy(wwan_class); } From patchwork Tue Jun 8 04:02:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Ryazanov X-Patchwork-Id: 12305289 X-Patchwork-Delegate: johannes@sipsolutions.net 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.8 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,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 3ECCFC4743F for ; Tue, 8 Jun 2021 04:03:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1E2BB6124B for ; Tue, 8 Jun 2021 04:03:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230314AbhFHEFG (ORCPT ); Tue, 8 Jun 2021 00:05:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34234 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230223AbhFHEFG (ORCPT ); Tue, 8 Jun 2021 00:05:06 -0400 Received: from mail-lj1-x234.google.com (mail-lj1-x234.google.com [IPv6:2a00:1450:4864:20::234]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 458A1C06178B; Mon, 7 Jun 2021 21:03:00 -0700 (PDT) Received: by mail-lj1-x234.google.com with SMTP id bn21so25136922ljb.1; Mon, 07 Jun 2021 21:03:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=YmBtymAM7wgwIofW8yEeRoReg1tpJoUaeSkobDLn7ao=; b=XOGTFK/tMSLZL6v16lZUIQSDZWe5b9M4sWftQVzagrB3DYWPYW/hQE5bDkd3KlyhBd fSL10yZS2iw41aZ1Lot3KFmSN0bYCHIc+wh5+n5J6bBV+QgG2nAQrKkWrQtyM5omau+o 7XRPAarOMgGEVbfJFhMxtVWbCIYgKamkYnRnDxlBbuONeSGNhpulqmHqIXsJzwI2ezZg HFD3cMnQYAg6bGDykUhPZuv+xqtxA9wGhJvtkVouccc62xm4K9SJcF5bUzxS/PaZ/6J8 t6LlPjtrn/Wf9E+8sWC4zb5qe69+vurBx6KNHLarAsQ4VGW/ZVe+sTrIE3DPlRZnE7Et V/Sg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=YmBtymAM7wgwIofW8yEeRoReg1tpJoUaeSkobDLn7ao=; b=eEZEbBP+V9TKAc3lGeDwKbxwW3e/Rh77gAfaXV74KK4ZquWr7FuC+VFF+oZaXduVJ0 JNU9WvqmuNySW71zpbtkje0GXoXF8i5xwxdWzwbOzLTarSf1E+AD1AtPwpaR+72HPqqi wOFZwAdlrwh7A2URwVKgvnj9pi/8EoXq209lsjx2iFFMtdWBdFmD+PC7ncJrxxNGjLQL eoBl+deUaFdGouNEttBQIGIEggG4BDAO3bpasMbIBlkARZ1camRAgvM7FyXY5jIXJec3 tRb0Lun/E5j2mrz1OGGObPN3z9axu39pyhWk1KXYkB98jxszh3tHCHTxDFZ2nOAqcRnY FIVw== X-Gm-Message-State: AOAM5310UTLWWqcS2vFJk0K2U40sX8mhtNe1jBpINSLPRy6uYmoF5GIo j59P+kIm6Y3jWVroZ8JEB+Q= X-Google-Smtp-Source: ABdhPJyBbKuxc9FAMrZ38/YD56E1BgF3YXX7mMQSK9xI+gisLAIjScf9eKU7LQrLeIcLW1CANS4gEQ== X-Received: by 2002:a2e:84cc:: with SMTP id q12mr6541116ljh.274.1623124976959; Mon, 07 Jun 2021 21:02:56 -0700 (PDT) Received: from rsa-laptop.internal.lan ([217.25.229.52]) by smtp.gmail.com with ESMTPSA id l23sm1729096lfj.26.2021.06.07.21.02.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Jun 2021 21:02:56 -0700 (PDT) From: Sergey Ryazanov To: Loic Poulain , "David S. Miller" , Jakub Kicinski Cc: netdev@vger.kernel.org, linux-wireless@vger.kernel.org Subject: [PATCH 08/10] net: wwan: core: implement TIOCINQ ioctl Date: Tue, 8 Jun 2021 07:02:39 +0300 Message-Id: <20210608040241.10658-9-ryazanov.s.a@gmail.com> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210608040241.10658-1-ryazanov.s.a@gmail.com> References: <20210608040241.10658-1-ryazanov.s.a@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org It is quite common for a userpace program to fetch the buffered amount of data in the rx queue to avoid the read block. Implement the TIOCINQ ioctl to make the migration to the WWAN port usage smooth. Despite the fact that the read call will return no more data than the size of a first skb in the queue, TIOCINQ returns the entire amount of buffered data (sum of all queued skbs). This is done to prevent the breaking of programs that optimize reading, avoiding it if the buffered amount of data is too small. Signed-off-by: Sergey Ryazanov Reviewed-by: Loic Poulain --- drivers/net/wwan/wwan_core.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/drivers/net/wwan/wwan_core.c b/drivers/net/wwan/wwan_core.c index 9346b2661eb3..d5a197da4a41 100644 --- a/drivers/net/wwan/wwan_core.c +++ b/drivers/net/wwan/wwan_core.c @@ -12,6 +12,7 @@ #include #include #include +#include #include /* Maximum number of minors in use */ @@ -618,6 +619,30 @@ static __poll_t wwan_port_fops_poll(struct file *filp, poll_table *wait) return mask; } +static long wwan_port_fops_ioctl(struct file *filp, unsigned int cmd, + unsigned long arg) +{ + struct wwan_port *port = filp->private_data; + + switch (cmd) { + case TIOCINQ: { /* aka SIOCINQ aka FIONREAD */ + unsigned long flags; + struct sk_buff *skb; + int amount = 0; + + spin_lock_irqsave(&port->rxq.lock, flags); + skb_queue_walk(&port->rxq, skb) + amount += skb->len; + spin_unlock_irqrestore(&port->rxq.lock, flags); + + return put_user(amount, (int __user *)arg); + } + + default: + return -ENOIOCTLCMD; + } +} + static const struct file_operations wwan_port_fops = { .owner = THIS_MODULE, .open = wwan_port_fops_open, @@ -625,6 +650,10 @@ static const struct file_operations wwan_port_fops = { .read = wwan_port_fops_read, .write = wwan_port_fops_write, .poll = wwan_port_fops_poll, + .unlocked_ioctl = wwan_port_fops_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = compat_ptr_ioctl, +#endif .llseek = noop_llseek, }; From patchwork Tue Jun 8 04:02:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Ryazanov X-Patchwork-Id: 12305303 X-Patchwork-Delegate: johannes@sipsolutions.net 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.8 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,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 CCDE8C4743D for ; Tue, 8 Jun 2021 04:04:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BAC9B60FE3 for ; Tue, 8 Jun 2021 04:04:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231217AbhFHEGA (ORCPT ); Tue, 8 Jun 2021 00:06:00 -0400 Received: from mail-lf1-f44.google.com ([209.85.167.44]:42556 "EHLO mail-lf1-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230522AbhFHEF7 (ORCPT ); Tue, 8 Jun 2021 00:05:59 -0400 Received: by mail-lf1-f44.google.com with SMTP id j2so2243755lfg.9; Mon, 07 Jun 2021 21:03:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=eweejLmoSrLMGgxviVt5Oq8o8u/eXalaOHg4faHNMwQ=; b=DEicVTJlL9BJlXrfsbLc8FfsrzgKQgxkUoQ5o1rbmVI3H5agGQujSv0jcl7ut64fJd cv367FiRFk0s39P05eIUsVZTue7u0k9cmoLtsmVqp3zUq0Gi7+PaH9OSNOM1jy3WEVah OgVySP537dX9jqq7DzBQWM6J/uaiKaHk0Qb9KBIWkxReQsLLVBhIOUOdQS/38BWPgXAS D9by5jIy6im5w6CeaxjnQQ4EDteImBKa5Ec+l6nFQRP4FAlE4R+P76kcsFFdww7eSaEy rG6GaS2Zt8Mctcp9AQox91F+Gx5Sf/DxgkJKy+TLBW8Ywu9pIbitj+PcuGodxR71CrZ/ pfiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=eweejLmoSrLMGgxviVt5Oq8o8u/eXalaOHg4faHNMwQ=; b=TSvw63mmogpV6Z2vUXQYLuG7gY71dlYwds3lZH46TsTRkzAokSyMSG/Qs6ib+0+Jv5 wn1JPoR/RMhWqCECCQmDznjPWzUxb2WWT6JhCymBj7iHgbNfggDHNiEtCz6Ibhhsqm9J QezDUiiHtSr++sdNtt+epGVzBNj3DQkIKECFNhz/gPr3EmSdzpqDUlErAXybfQHk5J3t 99wRhBLIomDYN8fuGfWIBCen1CclrKiZs0aq0um/LWEoRBhkvdy+3UdE/JxXMLBR3XgE PdfwPYFJHNPLJN+QWQ9mmBWKFHbpLIbGH38WXwQZjh7apAH21KzuIRxPLcE2ZGSHjEmu ImbQ== X-Gm-Message-State: AOAM530bpuZWJMbLC9vK8/5pmKuMGU5Pfe0RDRdrVEN8S03vssZxJiBu HvR98umXrSIHhwmXBbZ0MSY= X-Google-Smtp-Source: ABdhPJwByhS19BKXJuwk46HkmvNENjteCYJTB4mxLWJk69lD07yMBvJbeiWohHjAmJI1DI/YGkEWow== X-Received: by 2002:a19:ec14:: with SMTP id b20mr14595414lfa.244.1623124977790; Mon, 07 Jun 2021 21:02:57 -0700 (PDT) Received: from rsa-laptop.internal.lan ([217.25.229.52]) by smtp.gmail.com with ESMTPSA id l23sm1729096lfj.26.2021.06.07.21.02.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Jun 2021 21:02:57 -0700 (PDT) From: Sergey Ryazanov To: Loic Poulain , "David S. Miller" , Jakub Kicinski Cc: netdev@vger.kernel.org, linux-wireless@vger.kernel.org Subject: [PATCH 09/10] net: wwan: core: implement terminal ioctls for AT port Date: Tue, 8 Jun 2021 07:02:40 +0300 Message-Id: <20210608040241.10658-10-ryazanov.s.a@gmail.com> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210608040241.10658-1-ryazanov.s.a@gmail.com> References: <20210608040241.10658-1-ryazanov.s.a@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org It is not unreasonable to assume that users will use terminal emulation software to communicate directly with a WWAN device over the AT port. But terminal emulators will refuse to work with a device that does not support terminal IOCTLs (e.g. TCGETS, TCSETS, TIOCMSET, etc.). To make it possible to interact with the WWAN AT port using a terminal emulator, implement a minimal set of terminal IOCTLs. The implementation is rather stub, no passed data are actually used to control a port behaviour. An obtained configuration is kept inside the port structure and returned back by a request. The latter is done to fool a program that will test the configuration status by comparing the readed back data from the device with earlier configured ones. Tested with fresh versions of minicom and picocom terminal apps. MBIM, QMI and other ports for binary protocols can hardly be considered a terminal device, so terminal IOCTLs are only implemented for the AT port. Signed-off-by: Sergey Ryazanov --- drivers/net/wwan/wwan_core.c | 91 ++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/drivers/net/wwan/wwan_core.c b/drivers/net/wwan/wwan_core.c index d5a197da4a41..38da3124d81e 100644 --- a/drivers/net/wwan/wwan_core.c +++ b/drivers/net/wwan/wwan_core.c @@ -51,6 +51,8 @@ struct wwan_device { * @dev: Underlying device * @rxq: Buffer inbound queue * @waitqueue: The waitqueue for port fops (read/write/poll) + * @data_lock: Port specific data access serialization + * @at_data: AT port specific data */ struct wwan_port { enum wwan_port_type type; @@ -61,6 +63,13 @@ struct wwan_port { struct device dev; struct sk_buff_head rxq; wait_queue_head_t waitqueue; + struct mutex data_lock; /* Port specific data access serialization */ + union { + struct { + struct ktermios termios; + int mdmbits; + } at_data; + }; }; static ssize_t index_show(struct device *dev, struct device_attribute *attr, char *buf) @@ -230,6 +239,7 @@ static void wwan_port_destroy(struct device *dev) struct wwan_port *port = to_wwan_port(dev); ida_free(&minors, MINOR(port->dev.devt)); + mutex_destroy(&port->data_lock); skb_queue_purge(&port->rxq); mutex_destroy(&port->ops_lock); kfree(port); @@ -344,6 +354,7 @@ struct wwan_port *wwan_create_port(struct device *parent, mutex_init(&port->ops_lock); skb_queue_head_init(&port->rxq); init_waitqueue_head(&port->waitqueue); + mutex_init(&port->data_lock); port->dev.parent = &wwandev->dev; port->dev.class = wwan_class; @@ -619,10 +630,90 @@ static __poll_t wwan_port_fops_poll(struct file *filp, poll_table *wait) return mask; } +/* Implements minimalistic stub terminal IOCTLs support */ +static long wwan_port_fops_at_ioctl(struct wwan_port *port, unsigned int cmd, + unsigned long arg) +{ + int ret = 0; + + mutex_lock(&port->data_lock); + + switch (cmd) { + case TCFLSH: + break; + + case TCGETS: + if (copy_to_user((void __user *)arg, &port->at_data.termios, + sizeof(struct termios))) + ret = -EFAULT; + break; + + case TCSETS: + case TCSETSW: + case TCSETSF: + if (copy_from_user(&port->at_data.termios, (void __user *)arg, + sizeof(struct termios))) + ret = -EFAULT; + break; + +#ifdef TCGETS2 + case TCGETS2: + if (copy_to_user((void __user *)arg, &port->at_data.termios, + sizeof(struct termios2))) + ret = -EFAULT; + break; + + case TCSETS2: + case TCSETSW2: + case TCSETSF2: + if (copy_from_user(&port->at_data.termios, (void __user *)arg, + sizeof(struct termios2))) + ret = -EFAULT; + break; +#endif + + case TIOCMGET: + ret = put_user(port->at_data.mdmbits, (int __user *)arg); + break; + + case TIOCMSET: + case TIOCMBIC: + case TIOCMBIS: { + int mdmbits; + + if (copy_from_user(&mdmbits, (int __user *)arg, sizeof(int))) { + ret = -EFAULT; + break; + } + if (cmd == TIOCMBIC) + port->at_data.mdmbits &= ~mdmbits; + else if (cmd == TIOCMBIS) + port->at_data.mdmbits |= mdmbits; + else + port->at_data.mdmbits = mdmbits; + break; + } + + default: + ret = -ENOIOCTLCMD; + } + + mutex_unlock(&port->data_lock); + + return ret; +} + static long wwan_port_fops_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) { struct wwan_port *port = filp->private_data; + int res; + + if (port->type == WWAN_PORT_AT) { /* AT port specific IOCTLs */ + res = wwan_port_fops_at_ioctl(port, cmd, arg); + if (res != -ENOIOCTLCMD) + return res; + } switch (cmd) { case TIOCINQ: { /* aka SIOCINQ aka FIONREAD */ From patchwork Tue Jun 8 04:02:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Ryazanov X-Patchwork-Id: 12305291 X-Patchwork-Delegate: johannes@sipsolutions.net 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.8 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,USER_AGENT_GIT autolearn=unavailable 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 4137EC47082 for ; Tue, 8 Jun 2021 04:03:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 29BBC6120F for ; Tue, 8 Jun 2021 04:03:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230365AbhFHEFK (ORCPT ); Tue, 8 Jun 2021 00:05:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34236 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230361AbhFHEFJ (ORCPT ); Tue, 8 Jun 2021 00:05:09 -0400 Received: from mail-lf1-x12f.google.com (mail-lf1-x12f.google.com [IPv6:2a00:1450:4864:20::12f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4E232C061795; Mon, 7 Jun 2021 21:03:00 -0700 (PDT) Received: by mail-lf1-x12f.google.com with SMTP id w33so29887881lfu.7; Mon, 07 Jun 2021 21:03:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=zdX62WlQ3p8rs50GDVVp4jfIL+laVX6G6USVtA2GR9M=; b=g5pN/FchaU0RenbJFPXPQr4TpJMqLe+hFbEu9qXh/uAt5e+kClyVNjTVjWREYIPNTW baPT1dcPq35GRbilSK+H0KT2JmNhDlJAwMrvDgUOJJKWC/ekJv0poNO411+jmOInCjsX ZB6trJXyXofMmSqysjx7z6rtJUFukIu9f7LV7Zk8cqUw59+Yu870Dkc6D78/tBjozX6B LUfPRt6Ue/Wf8jjTTjTX7w8236nLrAlfwsDvyvwQLwzmG1GY2fskV5lhDZuNMApRPOnR L5HN2itKxwXgHjrMrbsmuh8xUbH8yS98FKjo0K/bQdWwHaCoZmFAcbxfzzNHPGqRvaVn 3y+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=zdX62WlQ3p8rs50GDVVp4jfIL+laVX6G6USVtA2GR9M=; b=ASErEg5s4z5q3WOCzufrR6d0vSnPfJpY0srHjc1i/R0xVhIzHJtTcJumEfQOT0ihux FaBDuYvQLWkaXyiAWEPa72l9qTtJXQSnS8vPzugpGChTqv2gWnhrUfCVbOi/Z7TnTsRV C7/luTcoVaU0Kdf6760zzgxuCR6U7K+FtHZQQ7+N6IcWr+pPvDu0nXYEbZ7/XLkhLloL zLZNPytD2nQcJQWZ+IkO6DfBEbIpWUphTO+sSbfTAhQkEbwbWfhIpd7D1oZaqwJSc9LU mPF3B5WmBENK4sXApuWVfQ5YkaNqnzB817Koax4Bag2FCUfqqTAMMQYB0xFRBcdUOJcO AJqA== X-Gm-Message-State: AOAM531eH0UuIPj01N9OcZiqzmJFIkYBVIFKpSbk0jRQpiobSZ3kfMYh ep7IM4UoQy6l8lPOfQZceh8= X-Google-Smtp-Source: ABdhPJw6NARPj59vStBB/DjqY4pVBcOetCaXqKqJozj4vaPv9wTVOhKCG9WnSAWBL4PXtRp00klsjg== X-Received: by 2002:a19:7909:: with SMTP id u9mr9815144lfc.13.1623124978631; Mon, 07 Jun 2021 21:02:58 -0700 (PDT) Received: from rsa-laptop.internal.lan ([217.25.229.52]) by smtp.gmail.com with ESMTPSA id l23sm1729096lfj.26.2021.06.07.21.02.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Jun 2021 21:02:58 -0700 (PDT) From: Sergey Ryazanov To: Loic Poulain , "David S. Miller" , Jakub Kicinski Cc: netdev@vger.kernel.org, linux-wireless@vger.kernel.org Subject: [PATCH 10/10] net: wwan: core: purge rx queue on port close Date: Tue, 8 Jun 2021 07:02:41 +0300 Message-Id: <20210608040241.10658-11-ryazanov.s.a@gmail.com> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210608040241.10658-1-ryazanov.s.a@gmail.com> References: <20210608040241.10658-1-ryazanov.s.a@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org Purge the rx queue as soon as a user closes the port, just after the port stop callback invocation. This is to prevent feeding a user that will open the port next time with outdated and possibly unrelated data. While at it also remove the odd skb_queue_purge() call in the port device destroy callback. The queue will be purged just before the callback is ivoncated in the wwan_remove_port() function. Signed-off-by: Sergey Ryazanov --- drivers/net/wwan/wwan_core.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/drivers/net/wwan/wwan_core.c b/drivers/net/wwan/wwan_core.c index 38da3124d81e..45a41aee8958 100644 --- a/drivers/net/wwan/wwan_core.c +++ b/drivers/net/wwan/wwan_core.c @@ -240,7 +240,6 @@ static void wwan_port_destroy(struct device *dev) ida_free(&minors, MINOR(port->dev.devt)); mutex_destroy(&port->data_lock); - skb_queue_purge(&port->rxq); mutex_destroy(&port->ops_lock); kfree(port); } @@ -462,8 +461,11 @@ static void wwan_port_op_stop(struct wwan_port *port) { mutex_lock(&port->ops_lock); port->start_count--; - if (port->ops && !port->start_count) - port->ops->stop(port); + if (!port->start_count) { + if (port->ops) + port->ops->stop(port); + skb_queue_purge(&port->rxq); + } mutex_unlock(&port->ops_lock); }