Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp6391699pxb; Wed, 17 Feb 2021 03:20:51 -0800 (PST) X-Google-Smtp-Source: ABdhPJwXn0EHR9hf+vi/a9sT9+Zce9wnX7mYjvUkq+If9Xisdqh2PaT7Ulh3jqjDnuNJBKTtS7FD X-Received: by 2002:a05:6402:3198:: with SMTP id di24mr25583368edb.340.1613560850904; Wed, 17 Feb 2021 03:20:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1613560850; cv=none; d=google.com; s=arc-20160816; b=PkzK/gGRRU6+icyaytPM38Q55BMsT9ZB94CF7MRixFhKica6+asbWC3eDmxlWfhoRl BvcO15q2N0tucf5035V0ulMzXtZYpEHQJFE6RKkJDYb/OOwvJrYOeqNn3Ta4TqGZBBCi pFBz92HDx/LsYw7sTABS/i8XY0e+AK0m7keM+tC2adsSorZhFZuSY4x3WqP9cZOmgS5c l2Lr+QiAoP4q1MctVz/FvxUItA762EaUtl97dQFsPVo+50N7wJSukytpjkH6JpvMfa9y pn6UTddki8yVCPvW9GPrv04TyLlxrXvTKmmTgwy3u6EPBja4Eu8sj6E/bBvMujQvJRw+ dHuQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=PH6CC7cRBkshSV2InMuUxMWSwr3BBhT4qwE65U+YVvU=; b=IK/DU8N/Y/URwb3uhcGRd7Hup3oo7cUPJp90waPG13lMqdP1DXg4UmvzjCDTk+nmza 4WqJ8/8T+Io0LdecN/d+1ci7n+2hrYW5C0V5HV1y7knTv57Q2TWnxHDM5Y22HnLiqm04 wUYec7QhLPZgZItRhUHgkLzzafFlsh1LlTtoqq7C/PJdUq4BicQIU712U/HwZ1ylT+Ws VHBPvIusGX29F/E7gAQsN26Gzu0vFnQO1ma1m7wjAYsopTrB0Wls9V67xnOWGIwbp3cw pnU0+Uo2iWTCM0WvF8RdvVmBLEMejdOIjphQ2T7kACjQOIOq027SdgK6CV0osfnqm+1O 7VFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=QUZ0dRgE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=suse.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k16si1081429edq.147.2021.02.17.03.20.21; Wed, 17 Feb 2021 03:20:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=QUZ0dRgE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=suse.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230332AbhBQLP4 (ORCPT + 99 others); Wed, 17 Feb 2021 06:15:56 -0500 Received: from mx2.suse.de ([195.135.220.15]:37262 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231400AbhBQLPc (ORCPT ); Wed, 17 Feb 2021 06:15:32 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1613560480; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=PH6CC7cRBkshSV2InMuUxMWSwr3BBhT4qwE65U+YVvU=; b=QUZ0dRgEsYA2HRhsgpmyhdUAomeYpEZtLJAIuFtEa2HJ4O1K47cDQqiUaUzMZnUZbJonno PNx0CqExSHIIPTuhpJIEUahBInpf4rK+kKCyUr4GbofrahWW82RQZ9RBlQf1zppXNqwh87 S7bnAWWMn9ZFXV3OLDH4iyIWuFuxRYE= Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 93DC5AEC4; Wed, 17 Feb 2021 11:14:40 +0000 (UTC) Date: Wed, 17 Feb 2021 12:14:39 +0100 From: Petr Mladek To: Yiwei Zhang Cc: Christoph Hellwig , Andrew Morton , Felix Kuehling , Jens Axboe , "J. Bruce Fields" , Peter Zijlstra , Frederic Weisbecker , Marcelo Tosatti , Ilias Stamatis , Rob Clark , Mathieu Desnoyers , Liang Chen , Linux Kernel Mailing List , kernel-team Subject: Re: [PATCH] kthread: add kthread_mod_pending_delayed_work api Message-ID: References: <20210214000611.2169820-1-zzyiwei@android.com> <20210216091128.GA3973504@infradead.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue 2021-02-16 10:58:36, Yiwei Zhang wrote: > On Mon, Feb 15, 2021 at 5:28 AM Petr Mladek wrote: > > > > On Sun 2021-02-14 00:06:11, Yiwei Zhang wrote: > > > The existing kthread_mod_delayed_work api will queue a new work if > > > failing to cancel the current work due to no longer being pending. > > > However, there's a case that the same work can be enqueued from both > > > an async request and a delayed work, and a racing could happen if the > > > async request comes right after the timeout delayed work gets > > > scheduled, > > > > By other words, you want to modify the delayed work only when > > it is still waiting in the queue. You do not want to queue new > > work when it has not been already queued. Do I get it correctly? > > > Yes, you are correct. > > > Could you please provide a patch where the new API is used? > > > Currently it will only get used in a downstream gpu driver. > > > > because the clean up work may not be safe to run twice. > > > > This looks like a bad design of the code. There is likely > > another race that might break it. You should ask the following > > questions: > > > > Why anyone tries to modify the clean up work when it has been already > > queued? There should be only one location/caller that triggers the clean up. > > > The clean up work was initially queued as a safe timeout work just in > case the userspace doesn't queue the cleanup work(e.g. process > crashing), which leaves the global driver in an incorrect driver > state(e.g. power state due to some hinting). In addition, the cleanup > work will also have to clean the cache allocated work struct as well > in the racing scenario. > > > Could anyone queue any work to the workqueue after the clean up > > work was queued? The cleanup work should be the last queued one. > > The workqueue user must inform all other users that the queue > > is being destroyed and nobody is allowed to queue any work > > any longer. > > > User can queue the initial work(internally it queues a cleanup work > with a big timeout in case user doesn't queue it later). Then after > user has done stuff within the scope, the user will queue the cleanup > work again to cancel out the effect, which is when it may race with > the underlying timeout'ed cleanup work. And this is exactly the design problem. If there race is possible then there are three possible scenarios: 1. User does the clean up before the timeout. This is the scenario where things work as expected. 2. User triggered clean up races with the clean up triggered by timeout. You try to handle this scenario by this patch. 3. User does clean up after the clean up has already been done by the timeout. It means that the user used the driver after it has already been cleaned up. This should not happen. I guess that the user commands will fail when the device has been cleaned up in the meantime. By other words, you are focusing on a small race window. But there is much bigger problem when the user could still use the cleaned up driver. There must be a better solution. You should avoid the timer because it is not reliable. The following comes to my mind: 1. The userspace application might do the clean up from SIGKILL handler. It will do the clean up even when it crashes. But you would still rely on userspace to do the correct thing. 2. I do not see a clean solution in kernel One possibility might be to register something called from __put_task_struct(). It seems profile_handoff_task() calls some notifiers that can be registered from anywhere. Another possibility might be to register a notifier called by profile_task_exit(tsk) that is called from do_exit(). It is not a clean solution because profile_task has another purpose. It might make sense to introduce a new generic notifier that is called during the task exit for this purpose. I am sure that it might have even more users. Anyway, look for put_task_struct(). It seems to be called in some drivers when destroying. I wonder if there is something that you might need. Best Regards, Petr