From patchwork Mon Oct 30 21:46:32 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Lyle X-Patchwork-Id: 10033305 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 7AC206039A for ; Mon, 30 Oct 2017 21:47:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6CB1C28945 for ; Mon, 30 Oct 2017 21:47:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 61A6228946; Mon, 30 Oct 2017 21:47:01 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9FA882896F for ; Mon, 30 Oct 2017 21:47:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932586AbdJ3Vq7 (ORCPT ); Mon, 30 Oct 2017 17:46:59 -0400 Received: from mail-pf0-f193.google.com ([209.85.192.193]:55886 "EHLO mail-pf0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932294AbdJ3Vq5 (ORCPT ); Mon, 30 Oct 2017 17:46:57 -0400 Received: by mail-pf0-f193.google.com with SMTP id 17so12058484pfn.12 for ; Mon, 30 Oct 2017 14:46:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lyle-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=2PfNlExuqQ+EN1WBwKEFsyLggKnr6UjUUtPD2n6/UWI=; b=jntgXGXnh6OgczwwXZD7eD9R8l0koScIlpCyBTOZUH8zhJXluWwxhdIjULRv9e4QVu u8m3+GE17MnWuOsqLX0SM0916yoYs+pNZrGrP3fgQ0xbamwgCuZR03RAAhXuFW/wq2n/ 6v0mFi7SRlA6je31HdTk7Xoxsmj+O/EUOQgy83T4UgSmaOQOxJRAI/S9TJV7t4c/NPCi qe/tDuk/BIwJlsG/8DlT9nuwE8Pc0lJSshxpeduzJCQJAvH5fB0I5IVwpkFJlOwI3pXk IDU6TgqsbhuLnTs2mk2rQcYiFbQGcVSmMJzVmPuJ9CjfdbGvw7HbdYMznFfE1uxYTtOq X/ug== 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:in-reply-to :references; bh=2PfNlExuqQ+EN1WBwKEFsyLggKnr6UjUUtPD2n6/UWI=; b=SV5bmMVEtF0reK/dGgjNR37iD2DPsYA2i/oD3y7NLstjk6xxHBrXjCHCkQ18zoCLfR 13lySQq4UoWhz70Wp/rcn4MQMAZ1P87cPcFCRjVanms6LslRQbjJRi6vupXjluBV9leU gV3ZjkglcHxPeHbYjXCjaX9LCzl8MnPFhjj0iKGjXRTOAfOwkYXi5qTWA/UZvyKq0KaL 2nwBGz/sluyDT8Ni5lFlKnNwTNnKp/M6P/+rtFBp7hKEXGBqrz96AtAXPKKspo/9RajA OneLqb8wntNIt/i8is+FH4Sq/IwNeioxB5yXeHwNG07xTLjuL+sCNJXl64bLNoNbsJLM dGmQ== X-Gm-Message-State: AMCzsaUCwhbPmGq/2aB622d41VUhoUQGqWNT2f5IRxhT9ie1MN4pP6Zm wDRDeug6F1zYdUS1zNdX09PrVXWg7BM= X-Google-Smtp-Source: ABhQp+S1ipBiVjYyMRf/z3CMNk2hEJX16dqcpsrNM7erb65yhjSME9JGr+l6479ruSxTMktSH8MJWg== X-Received: by 10.84.215.9 with SMTP id k9mr8309669pli.284.1509400016991; Mon, 30 Oct 2017 14:46:56 -0700 (PDT) Received: from localhost.localdomain (68-189-67-104.dhcp.prtv.ca.charter.com. [68.189.67.104]) by smtp.gmail.com with ESMTPSA id t78sm23733329pgb.93.2017.10.30.14.46.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 30 Oct 2017 14:46:56 -0700 (PDT) From: Michael Lyle To: linux-bcache@vger.kernel.org, linux-block@vger.kernel.org Cc: axboe@fb.com, Elena Reshetova Subject: [PATCH 2/5] bcache: convert cached_dev.count from atomic_t to refcount_t Date: Mon, 30 Oct 2017 14:46:32 -0700 Message-Id: <20171030214635.29888-3-mlyle@lyle.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20171030214635.29888-1-mlyle@lyle.org> References: <20171030214635.29888-1-mlyle@lyle.org> Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Elena Reshetova atomic_t variables are currently used to implement reference counters with the following properties: - counter is initialized to 1 using atomic_set() - a resource is freed upon counter reaching zero - once counter reaches zero, its further increments aren't allowed - counter schema uses basic atomic operations (set, inc, inc_not_zero, dec_and_test, etc.) Such atomic variables should be converted to a newly provided refcount_t type and API that prevents accidental counter overflows and underflows. This is important since overflows and underflows can lead to use-after-free situation and be exploitable. The variable cached_dev.count is used as pure reference counter. Convert it to refcount_t and fix up the operations. Suggested-by: Kees Cook Reviewed-by: David Windsor Reviewed-by: Hans Liljestrand Reviewed-by: Michael Lyle Signed-off-by: Elena Reshetova --- drivers/md/bcache/bcache.h | 7 ++++--- drivers/md/bcache/super.c | 6 +++--- drivers/md/bcache/writeback.h | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/drivers/md/bcache/bcache.h b/drivers/md/bcache/bcache.h index d77c4829c497..363ea6256b39 100644 --- a/drivers/md/bcache/bcache.h +++ b/drivers/md/bcache/bcache.h @@ -184,6 +184,7 @@ #include #include #include +#include #include #include @@ -296,7 +297,7 @@ struct cached_dev { struct semaphore sb_write_mutex; /* Refcount on the cache set. Always nonzero when we're caching. */ - atomic_t count; + refcount_t count; struct work_struct detach; /* @@ -805,13 +806,13 @@ do { \ static inline void cached_dev_put(struct cached_dev *dc) { - if (atomic_dec_and_test(&dc->count)) + if (refcount_dec_and_test(&dc->count)) schedule_work(&dc->detach); } static inline bool cached_dev_get(struct cached_dev *dc) { - if (!atomic_inc_not_zero(&dc->count)) + if (!refcount_inc_not_zero(&dc->count)) return false; /* Paired with the mb in cached_dev_attach */ diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c index 72c3b2929ef0..46134c45c6f6 100644 --- a/drivers/md/bcache/super.c +++ b/drivers/md/bcache/super.c @@ -902,7 +902,7 @@ static void cached_dev_detach_finish(struct work_struct *w) closure_init_stack(&cl); BUG_ON(!test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags)); - BUG_ON(atomic_read(&dc->count)); + BUG_ON(refcount_read(&dc->count)); mutex_lock(&bch_register_lock); @@ -1029,7 +1029,7 @@ int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c) * dc->c must be set before dc->count != 0 - paired with the mb in * cached_dev_get() */ - atomic_set(&dc->count, 1); + refcount_set(&dc->count, 1); /* Block writeback thread, but spawn it */ down_write(&dc->writeback_lock); @@ -1041,7 +1041,7 @@ int bch_cached_dev_attach(struct cached_dev *dc, struct cache_set *c) if (BDEV_STATE(&dc->sb) == BDEV_STATE_DIRTY) { bch_sectors_dirty_init(&dc->disk); atomic_set(&dc->has_dirty, 1); - atomic_inc(&dc->count); + refcount_inc(&dc->count); bch_writeback_queue(dc); } diff --git a/drivers/md/bcache/writeback.h b/drivers/md/bcache/writeback.h index 34bcf49d737b..7d25bff37a9b 100644 --- a/drivers/md/bcache/writeback.h +++ b/drivers/md/bcache/writeback.h @@ -91,7 +91,7 @@ static inline void bch_writeback_add(struct cached_dev *dc) { if (!atomic_read(&dc->has_dirty) && !atomic_xchg(&dc->has_dirty, 1)) { - atomic_inc(&dc->count); + refcount_inc(&dc->count); if (BDEV_STATE(&dc->sb) != BDEV_STATE_DIRTY) { SET_BDEV_STATE(&dc->sb, BDEV_STATE_DIRTY);