From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754695AbcE3Koz (ORCPT ); Mon, 30 May 2016 06:44:55 -0400 Received: from metis.ext.4.pengutronix.de ([92.198.50.35]:37915 "EHLO metis.ext.4.pengutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754541AbcE3Kox (ORCPT ); Mon, 30 May 2016 06:44:53 -0400 From: Markus Pargmann To: "Pranay Kr. Srivastava" Cc: nbd-general@lists.sourceforge.net, linux-kernel@vger.kernel.org Subject: Re: [PATCH 3/4] make nbd device wait for its users. Date: Mon, 30 May 2016 12:44:50 +0200 Message-ID: <16646016.gAJCF3ZoFN@adelgunde> User-Agent: KMail/4.14.1 (Linux/4.5.0-0.bpo.2-amd64; KDE/4.14.2; x86_64; ; ) In-Reply-To: <1464089188-6155-4-git-send-email-pranjas@gmail.com> References: <1464089188-6155-1-git-send-email-pranjas@gmail.com> <1464089188-6155-4-git-send-email-pranjas@gmail.com> MIME-Version: 1.0 Content-Type: multipart/signed; boundary="nextPart3566293.han17kFZ1F"; micalg="pgp-sha256"; protocol="application/pgp-signature" X-SA-Exim-Connect-IP: 2001:67c:670:100:a61f:72ff:fe68:75ba X-SA-Exim-Mail-From: mpa@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --nextPart3566293.han17kFZ1F Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="us-ascii" Hi, sorry I couldn't fit the review into last week. On Tuesday 24 May 2016 14:26:27 Pranay Kr. Srivastava wrote: > When a timeout occurs or a recv fails, then > instead of abruplty killing nbd block device > wait for it's users to finish. >=20 > This is more required when filesystem(s) like > ext2 or ext3 don't expect their buffer heads to > disappear while the filesystem is mounted. >=20 > Use a kref for users using this. The device will > be released for kref count of 2, not less or more. >=20 > Signed-off-by: Pranay Kr. Srivastava > --- > drivers/block/nbd.c | 51 +++++++++++++++++++++++++++++++++++++++++= ++++++++++ > 1 file changed, 51 insertions(+) >=20 > diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c > index af86c9b..59db890 100644 > --- a/drivers/block/nbd.c > +++ b/drivers/block/nbd.c > @@ -71,6 +71,8 @@ struct nbd_device { > =09struct dentry *dbg_dir; > #endif > =09struct work_struct ws_nbd; > +=09struct kref users; > +=09struct completion user_completion; > }; > =20 > #if IS_ENABLED(CONFIG_DEBUG_FS) > @@ -674,6 +676,7 @@ static void nbd_reset(struct nbd_device *nbd) > =09nbd->flags =3D 0; > =09nbd->xmit_timeout =3D 0; > =09INIT_WORK(&nbd->ws_nbd, nbd_work_func); > +=09init_completion(&nbd->user_completion); > =09queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue); > =09del_timer_sync(&nbd->timeout_timer); > } > @@ -807,6 +810,7 @@ static int __nbd_ioctl(struct block_device *bdev,= struct nbd_device *nbd, > =09=09kthread_stop(thread); > =20 > =09=09sock_shutdown(nbd); > +=09=09wait_for_completion(&nbd->user_completion); > =09=09mutex_lock(&nbd->tx_lock); > =09=09nbd_clear_que(nbd); > =09=09kill_bdev(bdev); > @@ -858,12 +862,58 @@ static int nbd_ioctl(struct block_device *bdev,= fmode_t mode, > =09return error; > } > =20 > +static void nbd_kref_release(struct kref *kref_users) > +{ > +=09struct nbd_device *nbd =3D container_of(kref_users, struct nbd_de= vice, > +=09=09=09users); > +=09pr_debug("Releasing kref [%s]\n", __FUNCTION__); > +=09complete(&nbd->user_completion); > + > +} > + > +static int nbd_open(struct block_device *bdev, fmode_t mode) > +{ > +=09struct nbd_device *nbd_dev =3D bdev->bd_disk->private_data; > + > +=09kref_get(&nbd_dev->users); > +=09pr_debug("Opening nbd_dev %s. Active users =3D %u\n", > +=09=09=09bdev->bd_disk->disk_name, > +=09=09=09atomic_read(&nbd_dev->users.refcount) - 1); > +=09return 0; > +} > + > +static void nbd_release(struct gendisk *disk, fmode_t mode) > +{ > +=09struct nbd_device *nbd_dev =3D disk->private_data; > +=09/* > +=09*kref_init initializes ref count to 1, so we > +=09*we check for refcount to be 2 for a final put. > +=09* > +=09*kref needs to be re-initialized just here as the > +=09*other process holding it must see the ref count as 2. > +=09*/ > +=09kref_put(&nbd_dev->users, nbd_kref_release); > + > +=09if (atomic_read(&nbd_dev->users.refcount) =3D=3D 2) { > +=09=09kref_sub(&nbd_dev->users, 2, nbd_kref_release); > +=09=09kref_init(&nbd_dev->users); > +=09=09kref_get(&nbd_dev->users); Reading the refcount directly seems not to be as it supposed to be. Why don't you put a kref_init() and kref_put() call into NBD_DO_IT? Thi= s way you don't have to work around the property that kref_init() starts with a refcount of 1 but you can use it. For example: =09NBD_DO_IT: =09=09kref_init() =09=09... =09=09kref_put() =09nbd_thread_recv() and nbd_thread_send(): =09=09kref_get() =09=09... =09=09kref_put() =09In nbd_open() you could use kref_get_unless_zero() to avoid =09opening a not connected device. =09nbd_release() would then be a very simple kref_put() without =09checking for 2 and so on. Also there are some checkpatch issues with this patch. Best Regards, Markus > +=09} > + > +=09pr_debug("Closing nbd_dev %s. Active users =3D %u\n", > +=09=09=09disk->disk_name, > +=09=09=09atomic_read(&nbd_dev->users.refcount) - 1); > +} > + > static const struct block_device_operations nbd_fops =3D { > =09.owner =3D=09THIS_MODULE, > =09.ioctl =3D=09nbd_ioctl, > =09.compat_ioctl =3D=09nbd_ioctl, > +=09.open =3D =09nbd_open, > +=09.release =3D =09nbd_release > }; > =20 > + > static void nbd_work_func(struct work_struct *ws_nbd) > { > =09struct nbd_device *nbd_dev =3D container_of(ws_nbd, struct nbd_de= vice, > @@ -1098,6 +1148,7 @@ static int __init nbd_init(void) > =09=09disk->first_minor =3D i << part_shift; > =09=09disk->fops =3D &nbd_fops; > =09=09disk->private_data =3D &nbd_dev[i]; > +=09=09kref_init(&nbd_dev[i].users); > =09=09sprintf(disk->disk_name, "nbd%d", i); > =09=09nbd_reset(&nbd_dev[i]); > =09=09add_disk(disk); >=20 =2D-=20 Pengutronix e.K. | = | Industrial Linux Solutions | http://www.pengutronix.de/= | Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 = | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-555= 5 | --nextPart3566293.han17kFZ1F Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part. Content-Transfer-Encoding: 7Bit -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQIcBAABCAAGBQJXTBmiAAoJEEpcgKtcEGQQ/aUP/iRg5KkUGQfnCuEVy8WRDk0q mfUf1D5MmwKD41WNqP56MV/+DJlDUEWPSzqPTP9YaiBjUA2nW9CElMT3xsOKZUK9 tMMJQFKzNPTk4w6A6js7LQ5b8UeZlvXoKl/chv5DXJXj9I8hbYe97K/xK50ZySDA sUQubT/3cFaLsQp/tayxLQkL96I5hAf0GqQ1PMFp3J5sOWxELaELhSkMz2XB3IFv EnSsImXm3z1Qwwx/r4uNLRZyJnjcwqP8j8jynONRPDwJNI1sOlo+O+QFZj930G6p ozXNLnZSJ4XLY03FViS5VfItAlYOBBeDLAB/OMsqhbBS87NzNqSzG5JWvFXdGKJX cL0fhKQ0fjKJXXuf2qDvoga8aKGXel21Alv0NFDGuy1+sQ/K0bz6Gi0tZ3/+LU0t hpNbBnw8gOX4/2DjJtlqk/aZw01bJERk45KM5sVOz3Iaq78Hd4zZ+aRJdXN0JIv6 jWRQTZCXTjBlumgl63iML7A6sHFwxBuFDOVgezt/DmQF+pDFjHEWUH/eN8Lk3/S3 9ReE/5RwkF6zj2qmbADW7SmPMibLGUJCxI8niFiQ/bKBr6aZqTEHc6/2BVj6tjqb 1WeLAKDy1/72WzH4ty66MwvXUvIbfsZqfawLt5V5MVdqGhMqjfCDy96FUX88Rfqi qNUsqOGdAYxwLV7hL2mP =PRp8 -----END PGP SIGNATURE----- --nextPart3566293.han17kFZ1F--