Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp331791imm; Fri, 21 Sep 2018 15:31:15 -0700 (PDT) X-Google-Smtp-Source: ANB0Vdb50qbFcDNSN2vJz4+bdzM2UmERAtV3+h5xhkyFjPeYyUCf/lX200weP7XbqPlAS/wyRAZY X-Received: by 2002:aa7:8713:: with SMTP id b19-v6mr48503167pfo.151.1537569075526; Fri, 21 Sep 2018 15:31:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537569075; cv=none; d=google.com; s=arc-20160816; b=N+wiwOXrPKa25e37/1T6yNsy0QoqkbdHs7gdJRUrxArQNNb6m4wTDkkkajnjf7JiaU tXG5CnNY+dF2YJQvPeBqkK/F1VDP6KCRyVqo8nwCm7W+k+ATW22NeQg5/g9lKZtPm96J eGAmy2jZ8YE2QFQlRyEi3cX6vhfk0UoAWsng6LdMnfNNcxHszu5Hybp300F6oFEv41MW tp+dR2Lvj8PpLUDy9/KQ+Iz/JncCSp+XUdxi0xT7t6m4PoEz/54tr5cwj5OKSgnTWn6j iIUE1s+P+x9k/RXncg33V0+fshV9aIlngboagn2nf+WPRiDw4b7AQiq97spfVmCn6Ovg Dolg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=ZeWgS+diNE7/dHFBD7JXb2Rc03GJPWckP3zrVe94FtI=; b=vdYGbG6+raqiAvrBHzx05Xio7PfD/vHEP7YncxSbMC+01/DXTaVgjt0GCbzCr99SQe x6biuE6Z+ojbaPIRvOnq/KYqb1+2+JfITjh5EpbpIaSxTVfp0JEO59sgiEuK+lscWog8 OuDM3h34YL0w8IXLsG4V0bFrKQWiRDqWHMy8LbjaggRq9+nNINMcpxHR4ybIaOMKN09F QQKn6s5Rqo9+ADYjM4Z6Ycfntwqec549Fl7+M3aDyUeJrqk9+w1Z5YoTeSNjSVfQdhsB 3qiFVX/HZiB55dU2HzX3fwys2yqPmDY8EBtDugJXTIHFO/kSRRSEbIE2umAIUHYPBa8/ Hbbw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="EvBxrq3/"; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m6-v6si5509390pgg.265.2018.09.21.15.30.59; Fri, 21 Sep 2018 15:31:15 -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=@gmail.com header.s=20161025 header.b="EvBxrq3/"; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726376AbeIVEV1 (ORCPT + 99 others); Sat, 22 Sep 2018 00:21:27 -0400 Received: from mail-lj1-f196.google.com ([209.85.208.196]:40313 "EHLO mail-lj1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725750AbeIVEV1 (ORCPT ); Sat, 22 Sep 2018 00:21:27 -0400 Received: by mail-lj1-f196.google.com with SMTP id j19-v6so13372945ljc.7; Fri, 21 Sep 2018 15:30:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=ZeWgS+diNE7/dHFBD7JXb2Rc03GJPWckP3zrVe94FtI=; b=EvBxrq3/ZRLX1CSNYwXxhGsr3/vVZb8zIMpbNb8KhXYzXlCwIMm0K8UVsP+qUPel12 HsG6fMQWpqAIvfHAlBJXwKfH+DRIcQ0LXDnZaM24OmSWcdq+Q1Lq0qNl9UMoImwjfJLp eaNxnBi3B8QCAHTLDVtbRsFGQbamolqtd51WXVmor3dneRUYD9zHM3lNLY16sFcjC/aE zzI77uirXJ7ksFKdhjCNjn4oqCZy9sZ4XwGhEQepQlRuiNQEiBI7cQeQL+VxHHgb6aSI q1gdJTWGaA8tBCxrKahIUbqLEIKtYI8Ykyrq+b1rZShWswrE97Srt6EEc7r6Mx/B5fHc ZuAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=ZeWgS+diNE7/dHFBD7JXb2Rc03GJPWckP3zrVe94FtI=; b=e4qyYc662/YyOyYUOd1dLHEIZerIcHTy40KwsvnYRMduVQP0loPFxGJjm3dp7A0RxP WimbR3NiPHzP+9Kjkzh3GGpbAkPa+tI9MB8OTWNVRSNFvxN3y2q8uV2CfHiEdI9vPF/1 21KpUNB0TDtm4M8Mgpcqrd4Rrac9qmFMNHDwIpYeogz5NACxBRA17Ixkv2Qkcs47ZbYf 0b9siEOJe5CIeJOCGfgxcxzRhakC4hEESZyxfmk+3xSX+LRQwXvAcmY25PPlT2JpZGcm GdBR8C86ylz4RNAUDTsS3uvKhFATsSG+AI47L4ntw1B9aOH/Hv2Oq56q2t459Jk1AM9x yW+w== X-Gm-Message-State: APzg51CcsOLY2mYinl6wPEU3s8JLemgjEUDlCXouv0noem5B3BBTgsRn YXK1IsqwJKVZoVRODQYbx9vwAIsfzR1Qk5Iz3O9H6es= X-Received: by 2002:a2e:800e:: with SMTP id j14-v6mr3705186ljg.114.1537569030303; Fri, 21 Sep 2018 15:30:30 -0700 (PDT) MIME-Version: 1.0 References: <20180920211842.13721-1-longli@linuxonhyperv.com> <20180920211842.13721-4-longli@linuxonhyperv.com> In-Reply-To: <20180920211842.13721-4-longli@linuxonhyperv.com> From: Pavel Shilovsky Date: Fri, 21 Sep 2018 15:30:18 -0700 Message-ID: Subject: Re: [PATCH V3 (resend) 4/7] CIFS: Add support for direct I/O write To: Long Li Cc: Steve French , linux-cifs , samba-technical , Kernel Mailing List , linux-rdma@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org =D1=87=D1=82, 20 =D1=81=D0=B5=D0=BD=D1=82. 2018 =D0=B3. =D0=B2 14:22, Long = Li : > > From: Long Li > > With direct I/O write, user supplied buffers are pinned to the memory and= data > are transferred directly from user buffers to the transport layer. > > Change in v3: add support for kernel AIO > > Signed-off-by: Long Li > --- > fs/cifs/cifsfs.h | 1 + > fs/cifs/file.c | 196 ++++++++++++++++++++++++++++++++++++++++++++++---= ------ > 2 files changed, 166 insertions(+), 31 deletions(-) > > diff --git a/fs/cifs/cifsfs.h b/fs/cifs/cifsfs.h > index ed5479c..cc54051 100644 > --- a/fs/cifs/cifsfs.h > +++ b/fs/cifs/cifsfs.h > @@ -104,6 +104,7 @@ extern ssize_t cifs_user_readv(struct kiocb *iocb, st= ruct iov_iter *to); > extern ssize_t cifs_direct_readv(struct kiocb *iocb, struct iov_iter *to= ); > extern ssize_t cifs_strict_readv(struct kiocb *iocb, struct iov_iter *to= ); > extern ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *fro= m); > +extern ssize_t cifs_direct_writev(struct kiocb *iocb, struct iov_iter *f= rom); > extern ssize_t cifs_strict_writev(struct kiocb *iocb, struct iov_iter *f= rom); > extern int cifs_lock(struct file *, int, struct file_lock *); > extern int cifs_fsync(struct file *, loff_t, loff_t, int); > diff --git a/fs/cifs/file.c b/fs/cifs/file.c > index 6a939fa..2a5d209 100644 > --- a/fs/cifs/file.c > +++ b/fs/cifs/file.c > @@ -2537,6 +2537,8 @@ cifs_write_from_iter(loff_t offset, size_t len, str= uct iov_iter *from, > loff_t saved_offset =3D offset; > pid_t pid; > struct TCP_Server_Info *server; > + struct page **pagevec; > + size_t start; > > if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) > pid =3D open_file->pid; > @@ -2553,38 +2555,74 @@ cifs_write_from_iter(loff_t offset, size_t len, s= truct iov_iter *from, > if (rc) > break; > > - nr_pages =3D get_numpages(wsize, len, &cur_len); > - wdata =3D cifs_writedata_alloc(nr_pages, > + if (ctx->direct_io) { > + cur_len =3D iov_iter_get_pages_alloc( > + from, &pagevec, wsize, &start); > + if (cur_len < 0) { > + cifs_dbg(VFS, > + "direct_writev couldn't get user = pages " > + "(rc=3D%zd) iter type %d iov_offs= et %zd count" > + " %zd\n", > + cur_len, from->type, > + from->iov_offset, from->count); > + dump_stack(); > + break; > + } > + iov_iter_advance(from, cur_len); > + > + nr_pages =3D (cur_len + start + PAGE_SIZE - 1) / = PAGE_SIZE; > + > + wdata =3D cifs_writedata_direct_alloc(pagevec, > cifs_uncached_writev_complet= e); > - if (!wdata) { > - rc =3D -ENOMEM; > - add_credits_and_wake_if(server, credits, 0); > - break; > - } > + if (!wdata) { > + rc =3D -ENOMEM; > + add_credits_and_wake_if(server, credits, = 0); > + break; > + } > > - rc =3D cifs_write_allocate_pages(wdata->pages, nr_pages); > - if (rc) { > - kfree(wdata); > - add_credits_and_wake_if(server, credits, 0); > - break; > - } > > - num_pages =3D nr_pages; > - rc =3D wdata_fill_from_iovec(wdata, from, &cur_len, &num_= pages); > - if (rc) { > - for (i =3D 0; i < nr_pages; i++) > - put_page(wdata->pages[i]); > - kfree(wdata); > - add_credits_and_wake_if(server, credits, 0); > - break; > - } > + wdata->page_offset =3D start; > + wdata->tailsz =3D > + nr_pages > 1 ? > + cur_len - (PAGE_SIZE - start) - > + (nr_pages - 2) * PAGE_SIZE : > + cur_len; > + } else { > + nr_pages =3D get_numpages(wsize, len, &cur_len); > + wdata =3D cifs_writedata_alloc(nr_pages, > + cifs_uncached_writev_complet= e); > + if (!wdata) { > + rc =3D -ENOMEM; > + add_credits_and_wake_if(server, credits, = 0); > + break; > + } > > - /* > - * Bring nr_pages down to the number of pages we actually= used, > - * and free any pages that we didn't use. > - */ > - for ( ; nr_pages > num_pages; nr_pages--) > - put_page(wdata->pages[nr_pages - 1]); > + rc =3D cifs_write_allocate_pages(wdata->pages, nr= _pages); > + if (rc) { > + kfree(wdata); > + add_credits_and_wake_if(server, credits, = 0); > + break; > + } > + > + num_pages =3D nr_pages; > + rc =3D wdata_fill_from_iovec(wdata, from, &cur_le= n, &num_pages); > + if (rc) { > + for (i =3D 0; i < nr_pages; i++) > + put_page(wdata->pages[i]); > + kfree(wdata); > + add_credits_and_wake_if(server, credits, = 0); > + break; > + } > + > + /* > + * Bring nr_pages down to the number of pages we = actually used, > + * and free any pages that we didn't use. > + */ > + for ( ; nr_pages > num_pages; nr_pages--) > + put_page(wdata->pages[nr_pages - 1]); > + > + wdata->tailsz =3D cur_len - ((nr_pages - 1) * PAG= E_SIZE); > + } > > wdata->sync_mode =3D WB_SYNC_ALL; > wdata->nr_pages =3D nr_pages; > @@ -2593,7 +2631,6 @@ cifs_write_from_iter(loff_t offset, size_t len, str= uct iov_iter *from, > wdata->pid =3D pid; > wdata->bytes =3D cur_len; > wdata->pagesz =3D PAGE_SIZE; > - wdata->tailsz =3D cur_len - ((nr_pages - 1) * PAGE_SIZE); > wdata->credits =3D credits; > wdata->ctx =3D ctx; > kref_get(&ctx->refcount); > @@ -2687,8 +2724,9 @@ static void collect_uncached_write_data(struct cifs= _aio_ctx *ctx) > kref_put(&wdata->refcount, cifs_uncached_writedata_releas= e); > } > > - for (i =3D 0; i < ctx->npages; i++) > - put_page(ctx->bv[i].bv_page); > + if (!ctx->direct_io) > + for (i =3D 0; i < ctx->npages; i++) > + put_page(ctx->bv[i].bv_page); > > cifs_stats_bytes_written(tcon, ctx->total_len); > set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(dentry->d_inode)->flags= ); > @@ -2703,6 +2741,102 @@ static void collect_uncached_write_data(struct ci= fs_aio_ctx *ctx) > complete(&ctx->done); > } > > +ssize_t cifs_direct_writev(struct kiocb *iocb, struct iov_iter *from) the same comment as for readv: this function is almost identical to cifs_user_writev, can't the latter be refactored to avoid code duplication? > +{ > + struct file *file =3D iocb->ki_filp; > + ssize_t total_written =3D 0; > + struct cifsFileInfo *cfile; > + struct cifs_tcon *tcon; > + struct cifs_sb_info *cifs_sb; > + struct TCP_Server_Info *server; > + size_t len =3D iov_iter_count(from); > + int rc; > + struct cifs_aio_ctx *ctx; > + > + /* > + * iov_iter_get_pages_alloc doesn't work with ITER_KVEC. > + * In this case, fall back to non-direct write function. > + * this could be improved by getting pages directly in ITER_KVEC > + */ > + if (from->type & ITER_KVEC) { > + cifs_dbg(FYI, "use non-direct cifs_user_writev for kvec I= /O\n"); > + return cifs_user_writev(iocb, from); > + } > + > + rc =3D generic_write_checks(iocb, from); > + if (rc <=3D 0) > + return rc; > + > + cifs_sb =3D CIFS_FILE_SB(file); > + cfile =3D file->private_data; > + tcon =3D tlink_tcon(cfile->tlink); > + server =3D tcon->ses->server; > + > + if (!server->ops->async_writev) > + return -ENOSYS; > + > + ctx =3D cifs_aio_ctx_alloc(); > + if (!ctx) > + return -ENOMEM; > + > + ctx->cfile =3D cifsFileInfo_get(cfile); > + > + if (!is_sync_kiocb(iocb)) > + ctx->iocb =3D iocb; > + > + ctx->pos =3D iocb->ki_pos; > + > + ctx->direct_io =3D true; > + ctx->iter =3D *from; > + ctx->len =3D len; > + > + /* grab a lock here due to read response handlers can access ctx = */ > + mutex_lock(&ctx->aio_mutex); > + > + rc =3D cifs_write_from_iter(iocb->ki_pos, ctx->len, from, > + cfile, cifs_sb, &ctx->list, ctx); > + > + /* > + * If at least one write was successfully sent, then discard any = rc > + * value from the later writes. If the other write succeeds, then > + * we'll end up returning whatever was written. If it fails, then > + * we'll get a new rc value from that. > + */ > + if (!list_empty(&ctx->list)) > + rc =3D 0; > + > + mutex_unlock(&ctx->aio_mutex); > + > + if (rc) { > + kref_put(&ctx->refcount, cifs_aio_ctx_release); > + return rc; > + } > + > + if (!is_sync_kiocb(iocb)) { > + kref_put(&ctx->refcount, cifs_aio_ctx_release); > + return -EIOCBQUEUED; > + } > + > + rc =3D wait_for_completion_killable(&ctx->done); > + if (rc) { > + mutex_lock(&ctx->aio_mutex); > + ctx->rc =3D rc =3D -EINTR; > + total_written =3D ctx->total_len; > + mutex_unlock(&ctx->aio_mutex); > + } else { > + rc =3D ctx->rc; > + total_written =3D ctx->total_len; > + } > + > + kref_put(&ctx->refcount, cifs_aio_ctx_release); > + > + if (unlikely(!total_written)) > + return rc; > + > + iocb->ki_pos +=3D total_written; > + return total_written; > +} > + > ssize_t cifs_user_writev(struct kiocb *iocb, struct iov_iter *from) > { > struct file *file =3D iocb->ki_filp; > -- > 2.7.4 > -- Best regards, Pavel Shilovsky