qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Richard W.M. Jones" <rjones@redhat.com>
To: Jeff Cody <jcody@redhat.com>
Cc: qemu-devel@nongnu.org, qemu-block@nongnu.org,
	mitake.hitoshi@lab.ntt.co.jp, namei.unix@gmail.com,
	kwolf@redhat.com, eblake@redhat.com
Subject: Re: [Qemu-devel] [PATCH v3 1/7] block/ssh: don't call libssh2_init() in block_init()
Date: Wed, 8 Nov 2017 11:39:46 +0000	[thread overview]
Message-ID: <20171108113946.GO2787@redhat.com> (raw)
In-Reply-To: <27e0bf9dfb1370e8beb08c7b5ad6894540fff13a.1510093478.git.jcody@redhat.com>

On Tue, Nov 07, 2017 at 05:27:18PM -0500, Jeff Cody wrote:
> We don't need libssh2 failure to be fatal (we could just opt to not
> register the driver on failure). But, it is probably a good idea to
> avoid external library calls during the block_init(), and call the
> libssh2 global init function on the first usage, returning any errors.
> 
> Signed-off-by: Jeff Cody <jcody@redhat.com>
> ---
>  block/ssh.c | 37 ++++++++++++++++++++++++++-----------
>  1 file changed, 26 insertions(+), 11 deletions(-)
> 
> diff --git a/block/ssh.c b/block/ssh.c
> index b049a16..de81ec8 100644
> --- a/block/ssh.c
> +++ b/block/ssh.c
> @@ -83,12 +83,28 @@ typedef struct BDRVSSHState {
>      bool unsafe_flush_warning;
>  } BDRVSSHState;
>  
> -static void ssh_state_init(BDRVSSHState *s)
> +static bool ssh_libinit_called;
> +
> +static int ssh_state_init(BDRVSSHState *s, Error **errp)
>  {
> +    int ret;
> +
> +    if (!ssh_libinit_called) {
> +        ret = libssh2_init(0);
> +        if (ret) {
> +            error_setg(errp, "libssh2 initialization failed with %d", ret);
> +            return ret;
> +        }
> +        ssh_libinit_called = true;
> +    }
> +
> +
>      memset(s, 0, sizeof *s);
>      s->sock = -1;
>      s->offset = -1;
>      qemu_co_mutex_init(&s->lock);
> +
> +    return 0;

Is this thread safe?  Is there a case where multiple ssh URL opens
could race off against each other and we could end up calling
libssh2_init in parallel?  (According to the libssh2_init
documentation, the function is not thread-safe so should not be called
in parallel on multiple threads)

Rich.

>  }
>  
>  static void ssh_state_free(BDRVSSHState *s)
> @@ -773,7 +789,9 @@ static int ssh_file_open(BlockDriverState *bs, QDict *options, int bdrv_flags,
>      int ret;
>      int ssh_flags;
>  
> -    ssh_state_init(s);
> +    if (ssh_state_init(s, errp)) {
> +        return -EIO;
> +    }
>  
>      ssh_flags = LIBSSH2_FXF_READ;
>      if (bdrv_flags & BDRV_O_RDWR) {
> @@ -821,8 +839,13 @@ static int ssh_create(const char *filename, QemuOpts *opts, Error **errp)
>      BDRVSSHState s;
>      ssize_t r2;
>      char c[1] = { '\0' };
> +    Error *local_err = NULL;
>  
> -    ssh_state_init(&s);
> +    ret = ssh_state_init(&s, &local_err);
> +    if (local_err) {
> +        error_propagate(errp, local_err);
> +        return ret;
> +    }
>  
>      /* Get desired file size. */
>      total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
> @@ -1213,14 +1236,6 @@ static BlockDriver bdrv_ssh = {
>  
>  static void bdrv_ssh_init(void)
>  {
> -    int r;
> -
> -    r = libssh2_init(0);
> -    if (r != 0) {
> -        fprintf(stderr, "libssh2 initialization failed, %d\n", r);
> -        exit(EXIT_FAILURE);
> -    }
> -
>      bdrv_register(&bdrv_ssh);
>  }
>  
> -- 
> 2.9.5

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-builder quickly builds VMs from scratch
http://libguestfs.org/virt-builder.1.html

  parent reply	other threads:[~2017-11-08 11:39 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-07 22:27 [Qemu-devel] [PATCH v3 0/7] Code cleanup and minor fixes (for 2.11) Jeff Cody
2017-11-07 22:27 ` [Qemu-devel] [PATCH v3 1/7] block/ssh: don't call libssh2_init() in block_init() Jeff Cody
2017-11-07 22:48   ` Eric Blake
2017-11-08 11:39   ` Richard W.M. Jones [this message]
2017-11-07 22:27 ` [Qemu-devel] [PATCH v3 2/7] block/ssh: make compliant with coding guidelines Jeff Cody
2017-11-08 10:50   ` [Qemu-devel] [Qemu-block] " Darren Kenny
2017-11-08 11:38   ` [Qemu-devel] " Richard W.M. Jones
2017-11-07 22:27 ` [Qemu-devel] [PATCH v3 3/7] block/sheepdog: remove spurious NULL check Jeff Cody
2017-11-08 10:48   ` [Qemu-devel] [Qemu-block] " Darren Kenny
2017-11-07 22:27 ` [Qemu-devel] [PATCH v3 4/7] block/sheepdog: code beautification Jeff Cody
2017-11-08 10:52   ` [Qemu-devel] [Qemu-block] " Darren Kenny
2017-11-07 22:27 ` [Qemu-devel] [PATCH v3 5/7] block/curl: check error return of curl_global_init() Jeff Cody
2017-11-08 10:51   ` [Qemu-devel] [Qemu-block] " Darren Kenny
2017-11-08 11:41   ` [Qemu-devel] " Richard W.M. Jones
2017-11-07 22:27 ` [Qemu-devel] [PATCH v3 6/7] block/curl: fix minor memory leaks Jeff Cody
2017-11-08  0:13   ` Eric Blake
2017-11-08  5:08   ` Philippe Mathieu-Daudé
2017-11-08 11:45   ` Richard W.M. Jones
2017-11-07 22:27 ` [Qemu-devel] [PATCH v3 7/7] block/curl: code cleanup to comply with coding style Jeff Cody
2017-11-08 10:47   ` [Qemu-devel] [Qemu-block] " Darren Kenny
2017-11-08 14:26     ` Eric Blake
2017-11-08 14:50       ` Darren Kenny
2017-11-08 15:04         ` Paolo Bonzini
2017-11-08 15:01     ` Paolo Bonzini
2017-11-08 11:46   ` [Qemu-devel] " Richard W.M. Jones
2017-12-18 21:05 ` [Qemu-devel] [PATCH v3 0/7] Code cleanup and minor fixes (for 2.11) Jeff Cody

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171108113946.GO2787@redhat.com \
    --to=rjones@redhat.com \
    --cc=eblake@redhat.com \
    --cc=jcody@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mitake.hitoshi@lab.ntt.co.jp \
    --cc=namei.unix@gmail.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).