linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Davidlohr Bueso <dave@stgolabs.net>
To: Kefeng Wang <wangkefeng.wang@huawei.com>
Cc: paulmck@linux.vnet.ibm.com, linux-kernel@vger.kernel.org,
	peterz@infradead.org, mingo@redhat.com,
	Josh Triplett <josh@joshtriplett.org>,
	"Guohanjun (Hanjun Guo)" <guohanjun@huawei.com>
Subject: Re: [PATCH v2] locktorture: Fix NULL pointer when torture_type is invalid
Date: Thu, 3 Mar 2016 00:36:26 -0800	[thread overview]
Message-ID: <20160303083626.GA10957@linux-uzut.site> (raw)
In-Reply-To: <56D7BE33.5010605@huawei.com>

On Thu, 03 Mar 2016, Kefeng Wang wrote:

>Even if we merge Davidlohr's patch, I think we still need my v2 patch,
>here is a scene,
>----------
>cxt.lwsa = kmalloc(sizeof(*cxt.lwsa) * cxt.nrealwriters_stress, GFP_KERNEL);
>if (cxt.lwsa == NULL) {
>	goto unwind;
>}
>
>or
>
>cxt.lrsa = kmalloc(sizeof(*cxt.lrsa) * cxt.nrealreaders_stress, GFP_KERNEL);
>if (cxt.lrsa == NULL) {
>        VERBOSE_TOROUT_STRING("cxt.lrsa: Out of memory");
>        firsterr = -ENOMEM;
>        kfree(cxt.lwsa);
>        goto unwind;
>}
>----------
>we will get cxt.lwsa = NULL, and go to cleanup, then in
>
>static void __torture_print_stats(char *page,
>                                  struct lock_stress_stats *statp, bool write)
>{
>        bool fail = 0;
>        int i, n_stress;
>        long max = 0;
>        long min = statp[0].n_lock_acquired;   // here, *we will meet NULL pointer dereference*
>
>}

You are correct here, although very unlikely to hit a ENOMEM path, and because
of the nature of the module, you have bigger problems than this anyway. That said,
yes my patch only addresses this partially.

>and my patch v2 solve this issue too, so it is still needed.

But your patch is still too ad-hoc and still does not strike me to be the
correct way of dealing with the issue due to the already mentioned issues.
Lets instead think about how we call lock_torture_cleanup().

Callers are failed paths when loading the module, timed-shutdown and module_exit.
All of these assume there is at least the writer stats existing (lwsa). That's
actually why we have the "Start of test" shown immediately after doing basic checks.
In my patch I had just assumed this was limited to sanitizing parameters, and
overlooked mem allocation bits.

The below should take care of both issues, what do you think?

Thanks,
Davidlohr

<8-------------------------------------------------------------------------
Subject: [PATCH] locktorture: Fix nil pointer dereferencing for cleanup paths

It has been found that paths that invoke cleanups through
lock_torture_cleanup() can incur in nil pointer dereferencing
bugs during the statistics printing phase. This is mainly
because we should not be calling into statistics before we are
sure things have been setup correctly.

Specifically, early checks (and the need for handling this in
the cleanup call) only include parameter checks and basic
statistics allocation. Once we start write/read kthreads
we then consider the test as started. As such, update the func
in question to check for cxt.lwsa writer stats, if not set,
we either have a bogus parameter or ENOMEM situation and
therefore only need to deal with general torture calls.

Signed-off-by: Davidlohr Bueso <dbueso@suse.de>
---
XXX: while looking at the code, do we need at least a stat_interval > 0
check before stopping the lock_torture_stats kthread?

  kernel/locking/locktorture.c | 11 +++++++++++
  1 file changed, 11 insertions(+)

diff --git a/kernel/locking/locktorture.c b/kernel/locking/locktorture.c
index 8ef1919..1942848 100644
--- a/kernel/locking/locktorture.c
+++ b/kernel/locking/locktorture.c
@@ -748,6 +748,15 @@ static void lock_torture_cleanup(void)
  	if (torture_cleanup_begin())
  		return;
  
+	/*
+	 * Indicates early cleanup, meaning that the test has not run,
+	 * such as when passing bogus args when loading the module. As
+	 * such, only perform the underlying torture-specific cleanups,
+	 * and avoid anything related to locktorture.
+	 */
+	if (!cxt.lwsa)
+		goto end;
+
  	if (writer_tasks) {
  		for (i = 0; i < cxt.nrealwriters_stress; i++)
  			torture_stop_kthread(lock_torture_writer,
@@ -776,6 +785,7 @@ static void lock_torture_cleanup(void)
  	else
  		lock_torture_print_module_parms(cxt.cur_ops,
  						"End of test: SUCCESS");
+end:
  	torture_cleanup_end();
  }
  
@@ -878,6 +888,7 @@ static int __init lock_torture_init(void)
  			cxt.lrsa[i].n_lock_acquired = 0;
  		}
  	}
+
  	lock_torture_print_module_parms(cxt.cur_ops, "Start of test");
  
  	/* Prepare torture context. */
-- 
2.1.4

  reply	other threads:[~2016-03-03  8:36 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-28  4:25 [PATCH v2] locktorture: Fix NULL pointer when torture_type is invalid Kefeng Wang
2016-01-30  2:46 ` Kefeng Wang
2016-01-31  0:27   ` Paul E. McKenney
2016-01-31 22:17     ` Davidlohr Bueso
2016-02-01  2:25       ` Kefeng Wang
2016-02-01  3:02         ` Davidlohr Bueso
2016-02-01  3:28           ` Kefeng Wang
2016-02-02  6:46             ` Paul E. McKenney
2016-02-03  0:23               ` Davidlohr Bueso
2016-03-02 19:55                 ` Davidlohr Bueso
2016-03-02 21:12                   ` Paul E. McKenney
2016-03-03  1:37                     ` Kefeng Wang
2016-03-03  4:31                       ` Kefeng Wang
2016-03-03  8:36                         ` Davidlohr Bueso [this message]
2016-03-04 18:41                           ` Davidlohr Bueso
2016-03-07  2:00                           ` Kefeng Wang
2016-03-07  5:40                             ` Davidlohr Bueso
2016-03-07  7:02                               ` Kefeng Wang
2016-03-07 13:37                                 ` Paul E. McKenney
2016-03-08  2:10                                   ` Kefeng Wang
2016-03-08 19:51                                     ` Paul E. McKenney
2016-03-03 14:14                       ` Paul E. McKenney

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160303083626.GA10957@linux-uzut.site \
    --to=dave@stgolabs.net \
    --cc=guohanjun@huawei.com \
    --cc=josh@joshtriplett.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=paulmck@linux.vnet.ibm.com \
    --cc=peterz@infradead.org \
    --cc=wangkefeng.wang@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).