Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp194768ybi; Thu, 11 Jul 2019 17:33:10 -0700 (PDT) X-Google-Smtp-Source: APXvYqzbZsAotBDHdFMcXTYKwCbySgNvrY9SZeeZAKIzNagjad9l6Q7hR8EipftcZcmBJQ8SZbFo X-Received: by 2002:a63:d04e:: with SMTP id s14mr7021531pgi.189.1562891590360; Thu, 11 Jul 2019 17:33:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562891590; cv=none; d=google.com; s=arc-20160816; b=m2NzcKMP11wH/koqvBnWG8YHo+Im75Wnj1oih62zQLvBykkzjzNGA/dSo5qPcEywIf kPnMMiHfwiZpRk03F0maYJxxj5r/27ZiCWtr+rsqTwfBxhieDTPYXVfG1lsGJsymdatG KczFQao2aFhihe2EAAFjDIzc8ciSPeqa6LS/P2VYBNVbXcw8zN4E2PuXhYUM+8aJRz3F aO98D8uCrReCNmsZQ2DvbHwSNRmSK031Ld9Rz2nWX7FfOHDKI9zKY9JK5YW0QWwj+ST6 tx1ZS3yPhJv/wDjuG/1RlyUONEp8nlf9m2Dur+1Wkc8+WOWb0oy/5Jwb0LvxJ4J9zs17 HwzA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=U6giQvniffdnvehvmw4JsgX0bsHcF+ljzmPtU5hra14=; b=y//krwz7E/M7eaOpXSAqc/U7J8SVDDdU+gfrIxgfJ1WdxF1yOtT7CG3Juxa3ycI1gW GdQLX7M4QdLYNeYIsp0h3J/DkNKtoVT6FMFL5vW5wgQjCddE4gKGhbyT+mTyEGXWuu7U QM3Z37MkPMg0lyqJm5pz3X+egLc+OPe8l9UPG58ZY6hO6vWfjXM3K9eZZ2SttM7V2DAq uvVa464WZruyUBRd6S3KbMGjmiO5wxfmKZ/UwmdpfPXhTG4J3Z39eeGM0/cNfJJcW3Zz IhESaH8G0D2PbKm/YLoi6Xc7wxzOQ1WArtjlUk4DU6vGCoagOh+knDY1L7JFoWElF98+ 8cew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=TNBTRT9q; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r19si6774361pfh.50.2019.07.11.17.32.55; Thu, 11 Jul 2019 17:33:10 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=TNBTRT9q; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730607AbfGLAcg (ORCPT + 99 others); Thu, 11 Jul 2019 20:32:36 -0400 Received: from mail.kernel.org ([198.145.29.99]:52276 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728582AbfGLAcf (ORCPT ); Thu, 11 Jul 2019 20:32:35 -0400 Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 0EC90216C4 for ; Fri, 12 Jul 2019 00:32:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1562891554; bh=mWM1msUOi3sWCWjf4g3ssBFe9C4HtXvJFdU4LXMJXnc=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=TNBTRT9qR6L8EFYhNz5ASz+ZSFBFK7Podkuohl7zBGCBx3dor5jxsyP6TgcBTP4vx rRURMYTNBt/YTG1Vrh0NTFXQa2ENANmkzHM28Eg8BloZigmelWTTjfGQsAoJsDUFVi Dc9aFjzSzlb3K5GcZjhEvMO5QIwy/352cHXlh5oA= Received: by mail-wr1-f54.google.com with SMTP id p13so8073881wru.10 for ; Thu, 11 Jul 2019 17:32:33 -0700 (PDT) X-Gm-Message-State: APjAAAX0WkDiDCwMVW9BX3FYb2+6ODO0ZDvefGMZ3sBXGdHdDLXJ6+R0 087RUnrRc3QDOfweX9c4FL4+U9Bh40b02Upzbyo3ag== X-Received: by 2002:adf:dd0f:: with SMTP id a15mr7002359wrm.265.1562891552611; Thu, 11 Jul 2019 17:32:32 -0700 (PDT) MIME-Version: 1.0 References: <20190712014223.66326995@hikaru> In-Reply-To: <20190712014223.66326995@hikaru> From: Andy Lutomirski Date: Thu, 11 Jul 2019 17:32:21 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: On To: Carlo Wood Cc: wangyun@linux.vnet.ibm.com, palewis@adobe.com, LKML , Linux API , Al Viro , Andrew Morton , jbaron@redhat.com, pholland@adobe.com, Davide Libenzi , Michael Kerrisk , "Paul E. McKenney" , Neal Cardwell Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jul 11, 2019 at 5:01 PM Carlo Wood wrote: > > I believe that the only safe solution is to let the Event Loop > Thread do the deleting. So, if all else fails I'll have to add > objects that a Worker Thread thinks need to be deleted to a > FIFO that is processed by the Event Loop Thread before entering > epoll_wait(). But that is a lot of extra code for something > that could be achieved with just a small change to epoll: This doesn't seem so bad at all. > > > I propose to add a new EPOLL event EPOLLCLOSED that will cause > epoll_wait to return (for that event) whenever a file descriptor is > closed. This totally falls apart if you ever want to add a feature to your library to detach the handler for a given fd without closing the fd. > > The Worker Thread then does not remove an object from the > interest list, but either adds (if it was removed before) or > modifies the event (using EPOLL_CTL_MOD) to watch that fd > for closing, and then closes it. > > Aka, > > Working Thread: > > epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &event); > close(fd); > > where event contains the new EPOLLCLOSED (compare EPOLLOUT, EPOLLIN > etc). > > This must then guarantee the event EPOLLCLOSED to be reported > by exactly one epoll_wait(), the caller thread of which can then > proceed with deleting the resources. > > Note that close(fd) must cause the removal from the interest list > of any epoll struct before causing the event - and that the > EPOLLCLOSED event may only be reported after all other events > for that fd have already been reported (although it would be > ok to report them at the same time: simply handle the other > events first). This is a bunch of subtle semantics in the kernel to support your particular use case. > > I am not sure how this will work when more than one thread > calls epoll_wait and more than one watch the same fd: in > that case it is less trivial because then it seems always > possible that the EPOLLCLOSED event will be reported before > another event in another thread. But this case is fairly straightforward with the user mode approach -- for example, add it to the list for all threads calling epoll_wait. Or otherwise defer the deletion until all epoll_wait threads have woken.