Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp1318092pxk; Fri, 2 Oct 2020 06:46:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyaeHPE4LkhhuYphegulBrzfe+Os1b/3QH5nJgnveY8//LHOcUgLUWoAopL8L2nxeW9WKv/ X-Received: by 2002:a17:906:f205:: with SMTP id gt5mr2347782ejb.48.1601646375596; Fri, 02 Oct 2020 06:46:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601646375; cv=none; d=google.com; s=arc-20160816; b=Q0Thi/xC6EY946AIhrfGC9UTZLi846/OSfWxzm6fFwQ5eeY8e0MTURJUbPcvc1+fpJ ceShogtgkokOOQkcoV2BbM+9r8ob5d+SUYL0Wilem1wnLN0shMRrpDEpOLe9NqVuIntv MVWZT1ZZhoIOzTs6S6j/xQSOM/DqsYJu2u7mPJggTrYspXikDrnw50wK1/eySkKP6Ekj prUqfYC6RdDtwaIc3vogCRhFQX2KMQtfMl/Zq6Zl0yCielC8IRE3BK1pDD789au+gjx8 NTUplFyRcTAJZofBYePqf2CA3SiGbWRbGpQB8pdVYzWn9u3IoK7QWDGD8VasjD2QuEB3 HCaw== 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=HuaZxNIjoxmWBPLmDlP+LOvkz0SeC33XVwnI4UoKcgE=; b=iJ1HVsk6urMQQl91115fPV6tlo7njLsxt1Cc3veeokLK5n+nUlkxo/RAf1SdMVpZaB CwgwfgjiDZQwtV6y7oa+Jn+8xfznmUVJNNN2XWxTLSMcdTF/h8UjbTNLngkU+kvN25U7 MMzFyniDA0eSGzsIAmI0px154RyqmoK6f+lQjaeb0pdvvT2RelKnytSsvTFCXj1RyimR dqTuLkOnDpjae7nsjwDzC98w1JCleNviX+bAP+R6RH2HthgGp//xWJG5FxZ8rmqkoydv nnH8QkWt+lQxMFZCa/ekn9LQUn9fgyG6VgNRDixv6++bFpDDmQN+HWBYqTwUnTQqYWzm V/7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel-dk.20150623.gappssmtp.com header.s=20150623 header.b=08Ay7ZlS; 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 b6si1211999edu.343.2020.10.02.06.45.53; Fri, 02 Oct 2020 06:46:15 -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=08Ay7ZlS; 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 S2387942AbgJBNo4 (ORCPT + 99 others); Fri, 2 Oct 2020 09:44:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47646 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387768AbgJBNoz (ORCPT ); Fri, 2 Oct 2020 09:44:55 -0400 Received: from mail-il1-x144.google.com (mail-il1-x144.google.com [IPv6:2607:f8b0:4864:20::144]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A9E2C0613E2 for ; Fri, 2 Oct 2020 06:44:55 -0700 (PDT) Received: by mail-il1-x144.google.com with SMTP id n16so1263411ilm.8 for ; Fri, 02 Oct 2020 06:44:55 -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=HuaZxNIjoxmWBPLmDlP+LOvkz0SeC33XVwnI4UoKcgE=; b=08Ay7ZlS8S/Op/x80hiuzkfNTS5Oj+MH/QKuVeRlMOF2r4DyLOxGukrNI+Ow4+oYPG rr2RDuVx8BxmHopJA7CP75K2yMZcGzSehFb8uvLT3uuZ5wtU3vWP6sMJ8Z/rycSuUtbS o4PUSyxqOh/0tH8aPpbjLtcdpidN4IxgH5rn3TlvZQtMulRexpF0NoZJlZC1WAwiKbMW uRsJeSsuGF5CXMXJAWyNJ12uSt4SqVNzZD6zTrGwoS9xofivIMtpIxVnu0R5rvqe40EU dLWo6dRNhTg1I3eXah4YLNPFhfWodsha8SFKJx4JZ1QhOh0md9iko1GwCtSnjrF78ems X3Ig== 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=HuaZxNIjoxmWBPLmDlP+LOvkz0SeC33XVwnI4UoKcgE=; b=Fn3D+gRSIRX1MDZpANyL2IAunLM+2oRgcLjJVSQRBlIfH3YgKywVxVN+PkUz0NR+yS +Yjqlm6QdilFdYNESY+ECfvD/mFI2n9Ksteuer2wcmQxqua0BGF5J+S+W+aHUx7RNpxa iYRO8g7kubGYYytvt2u1XVW0wMCgxplnxBGwMPMWYyeayHeZJwU2fngf8StDKxrEu+Un s4rQuYllTpNTDQYFF0tfJrBl3bpV9S1AlGknUmhiZdmDXuDhqH5+ubCdYqZZYeSyTyrv lcmTX/PQBRViNdnv8CG5AwLS4WyuteQ+XoAOwWMiLDTuqo0zjSYfl0Iyzz1g4FNfuRRw kirg== X-Gm-Message-State: AOAM5328maZrq0uokuWf46qczmO5BJTCG9B4rBnjXiU14QyjTtE5SvaE oe7azDQ2ah1vFzFsf3oKVT9CvQ== X-Received: by 2002:a92:c212:: with SMTP id j18mr1814640ilo.244.1601646294636; Fri, 02 Oct 2020 06:44:54 -0700 (PDT) Received: from [192.168.1.30] ([65.144.74.34]) by smtp.gmail.com with ESMTPSA id d1sm754041ila.67.2020.10.02.06.44.53 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 02 Oct 2020 06:44:54 -0700 (PDT) Subject: Re: [PATCH RFC v2] kernel: decouple TASK_WORK TWA_SIGNAL handling from signals To: Hillf Danton Cc: Oleg Nesterov , io-uring , LKML , Peter Zijlstra , Thomas Gleixner References: <3ce9e205-aad0-c9ce-86a7-b281f1c0237a@kernel.dk> <20201001162719.GD13633@redhat.com> <20201002133813.3180-1-hdanton@sina.com> From: Jens Axboe Message-ID: Date: Fri, 2 Oct 2020 07:44:53 -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: <20201002133813.3180-1-hdanton@sina.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 10/2/20 7:38 AM, Hillf Danton wrote: > > On Thu, 1 Oct 2020 11:27:04 -0600 Jens Axboe wrote: >> On 10/1/20 10:27 AM, Oleg Nesterov wrote: >>> Jens, >>> >>> I'll read this version tomorrow, but: >>> >>> On 10/01, Jens Axboe wrote: >>>> >>>> static inline int signal_pending(struct task_struct *p) >>>> { >>>> - return unlikely(test_tsk_thread_flag(p,TIF_SIGPENDING)); >>>> +#ifdef TIF_TASKWORK >>>> + /* >>>> + * TIF_TASKWORK isn't really a signal, but it requires the same >>>> + * behavior of restarting the system call to force a kernel/user >>>> + * transition. >>>> + */ >>>> + return unlikely(test_tsk_thread_flag(p, TIF_SIGPENDING) || >>>> + test_tsk_thread_flag(p, TIF_TASKWORK)); >>>> +#else >>>> + return unlikely(test_tsk_thread_flag(p, TIF_SIGPENDING)); >>>> +#endif >>> >>> This change alone is already very wrong. >>> >>> signal_pending(task) == T means that this task will do get_signal() as >>> soon as it can, and this basically means you can't "divorce" SIGPENDING >>> and TASKWORK. >>> >>> Simple example. Suppose we have a single-threaded task T. >>> >>> Someone does task_work_add(T, TWA_SIGNAL). This makes signal_pending()==T >>> and this is what we need. >>> >>> Now suppose that another task sends a signal to T before T calls >>> task_work_run() and clears TIF_TASKWORK. In this case SIGPENDING won't >>> be set because signal_pending() is already set (see wants_signal), and >>> this means that T won't notice this signal. >> >> That's a good point, and I have been thinking along those lines. The >> "problem" is the two different use cases: >> >> 1) The "should I return from schedule() or break out of schedule() loops >> kind of use cases". >> >> 2) Internal signal delivery use cases. >> >> The former wants one that factors in TIF_TASKWORK, while the latter >> should of course only look at TIF_SIGPENDING. >> >> Now, my gut reaction would be to have __signal_pending() that purely >> checks for TIF_SIGPENDING, and make sure we use that on the signal >> delivery side of things. Or something with a better name than that, but >> functionally the same. Ala: >> >> static inline int __signal_pending(struct task_struct *p) >> { >> return unlikely(test_tsk_thread_flag(p, TIF_SIGPENDING)); >> } >> >> static inline int signal_pending(struct task_struct *p) >> { >> #ifdef TIF_TASKWORK >> return unlikely(test_tsk_thread_flag(p, TIF_TASKWORK)|| >> __signal_pending(p)); >> #else >> return __signal_pending(p)); >> #endif >> } >> >> and then use __signal_pending() on the signal delivery side. >> >> It's still not great in the sense that renaming signal_pending() would >> be a better choice, but that's a whole lot of churn... > > To avoid that churn, IIUC replace TWA_SIGNAL with TWA_RESUME on > adding task work, which is compensated by adding a counter of > event source in IO ctx and waiting for event to arrive instead > of signal. That doesn't work. If the task is waiting in cqring_wait(), then there's no issue already. The problem is if it's waiting somewhere else. Imagine three threads, call them T1-3. T1 creates a pipe, and creates a ring. T1 queues a poll request for the read end of the pipe, and now does a wait for T2. T2 is a completer thread, so it ends up waiting for events on the ring. T2 is now in cqring_wait(). T3 is created, and it writes to the pipe. This write triggers the original poll request from T1, and task_work is now queued for T1. This task work needs to be processed for T2 to wakeup and complete, but it can't since T1 is in pthread_join() for T2. This is why TWA_SIGNAL is needed, we need it to break the T1 wait loop and process this work. No amount of changes in io_uring can fix this dependency, and if you look at the last series posted, it does in fact not even have any io_uring changes. Hence the goal is to have TWA_SIGNAL have the same kind of semantics it does now, but decoupled from ->sighand since that is problematic on particularly threaded setups. -- Jens Axboe