2016-03-02 19:56:10

by Davidlohr Bueso

[permalink] [raw]
Subject: Re: [PATCH v2] locktorture: Fix NULL pointer when torture_type is invalid

On Tue, 02 Feb 2016, Davidlohr Bueso wrote:

I've just hit this issue myself and remembered this thread :)

Paul, folks, does the below patch look reasonable to you? If so
I can properly resend. thanks.

>On Mon, 01 Feb 2016, Paul E. McKenney wrote:
>
>>On Mon, Feb 01, 2016 at 11:28:07AM +0800, Kefeng Wang wrote:
>
>>>Just like I mentioned before, keep consistent with rcutorture???
>
>Because rcutorture does it doesn't mean locktorture has to do it ;)
>In any case, I'd suggest the same be done for rcutorture.
>
>[...]
>
>>
>>Hmmm... If nothing happened, then I agree that it makes sense not to
>>print any statistics. But if some testing actually was carried out, then
>>we really need to print the statistics.
>
>Right, so how about the following? It introduces an early cleanup helper
>that all it does is do torture specific cleanups. I don't really love the
>begin/end calls there, but it's not the end of the world and it seems better
>than a more messier refactoring. ie, I had also considered adding an 'early'
>flag to lock_torture_cleanup() such that we can enable it for this bogus param
>scenario, but seems over complicating things and we also call it for such a
>small issue.
>
>Thanks,
>Davidlohr
>
>
>diff --git a/kernel/locking/locktorture.c b/kernel/locking/locktorture.c
>index 8ef1919..05e2649 100644
>--- a/kernel/locking/locktorture.c
>+++ b/kernel/locking/locktorture.c
>@@ -741,6 +741,19 @@ lock_torture_print_module_parms(struct lock_torture_ops *cur_ops,
> onoff_interval, onoff_holdoff);
> }
>+/*
>+ * 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.
>+ */
>+static inline void lock_torture_early_cleanup(void)
>+{
>+ if (torture_cleanup_begin())
>+ return;
>+ torture_cleanup_end();
>+}
>+
> static void lock_torture_cleanup(void)
> {
> int i;
>@@ -811,8 +824,10 @@ static int __init lock_torture_init(void)
> for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
> pr_alert(" %s", torture_ops[i]->name);
> pr_alert("\n");
>- firsterr = -EINVAL;
>- goto unwind;
>+
>+ torture_init_end();
>+ lock_torture_early_cleanup();
>+ return -EINVAL;
> }
> if (cxt.cur_ops->init)
> cxt.cur_ops->init();


2016-03-02 21:12:24

by Paul E. McKenney

[permalink] [raw]
Subject: Re: [PATCH v2] locktorture: Fix NULL pointer when torture_type is invalid

On Wed, Mar 02, 2016 at 11:55:43AM -0800, Davidlohr Bueso wrote:
> On Tue, 02 Feb 2016, Davidlohr Bueso wrote:
>
> I've just hit this issue myself and remembered this thread :)
>
> Paul, folks, does the below patch look reasonable to you? If so
> I can properly resend. thanks.

If it works for Kefeng Wang, I would be happy to take it.

Thanx, Paul

