This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH RFC] libio: Fix deadlock between freopen and fclose [BZ #24963]
- From: Szabolcs Nagy <Szabolcs dot Nagy at arm dot com>
- To: 谢宜生(毅晟) <yisheng dot xys at alibaba-inc dot com>, "libc-alpha at sourceware dot org" <libc-alpha at sourceware dot org>
- Cc: nd <nd at arm dot com>, "rth at tamu dot ed" <rth at tamu dot ed>, "carlos at redhat dot com" <carlos at redhat dot com>, 宋丹峰(煜昊) <danfeng dot sdf at alibaba-inc dot com>
- Date: Mon, 23 Sep 2019 10:05:10 +0000
- Subject: Re: [PATCH RFC] libio: Fix deadlock between freopen and fclose [BZ #24963]
- Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=arm.com; dmarc=pass action=none header.from=arm.com; dkim=pass header.d=arm.com; arc=none
- Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=O6UidM0GFW1QBRY9f1X3KSlRgaQrJRdaAOhzUIFcm9k=; b=jLXT0odRQBHWSkZZc+dYzCJ/R62JW6JLMnr4gOTAXGu4Gwa0lQZzWicWE+lxNTrh4SC1mMKB1Sq3AmA+KNeu9dqYhmstnmCxoQ+FSUxqvjj8EMORZlVnHCUlmqvG8PRIw84Lx9n21cLkpzwZVivcytCgW//hw0/qW+jW4444uxzznqr+Yr+PsRBoTZQ8md7Wpj8fvJniJ0w1NhrOr0SM24HXr/upABW/xgQqF/4ovhzjyf94FYX9cXkIIlvLoN/9G6LM7RUHkFU08ppmvkc8BdX4uB6Mr7+IgbyBNCJegeRIEffn7wrznL4X6An80ri7pm0XXa4mjOHW8MCxZdh4sw==
- Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=dPcGIaKCwG9tiFaSCvTzWvg5pJy3Y5iBHGl9WRiCPCx56Chz9MhvxhnuGIsO/lJ5bup/8zXR77WzIu8kaJW6exiRVAQsuuq2izM8kY8PptoHPZd7Xp+4V6KmVrTENlCORGHAhoCDjQlknN3rmBPWy307GyIa/TmXko/mh2JFRcZ78NFGfAB41ZDidki6eJPJSBfMfXN6XHhwfosQKUDfbZ987p6WsbbpsOLiHu55d+7senYE38kH1FaUTJ2RgynWSSXZa2zOStL6YscP+WjqkLHLl6oTNvIbNvfUVAkApQo4JhrZSU+2l14nrlic0dz5n3+lxhJ2WlRUSbiVDvpo+w==
- Original-authentication-results: spf=none (sender IP is ) smtp.mailfrom=Szabolcs dot Nagy at arm dot com;
- References: <f6757145-6d1a-e1e5-6b2b-9f7b7e65e26b@alibaba-inc.com>
On 21/09/2019 04:21, 谢宜生(毅晟) wrote:
> we find a deadlock between fclose and freopen as following:
> CPU0 CPU1
> freopen fclose
> ->_IO_acquire_lock (fp) ->_IO_un_link
> ->_IO_file_close_it ->_IO_lock_lock(list_all_lock)
> ->_IO_un_link
> ->_IO_lock_lock (list_all_lock)<-wait here
> ->_IO_flockfile((_IO_FILE *) fp); <-- wait here
>
> As Carlos pointed that this maybe the bug of in _IO_new_fclose, which
> can be fixed by locking fp first, then with fp acquired lock the whole
> list.
if we want to keep freopen as is then an fp cannot
be locked after list_all_lock is held, if another
thread might freopen(fp).
this affects more than just fclose, e.g.
fflush(NULL) would have the same lock ordering
(first locks list_all_lock, then locks each fp)
this tells me that a different fix is needed,
either way there should be a bug report about
it in bugzilla (and the id referenced in the
changelog of a patch submission assuming you
have the fsf copyright assignment sorted out).
>
> Signed-off-by: Yisheng Xie <yisheng.xys@alibaba-inc.com>
> ---
> libio/iofclose.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/libio/iofclose.c b/libio/iofclose.c
> index 398b86d597..fe262cf6aa 100644
> --- a/libio/iofclose.c
> +++ b/libio/iofclose.c
> @@ -44,11 +44,11 @@ _IO_new_fclose (FILE *fp)
> return _IO_old_fclose (fp);
> #endif
>
> + _IO_acquire_lock (fp);
> /* First unlink the stream. */
> if (fp->_flags & _IO_IS_FILEBUF)
> _IO_un_link ((struct _IO_FILE_plus *) fp);
>
> - _IO_acquire_lock (fp);
> if (fp->_flags & _IO_IS_FILEBUF)
> status = _IO_file_close_it (fp);
> else
> --
> 2.23.0
>