From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ej1-f42.google.com (mail-ej1-f42.google.com [209.85.218.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B73FE42D76C for ; Wed, 5 Aug 2026 11:13:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.42 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785928438; cv=none; b=ImY4BqJZJL5sFg+aXjW4xo/NOr1uWuMy0F6Ndkxkp8BAXYIP3BW5cBDbUSlUDXkCPnA5FIr9zGi4ry1JbOLmr4FiB3UVoVgsHthCS1+a+zZ0tXbP28uDo1MGMRNtYOcDjueChkyEQ0mOeBKz4jEFpEfNao3TQkXmQd/JtFYovfo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785928438; c=relaxed/simple; bh=vFw3z/0ga7sdQjaC0XyqfiOT1pcWYRGLUTotaySJ1yo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=gVn+nJg2LZ/ACF34WvTthIOwjFmlAcpzaSZzRlZE2gWPwaDmyOa14kVOaYNa9F7s2gqfmDmL/r9kro+C+j835fR7tX5/tEvQ9x4IbvoiZfCMWFAg6V6LTF08rsIgHXhXihjJGBXSA/0uCH3gSePop6K4JaqVYAX6a++tjLP0ugo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=bbhTZm9J; arc=none smtp.client-ip=209.85.218.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="bbhTZm9J" Received: by mail-ej1-f42.google.com with SMTP id a640c23a62f3a-c15cd3fd760so103773866b.2 for ; Wed, 05 Aug 2026 04:13:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1785928435; x=1786533235; darn=vger.kernel.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to:content-type; bh=xQRw7gsO2UeKw/N04jyZ2vRCrKCyzvuIcQFmZJeWnLc=; b=bbhTZm9JJGN1hq/kn8a0dXjAjAChfmqGs/82ek3IgU+GjunyIYer3HgieNPuYudP4u q8LGiZw3vGRHTnSwlHEYkqbagSFQ/TfsGgOB0nDgIVWQSCIfF0Y3l8D87g1SBeHXumUh TuRU6qPXGvr2mLqOcsdsgYgto+8dxgD5FsoSJiX0cp6OjsRxDGFNGRTBSzYvr79LXmNt idcTexCFDsNIT6pPUBCz/DtT78JVPIrifOSAkwDi6wgrglCp8S4RSaF3QHTofAskcANG aASMSQ9sLkq0M/OnN6NukVVKXJlGK5L4xsTo22QHLEY5dPmCK8oPpfG9x1WLUwg/xNmL eILg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785928435; x=1786533235; h=references:in-reply-to:message-id:date:subject:cc:to:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=xQRw7gsO2UeKw/N04jyZ2vRCrKCyzvuIcQFmZJeWnLc=; b=PnPWJHv9yqnPUOI3Oc7rQ2tF0WYW3qVhLoqFHrveya6T7rNAvBWK5DcTd2eyXkYdNo b95MzvCY3TOlTtqcuBoL2NbmvPFSWzCqmmI77iNSuZ5DA5MOEKQH9c7JMwLDylsttrXD RG8ouqT6O4Vz27OjNPFugREeH7O3uAssEXaEkub+dYMRq/AVCqBnRywtwsx26UuFgbbO 2iwPVlfOwV7wEWr/dsWbqTgOAQbmfWpMjOVej0+seaj2RRoR00EalT6n79FT2lmvglSY nGu8MWMA6v6EYVp9Sq29sVYqtpOF41GkYtvdyqebdTRopiEzxCJfkG7EnWiTO3OcF7l4 SuUw== X-Forwarded-Encrypted: i=1; AHgh+Rppu9DV2cv87E2mJsnFG9Szf9PddMEsyKggxO4nK7/fKL/k6B20zMRXIYj2RvaEeT/sMW+H68FW0pUJhR8=@vger.kernel.org X-Gm-Message-State: AOJu0YwJ1SGpfHP3bW5gWxAcDtsgUtI/P7WlBEwVJ5zoDZT6+7WtTsUG NzeWTSbwff8a14BWPDvxTKrxAi/Hf3+KCSlp6+5GIuQpVnH1LljY2lOMuCGEejuT X-Gm-Gg: AR+sD12gMDTAB/JlAMvFci6/g+hfoWtYZPWmGQQY2UgYyb3K5o0BUJWyQqeRr42e18n LUpN2SFEpiCTMLAjdqRi/wh+veP7XuQVylg39jI5y3SJBEy+wPi4TXf06e1NkFAg4Ln+eNQMwV3 0SJnZbGgkyF2wBQYYJZHVOyMSbGFs84c5M71r4lFve0LFHwV05qyqcj1FVFJIgLFGcCQWhjxy1O F3Oa8NZq52SJrmNqzk04pQOW79kZc33Ok4/Xr5E8otCNlprdQWG8pGYkoFpXEPm0Vu9oO2bXCQ/ 5X8IOYITUAD+3272rIh0J81RUdI0CBQU78d+1hoMwxu1rpD2IwptWihQSRB8IIZzlnWXDuion8G JGaDKuSqmSeUSCUmz5MNYd9fi7+jIzHzbd9KuWcwtqaax75Ml1l8A+97hEs+xjNwXX3/wBcw236 B4kksOUmLq+BjBNNX2U4+iRFhB22YPLFcvAN16gZ5IHg9QQAnMM24aQkF06aYXXts5noN5c9tSb KRh/F4n0qTPE+7Q X-Received: by 2002:a17:907:9616:b0:c1f:922d:34c3 with SMTP id a640c23a62f3a-c2039b8150fmr253371666b.14.1785928434640; Wed, 05 Aug 2026 04:13:54 -0700 (PDT) Received: from localhost (c-85-228-45-68.bbcust.telenor.se. [85.228.45.68]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-c2053debd30sm4590566b.7.2026.08.05.04.13.53 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 05 Aug 2026 04:13:53 -0700 (PDT) From: Eli Billauer To: gregkh@linuxfoundation.org Cc: arnd@arndb.de, linux-kernel@vger.kernel.org, corbet@lwn.net, Eli Billauer Subject: [PATCH v5 1/7] char: xillybus: Improve control of execution flow with mutexes Date: Wed, 5 Aug 2026 13:13:31 +0200 Message-Id: <20260805111337.69178-2-eli.billauer@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20260805111337.69178-1-eli.billauer@gmail.com> References: <20260805111337.69178-1-eli.billauer@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: This commit addresses two issues by using mutexes: (1) Add a mutex to protect the fifo_buf_order global variable. The purpose of this variable is avoid repeated failed calls to __get_free_pages() for allocating FIFO memory, when the chunk size was too big. However, if two drivers are initialized at the same time, fifo_init() may run in parallel, and fifo_buf_order may be reduced too much. This is a far-fetched scenario, now completely prevented by fifo_buf_order_mutex. (2) setup_channels() acquires process_in_mutex to prevent process_bulk_in() from accessing the xillyusb_dev struct. With correctly working hardware, process_bulk_in() is never called while setup_channels() runs, because the device has no reason to send data in that phase. The mutex ensures that process_bulk_in() does not touch the members that setup_channels() alters. There is no similar protection for data flow in the other direction, because during the setup process, the only outbound data is the BULK endpoint used for commands, and it remains untouched after its initial setup. Assisted-by: Deepseek:v4-pro Kimi:K2.6 ChatGPT:GPT-5.5 Claude:Sonnet-4.6 Assisted-by: Sashiko-0.2.5:gemini-3.1-pro-preview Signed-off-by: Eli Billauer --- Notes: Changelog: ========= No change on v4->v5. Changes v3->v4: -- Use plain mutex_lock() in fifo_init() rather than guard() in order to avoid mixing guard() with goto, following Sashiko's remark + add attribution to Sashiko for this. Changes v2->v3: -- Add Assisted-by tag to description No change on v1->v2. drivers/char/xillybus/xillyusb.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/drivers/char/xillybus/xillyusb.c b/drivers/char/xillybus/xillyusb.c index 34e7ad3bcab3..560c3568fc96 100644 --- a/drivers/char/xillybus/xillyusb.c +++ b/drivers/char/xillybus/xillyusb.c @@ -50,6 +50,7 @@ MODULE_LICENSE("GPL v2"); static const char xillyname[] = "xillyusb"; static unsigned int fifo_buf_order; +static DEFINE_MUTEX(fifo_buf_order_mutex); static struct workqueue_struct *wakeup_wq; #define USB_VENDOR_ID_XILINX 0x03fd @@ -375,6 +376,8 @@ static int fifo_init(struct xillyfifo *fifo, unsigned int log2_fifo_buf_size; + mutex_lock(&fifo_buf_order_mutex); + retry: log2_fifo_buf_size = fifo_buf_order + PAGE_SHIFT; @@ -395,8 +398,10 @@ static int fifo_init(struct xillyfifo *fifo, fifo->mem = kmalloc_array(fifo->bufnum, sizeof(void *), GFP_KERNEL); - if (!fifo->mem) + if (!fifo->mem) { + mutex_unlock(&fifo_buf_order_mutex); return -ENOMEM; + } for (i = 0; i < fifo->bufnum; i++) { fifo->mem[i] = (void *) @@ -413,6 +418,8 @@ static int fifo_init(struct xillyfifo *fifo, fifo->writebuf = 0; spin_lock_init(&fifo->lock); init_waitqueue_head(&fifo->waitq); + + mutex_unlock(&fifo_buf_order_mutex); return 0; memfail: @@ -426,6 +433,7 @@ static int fifo_init(struct xillyfifo *fifo, fifo_buf_order--; goto retry; } else { + mutex_unlock(&fifo_buf_order_mutex); return -ENOMEM; } } @@ -1943,6 +1951,9 @@ static int setup_channels(struct xillyusb_dev *xdev, struct xillyusb_channel *chan, *new_channels; int i; + /* Don't let process_bulk_in() run while we change the channels */ + guard(mutex)(&xdev->process_in_mutex); + chan = kzalloc_objs(*chan, num_channels); if (!chan) return -ENOMEM; -- 2.34.1