> >On Mon, 01 Feb 2016, Paul E. McKenney wrote:
> >
> >>On Mon, Feb 01, 2016 at 11:28:07AM +0800, Kefeng Wang wrote:
> >
> >>>Just like I mentioned before, keep consistent with rcutorture???
> >
> >Because rcutorture does it doesn't mean locktorture has to do it ;)
> >In any case, I'd suggest the same be done for rcutorture.
> >
> >[...]
> >
> >>
> >>Hmmm... If nothing happened, then I agree that it makes sense not to
> >>print any statistics. But if some testing actually was carried out, then
> >>we really need to print the statistics.
> >
> >Right, so how about the following? It introduces an early cleanup helper
> >that all it does is do torture specific cleanups. I don't really love the
> >begin/end calls there, but it's not the end of the world and it seems better
> >than a more messier refactoring. ie, I had also considered adding an 'early'
> >flag to lock_torture_cleanup() such that we can enable it for this bogus param
> >scenario, but seems over complicating things and we also call it for such a
> >small issue.
> >
> >Thanks,
> >Davidlohr
> >
> >
> >diff --git a/kernel/locking/locktorture.c b/kernel/locking/locktorture.c
> >index 8ef1919..05e2649 100644
> >--- a/kernel/locking/locktorture.c
> >+++ b/kernel/locking/locktorture.c
> >@@ -741,6 +741,19 @@ lock_torture_print_module_parms(struct lock_torture_ops *cur_ops,
> > onoff_interval, onoff_holdoff);
> >}
> >+/*
> >+ * 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.
> >+ */
> >+static inline void lock_torture_early_cleanup(void)
> >+{
> >+ if (torture_cleanup_begin())
> >+ return;
> >+ torture_cleanup_end();
> >+}
> >+
> >static void lock_torture_cleanup(void)
> >{
> > int i;
> >@@ -811,8 +824,10 @@ static int __init lock_torture_init(void)
> > for (i = 0; i < ARRAY_SIZE(torture_ops); i++)
> > pr_alert(" %s", torture_ops[i]->name);
> > pr_alert("\n");
> >- firsterr = -EINVAL;
> >- goto unwind;
> >+
> >+ torture_init_end();
> >+ lock_torture_early_cleanup();
> >+ return -EINVAL;
> > }
> > if (cxt.cur_ops->init)
> > cxt.cur_ops->init();
>

2016-03-03 01:40:14

by Kefeng Wang

[permalink] [raw]
Subject: Re: [PATCH v2] locktorture: Fix NULL pointer when torture_type is invalid



On 2016/3/3 5:12, Paul E. McKenney wrote:
> On Wed, Mar 02, 2016 at 11:55:43AM -0800, Davidlohr Bueso wrote:
>> On Tue, 02 Feb 2016, Davidlohr Bueso wrote:
>>
>> I've just hit this issue myself and remembered this thread :)
>>
>> Paul, folks, does the below patch look reasonable to you? If so
>> I can properly resend. thanks.
>
> If it works for Kefeng Wang, I would be happy to take it.

Yes, it works for me, tested on my board.


>
> Thanx, Paul
>


2016-03-03 04:40:45

by Kefeng Wang

[permalink] [raw]
Subject: Re: [PATCH v2] locktorture: Fix NULL pointer when torture_type is invalid

Hi Davidlohr and Paul,

On 2016/3/3 9:37, Kefeng Wang wrote:
>
>
> On 2016/3/3 5:12, Paul E. McKenney wrote:
>> On Wed, Mar 02, 2016 at 11:55:43AM -0800, Davidlohr Bueso wrote:
>>> On Tue, 02 Feb 2016, Davidlohr Bueso wrote:
>>>
>>> I've just hit this issue myself and remembered this thread :)
>>>
>>> Paul, folks, does the below patch look reasonable to you? If so
>>> I can properly resend. thanks.
>>
>> If it works for Kefeng Wang, I would be happy to take it.
>
> Yes, it works for me, tested on my board.
>

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*

}

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

Thanks,
Kefeng


>
>>
>> Thanx, Paul
>>
>

2016-03-03 08:36:36

by Davidlohr Bueso

[permalink] [raw]
Subject: Re: [PATCH v2] locktorture: Fix NULL pointer when torture_type is invalid

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 <[email protected]>
---
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

2016-03-03 14:14:32

by Paul E. McKenney

[permalink] [raw]
Subject: Re: [PATCH v2] locktorture: Fix NULL pointer when torture_type is invalid

On Thu, Mar 03, 2016 at 09:37:42AM +0800, Kefeng Wang wrote:
>
>
> On 2016/3/3 5:12, Paul E. McKenney wrote:
> > On Wed, Mar 02, 2016 at 11:55:43AM -0800, Davidlohr Bueso wrote:
> >> On Tue, 02 Feb 2016, Davidlohr Bueso wrote:
> >>
> >> I've just hit this issue myself and remembered this thread :)
> >>
> >> Paul, folks, does the below patch look reasonable to you? If so
> >> I can properly resend. thanks.
> >
> > If it works for Kefeng Wang, I would be happy to take it.
>
> Yes, it works for me, tested on my board.

