From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ed1-f41.google.com (mail-ed1-f41.google.com [209.85.208.41]) (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 C13123FD121 for ; Wed, 5 Aug 2026 09:35:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785922544; cv=none; b=Nd/amFbOCeKwr7qB2DIITRItsELBJNRChBU/2j/rfJd0r/shRRfu0WoSC5BKKwzerzRx/IOIS+pKZCvSVSxWU5DtaiHMaJeDQLpTitKLde41rru8krPsWrqfFzQVlQF6KOWwWRRvdiCwcLD3inagd7oTNIM9Xu3yGdEDpIj3+vA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785922544; c=relaxed/simple; bh=q66HLdCYKRRsu+CZElgAerKAoYzQDJBRy2q/4u8Fk/g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=SQlrOppg6DXP73XW30tzLMh0bEKcZ7NsvdIdVUekStrzcefrk01HMqKVwEfrM9/mix2MRyGUx3PbDZbzpkshEMK+nDfer47f+IVrrerx6qWMG9biO879r3x+dd8VUZ0DiQbEr9Xf3M8xIwKeajGhaVuQnmeUK4qglF2JhR9uJyw= 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=pjfkSHl1; arc=none smtp.client-ip=209.85.208.41 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="pjfkSHl1" Received: by mail-ed1-f41.google.com with SMTP id 4fb4d7f45d1cf-69f7fa1c548so1125635a12.2 for ; Wed, 05 Aug 2026 02:35:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1785922541; x=1786527341; 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=Nxq12XQVNJ86CkZORzZuuH+9O2BmrlK1pCWISZ3LSdw=; b=pjfkSHl1k8bzL35jk7EJwmya5IdFfRndrxo578MjWWo/vmsiIc3/LeZLJja3C1J3nM AZBhXi0r+X/08OchrSpNBb4YTJO4MrZDB2dGcWrl5MzHFxg/6mF3CpDdeFQTbjv5CERs aq3iX2NmCRRtZhnXmUT1xfIAN5rnr7HMW5628xztv66dNjzbiMlSOBvhPzb/VnGo4WUw gzgIVDSb5ySyNQYRyxITepQyx4ufjtfF5AORiuoDPZtvzyi+0UPRcDLkd1K0lGWAwhWy objtKI8yeH+SVFVuExRfb9vDNyGYysdq+D1xs/Rp/ywHOS8pbub8os+elfMKM2I/0xsG EV+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785922541; x=1786527341; 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=Nxq12XQVNJ86CkZORzZuuH+9O2BmrlK1pCWISZ3LSdw=; b=DLkTDx/LtOeZsp6n8MPyL1xJ/5a0IsTkPLDNLZ51E71zEjSlDjvha11m9LsPinKbY+ zmQ7MuLw/fk82W3TbTNRTzx14aL9HbTVMXEK1jHY+scrSuRtqQp0OpbsOF/7+zUeDI/7 90C+oNXp3bKbNJ9GvDFSXuzYDLIUEyXsPlWvqaLHBYXF21Pgj+36DqAW+sx+Qn7eLH36 kaAK23czb8bDnaTqKBToR/Z44F2MwkLTCOpof0WgFjosPqKdhxGAmalAgOR8wiWhKCrZ U6DWcdSIc+cbDNyfSpLHMdXH2jBEITtRjZvaf0glDFCJqhSKp8vStnKsvAKMUcIebJzQ NbCg== X-Forwarded-Encrypted: i=1; AHgh+RonqophAcUae16dHhRgjxdt+MnoT6QUo/aKPqpP+QVNgZrzZIontS7tVL0xfLonaoDxfNv5XBYwW8KlF98=@vger.kernel.org X-Gm-Message-State: AOJu0YxuV8/I5C9LdBam5nWJE7J85LD+OgzHDCrNa3gGaWYIdtg2i0oB q6bczmjlC1hq23ZeF0tyKfBWzTZD7R8Z/BZ3GnVmxE4CkBouY2kK7ObG X-Gm-Gg: AR+sD12+4A55JCQC9xUx0NjzXZa3/fLAWhOi+ch4xBS7imquY9cm/NwwCp8IzslXijl 8MlzFDRGIaaLkn/Kk77aw9OnO9LssDt8PlgsZR3mdB7URZJlJ0AL/POHS8l+Mt2aVSaQwCd2+Ij dNj8wTlYlR3Qy48f5DKe7nOHzFjGAMJyLwxc9Ub0kYl8QYt3z7TWHEc8T+OFmz3o9Fs+RbUdpkY MwD42Ro6i4O1CovT16oalzIKUoRDOrB1QkTpWUrNqHQIjEq45ELWw4VUH7cri8GtfnAkSoQMyRn vRnylvotIlI6mw1MnQ6E6I3JhLU+82XAo97HNRYPj0dPUtLx1xvq9vYrM2dr9Rd8uET7WSicPEa oTfR/JDEkTiCowkRZup+oy5KGVc5DEclWriqF8NeE4xVP46t9qXuxibs7qFpAW/xuTcLMqTgMY8 L5wbAY9Sbc2CcdTuvwi5Q9OoEjKBJcBpcychq/o6MbK/196wHJFX0WCl2sdhnuUG6ntu65z431y 7UV0gpcSMow8IpM X-Received: by 2002:a05:6402:4559:b0:698:b6c8:70db with SMTP id 4fb4d7f45d1cf-6a14f0b916bmr2198132a12.8.1785922540868; Wed, 05 Aug 2026 02:35:40 -0700 (PDT) Received: from localhost (c-85-228-45-68.bbcust.telenor.se. [85.228.45.68]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-6a1453d3825sm1729158a12.0.2026.08.05.02.35.39 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 05 Aug 2026 02:35:40 -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 v4 1/7] char: xillybus: Improve control of execution flow with mutexes Date: Wed, 5 Aug 2026 11:34:30 +0200 Message-Id: <20260805093436.59740-2-eli.billauer@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20260805093436.59740-1-eli.billauer@gmail.com> References: <20260805093436.59740-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: ========= 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