Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp854916pxk; Thu, 17 Sep 2020 19:11:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxsOgUL55ch9dL+WNJMgQmY5yYuUe1s2yPOii/nycas7rZAIH2y1hrtgHsauEjZ/dMVbgCS X-Received: by 2002:a05:6402:1818:: with SMTP id g24mr35486191edy.332.1600395117281; Thu, 17 Sep 2020 19:11:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600395117; cv=none; d=google.com; s=arc-20160816; b=nDsSxRGqt+X+vKliExxvdwraqMfE8E6OtAkNGnoCwK5qI8eyvRy2ysoGNrLcJ3bTov +g+xcgd7FoLe1lvqsueLuGinCetUfy5YW+EQEAWGMtwn1Un+k4bfrpK/qx+0jjd4acFx pzbOKVLo8D9RI7BWXyMU2hAsIda8cFXDBYgnSKr1C8/yhM0bDwBjKkZHAoEf+jF/aLS3 ORxg9l6lTKCxYp6fNxb4q7QNzAzl9SFoSpK2tvPoFXy08kgj5IKz47Tigopm6DADoWt5 m4IXHAy5cGFpIRzpVnafbsb4QPXoQ/0iW3WcJaMRzwv2nx5/HHQ5VnX0fI2ns/yr1UEF r4uA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=8pWzYI9/QEGOhCkOAVTP5vb7htAHtU5DvrWQmH3RRQA=; b=KPdXYPLqf9t0Z97abmOPuwPMHC81VLfBUe+U3oTlPVJxUrH37i061m/dtDzl6zd08A V0KnWrtlGC05nIT7xLbDIEs7sxtG+iwMFsEyiRREfGRkPTMoRa0CwOs6Ll50ZjuBKneu GdkVO5PItUPRM/KA5ttILzjHHT344TD5Dy/iOorXXUtP5WaGJ98WkFdkxDGSvTzEVWqA ko1WOB7nglS/Bc15LeyIsmsMMcyFFrvRfYOnVlbKv9pL7Ffq6YSJu83LOrzHAZFz+wWq ZKznIPYUDcAw5p7oGeB5Cu+Xq9yp6XAOkUyNa/T1db++N2E/ZXiJO2BrcVEf0NC8Epxz Y73Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=L3JwIMig; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f2si1117443edn.26.2020.09.17.19.11.34; Thu, 17 Sep 2020 19:11:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=L3JwIMig; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727967AbgIRCHm (ORCPT + 99 others); Thu, 17 Sep 2020 22:07:42 -0400 Received: from mail.kernel.org ([198.145.29.99]:57930 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727954AbgIRCHe (ORCPT ); Thu, 17 Sep 2020 22:07:34 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 4404323770; Fri, 18 Sep 2020 02:07:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600394853; bh=t/dL6LPjtS1TrQtU8/Vc04Fb463C66wB6aGtkDlVgYg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=L3JwIMigzWLgS9l3eA36aJ3Vw4zbWR81Q41AKl6r28wS4KspXjT8VVEM7B4KM2heo s9gg57b0TiZIAFol5HrdHxIphHbWgPlLJDoO8OaubXDauhh3E1jFecJgtymmAEKGNY BszDDpG68QaH2XQDwTwqBD/dFwW+1NmO+AgLf0wA= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Madhuparna Bhowmik , Andrew Morton , Alexandre Bounine , Matt Porter , Dan Carpenter , Mike Marshall , Thomas Gleixner , Ira Weiny , Allison Randal , Pavel Andrianov , Linus Torvalds , Sasha Levin Subject: [PATCH AUTOSEL 5.4 308/330] rapidio: avoid data race between file operation callbacks and mport_cdev_add(). Date: Thu, 17 Sep 2020 22:00:48 -0400 Message-Id: <20200918020110.2063155-308-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200918020110.2063155-1-sashal@kernel.org> References: <20200918020110.2063155-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Madhuparna Bhowmik [ Upstream commit e1c3cdb26ab881b77486dc50370356a349077c74 ] Fields of md(mport_dev) are set after cdev_device_add(). However, the file operation callbacks can be called after cdev_device_add() and therefore accesses to fields of md in the callbacks can race with the rest of the mport_cdev_add() function. One such example is INIT_LIST_HEAD(&md->portwrites) in mport_cdev_add(), the list is initialised after cdev_device_add(). This can race with list_add_tail(&pw_filter->md_node,&md->portwrites) in rio_mport_add_pw_filter() which is called by unlocked_ioctl. To avoid such data races use cdev_device_add() after initializing md. Found by Linux Driver Verification project (linuxtesting.org). Signed-off-by: Madhuparna Bhowmik Signed-off-by: Andrew Morton Acked-by: Alexandre Bounine Cc: Matt Porter Cc: Dan Carpenter Cc: Mike Marshall Cc: Thomas Gleixner Cc: Ira Weiny Cc: Allison Randal Cc: Pavel Andrianov Link: http://lkml.kernel.org/r/20200426112950.1803-1-madhuparnabhowmik10@gmail.com Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- drivers/rapidio/devices/rio_mport_cdev.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/drivers/rapidio/devices/rio_mport_cdev.c b/drivers/rapidio/devices/rio_mport_cdev.c index 10af330153b5e..0b85a80ae7ef6 100644 --- a/drivers/rapidio/devices/rio_mport_cdev.c +++ b/drivers/rapidio/devices/rio_mport_cdev.c @@ -2384,13 +2384,6 @@ static struct mport_dev *mport_cdev_add(struct rio_mport *mport) cdev_init(&md->cdev, &mport_fops); md->cdev.owner = THIS_MODULE; - ret = cdev_device_add(&md->cdev, &md->dev); - if (ret) { - rmcd_error("Failed to register mport %d (err=%d)", - mport->id, ret); - goto err_cdev; - } - INIT_LIST_HEAD(&md->doorbells); spin_lock_init(&md->db_lock); INIT_LIST_HEAD(&md->portwrites); @@ -2410,6 +2403,13 @@ static struct mport_dev *mport_cdev_add(struct rio_mport *mport) #else md->properties.transfer_mode |= RIO_TRANSFER_MODE_TRANSFER; #endif + + ret = cdev_device_add(&md->cdev, &md->dev); + if (ret) { + rmcd_error("Failed to register mport %d (err=%d)", + mport->id, ret); + goto err_cdev; + } ret = rio_query_mport(mport, &attr); if (!ret) { md->properties.flags = attr.flags; -- 2.25.1