Received: by 2002:ab2:6a05:0:b0:1f8:1780:a4ed with SMTP id w5csp3280604lqo; Wed, 15 May 2024 05:24:16 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUIPrYiRka+2+db3KPvBu+gNKdB6Ju4w87vJMKjuVk8TyZI6jmYmpBG63Hu8v3sg4nHyFeX4MqUSpaj5gbZeYp6Vw8xwTtETOIdtHF1Aw== X-Google-Smtp-Source: AGHT+IEeXIM3eoWqhNdrKuLxhouVrYPZ0+t4NtKr27ywF7qSd3wHrrx9adJfzuLvApm9JGfyvcrl X-Received: by 2002:a05:6214:4806:b0:6a0:def5:8281 with SMTP id 6a1803df08f44-6a16791bce2mr264310976d6.11.1715775856587; Wed, 15 May 2024 05:24:16 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715775856; cv=pass; d=google.com; s=arc-20160816; b=nwOyYQgFbFdHK3g6zOM5y1RqhpN871dt848yXry21iUHMwfZozXGrryS0YekONnSpt 5PDfbu0i0doJ354QBV61KKzelu6QrNDZAlmjau4OZ4ofZZZ9AqvE0WirM6UGwrNZd5Um iEPIsRJ8ILxUS5CipwZ8lMBhLziXQmerpPDPOFVD4KaX1lse+DdQptuu77ww1Y9BVQqs AlCBXzfObXnE/6IAgueeQz2wPWvTzy9yypJ70oM6XgTvS7riFpPE//jxMWWEYcynR8F2 CyAJJgRr3mTPwK6EK/GRu8d7jsGDSHEEGxUgaQgQ7wyHZpkh6wbHv2irVAF4sMxSqMb8 17jw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:subject:cc:to:from :date:dkim-signature:dkim-filter; bh=HDwiITVAhgMeGNymYNoEWwNrSE2oaf7c2NuxaCTQgOU=; fh=k7//ss33FVjRUtfKk2O1J6bU4mgYk7/j1Aq1k17fb74=; b=ftqr0BR5sKwfB1fcSjRPK2/w77/YAtYgqH18IZsBHhQDpaq+cF9KoJYWCpqjqN5YAV qRTHTWEVd6aeAfAuZHC7jOHsOMWWI4scGU08Ytk0BWV0Jv2Adg8x9ikB+VfJYgDGK4GL UOnWPtik5MM4CWtUUm2xucSC3wLGwbS2bUZr9vWp+6FcfLv+qQAES1R6u/74E27xroHf s4tUD1KR7i+2GhJ13LysbBez/iFalgg5ylZiOOxmmPHo9JDzG+4ZRj35/3POIraRLxKX +62+txwU2pN/R8ShEvjPfdb6Mi4Sd1azTIrm5Yi/5zoKT8I8jaRgN5pAn1tk9Jdh44id f63A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ispras.ru header.s=default header.b=khh65UF6; arc=pass (i=1 spf=pass spfdomain=ispras.ru dkim=pass dkdomain=ispras.ru dmarc=pass fromdomain=ispras.ru); spf=pass (google.com: domain of linux-kernel+bounces-179844-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-179844-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ispras.ru Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id 6a1803df08f44-6a15f29b727si136293886d6.223.2024.05.15.05.24.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 May 2024 05:24:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-179844-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@ispras.ru header.s=default header.b=khh65UF6; arc=pass (i=1 spf=pass spfdomain=ispras.ru dkim=pass dkdomain=ispras.ru dmarc=pass fromdomain=ispras.ru); spf=pass (google.com: domain of linux-kernel+bounces-179844-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-179844-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ispras.ru Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 3E2D21C211A8 for ; Wed, 15 May 2024 12:24:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0F2A174C14; Wed, 15 May 2024 12:24:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=ispras.ru header.i=@ispras.ru header.b="khh65UF6" Received: from mail.ispras.ru (mail.ispras.ru [83.149.199.84]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0EC2A757F3 for ; Wed, 15 May 2024 12:24:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=83.149.199.84 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715775847; cv=none; b=nIfMQJZvAa0aHWSpooufM4/o1D52+Q4vmhx9PPzlkjIfKWwla6CBohk31ZtioQGdN6gbHEW/CYkQ7ImI0zLvsjvKqP3UEpDmWgsAXzUj5Rr62vdQqhojHcZnyd9DNxJaSxndLgBPlExz6bzVQWGQbioAvwGItjENmWJ2XwVYJtU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715775847; c=relaxed/simple; bh=4+KfGkFHnXnfeiiqw1n5KZYFBRATt/3WBm0/6HQCsXg=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type: Content-Disposition:In-Reply-To; b=OukFWsjNrUbK7WBoJUvwtBCwE2/v7a5QcoWsnjwUcHOX4xDEBUZzr0SEMbSStVdqNlDbEGkpytUYe8yD26/xYJkpai6gxaqQzyA2k4oZBwv9y1V+Kw3hmUEkKD9K7bQHUHnNeERCwnuKKTO6zS+XG/TIJF3VVgtLriyLhOh5Wbk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ispras.ru; spf=pass smtp.mailfrom=ispras.ru; dkim=pass (1024-bit key) header.d=ispras.ru header.i=@ispras.ru header.b=khh65UF6; arc=none smtp.client-ip=83.149.199.84 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ispras.ru Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ispras.ru Received: from fedor-21d0 (unknown [5.228.116.47]) by mail.ispras.ru (Postfix) with ESMTPSA id E55E74076720; Wed, 15 May 2024 12:15:10 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 mail.ispras.ru E55E74076720 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ispras.ru; s=default; t=1715775311; bh=HDwiITVAhgMeGNymYNoEWwNrSE2oaf7c2NuxaCTQgOU=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=khh65UF6Ts9c3SgqC/gXzjpuylzMFS5EN/Hl2V9m1ocmPgRSuBk+ItQQA2cHF3mQd vyfSbhxOfFwRUAZ0RCCgePdBsSvS9deKVxcB6+F0Qcc8+43pHrG1YPAy8VKJFepgWJ 1oyICLIty80I7OBJlQJSuRw2M5u+eFDO7t4GvBmo= Date: Wed, 15 May 2024 15:14:59 +0300 From: Fedor Pchelkin To: Robin Murphy Cc: Xiang Chen , Barry Song <21cnbao@gmail.com>, Christoph Hellwig , Marek Szyprowski , iommu@lists.linux.dev, linux-kernel@vger.kernel.org, Alexey Khoroshilov , lvc-project@linuxtesting.org Subject: Re: [PATCH v2 1/4] dma-mapping: benchmark: fix up kthread-related error handling Message-ID: <20240515-91223372820af49c191c67fc-pchelkin@ispras.ru> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <11456e13-23f8-43f7-8ffd-cd4e4ff825d7@arm.com> Hi, Thanks for review of the series! Robin Murphy wrote: > On 2024-05-04 12:47 pm, Fedor Pchelkin wrote: > > kthread creation failure is invalidly handled inside do_map_benchmark(). > > The put_task_struct() calls on the error path are supposed to balance the > > get_task_struct() calls which only happen after all the kthreads are > > successfully created. Rollback using kthread_stop() for already created > > kthreads in case of such failure. > > > > In normal situation call kthread_stop_put() to gracefully stop kthreads > > and put their task refcounts. This should be done for all started > > kthreads. > > Although strictly there were two overlapping bugs here, I'd agree that > it really doesn't seem worth the bother of trying to distinguish them. > I'm far from a kthread expert, but as best I can tell this looks like it > achieves a sensible final state. Modulo one nit below, > > Reviewed-by: Robin Murphy > > > Found by Linux Verification Center (linuxtesting.org). > > > > Fixes: 65789daa8087 ("dma-mapping: add benchmark support for streaming DMA APIs") > > Suggested-by: Robin Murphy > > Signed-off-by: Fedor Pchelkin > > --- > > kernel/dma/map_benchmark.c | 16 ++++++++++------ > > 1 file changed, 10 insertions(+), 6 deletions(-) > > > > diff --git a/kernel/dma/map_benchmark.c b/kernel/dma/map_benchmark.c > > index 02205ab53b7e..2478957cf9f8 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]); > > I think this means we'd end up actually starting the threads purely to > get them to see the KTHREAD_SHOULD_STOP flag and exit again? Not that > I'm too fussed about optimising an unexpected error path, however I > can't help but wonder if we might only need a put_task_struct() if we > can safely assume that the threads have never been started at this point. The threads have been already started to the moment by kthread_create_on_node() but put to uninterruptible sleep: please see kthread() function. They just have to be explicitly awakened, find that the KTHREAD_SHOULD_STOP flag was set and perform do_exit() in order to terminate and release all resources. The thread_fn won't be executed in such case. I feel there is no more convenient way for doing this differently than calling kthread_stop(). And the comment for kthread_stop() actually implies that it is intended to work also just after kthread creation. Calling put_task_struct() in that place will hit WARN_ON(!tsk->exit_state). I'd say the last call to this function should be made after (or in result of) the do_exit().