Very good, thank you!

Davidlohr, please do send me a cleaned-up version of your patch.

Thanx, Paul

2016-03-04 18:41:38

by Davidlohr Bueso

[permalink] [raw]
Subject: Re: [PATCH v2] locktorture: Fix NULL pointer when torture_type is invalid

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

fyi I've found another issue, completely unrelated to this one. We can
deref a nil ptr in the rtmutex torturing, which I hit last night.


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

Paul, so we would need this patch, assuming Kefeng is good with it (if so
could you please add a reported/tested-by tag?). As well as this next one,
unless you have any problems with the fix obviously.

Thanks,
Davidlohr

----8<--------------------------------------------------------------------
Subject: [PATCH] locktorture: Fix deboosting nil ptr dereferencing

For the case of rtmutex torturing we will randomly call into the
boost() handler, including upon module exiting when the tasks are
deboosted before stopping. In such cases the task may or may not have
already been boosted, and therefore the NULL being explicitly passed
can occur anywhere. Currently we only assume that the task will is
at a higher prio, and in consequence, dereference a nil pointer.

This patch fixes the case of a rmmod locktorture exploding while
pounding on the rtmutex lock (partial trace):

[83317.452251] task: ffff88081026cf80 ti: ffff880816120000 task.ti: ffff880816120000
[83317.452258] RIP: 0010:[<ffffffffa05c6185>] [<ffffffffa05c6185>] torture_random+0x5/0x60 [torture]
[83317.452261] RSP: 0018:ffff880816123eb0 EFLAGS: 00010206
[83317.452264] RAX: ffff88081026cf80 RBX: ffff880816bfa630 RCX: 0000000000160d1b
[83317.452267] RDX: 0000000000000000 RSI: 0000000000000202 RDI: 0000000000000000
[83317.452269] RBP: ffff88081026cf80 R08: 000000000000001f R09: ffff88017c20ca80
[83317.452271] R10: 0000000000000000 R11: 000000000048c316 R12: ffffffffa05d1840
[83317.452273] R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000000
[83317.452275] FS: 0000000000000000(0000) GS:ffff88203f880000(0000) knlGS:0000000000000000
[83317.452277] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[83317.452279] CR2: 0000000000000008 CR3: 0000000001c0a000 CR4: 00000000000406e0
[83317.452281] Stack:
[83317.452288] ffffffffa05d141d ffff880816bfa630 ffffffffa05d1922 ffff88081e70c2c0
[83317.452295] ffff880816bfa630 ffffffff81095fed 0000000000000000 ffffffff8107bf60
[83317.452297] ffff880816bfa630 ffffffff00000000 ffff880800000000 ffff880816123f08
[83317.452297] Call Trace:
[83317.452309] [<ffffffffa05d141d>] torture_rtmutex_boost+0x1d/0x90 [locktorture]
[83317.452315] [<ffffffffa05d1922>] lock_torture_writer+0xe2/0x170 [locktorture]
[83317.452321] [<ffffffff81095fed>] kthread+0xbd/0xe0
[83317.452325] [<ffffffff815cf40f>] ret_from_fork+0x3f/0x70

This patch ensures that if the random state pointer is not nil and current
is not boosted, then do nothing.

Signed-off-by: Davidlohr Bueso <[email protected]>
---
kernel/locking/locktorture.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/kernel/locking/locktorture.c b/kernel/locking/locktorture.c
index 8ef1919..9e9c5f4 100644
--- a/kernel/locking/locktorture.c
+++ b/kernel/locking/locktorture.c
@@ -394,12 +394,12 @@ static void torture_rtmutex_boost(struct torture_random_state *trsp)

