From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (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 803C33D6694 for ; Wed, 8 Apr 2026 18:36:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775673419; cv=none; b=Q4LXLk6e8RG9egsCG9yppW91+LspSMgDcubwGA+4zGqE5xYRO6refAb51IDrRHhuSrwGuajIH+mKlIQQkG961wcdlRJeUUNyEilXdxvMIzIce/gCVHDbGlC71grEgxeU4M2blL1LXWRdn7uHf9B4XtdLS9e+MwVellQAG+k6PXE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775673419; c=relaxed/simple; bh=W8QG6v4YVpjutuslb8+RqIsE3hZUYeq5LedpBI5iww8=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Ao6tqNIs91ayYsvHSqE5o5K79hhRG6XAWcq/L4QLjINifn3+57wh7Yw+PkpFa6sMV2ZEcDFdT+rf2hduSdrKdu8WCzoRoyFPGPFmA3nbmdcWLwFv516dCbEbNZ7p3Cy3nr3N/AuvqjYWjpzGlBk3NfeGm6UFWS5fkWobqe8hpAg= 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=KIt+oPoE; arc=none smtp.client-ip=209.85.210.180 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="KIt+oPoE" Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-82a7539851fso42781b3a.1 for ; Wed, 08 Apr 2026 11:36:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775673418; x=1776278218; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=hmHgdAG7IFH0XG56/s4FKP3nrKHnj+wgIcVKXbdVdsE=; b=KIt+oPoEhKaUzZnstzc3HBjIdFkwitc/cPpMeW1+z9mCiK/DVK0Nd6+e2z1iMOFkoO YYN/dLBD8Ze87dW7TgiHUt/CtK2878psEGU0BmDM/05rc8QNz6P4ZAfPqOcn5/petMrB UqEAOC0D5qYr4uQxBrDZq4X0NjPzyYf6Ihg86ZvZnFf5GxAcSssciy6SNzh4IL/b0KqM PbZA9rm/ml/cy0WKGxQpSppoZpddzRmC1ftgAK89d8ckg2V2gOuchzgMZ9r67t5iswSw 39Fj/G3m2+hiNVSdmvmcX10OR8m2pIIyvuSgZ9nedOLcNpe4zXFfaAfGC9mkw7MosHAx XVDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775673418; x=1776278218; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hmHgdAG7IFH0XG56/s4FKP3nrKHnj+wgIcVKXbdVdsE=; b=goJHs6FShDuQDrB/xO33D3WTrmfaMYd5uwM2VeqX7iCCFJXG5ukWS4Byi9/GHUZWmy hVHfADTCy6pswdPbup6uA6a2bnlVUnXxi/xcLO1GvTNj2fqa7hKUdZizLxLEZ1cUgWC5 eXn1tY9UnF3cWU9NAY8vZaEoSJlmtLbI8UortPRkuZE2s9C9GFJoPShzpa3uZvSXV1Li FPE/ZStBqIZtFCTUcck1NMmaa01gRl8zRhngeKLEwfXAyc1IoLiWauDfq1EdrHyQuhIc U3FXZTj6QS2WQfDoOB4cqykRC8zbUMruJrv6ce+lJDeUuO9pBGq/Gr9J0hARgImPaVlw zNow== X-Forwarded-Encrypted: i=1; AJvYcCWHBcVxoo/atW4R4pwCK0X7A5oAGW4GgaKzCrV1Wkw+t2Sca0t/ML/tYi6XdD8yeOSTqrMitsK58fezYBs=@vger.kernel.org X-Gm-Message-State: AOJu0YysfeHAC37k9kUonv1dvBI/b5mzK+NAe7VyJbXmnGVkyoV5xn+7 jNt2BFk+aOwy3TY+JHxbPx0FgE5sWi8c59OK5baJH2iNCUJ9Q0qLY+ADoTupMA== X-Gm-Gg: AeBDiess7yhERq7x2hJVyFfTdfjDC5vZ5lqsDzeywnuMCq+NbJvVBc+zt6oOZurbtVh jiFh4K5DbpI3WaHIp5Gmmj/+5dxfx1FNBOFkUUgb1SfehmrUPkueBpzHUaqcBFwjZ5LZgqEL1vN yAVovsD5/VqZPL85z7Rfz10N6BoIPGpkkY6JAWnUTIikCfV/aK5XIP6zsd18uI1CHKhc/9pn4Ti EM3R+ZhAI5otjSg7vfPLomzYfzkQ69p4+jdF2T/BIOOYAlJ8kwWnCONXjGZF9z7fHCg2/8RAjbM BeWKDZE4xX2rUAijgyri107GId3hvHqFs5MDMcuTV4ul+Utgz2wYeMgE4OADZNK0xCmVlTwU0wR Z50gRIQGihis7+iOx6npqx9WB3kHh0ysuXPX8fjD4dRBzlbwrqwQl8lUio1r1D8XnIuzfZKz337 hMke1GSKYl9KlVZx23vYcnfEq7dfwW6XzqEKYQf28GiZKAsXD3eXX44zNmNMczXqNg X-Received: by 2002:a05:7022:784:b0:128:d51a:5161 with SMTP id a92af1059eb24-12bfb76020cmr11560232c88.27.1775673417664; Wed, 08 Apr 2026 11:36:57 -0700 (PDT) Received: from google.com ([2a00:79e0:2ebe:8:f3cf:7538:b120:7924]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-12bfea5f860sm20588627c88.2.2026.04.08.11.36.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Apr 2026 11:36:57 -0700 (PDT) Date: Wed, 8 Apr 2026 11:36:54 -0700 From: Dmitry Torokhov To: Mohamad Raizudeen Cc: kees@kernel.org, linux-kernel@vger.kernel.org, linux-input@vger.kernel.org Subject: Re: [PATCH] Input: serio - fix O(n^2) complexity in serio_unregister_driver() Message-ID: References: <20260408162849.4639-1-raizudeen.kerneldev@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Wed, Apr 08, 2026 at 11:49:48PM +0530, Mohamad Raizudeen wrote: > On Wed, Apr 08, 2026 at 10:56:26AM -0700, Dmitry Torokhov wrote: > > On Wed, Apr 08, 2026 at 11:21:30PM +0530, Mohamad Raizudeen wrote: > > > Hi Dmitry, > > > > Please do not top-post. > > > > > > > > *We do not have such setups at the moment, but what about parent's parent's > > > parent?* > > > You are right. Even though we don't have such setups today, let me explain > > > why the patch works for arbitrary depth. > > > > > > If we have three ports linked like A->B->C (A is top, B is child of A, C is > > > child of B) and all use the same driver. > > > > What happens if B uses different driver from A? > > > > > > > > C sees its parent B is using the same driver, skip C > > > B sees its parent A is using the same driver, skip B > > > A has no parent using the same driver, collect A > > > > > > When we disconnect A, it automatically destroys B and C. So all ports are > > > cleaned up. The logic works for any number of levels. > > > > > > * Could you explain more about the use-after-free scenario?* > > > If we collected both A and B, disconnecting A would free B. Then when we > > > try to process B from the list, we would use memory that is already freed > > > that leads to crash. My patch avoids this by never collecting a port whose > > > parent is also using the same driver. > > > > But currently we restart scanning the list, so there won't be any stale > > entries. How would we end up with touching freed memory? > > > > Thanks. > > > > -- > > Dmitry > > Thank you for your careful review and for pointing out the mixed > driver nestingscenario (A bound to driver X, B bound to driver Y, C > bound to driver X). I completely missed that case. You are right my My > patch would collect both A and C, then disconnecting A would detroy B > and C, leading to a use-after-free when C is later processed from the > temporary list. The original goto approach handles this correctly by > restarting the scan. > > I am sorry for sending a flawed patch. I will withdraw it. > > I will try to deisign a better solution that works for all cases, > includes mixed driver nesting, before submitting again. Appreciate you looking at the code but I do not think that particular area needs addressing. If you would like to improve the subsystem I would recommend you look into making serio rely on the asynchronous driver probing, removing custom handling of async port registration. This would allow serio_register_port() to return errors and clean up bunch of things. This will complicate attempt to synchronously switch serio protocols, but I am willing to let go of that feature as I do not think anyone is actually using this. Thanks. -- Dmitry