From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49646) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dBmqO-0001rr-HF for qemu-devel@nongnu.org; Fri, 19 May 2017 14:41:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dBmqK-0003N1-Fn for qemu-devel@nongnu.org; Fri, 19 May 2017 14:41:44 -0400 Received: from mx1.redhat.com ([209.132.183.28]:43534) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dBmqK-0003MM-89 for qemu-devel@nongnu.org; Fri, 19 May 2017 14:41:40 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3E0E6C0467C5 for ; Fri, 19 May 2017 18:41:39 +0000 (UTC) Date: Fri, 19 May 2017 19:41:34 +0100 From: "Dr. David Alan Gilbert" Message-ID: <20170519184133.GU2081@work-vm> References: <1495176212-14446-1-git-send-email-peterx@redhat.com> <1495176212-14446-2-git-send-email-peterx@redhat.com> <20170519082538.GD4912@redhat.com> <20170519125139.GM2081@work-vm> <20170519125601.GD28392@redhat.com> <20170519130200.GO2081@work-vm> <20170519131344.GA22341@redhat.com> <20170519143312.GP2081@work-vm> <20170519145126.GA17164@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20170519145126.GA17164@redhat.com> Subject: Re: [Qemu-devel] [PATCH RFC 1/6] io: only allow return path for socket typed List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: "Daniel P. Berrange" Cc: Peter Xu , qemu-devel@nongnu.org, Juan Quintela * Daniel P. Berrange (berrange@redhat.com) wrote: > On Fri, May 19, 2017 at 03:33:12PM +0100, Dr. David Alan Gilbert wrote: > > * Daniel P. Berrange (berrange@redhat.com) wrote: > > > > shutdown() is safe, in that it stops any other threads accessing the fd > > > > but doesn't allow it's reallocation until the close; We perform the > > > > close only when we've joined all other threads that were using the fd. > > > > Any of the threads that do new calls on the fd get an error and quickly > > > > fall down their error paths. > > > > > > Ahh that's certainly an interesting scenario. That would certainly be > > > a problem with the migration code when this was originally written. > > > It had two QEMUFile structs each with an 'int fd' field, so when you > > > close 'fd' on one QEMUFile struct, it wouldn't update the other QEMUFile > > > used by another thread. > > > > > > Since we switched over to use QIOChannel though, I think the thread > > > scenario you describe should be avoided entirely. When you have multiple > > > QEMUFile objects, they each have a reference counted pointer to the same > > > underlying QIOChannel object instance. So when QEMUFile triggers a call > > > to qio_channel_close() in one thread, that'll set fd=-1 in the QIOChannel. > > > Since the other threads have a reference to the same QIOChannel object, > > > they'll now see this fd == -1 straightaway. > > > > > > So, IIUC, this should make the need for shutdown() redundant (at least > > > for the thread race conditions you describe). > > > > That's not thread safe unless you're doing some very careful locking. > > Consider: > > T1 T2 > > oldfd=fd tmp=fd > > fd=-1 > > close(oldfd) > > unrelated open() > > read(tmp,... > > > > In practice every use of fd will be a copy into a tmp and then the > > syscall; the unrelated open() could happen in another thread. > > (OK, the gap between the tmp and the read is tiny, although if we're > > doing multiple operations chances are the compiler will optimise > > it to the top of a loop). > > > > There's no way to make that code safe. > > Urgh, yes, I see what you mean. > > Currently the QIOChannelCommand implementation, uses a pair of anonymous > pipes for stdin/out to the child process. I wonder if we could switch > that to use socketpair() instead, thus letting us shutdown() on it too. > > Though I guess it would be sufficient for qio_channel_shutdown() to > merely kill the child PID, while leaving the FDs open, as then you'd > get EOF and/or EPIPE on the read/writes. Yes, I guess it's a question of which one is more likely to actually kill the exec child off; the socketpair is more likely to cause the source side migration code to cancel cleanly, although a kill -9 should sort out a wayward exec child. Dave > Regards, > Daniel > -- > |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| > |: https://libvirt.org -o- https://fstop138.berrange.com :| > |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :| -- Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK