Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp610558ybl; Mon, 12 Aug 2019 23:19:31 -0700 (PDT) X-Google-Smtp-Source: APXvYqwTXq2txGFkc1Dun8wqg9+0zc67vWbyjUGqprCmF5LI+uJgCheAGWW0v8Fglxkl9qjToO4h X-Received: by 2002:a17:902:4623:: with SMTP id o32mr35720147pld.112.1565677171545; Mon, 12 Aug 2019 23:19:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565677171; cv=none; d=google.com; s=arc-20160816; b=yNLpIOxJSfz9P5x86WSr/iABzvY3wpq6uCfyV5f6cuhEMm0szwXdsXQW3yzL/7Rnc0 JZPT+pRIgcn7up+v6IusnqjjvSjfvLd8P9BZf+tp4ooeaMwO9SUqaQKHN5h/tvUytd/V rPzdq5o9Nuv9alCMAheCu7xUU+maI6NBpJY5tn6y3OntbzLL1fLHkQDoGeMToPo5KfHw 7RQECr9sxPrBvQ+74TeKW6nyuhOeYx16KviIU/JkvhswbsgFseIb2XZBRM30VD7ekJCG KgHoagcC/eNHOtDIQUlCPvUKCmgA6vdqsDwQ3jnshQK1cNkm2Cb5N8mTaxcAdlfJKnMN YSSA== 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=nL9PyzNKtAFr+Adeyaa0mv/ZBzbqceeQurZwipQbI9U=; b=w/SkaVsZHocu7FupzXfOtNEuslMXfuuuMDSffNVlJMfHrc5dBl0KnfYeipR8HQ8x7C vobba/EWYjnPY4a0p/yG4um0R3UOK+IoGl1AmKxYtamUCvEbydm4jnHaNTQGmUW+fYqM vb6Ed6B/LEp77znHc+s3h8NJaUggRIUyM20lM/9Y3IyjJskGmmRH3u73jqU7jZhAJbwP 8s5U6lfMFE+haHxMBbyqqVZ/gSjDofThDnS/dnkSaY9Kp9uBUi6m6Ntq/mUtYsN6O/q+ UaDeiZrObFba2UYOD6H5BhUBTEEfVgFcaEY6wFgQyJVDJZuYb0207LjwGCYBugTtVBNv gj0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b="M6a1lbX/"; 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 k13si38175733pgt.285.2019.08.12.23.19.15; Mon, 12 Aug 2019 23:19:31 -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="M6a1lbX/"; 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 S1727704AbfHMGQs (ORCPT + 99 others); Tue, 13 Aug 2019 02:16:48 -0400 Received: from mail-pg1-f194.google.com ([209.85.215.194]:42403 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727371AbfHMGQr (ORCPT ); Tue, 13 Aug 2019 02:16:47 -0400 Received: by mail-pg1-f194.google.com with SMTP id p3so392785pgb.9; Mon, 12 Aug 2019 23:16:47 -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=nL9PyzNKtAFr+Adeyaa0mv/ZBzbqceeQurZwipQbI9U=; b=M6a1lbX/QMKRBowg/REKBlhpahKFtMjvvZG2VZlHVhn8xWNE8HMKDSiu2uNAFYXTLH 2az6Yz3meyyw5ob2HxAIYn3xlyYIZMsvr2BQShotqy9pxElgOb+4QqW6Ep/62XLU1n2w Vn/JWUFBYYG/i9oCq4qlSuAAdxX0e1Qqjy+CsNXrl80Q2ymuwCSg0PKeZ5zvDOYU0+CX a9y/1guN+eb+Ug6IiLSvmIB5eyENxmN5kpxxLT8zs3ezLZf1RILCDSFpz4GhvzUU4RgM Tj1tTp95hpuvfucv6lwUcCjHiAn5rk7bJt2IrhJazdYtQy5UthwgjeNagIUw28xYOMqr if+Q== 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=nL9PyzNKtAFr+Adeyaa0mv/ZBzbqceeQurZwipQbI9U=; b=UCtaFwH9HHLraucVm1E1/GgtwklTWVWgheGQABtuNHPFsFO932ZMVFjtt11PHUJb/A qSeTDlTmpsIgjPJGFs19dOqkGmM/nv8O2hNRaa7s9pVzisuI0uL//4+ZYDKNVp/Dr5QO b4uw5pG/sC7cGL8fu9s4p+0UH77el1HnImzM9okpQWlL6RMXF8HZti8fuCu58ssMpRZW +MPpIGTPfltAaFrdRd+5uZJ8i+phnYe39Gavu/M1vKvrU0xecZjb0CoglGBsXJ9aUBNI Wjg+XnMIXHEQHMwPwWAHmiPFK6/HszDgNfBobmJZ4fxZJ25o5iDu51FxGWntzz9xQQTi +wSQ== X-Gm-Message-State: APjAAAUbob5WLLkvNJpS2CFCho27ISvj1NWcuMDus+ZLINYg+roCN58E 4Y8J31F+a2jYw7VqhsQ3a/E= X-Received: by 2002:a17:90a:eb08:: with SMTP id j8mr772974pjz.72.1565677006908; Mon, 12 Aug 2019 23:16:46 -0700 (PDT) Received: from suzukaze.ipads-lab.se.sjtu.edu.cn ([89.31.126.54]) by smtp.gmail.com with ESMTPSA id k5sm6062037pfg.167.2019.08.12.23.16.43 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Mon, 12 Aug 2019 23:16:46 -0700 (PDT) From: Chuhong Yuan Cc: Ilya Dryomov , Sage Weil , Alex Elder , Jens Axboe , ceph-devel@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Chuhong Yuan Subject: [PATCH v2 2/3] rbd: Use refcount_t for refcount Date: Tue, 13 Aug 2019 14:16:41 +0800 Message-Id: <20190813061641.5428-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 --- drivers/block/rbd.c | 57 ++++++++------------------------------------- 1 file changed, 10 insertions(+), 47 deletions(-) diff --git a/drivers/block/rbd.c b/drivers/block/rbd.c index 3327192bb71f..74d2dddbe108 100644 --- a/drivers/block/rbd.c +++ b/drivers/block/rbd.c @@ -46,44 +46,12 @@ #include #include #include +#include #include "rbd_types.h" #define RBD_DEBUG /* Activate rbd_assert() calls */ -/* - * Increment the given counter and return its updated value. - * If the counter is already 0 it will not be incremented. - * If the counter is already at its maximum value returns - * -EINVAL without updating it. - */ -static int atomic_inc_return_safe(atomic_t *v) -{ - unsigned int counter; - - counter = (unsigned int)atomic_fetch_add_unless(v, 1, 0); - if (counter <= (unsigned int)INT_MAX) - return (int)counter; - - atomic_dec(v); - - return -EINVAL; -} - -/* Decrement the counter. Return the resulting value, or -EINVAL */ -static int atomic_dec_return_safe(atomic_t *v) -{ - int counter; - - counter = atomic_dec_return(v); - if (counter >= 0) - return counter; - - atomic_inc(v); - - return -EINVAL; -} - #define RBD_DRV_NAME "rbd" #define RBD_MINORS_PER_MAJOR 256 @@ -438,7 +406,7 @@ struct rbd_device { struct rbd_spec *parent_spec; u64 parent_overlap; - atomic_t parent_ref; + refcount_t parent_ref; struct rbd_device *parent; /* Block layer tags. */ @@ -1680,21 +1648,19 @@ static void rbd_dev_unparent(struct rbd_device *rbd_dev) */ static void rbd_dev_parent_put(struct rbd_device *rbd_dev) { - int counter; + bool is_dec_to_zero; if (!rbd_dev->parent_spec) return; - counter = atomic_dec_return_safe(&rbd_dev->parent_ref); - if (counter > 0) + is_dec_to_zero = refcount_dec_and_test_checked(&rbd_dev->parent_ref); + if (!is_dec_to_zero) return; /* Last reference; clean up parent data structures */ - if (!counter) + if (is_dec_to_zero) rbd_dev_unparent(rbd_dev); - else - rbd_warn(rbd_dev, "parent reference underflow"); } /* @@ -1707,20 +1673,17 @@ static void rbd_dev_parent_put(struct rbd_device *rbd_dev) */ static bool rbd_dev_parent_get(struct rbd_device *rbd_dev) { - int counter = 0; + bool is_inc_suc = false; if (!rbd_dev->parent_spec) return false; down_read(&rbd_dev->header_rwsem); if (rbd_dev->parent_overlap) - counter = atomic_inc_return_safe(&rbd_dev->parent_ref); + is_inc_suc = refcount_inc_not_zero_checked(&rbd_dev->parent_ref); up_read(&rbd_dev->header_rwsem); - if (counter < 0) - rbd_warn(rbd_dev, "parent reference overflow"); - - return counter > 0; + return is_inc_suc; } /* @@ -6823,7 +6786,7 @@ static int rbd_dev_probe_parent(struct rbd_device *rbd_dev, int depth) goto out_err; rbd_dev->parent = parent; - atomic_set(&rbd_dev->parent_ref, 1); + refcount_set(&rbd_dev->parent_ref, 1); return 0; out_err: -- 2.20.1