From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.5 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6C158C6377B for ; Wed, 21 Jul 2021 23:30:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4A1B061221 for ; Wed, 21 Jul 2021 23:30:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231243AbhGUWuV (ORCPT ); Wed, 21 Jul 2021 18:50:21 -0400 Received: from mail.kernel.org ([198.145.29.99]:56298 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230495AbhGUWuU (ORCPT ); Wed, 21 Jul 2021 18:50:20 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id AB7736121F; Wed, 21 Jul 2021 23:30:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1626910256; bh=VMOgEl7XFLHAT/9MyUGH631UPK2nLrJgvCVfTC76OXE=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=avyfBZd+m2fsiaqVE7LhlJdjZdPSMSSlFazuZF0elabwpN7xyAp35Xyrgyf74TXuM Hpx2hEe7va9UoGiICUmIpKg2RrJf6sFlGjgs0IkxbzMKSv/MeryAzJd/d3wqzXCq// Urxn/91473LXpqbWJV5pj+eFAO7+Ju3G8znaQgIeAftceXIwqKWBOfjoCYZwMmbnn+ JNdnRG1oV+NYjJNZo0cyqCzBe5/iK8cY/dODQ98Egm2uZo01iTdJv7LiUCkOly1FYQ lDtIUJ2JTTC6Y7etJYMgLmgqh10B2zpPuBXX6Snr/uQ//FZoD3o/FmDRLL+5pxxACK bvt+tbO6ZaaWg== Date: Wed, 21 Jul 2021 16:30:56 -0700 From: "Darrick J. Wong" To: Xiyu Yang Cc: Christoph Hellwig , linux-xfs@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, yuanxzhang@fudan.edu.cn, Xin Tan Subject: Re: [PATCH] iomap: Convert from atomic_t to refcount_t on iomap_dio->ref Message-ID: <20210721233056.GC8639@magnolia> References: <1626683544-64155-1-git-send-email-xiyuyang19@fudan.edu.cn> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1626683544-64155-1-git-send-email-xiyuyang19@fudan.edu.cn> Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org On Mon, Jul 19, 2021 at 04:32:24PM +0800, Xiyu Yang wrote: > refcount_t type and corresponding API can protect refcounters from > accidental underflow and overflow and further use-after-free situations. > > Signed-off-by: Xiyu Yang > Signed-off-by: Xin Tan > --- > fs/iomap/direct-io.c | 11 ++++++----- > 1 file changed, 6 insertions(+), 5 deletions(-) > > diff --git a/fs/iomap/direct-io.c b/fs/iomap/direct-io.c > index 9398b8c31323..d1393579a954 100644 > --- a/fs/iomap/direct-io.c > +++ b/fs/iomap/direct-io.c > @@ -3,6 +3,7 @@ > * Copyright (C) 2010 Red Hat, Inc. > * Copyright (c) 2016-2018 Christoph Hellwig. > */ > +#include > #include > #include > #include > @@ -28,7 +29,7 @@ struct iomap_dio { > const struct iomap_dio_ops *dops; > loff_t i_size; > loff_t size; > - atomic_t ref; > + refcount_t ref; > unsigned flags; > int error; > bool wait_for_completion; > @@ -62,7 +63,7 @@ EXPORT_SYMBOL_GPL(iomap_dio_iopoll); > static void iomap_dio_submit_bio(struct iomap_dio *dio, struct iomap *iomap, > struct bio *bio, loff_t pos) > { > - atomic_inc(&dio->ref); > + refcount_inc(&dio->ref); Seems all right to me, though I wonder if any of the more performance-minded people have comments about the overhead of refcount_t vs. atomic_t? Reviewed-by: Darrick J. Wong --D > > if (dio->iocb->ki_flags & IOCB_HIPRI) > bio_set_polled(bio, dio->iocb); > @@ -158,7 +159,7 @@ static void iomap_dio_bio_end_io(struct bio *bio) > if (bio->bi_status) > iomap_dio_set_error(dio, blk_status_to_errno(bio->bi_status)); > > - if (atomic_dec_and_test(&dio->ref)) { > + if (refcount_dec_and_test(&dio->ref)) { > if (dio->wait_for_completion) { > struct task_struct *waiter = dio->submit.waiter; > WRITE_ONCE(dio->submit.waiter, NULL); > @@ -471,7 +472,7 @@ __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, > return ERR_PTR(-ENOMEM); > > dio->iocb = iocb; > - atomic_set(&dio->ref, 1); > + refcount_set(&dio->ref, 1); > dio->size = 0; > dio->i_size = i_size_read(inode); > dio->dops = dops; > @@ -611,7 +612,7 @@ __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, > * after we got woken by the I/O completion handler. > */ > dio->wait_for_completion = wait_for_completion; > - if (!atomic_dec_and_test(&dio->ref)) { > + if (!refcount_dec_and_test(&dio->ref)) { > if (!wait_for_completion) > return ERR_PTR(-EIOCBQUEUED); > > -- > 2.7.4 >