Received: by 2002:a9a:4c47:0:b029:116:c383:538 with SMTP id u7csp941127lko; Tue, 13 Jul 2021 13:27:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyELrusKLIoKz7FBDm3TqZFLLSL5xSSid9Pt1eFsmgJ4ikISU4Fps4P+AzUg+zCrd05+ZiA X-Received: by 2002:a17:906:3b44:: with SMTP id h4mr1199285ejf.462.1626208048511; Tue, 13 Jul 2021 13:27:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626208048; cv=none; d=google.com; s=arc-20160816; b=ZJmDbblAScZXJVMeR4ySVNScjLd4FBNwNLWjwuVDOGzSIlUFcLWj9HERxODZG821sU cSH1Izd4OAsANmVOtkQFDFUA3J5YHOnUCbp6HPKzXr7C4za4xmSmG6itiCg7q7Ki4ttD +XvDi/zqYEmWRV8k8X6I0okONBMZ/pK01FT12NbJDyiVfs06hgRks9oJBlo714cosbIz TjPeCl5MjGzZXYIJNeHu2MZ+NGjFhpnAG/t95FfEw2nNcNIkDkFodAkais+1ZSiZ3CNN pe0qS2kqN0NUcz4JnL2SWSfLbpsPzbP0tGcOd8pJC2MlLEAJa5tqzHk78+oEkyWt58RT bivw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=3SJYzIMZume2KNriSWijNRDiLHtG1WzjjPHKuOZ9Ue8=; b=B9adNbJN+mDwwc+0JGAW0UlYFBP0hc+jABdmBvLQa1fW6xXJSrOKuUJc9D2RMDC67D F7BWHLkOYaEYk2Ciz8VwI3ZG8aQR1y3HWfd0d+gki3LRB3Mfap/Keg32UuUiPU2vpO2z ZLipfW0lhAf4s1lcJRlilruNwsnpSg+YbxR1tVK0aeL275MsMFn5OTIQdhaFJBu2hu81 Ou3U4/xOIzH7BwDk+nDIcRCeBFwsum2Rs767wiLv+FDz8HYwgQgtDDSZ5muwKbBn0WtB ucdMcw27hOWlzBgc+Two0EtR33nzlGr3KbOMfWzNRQ5k3tvuYE4hDcVngOAIfq7rXxxu uJjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel-dk.20150623.gappssmtp.com header.s=20150623 header.b=arhAHpMr; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dc28si17455286edb.444.2021.07.13.13.26.19; Tue, 13 Jul 2021 13:27:28 -0700 (PDT) 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=@kernel-dk.20150623.gappssmtp.com header.s=20150623 header.b=arhAHpMr; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235079AbhGMUZC (ORCPT + 99 others); Tue, 13 Jul 2021 16:25:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234290AbhGMUZC (ORCPT ); Tue, 13 Jul 2021 16:25:02 -0400 Received: from mail-ot1-x331.google.com (mail-ot1-x331.google.com [IPv6:2607:f8b0:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 065B9C0613DD for ; Tue, 13 Jul 2021 13:22:12 -0700 (PDT) Received: by mail-ot1-x331.google.com with SMTP id f12-20020a056830204cb029048bcf4c6bd9so116606otp.8 for ; Tue, 13 Jul 2021 13:22:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=3SJYzIMZume2KNriSWijNRDiLHtG1WzjjPHKuOZ9Ue8=; b=arhAHpMrM4XdjNZA/usB70Edlw9HGL6wxogv6gPW2m+onzP1OMZZopdjU/qB/cIE8Z wSMYdviN4Lc73GYN3Ms+N5Nrae73gA8KBAYYVjG4nkqtp6TOkFo1IrRAWd7RmwgmDmJL FMMc17efx4LQm778xSmzffP4xqzHXml9L87i/9uCblpjsFXhqEYb6j63JDHzFstZtMiZ E9xWDGp1lBriq/YrGp+ouKHb1YmFbkAk0HL2SAOmUo0kVQFz/PlUpij9LFyb3Y+zTaVA GY8g704LNe/3+2uvCrupRdnhmTmI8rOvR0GRptPFZzx8dWCvZGNuL4EOJmUwdEtQoCvG MpHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=3SJYzIMZume2KNriSWijNRDiLHtG1WzjjPHKuOZ9Ue8=; b=LHSYQc1y0n2msMGG39SiR1Ll+Yc1cgrcdA8aK+H46+OxmnuvmW2cN7Z31ObXNRPgh/ 4EO5Dg1TykfEs/KlHvkmtKrWqyeQE+7x2SBZs71RcXtHWG1XV2OWobHvPzN6N55foowI IYfVWweeJx4e8whYvwMa7IoBWVQWPOAKsbsGB6R88Jx0SQ73jDObKcER1DDdRBdMmaQL a9boMJgEZLVtrX1YagVZC/n2Xj6LTVM0VHzHo+/0zWygAU5lqD9sbXFZdtlL1kR/FuRJ Yvf1fXyTqBXqZ35auQqB3MhFur7g60On43Ra7C+ASa7tB+trHkdBAouP8AluVKh4fcHJ VMiA== X-Gm-Message-State: AOAM532Zg+3RMsGl9hasnfQzGO7AfNnwZVJFI0U9r7QEQRoywA+NnxMr fD2BgG1pyiNTU8qiZsAjqqdtVQ== X-Received: by 2002:a9d:4112:: with SMTP id o18mr2972304ote.128.1626207731276; Tue, 13 Jul 2021 13:22:11 -0700 (PDT) Received: from [192.168.1.134] ([198.8.77.61]) by smtp.gmail.com with ESMTPSA id x20sm3166160otq.62.2021.07.13.13.22.10 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 13 Jul 2021 13:22:10 -0700 (PDT) Subject: Re: [PATCH] task_work: return -EBUSY when adding same work To: David Laight , yaozhenguo , "oleg@redhat.comm" Cc: "linux-kernel@vger.kernel.org" , "yaozhenguo@jd.com" References: <20210709122712.42844-1-yaozhenguo1@gmail.com> <872612b5-b9c6-43aa-a167-1c204d0f1c5a@kernel.dk> <3079d213543c4d398d96031e6da26c82@AcuMS.aculab.com> From: Jens Axboe Message-ID: Date: Tue, 13 Jul 2021 14:22:10 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <3079d213543c4d398d96031e6da26c82@AcuMS.aculab.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 7/13/21 4:41 AM, David Laight wrote: > From: Jens Axboe >> Sent: 09 July 2021 15:18 > ... >>> */ >>> int task_work_add(struct task_struct *task, struct callback_head *work, >>> enum task_work_notify_mode notify) >>> @@ -41,6 +41,8 @@ int task_work_add(struct task_struct *task, struct callback_head *work, >>> head = READ_ONCE(task->task_works); >>> if (unlikely(head == &work_exited)) >>> return -ESRCH; >>> + if (unlikely(head == work)) >>> + return -EBUSY; >>> work->next = head; >>> } while (cmpxchg(&task->task_works, head, work) != head); >> >> I don't think there's anything conceptually wrong with this patch, but >> it makes me think that you hit this condition. It's really a bug in the >> caller, of course, is a WARN_ON_ONCE() warranted here? And who was the >> caller? > > How can the caller know that the task is on the queue? It's similar to double adding a list entry to a list. It's the callers responsibility to make sure that doesn't happen. The item happens to be per-task here, but that doesn't really change the mechanics of it. > There will be a race condition just before the work function > is called and/or just after it returns that the caller > can't detect. > The check needs to be done atomically with the code that > removes the work item from the list. We're not polluting task_work with caller specific code like that, as far as I'm concerned. Fix it in the caller. By the time ->func is run, the item is off the original list and it can get re-added just fine. If the event triggers after removing from the list but before ->func is called, I don't see a problem with that. The caller just wants to ensure that func is run, which it will be. If the caller needs stronger guarantees than that, then it should implement them separately on top of task_work. -- Jens Axboe