if (!rt_task(current)) {
/*
- * (1) Boost priority once every ~50k operations. When the
+ * Boost priority once every ~50k operations. When the
* task tries to take the lock, the rtmutex it will account
* for the new priority, and do any corresponding pi-dance.
*/
- if (!(torture_random(trsp) %
- (cxt.nrealwriters_stress * factor))) {
+ if (trsp && !(torture_random(trsp) %
+ (cxt.nrealwriters_stress * factor))) {
policy = SCHED_FIFO;
param.sched_priority = MAX_RT_PRIO - 1;
} else /* common case, do nothing */
--
2.1.4

2016-03-07 02:02:10

by Kefeng Wang

[permalink] [raw]
Subject: Re: [PATCH v2] locktorture: Fix NULL pointer when torture_type is invalid



On 2016/3/3 16:36, Davidlohr Bueso wrote:
> On Thu, 03 Mar 2016, Kefeng Wang wrote:
>

>
> 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 <[email protected]>
> ---
> 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;

Sorry for the late response, the cxt.lrsa should be taken into account too.

> +
> 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. */

2016-03-07 05:40:19

by Davidlohr Bueso

[permalink] [raw]
Subject: Re: [PATCH v2] locktorture: Fix NULL pointer when torture_type is invalid

On Mon, 07 Mar 2016, Kefeng Wang wrote:
>On 2016/3/3 16:36, Davidlohr Bueso wrote:

>> + /*
>> + * 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;
>
>Sorry for the late response, the cxt.lrsa should be taken into account too.

I am taking it into account, note that we kfree lwsa if lrsa fails memory
allocation. Of course we should be defensive, so go ahead and explicitly set
it to nil. v2 below, otherwise same patch.

-----8<--------------------------
Subject: [PATCH v2] 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

Reported-by: Kefeng Wang <[email protected]>
Signed-off-by: Davidlohr Bueso <[email protected]>
---
kernel/locking/locktorture.c | 12 ++++++++++++
1 file changed, 12 insertions(+)

diff --git a/kernel/locking/locktorture.c b/kernel/locking/locktorture.c
index 8ef1919..b5bc243 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();
}

@@ -870,6 +880,7 @@ static int __init lock_torture_init(void)
VERBOSE_TOROUT_STRING("cxt.lrsa: Out of memory");
firsterr = -ENOMEM;
kfree(cxt.lwsa);
+ cxt.lwsa = NULL;
goto unwind;
}

@@ -878,6 +889,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

2016-03-07 07:05:31

by Kefeng Wang

[permalink] [raw]
Subject: Re: [PATCH v2] locktorture: Fix NULL pointer when torture_type is invalid



On 2016/3/7 13:40, Davidlohr Bueso wrote:
> On Mon, 07 Mar 2016, Kefeng Wang wrote:
>> On 2016/3/3 16:36, Davidlohr Bueso wrote:
>
>>> + /*
>>> + * 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;
>>
>> Sorry for the late response, the cxt.lrsa should be taken into account too.
>
> I am taking it into account, note that we kfree lwsa if lrsa fails memory
> allocation. Of course we should be defensive, so go ahead and explicitly set
> it to nil. v2 below, otherwise same patch.

This one looks good, and tested on my board.


>
> -----8<--------------------------
> Subject: [PATCH v2] 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
>
> Reported-by: Kefeng Wang <[email protected]>
> Signed-off-by: Davidlohr Bueso <[email protected]>
> ---
> kernel/locking/locktorture.c | 12 ++++++++++++
> 1 file changed, 12 insertions(+)
>
> diff --git a/kernel/locking/locktorture.c b/kernel/locking/locktorture.c
> index 8ef1919..b5bc243 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();
> }
>
> @@ -870,6 +880,7 @@ static int __init lock_torture_init(void)
> VERBOSE_TOROUT_STRING("cxt.lrsa: Out of memory");
> firsterr = -ENOMEM;
> kfree(cxt.lwsa);
> + cxt.lwsa = NULL;
> goto unwind;
> }
>
> @@ -878,6 +889,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. */

2016-03-07 15:29:56

by Paul E. McKenney

[permalink] [raw]
Subject: Re: [PATCH v2] locktorture: Fix NULL pointer when torture_type is invalid

On Mon, Mar 07, 2016 at 03:02:05PM +0800, Kefeng Wang wrote:
>
>
> On 2016/3/7 13:40, Davidlohr Bueso wrote:
> > On Mon, 07 Mar 2016, Kefeng Wang wrote:
> >> On 2016/3/3 16:36, Davidlohr Bueso wrote:
> >
> >>> + /*
> >>> + * 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;
> >>
> >> Sorry for the late response, the cxt.lrsa should be taken into account too.
> >
> > I am taking it into account, note that we kfree lwsa if lrsa fails memory
> > allocation. Of course we should be defensive, so go ahead and explicitly set
> > it to nil. v2 below, otherwise same patch.
>
> This one looks good, and tested on my board.

Very good! May we add your Tested-by?

Thanx, Paul

> > -----8<--------------------------
> > Subject: [PATCH v2] 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
> >
> > Reported-by: Kefeng Wang <[email protected]>
> > Signed-off-by: Davidlohr Bueso <[email protected]>
> > ---
> > kernel/locking/locktorture.c | 12 ++++++++++++
> > 1 file changed, 12 insertions(+)
> >
> > diff --git a/kernel/locking/locktorture.c b/kernel/locking/locktorture.c
> > index 8ef1919..b5bc243 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();
> > }
> >
> > @@ -870,6 +880,7 @@ static int __init lock_torture_init(void)
> > VERBOSE_TOROUT_STRING("cxt.lrsa: Out of memory");
> > firsterr = -ENOMEM;
> > kfree(cxt.lwsa);
> > + cxt.lwsa = NULL;
> > goto unwind;
> > }
> >
> > @@ -878,6 +889,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. */
>

2016-03-08 02:12:18

by Kefeng Wang

[permalink] [raw]
Subject: Re: [PATCH v2] locktorture: Fix NULL pointer when torture_type is invalid



On 2016/3/7 21:37, Paul E. McKenney wrote:
> On Mon, Mar 07, 2016 at 03:02:05PM +0800, Kefeng Wang wrote:
>>
>>
>> On 2016/3/7 13:40, Davidlohr Bueso wrote:
>>> On Mon, 07 Mar 2016, Kefeng Wang wrote:
>>>> On 2016/3/3 16:36, Davidlohr Bueso wrote:
>>>
>>>>> + /*
>>>>> + * 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;
>>>>
>>>> Sorry for the late response, the cxt.lrsa should be taken into account too.
>>>
>>> I am taking it into account, note that we kfree lwsa if lrsa fails memory
>>> allocation. Of course we should be defensive, so go ahead and explicitly set
>>> it to nil. v2 below, otherwise same patch.
>>
>> This one looks good, and tested on my board.
>
> Very good! May we add your Tested-by?

Sure, please.

>
> Thanx, Paul


2016-03-08 19:52:08

by Paul E. McKenney

[permalink] [raw]
Subject: Re: [PATCH v2] locktorture: Fix NULL pointer when torture_type is invalid

On Tue, Mar 08, 2016 at 10:10:52AM +0800, Kefeng Wang wrote:
>
>
> On 2016/3/7 21:37, Paul E. McKenney wrote:
> > On Mon, Mar 07, 2016 at 03:02:05PM +0800, Kefeng Wang wrote:
> >>
> >>
> >> On 2016/3/7 13:40, Davidlohr Bueso wrote:
> >>> On Mon, 07 Mar 2016, Kefeng Wang wrote:
> >>>> On 2016/3/3 16:36, Davidlohr Bueso wrote:
> >>>
> >>>>> + /*
> >>>>> + * 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;
> >>>>
> >>>> Sorry for the late response, the cxt.lrsa should be taken into account too.
> >>>
> >>> I am taking it into account, note that we kfree lwsa if lrsa fails memory
> >>> allocation. Of course we should be defensive, so go ahead and explicitly set
> >>> it to nil. v2 below, otherwise same patch.
> >>
> >> This one looks good, and tested on my board.
> >
> > Very good! May we add your Tested-by?
>
> Sure, please.

Thank you, Kefeng!

Davidlohr, I tried applying your patches and got conflicts. Could you
please port them to -rcu and send me clean versions?

Thanx, Paul