Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp1975944ybz; Sun, 26 Apr 2020 08:22:18 -0700 (PDT) X-Google-Smtp-Source: APiQypLSLXj3Iy2UTzkj/roHDHGNGPD73eKzfqFJhpyvKz55m1AWUhMpTdeftD/D1NQBDx6/XDMF X-Received: by 2002:a17:906:4810:: with SMTP id w16mr15308437ejq.226.1587914538083; Sun, 26 Apr 2020 08:22:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587914538; cv=none; d=google.com; s=arc-20160816; b=uQYowJlE1IU4lld4EmqZauzKuI0cthqgrz96w7Uug1TMKL+xJ33/n+hrB+rOQ/iVGJ slMmW2mhyoJABnhYTOOVj8odyJQ7z5tv58y5S6RqUeIyS3jaBFzdE8uuUmxg/BmId65O fxxh7xWApJwEYjBtn1N1NwAQMZVbdIjHN5ngQnfoNd2ljWVe9EEj4hamHR1K7z25hOBJ 5wIacFt+h+3i0qOpbMOZdKhuLesVOln0KEDxeP+KjRJqWPp537mu+UxHDG75CJZckauZ G0Qi5cjIedMvRONPvtDaTpQTGgjzvqJOGzaNCXX5HMJzceEldUami2nNNzAf4DnAseKR wupA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-language :content-transfer-encoding:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=8ZWSGdX2c877cgHrdn+1pgMsM7ftlK0aUwzgQsojEoM=; b=JRZTGFM8EnZo4E4K3SUW2NI3ZBks/CpnwX5nj7Bw6ivR4HgFy/Jc/DY6gWXkygJdmh Y46izooG3s3keT0NnXg1FgVYcpSco7OQDjMKCH5CAfvcQT9WKURYCE3ZLHmMhwITPnSa QVzs4i3Q3Dz+I9ZNyafgGjkZ+/kJYvJN0glrWTsakHCk4w+4oSxC7b5/qn1NBN7d4lGs wa9fZHyu0xySALxrmndvXaIahO4IkhWUeTXMZxNBJVCANgOg3asYbmxQZXSIY0yZW4JM pGwIPVcrTOxsq7u9KnLptWfOIFNMpD8UBLwtEsNsvRPjjE1NVjVmlYpzpcOYGMGji3Ji xx+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Bq6h4Tyz; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id cm13si6198277edb.48.2020.04.26.08.21.52; Sun, 26 Apr 2020 08:22:18 -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=@gmail.com header.s=20161025 header.b=Bq6h4Tyz; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726155AbgDZPTy (ORCPT + 99 others); Sun, 26 Apr 2020 11:19:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44670 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725975AbgDZPTx (ORCPT ); Sun, 26 Apr 2020 11:19:53 -0400 Received: from mail-qt1-x841.google.com (mail-qt1-x841.google.com [IPv6:2607:f8b0:4864:20::841]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C2271C061A0F for ; Sun, 26 Apr 2020 08:19:53 -0700 (PDT) Received: by mail-qt1-x841.google.com with SMTP id b1so8521988qtt.1 for ; Sun, 26 Apr 2020 08:19:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding:content-language; bh=8ZWSGdX2c877cgHrdn+1pgMsM7ftlK0aUwzgQsojEoM=; b=Bq6h4TyzEtquKkwmgwsPqGZ4Y94CMA78QaeUILL+aY4WYaxGGhcIMYfXw96HmEJTE3 YhO204RJ8WhOTcxlqp1hRH2ivEfiPR+1ZyzuuRP4lwM06pRM1crEKNjcpxPv/LcbVusF G9ubRNrM193gsQzOouDyWbC3huFhcnRlGbKJKReNqSc0AG94lU/RM5dBK+cgN95+uIA4 GUlQiEnCwG/RTo5bEJWZb3h0Js0/eiGlxpwp8OT5JC+dWQ9VOhLpSxzKFhPz4/YyXF8o /uCTNyLh7AcRJ7uvLJptwqWrwDbyK6bT1XUHy023ZYZEfiggqyOJyPYxdFyqkprtk1mk PGHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=8ZWSGdX2c877cgHrdn+1pgMsM7ftlK0aUwzgQsojEoM=; b=WTFg1m3WV8UZgh3yDreI8MmHmJPcjUVVw3wgcX0r6SoBoIK/zUzTOTbHtyTjpXhRaM T2kwHNa9yT3gHgLUbtQulcVupT3PBvgub3BMcoouGosYkQ44mWWsFtbGxKkWCmcC/2/R 0cfr/t0xosyVjGYb+omWq+G45k2O+0GY9A3fDMO5uDwmcuonijecrFO25+/t+hG3wHTt IADEokTdCxpcZGHSonsycsfWmQf1IUZ+EYUXf/Q4JyTyq6eW4pju+Vglm3x/30iBMY1j VvQtwjgEebxrB3qo+TroLew3Zu5G+hLIgkctwJFgtPCCbxoVf0+GXPWKQfYJDQzcwaWU Cdwg== X-Gm-Message-State: AGi0PuaAMG62148+RMtO31gwqPTOYNq1zJOIKwvA3FXRiI4CUuxT6lTE 0SOXD5FFJYkUhJVLd81RK3/BLkDD X-Received: by 2002:aed:2ce6:: with SMTP id g93mr19899873qtd.264.1587914392813; Sun, 26 Apr 2020 08:19:52 -0700 (PDT) Received: from [10.0.2.15] (CPEac9e17937810-CM64777dd8e660.cpe.net.cable.rogers.com. [174.112.203.120]) by smtp.gmail.com with ESMTPSA id x55sm8115275qtk.3.2020.04.26.08.19.51 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 26 Apr 2020 08:19:52 -0700 (PDT) Subject: Re: [PATCH] rapidio: Avoid data race between file operation callbacks and mport_cdev_add(). To: madhuparnabhowmik10@gmail.com, mporter@kernel.crashing.org, akpm@linux-foundation.org, dan.carpenter@oracle.com, hubcap@omnibond.com, tglx@linutronix.de, ira.weiny@intel.com, allison@lohutok.net Cc: linux-kernel@vger.kernel.org, andrianov@ispras.ru, ldv-project@linuxtesting.org References: <20200426112950.1803-1-madhuparnabhowmik10@gmail.com> From: "alex.b" Message-ID: <2a0d3c96-c332-2c95-db10-ffba78f9c9a3@gmail.com> Date: Sun, 26 Apr 2020 11:19:51 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 In-Reply-To: <20200426112950.1803-1-madhuparnabhowmik10@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2020-04-26 7:29 a.m., madhuparnabhowmik10@gmail.com wrote: > From: Madhuparna Bhowmik > > 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 > --- > 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 8155f59ece38..40296378e5ee 100644 > --- a/drivers/rapidio/devices/rio_mport_cdev.c > +++ b/drivers/rapidio/devices/rio_mport_cdev.c > @@ -2379,13 +2379,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); > @@ -2405,6 +2398,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; Acked-by: Alexandre Bounine