Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp610441ybl; Mon, 12 Aug 2019 23:19:23 -0700 (PDT) X-Google-Smtp-Source: APXvYqyVoX/hkYivfBrzsU2QeWQeL5yFf85BmajxoS72zT9lNl+OjuACUU6OOPrxUSd3x5YqVlFr X-Received: by 2002:a62:1a45:: with SMTP id a66mr13686210pfa.142.1565677163476; Mon, 12 Aug 2019 23:19:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565677163; cv=none; d=google.com; s=arc-20160816; b=gyFxCdFjh+zxUfuuh53mMF+53fyjRbiIrSFkppeSt8QSa0WPbWVj5eS6cHtmLCwAto s8g/RoXieHyaAKwu+P/IIUT/U/oQuuxhxY+xh2FnrG/GzpUSxUHMEMMo24DAbxnzUnV+ PecBwdR1E5XfukVUWeQbJ5iy9Oa68BN4Mp2IlEyYpg2hLhj7fBWCi5Lw52kjPDneb4Pv mvr1ry5xo6IXrWBOwVMFcFkN0rvJ8EqpTwLALXwNuOzV8VN5hEPJbq1pS2Tkp17BHnp0 ZiAHLNHXWJRwx8wTe4vyELwYvVzbd6khYfrv8q7jnkUDmmRcr2QTMaCT7cZgJlRx0JQ5 K7QQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:to:content-transfer-encoding:mime-version :message-id:date:subject:cc:from:dkim-signature; bh=lB2W34FFRFhSU+6xsPtiaBhXXvBNBgq1m83tH9jQCgk=; b=iixfv9pvLCYzGF2R0OuSvhRCw2UrSdYVFnz/SKtIh2JZ+S4PuCv8GHNCBVsEESzId2 f/4U8SZyNQW+kepXVBD8RVgzydZp5HWKY0i3ZnwIFOXqr/ASESVhSJmAZit020Et5BcC okpcpLDpgYQ+Pw8je8PWQM54XzjJuGvvC5i0qaxDTGtvHV8ZiEPN2c0/eTacQ9c4aKnV ph9Hueb3NzoqFnz4PjZ+fjxq4mAx9xOZBTVvJt8Puow61A0ZVq54f4KgIf+n4xrOIKat ZC++KJ40Ff1mXKHU3x7yiKdcTQvdsrEz+r97S7a/GcXg7P+vy+YcN1D2Ib/XpXeh0Z23 /wNA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=lR0mpVGl; 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=fail (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 x6si6098167pln.226.2019.08.12.23.19.07; Mon, 12 Aug 2019 23:19:23 -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=fail header.i=@gmail.com header.s=20161025 header.b=lR0mpVGl; 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=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727742AbfHMGQ5 (ORCPT + 99 others); Tue, 13 Aug 2019 02:16:57 -0400 Received: from mail-pl1-f195.google.com ([209.85.214.195]:34277 "EHLO mail-pl1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727404AbfHMGQ4 (ORCPT ); Tue, 13 Aug 2019 02:16:56 -0400 Received: by mail-pl1-f195.google.com with SMTP id i2so48881435plt.1; Mon, 12 Aug 2019 23:16:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=lB2W34FFRFhSU+6xsPtiaBhXXvBNBgq1m83tH9jQCgk=; b=lR0mpVGl7S5m6GiFXrWKwRq9XC7qJOtZEOUALc7cmgel+BKbdeD2iiqbSm6eSQvJMy nOuO40CVTkMbqWBy/vbyR1IBBGNQFVd/w+pb6qlW5FYWdsIqviQBhejEhP/0xveynMmI vYEX5DvPhD6WtMhiBVmS6NoNE8EpTDP2nprVIoqp1dD4M8iwW/ZIbzIrdvUnnxOlfiwW 3X2olAWRnWo5GuLkRTzNn2959BE6SlZ5JD72dOEX0yCVy9I8Ty+6ERxWw4yJSBnqZNq2 WH47O9SfGx1ufW3OOF+gpMj4koZoFQX2Ejw6wNAJrtnGzpt+Z5JDDBMandk4lOtnQLVE SzOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=lB2W34FFRFhSU+6xsPtiaBhXXvBNBgq1m83tH9jQCgk=; b=BcNU7JozQCrJnYAzxRcCbP9BwJTRJm7uevM3n4f0/Jv5SZKl79TncumAPB3EY4b1H3 8nuiA3ssuGsdqxcD/RmezF/t1lSKTBjhSrcLoqg6KDVSF64E5QYcxvJ0UOofyoMu21vQ PN+BUJjh9fjsEbZl7a9YLp9NEvGX1DtDkzrSYdcoFlR8H8Ymm+a9XiDZy4qppN5IQDx1 PwIo66n4oF1f+5aR5h/s5zeDpWnyXdwH6GZtx4eW2TShRonZn5oJ5Zlr4qAXywOuIPb+ 7FfrYYxBb8XJpZueUMvUgK6OdY4N003jxXwgBhXl7ldDu7HEtFP38f5VrkLsYHObvtel Fzjw== X-Gm-Message-State: APjAAAXGIiqVDD5JkPkbDCQtysKWuErrGbXdbEkLi+yZ0wh5JOWvG5Is LATyJzF9ZbH07hbHbo+h/Ig= X-Received: by 2002:a17:902:7202:: with SMTP id ba2mr37151447plb.266.1565677015327; Mon, 12 Aug 2019 23:16:55 -0700 (PDT) Received: from suzukaze.ipads-lab.se.sjtu.edu.cn ([89.31.126.54]) by smtp.gmail.com with ESMTPSA id j15sm140141434pfn.150.2019.08.12.23.16.52 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Mon, 12 Aug 2019 23:16:54 -0700 (PDT) From: Chuhong Yuan Cc: Konrad Rzeszutek Wilk , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Jens Axboe , xen-devel@lists.xenproject.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Chuhong Yuan Subject: [PATCH v2 3/3] xen/blkback: Use refcount_t for refcount Date: Tue, 13 Aug 2019 14:16:50 +0800 Message-Id: <20190813061650.5483-1-hslester96@gmail.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit To: unlisted-recipients:; (no To-header on input) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Reference counters are preferred to use refcount_t instead of atomic_t. This is because the implementation of refcount_t can prevent overflows and detect possible use-after-free. So convert atomic_t ref counters to refcount_t. Signed-off-by: Chuhong Yuan --- Changes in v2: - Also convert pending_req::pendcnt to refcount_t. drivers/block/xen-blkback/blkback.c | 6 +++--- drivers/block/xen-blkback/common.h | 9 +++++---- drivers/block/xen-blkback/xenbus.c | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/drivers/block/xen-blkback/blkback.c b/drivers/block/xen-blkback/blkback.c index fd1e19f1a49f..b24bb0aea35f 100644 --- a/drivers/block/xen-blkback/blkback.c +++ b/drivers/block/xen-blkback/blkback.c @@ -1098,7 +1098,7 @@ static void __end_block_io_op(struct pending_req *pending_req, * the grant references associated with 'request' and provide * the proper response on the ring. */ - if (atomic_dec_and_test(&pending_req->pendcnt)) + if (refcount_dec_and_test(&pending_req->pendcnt)) xen_blkbk_unmap_and_respond(pending_req); } @@ -1395,7 +1395,7 @@ static int dispatch_rw_block_io(struct xen_blkif_ring *ring, bio_set_op_attrs(bio, operation, operation_flags); } - atomic_set(&pending_req->pendcnt, nbio); + refcount_set(&pending_req->pendcnt, nbio); blk_start_plug(&plug); for (i = 0; i < nbio; i++) @@ -1424,7 +1424,7 @@ static int dispatch_rw_block_io(struct xen_blkif_ring *ring, fail_put_bio: for (i = 0; i < nbio; i++) bio_put(biolist[i]); - atomic_set(&pending_req->pendcnt, 1); + refcount_set(&pending_req->pendcnt, 1); __end_block_io_op(pending_req, BLK_STS_RESOURCE); msleep(1); /* back off a bit */ return -EIO; diff --git a/drivers/block/xen-blkback/common.h b/drivers/block/xen-blkback/common.h index 1d3002d773f7..824d64a8339b 100644 --- a/drivers/block/xen-blkback/common.h +++ b/drivers/block/xen-blkback/common.h @@ -35,6 +35,7 @@ #include #include #include +#include #include #include #include @@ -309,7 +310,7 @@ struct xen_blkif { struct xen_vbd vbd; /* Back pointer to the backend_info. */ struct backend_info *be; - atomic_t refcnt; + refcount_t refcnt; /* for barrier (drain) requests */ struct completion drain_complete; atomic_t drain; @@ -343,7 +344,7 @@ struct pending_req { struct xen_blkif_ring *ring; u64 id; int nr_segs; - atomic_t pendcnt; + refcount_t pendcnt; unsigned short operation; int status; struct list_head free_list; @@ -362,10 +363,10 @@ struct pending_req { (_v)->bdev->bd_part->nr_sects : \ get_capacity((_v)->bdev->bd_disk)) -#define xen_blkif_get(_b) (atomic_inc(&(_b)->refcnt)) +#define xen_blkif_get(_b) (refcount_inc(&(_b)->refcnt)) #define xen_blkif_put(_b) \ do { \ - if (atomic_dec_and_test(&(_b)->refcnt)) \ + if (refcount_dec_and_test(&(_b)->refcnt)) \ schedule_work(&(_b)->free_work);\ } while (0) diff --git a/drivers/block/xen-blkback/xenbus.c b/drivers/block/xen-blkback/xenbus.c index 3ac6a5d18071..ecc5f9c5bf3f 100644 --- a/drivers/block/xen-blkback/xenbus.c +++ b/drivers/block/xen-blkback/xenbus.c @@ -169,7 +169,7 @@ static struct xen_blkif *xen_blkif_alloc(domid_t domid) return ERR_PTR(-ENOMEM); blkif->domid = domid; - atomic_set(&blkif->refcnt, 1); + refcount_set(&blkif->refcnt, 1); init_completion(&blkif->drain_complete); INIT_WORK(&blkif->free_work, xen_blkif_deferred_free); -- 2.20.1