Return-Path: Received: from mail-sn1nam01on0137.outbound.protection.outlook.com ([104.47.32.137]:51749 "EHLO NAM01-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S933500AbeCCWaz (ORCPT ); Sat, 3 Mar 2018 17:30:55 -0500 From: Sasha Levin To: "linux-kernel@vger.kernel.org" , "stable@vger.kernel.org" CC: Davidlohr Bueso , Davidlohr Bueso , "Paul E . McKenney" , Sasha Levin Subject: [PATCH AUTOSEL for 4.14 81/84] locking/locktorture: Fix num reader/writer corner cases Date: Sat, 3 Mar 2018 22:26:47 +0000 Message-ID: <20180303222518.26271-81-alexander.levin@microsoft.com> References: <20180303222518.26271-1-alexander.levin@microsoft.com> In-Reply-To: <20180303222518.26271-1-alexander.levin@microsoft.com> Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org List-ID: From: Davidlohr Bueso [ Upstream commit 2ce77d16db4240dd2e422fc0a5c26d3e2ec03446 ] Things can explode for locktorture if the user does combinations of nwriters_stress=3D0 nreaders_stress=3D0. Fix this by not assuming we always want to torture writer threads. Reported-by: Jeremy Linton Signed-off-by: Davidlohr Bueso Signed-off-by: Paul E. McKenney Reviewed-by: Jeremy Linton Tested-by: Jeremy Linton Signed-off-by: Sasha Levin --- kernel/locking/locktorture.c | 76 +++++++++++++++++++++++++---------------= ---- 1 file changed, 44 insertions(+), 32 deletions(-) diff --git a/kernel/locking/locktorture.c b/kernel/locking/locktorture.c index f24582d4dad3..6dca260eeccf 100644 --- a/kernel/locking/locktorture.c +++ b/kernel/locking/locktorture.c @@ -715,8 +715,7 @@ static void __torture_print_stats(char *page, { bool fail =3D 0; int i, n_stress; - long max =3D 0; - long min =3D statp[0].n_lock_acquired; + long max =3D 0, min =3D statp ? statp[0].n_lock_acquired : 0; long long sum =3D 0; =20 n_stress =3D write ? cxt.nrealwriters_stress : cxt.nrealreaders_stress; @@ -823,7 +822,7 @@ static void lock_torture_cleanup(void) * such, only perform the underlying torture-specific cleanups, * and avoid anything related to locktorture. */ - if (!cxt.lwsa) + if (!cxt.lwsa && !cxt.lrsa) goto end; =20 if (writer_tasks) { @@ -898,6 +897,13 @@ static int __init lock_torture_init(void) firsterr =3D -EINVAL; goto unwind; } + + if (nwriters_stress =3D=3D 0 && nreaders_stress =3D=3D 0) { + pr_alert("lock-torture: must run at least one locking thread\n"); + firsterr =3D -EINVAL; + goto unwind; + } + if (cxt.cur_ops->init) cxt.cur_ops->init(); =20 @@ -921,17 +927,19 @@ static int __init lock_torture_init(void) #endif =20 /* Initialize the statistics so that each run gets its own numbers. */ + if (nwriters_stress) { + lock_is_write_held =3D 0; + cxt.lwsa =3D kmalloc(sizeof(*cxt.lwsa) * cxt.nrealwriters_stress, GFP_KE= RNEL); + if (cxt.lwsa =3D=3D NULL) { + VERBOSE_TOROUT_STRING("cxt.lwsa: Out of memory"); + firsterr =3D -ENOMEM; + goto unwind; + } =20 - lock_is_write_held =3D 0; - cxt.lwsa =3D kmalloc(sizeof(*cxt.lwsa) * cxt.nrealwriters_stress, GFP_KER= NEL); - if (cxt.lwsa =3D=3D NULL) { - VERBOSE_TOROUT_STRING("cxt.lwsa: Out of memory"); - firsterr =3D -ENOMEM; - goto unwind; - } - for (i =3D 0; i < cxt.nrealwriters_stress; i++) { - cxt.lwsa[i].n_lock_fail =3D 0; - cxt.lwsa[i].n_lock_acquired =3D 0; + for (i =3D 0; i < cxt.nrealwriters_stress; i++) { + cxt.lwsa[i].n_lock_fail =3D 0; + cxt.lwsa[i].n_lock_acquired =3D 0; + } } =20 if (cxt.cur_ops->readlock) { @@ -948,19 +956,21 @@ static int __init lock_torture_init(void) cxt.nrealreaders_stress =3D cxt.nrealwriters_stress; } =20 - lock_is_read_held =3D 0; - cxt.lrsa =3D kmalloc(sizeof(*cxt.lrsa) * cxt.nrealreaders_stress, GFP_KE= RNEL); - if (cxt.lrsa =3D=3D NULL) { - VERBOSE_TOROUT_STRING("cxt.lrsa: Out of memory"); - firsterr =3D -ENOMEM; - kfree(cxt.lwsa); - cxt.lwsa =3D NULL; - goto unwind; - } - - for (i =3D 0; i < cxt.nrealreaders_stress; i++) { - cxt.lrsa[i].n_lock_fail =3D 0; - cxt.lrsa[i].n_lock_acquired =3D 0; + if (nreaders_stress) { + lock_is_read_held =3D 0; + cxt.lrsa =3D kmalloc(sizeof(*cxt.lrsa) * cxt.nrealreaders_stress, GFP_K= ERNEL); + if (cxt.lrsa =3D=3D NULL) { + VERBOSE_TOROUT_STRING("cxt.lrsa: Out of memory"); + firsterr =3D -ENOMEM; + kfree(cxt.lwsa); + cxt.lwsa =3D NULL; + goto unwind; + } + + for (i =3D 0; i < cxt.nrealreaders_stress; i++) { + cxt.lrsa[i].n_lock_fail =3D 0; + cxt.lrsa[i].n_lock_acquired =3D 0; + } } } =20 @@ -990,12 +1000,14 @@ static int __init lock_torture_init(void) goto unwind; } =20 - writer_tasks =3D kzalloc(cxt.nrealwriters_stress * sizeof(writer_tasks[0]= ), - GFP_KERNEL); - if (writer_tasks =3D=3D NULL) { - VERBOSE_TOROUT_ERRSTRING("writer_tasks: Out of memory"); - firsterr =3D -ENOMEM; - goto unwind; + if (nwriters_stress) { + writer_tasks =3D kzalloc(cxt.nrealwriters_stress * sizeof(writer_tasks[0= ]), + GFP_KERNEL); + if (writer_tasks =3D=3D NULL) { + VERBOSE_TOROUT_ERRSTRING("writer_tasks: Out of memory"); + firsterr =3D -ENOMEM; + goto unwind; + } } =20 if (cxt.cur_ops->readlock) { --=20 2.14.1