IOMMU Archive mirror
 help / color / mirror / Atom feed
From: Fedor Pchelkin <pchelkin@ispras.ru>
To: Robin Murphy <robin.murphy@arm.com>
Cc: Xiang Chen <chenxiang66@hisilicon.com>,
	Christoph Hellwig <hch@lst.de>,
	 Marek Szyprowski <m.szyprowski@samsung.com>,
	Barry Song <21cnbao@gmail.com>,
	iommu@lists.linux.dev,  linux-kernel@vger.kernel.org,
	Alexey Khoroshilov <khoroshilov@ispras.ru>,
	 lvc-project@linuxtesting.org
Subject: Re: [PATCH 1/2] dma-mapping: benchmark: fix up kthread creation error handling
Date: Fri, 3 May 2024 19:23:48 +0300	[thread overview]
Message-ID: <20240503-d09628dce22e71e600ebd907-pchelkin@ispras.ru> (raw)
In-Reply-To: <3c474453-fc89-42b2-9ddc-fdc3a2893064@arm.com>

Robin Murphy wrote:
> On 2024-05-02 5:18 pm, Fedor Pchelkin wrote:
> > If a kthread creation fails for some reason then uninitialized members of
> > the tasks array will be accessed on the error path since it is allocated
> > by kmalloc_array().
> > 
> > Limit the bound in such case.
> 
> I don't think this is right... The put_task_struct() calls on the error 
> path are supposed to balance the get_task_struct() calls which only 
> happen *after* all the threads are successfully created - see commit 

Thanks, Robin! You're right. Now I see this..

> d17405d52bac ("dma-mapping: benchmark: fix kernel crash when 
> dma_map_single fails") - although I now wonder whether that might have 
> been better done by replacing kthread_stop() with kthread_stop_put(). It 
> doesn't look like we've ever actually tried to free any previous threads 
> from the point of allocation failure.

It should have looked like the diff below, as you say. And it's probably
better to merge the two patches together so that we eliminate task leaks
in case kthread_stop_put() returns error like it is below.

Will rework that in v2.

diff --git a/kernel/dma/map_benchmark.c b/kernel/dma/map_benchmark.c
index 02205ab53b7e..3b7658ce1599 100644
--- a/kernel/dma/map_benchmark.c
+++ b/kernel/dma/map_benchmark.c
@@ -118,6 +118,8 @@ static int do_map_benchmark(struct map_benchmark_data *map)
                if (IS_ERR(tsk[i])) {
                        pr_err("create dma_map thread failed\n");
                        ret = PTR_ERR(tsk[i]);
+                       while (--i >= 0)
+                               kthread_stop(tsk[i]);
                        goto out;
                }
 
@@ -141,7 +143,7 @@ static int do_map_benchmark(struct map_benchmark_data *map)
 
        /* wait for the completion of benchmark threads */
        for (i = 0; i < threads; i++) {
-               ret = kthread_stop(tsk[i]);
+               ret = kthread_stop_put(tsk[i]);
                if (ret)
                        goto out;
        }
@@ -170,8 +172,6 @@ static int do_map_benchmark(struct map_benchmark_data *map)
        }
 
 out:
-       for (i = 0; i < threads; i++)
-               put_task_struct(tsk[i]);
        put_device(map->dev);
        kfree(tsk);
        return ret;

  reply	other threads:[~2024-05-03 16:24 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-02 16:18 [PATCH 0/2] dma-mapping: benchmark: adjust some error handling in do_map_benchmark Fedor Pchelkin
2024-05-02 16:18 ` [PATCH 1/2] dma-mapping: benchmark: fix up kthread creation error handling Fedor Pchelkin
2024-05-03  1:36   ` Barry Song
2024-05-03 12:29   ` Robin Murphy
2024-05-03 16:23     ` Fedor Pchelkin [this message]
2024-05-02 16:18 ` [PATCH 2/2] dma-mapping: benchmark: prevent potential kthread hang Fedor Pchelkin
2024-05-03  1:44   ` Barry Song
2024-05-03 15:39     ` Fedor Pchelkin

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=20240503-d09628dce22e71e600ebd907-pchelkin@ispras.ru \
    --to=pchelkin@ispras.ru \
    --cc=21cnbao@gmail.com \
    --cc=chenxiang66@hisilicon.com \
    --cc=hch@lst.de \
    --cc=iommu@lists.linux.dev \
    --cc=khoroshilov@ispras.ru \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lvc-project@linuxtesting.org \
    --cc=m.szyprowski@samsung.com \
    --cc=robin.murphy@arm.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 read-only IMAP folder(s) and NNTP newsgroup(s).