Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp3046714yba; Sun, 28 Apr 2019 15:50:14 -0700 (PDT) X-Google-Smtp-Source: APXvYqyZjcheVdq/MEGmXtq0KKikivoXKyYPwhdqJgAwhPFiFnC1YurbrL5n+4+cJmtc6/1IDgcw X-Received: by 2002:a17:902:20c6:: with SMTP id v6mr55977814plg.276.1556491814867; Sun, 28 Apr 2019 15:50:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556491814; cv=none; d=google.com; s=arc-20160816; b=x4HSrdJUIRDd9O51xAt/XIIfzGuhCf1P8aiJQTM4NPCdNu6rMAjut1Fm54dXspIKM0 5rd9HvFqkHog2lpt2faGW/mAIHVma0zxywKtJZkfg/NWH9+9LxechZbnylHlqm6UAvpo 10XVcWeaY751E8bl29dqbasI2gjUU55emMNr7Uk4qahfiLBtvwiN3RJIZk6I+XCO0/j2 pwZ8QaWFU7b0/klCw7/9XwPqg81dYm9Kqx2rZ6j6g8rFM7ycb4iF780LP4zf0BX0dTwm UkOiyfRi4aBySPrWZHoNve3FdL5i7lXkhyTZzVMKPS0Ef7QLmJkm+kP8fWC02wgkooeb N32g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:references :in-reply-to:subject:cc:date:to:from; bh=9ak4G/tT3qSpILTo14sOPx348gtvZ25q8V8tcpYYCYk=; b=E39OWb9nD38Kxv3tHm30n1QcjpldtkpUJ7cgUKy53rezzLuU3Er9R2JcCD3WjOPoVe J2sWYBVXI6NZzj6+aMYGdeRPnYfM9kC4GQ1XIfuQ7jxx0rWHtXk+xNl/UVUdb0ofHK7t 3T2Aqiug0qUL9jLem2+RlWJd67ZlQIP0xXVnbUi9cRjTIbBQMgQbsa40kzrpgsjyRT0L GQjGjHXesisuRu6VOvJumqFSXsuGCaEOxkCZM2tbaxuzcAIZWgboE1vzvfhEJB8i4pBP e67NK6qw3GqcTxaXVdXQl5dM7kQiKWuMHrwz/cZNDQI6gbwsdtXAWPr4dBw5piZGRERL 0GEg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d6si5698847pgk.129.2019.04.28.15.49.59; Sun, 28 Apr 2019 15:50:14 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726728AbfD1WrO (ORCPT + 99 others); Sun, 28 Apr 2019 18:47:14 -0400 Received: from mx2.suse.de ([195.135.220.15]:59782 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726393AbfD1WrN (ORCPT ); Sun, 28 Apr 2019 18:47:13 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id AAD92AE16; Sun, 28 Apr 2019 22:47:11 +0000 (UTC) From: NeilBrown To: Lukas Bulwahn , Shaohua Li , Arnd Bergmann , Himanshu Jha Date: Mon, 29 Apr 2019 08:47:02 +1000 Cc: clang-built-linux@googlegroups.com, linux-raid@vger.kernel.org, linux-kernel@vger.kernel.org, Lukas Bulwahn Subject: Re: [PATCH] md: properly lock and unlock in rdev_attr_store() In-Reply-To: <20190428104041.11262-1-lukas.bulwahn@gmail.com> References: <20190428104041.11262-1-lukas.bulwahn@gmail.com> Message-ID: <877ebd693t.fsf@notabene.neil.brown.name> MIME-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha256; protocol="application/pgp-signature" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --=-=-= Content-Type: text/plain Content-Transfer-Encoding: quoted-printable On Sun, Apr 28 2019, Lukas Bulwahn wrote: > rdev_attr_store() should lock and unlock mddev->reconfig_mutex in a > balanced way with mddev_lock() and mddev_unlock(). It does. > > But when rdev->mddev is NULL, rdev_attr_store() would try to unlock > without locking before. Resolve this locking issue.. This is incorrect. > > This locking issue was detected with Clang Thread Safety Analyser: Either the Clang Thread Safety Analyser is broken, or you used it incorrectly. > > drivers/md/md.c:3393:3: warning: releasing mutex 'mddev->reconfig_mutex' = that was not held [-Wthread-safety-analysis] > mddev_unlock(mddev); > ^ > > This warning was reported after annotating mutex functions and > mddev_lock() and mddev_unlock(). > > Fixes: 27c529bb8e90 ("md: lock access to rdev attributes properly") > Link: https://groups.google.com/d/topic/clang-built-linux/CvBiiQLB0H4/dis= cussion > Signed-off-by: Lukas Bulwahn > --- > Arnd, Neil, here a proposal to fix lock and unlocking asymmetry. > > I quite sure that if mddev is NULL, it should just return. If mddev is NULL, the code does return (with -EBUSY). All you've done is change things so it returns from a different part of the code. You haven't changed the behaviour at all. > > I am still puzzled if the return value from mddev_lock() should be really > return by rdev_attr_store() when it is not 0. But that was the behaviour > before, so I will keep it that way. Certainly it should. mddev_lock() either returns 0 to indicate success or -EINTR if it received a signal. If it was interrupted by a signal, then rdev_attr_store() should return =2DEINTR as well. As Arnd tried to explain, the only possible problem here is that the C compiler is allowed to assume that rdev->mddev never changes value, so in rv =3D mddev ? mddev_lock(mddev) : =3DEBUSY it could load rdev->mddev, test if it is NULL, then load it again and pass that value to mddev_lock() - the new value might be NULL which would cause problems. This could be fixed by changing struct mddev *mddev =3D rdev->mddev; to struct mddev *mddev =3D READ_ONCE(rdev->mddev); That is the only change that might be useful here. NeilBrown > > drivers/md/md.c | 4 +++- > 1 file changed, 3 insertions(+), 1 deletion(-) > > diff --git a/drivers/md/md.c b/drivers/md/md.c > index 05ffffb8b769..a9735d8f1e70 100644 > --- a/drivers/md/md.c > +++ b/drivers/md/md.c > @@ -3384,7 +3384,9 @@ rdev_attr_store(struct kobject *kobj, struct attrib= ute *attr, > return -EIO; > if (!capable(CAP_SYS_ADMIN)) > return -EACCES; > - rv =3D mddev ? mddev_lock(mddev): -EBUSY; > + if (!mddev) > + return -EBUSY; > + rv =3D mddev_lock(mddev); > if (!rv) { > if (rdev->mddev =3D=3D NULL) > rv =3D -EBUSY; > --=20 > 2.17.1 --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEG8Yp69OQ2HB7X0l6Oeye3VZigbkFAlzGLWYACgkQOeye3VZi gbkBWw//VvIj0hYlq+pmpcJXle/rFe9qJHKo9IgWyI3BO8tZ/T4qaUQN3YFBvXtd OxduNlDF3F+T3o/0GzQXPHCHxq/WHHR9Ju/m9DpZoa7jFAGzdSaZYoJENM4dkZhj 4CBqZ87ZDw3zU2YNrOYSMioU9W1yTKuVO3GNR917sMNDxJZyN/0gRh/as7UckBgz M/Ki41vyLUDdJKS4wx2nJKl9A1nULqknO4dfQlxIybSj68LtGHeTpJzn6bAKFTWa YKI9AcsBz0fonw/I6MRunP3yXy4DI7IqW7jY0ZY+5hyETw4knmlQl+vqa6Sh9AIC UPTkypaeWWTJ1jN/mFPR8gWv78dUUrIpDLGDghYWwLIivHC0Q9qCvk75KqkPF1Ii fQg/GGNAtdp0cAhWGiPc0o3XC7Q1+gkPsdDdxHKMq2L0HxKLWEKPFwV6D2w8CTTB HIgXFbfcb7uQGfByjLQfBT/O90D5v7qDrlRlNcw0sF6nFzBaAJJCSX86fWIPrSo2 OQ37gahhy9Af1oC19hXeM37sny7WMRsFTfM4AJ8rNuXQBdjKAcrM9RO0WSFeeheR ZQoK4SooN6pBits+PxKB1QDc/xCC+AiV64TQ5tqCiKPJ7uw1Uuz8kmpsdPVZ0H0E G8XdolzszybZ3AoQ6yai0ls0eHeCC/R7rbpfhhg3D/BVJCZaIZU= =tVLb -----END PGP SIGNATURE----- --=-=-=--