Received: by 10.192.165.148 with SMTP id m20csp286172imm; Wed, 9 May 2018 12:41:18 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpoBSGpLH2qnaIFeFwB+5Db27fGhxWktXwhxVeikoiP9PS8VKxnEp+m6OFyaNGJFatods+1 X-Received: by 2002:a63:6f0c:: with SMTP id k12-v6mr37126456pgc.380.1525894878742; Wed, 09 May 2018 12:41:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525894878; cv=none; d=google.com; s=arc-20160816; b=iwfWtK8wJGOOg6vMqKGAgVZlq/xtzK7v/pv+1xGVWX6mv7LewO89JlCCOdaBYgeF+l eDGASXm55QWkfN3ZBsS3L0JOJwuDY3PXZyP2YtnfSXKkvKoWaqdNFxmjrfsncMXWX8UD YLLAHuj+QvhNS+gmk4mY22YAdQVdDLfjIP2O5TvzcWqrx9MwFI1PpjcOjVCIFmREyBAd NgoeVjr6lP/nPEdd4bhUYBdu/q8Thb4OedouI9pIEFv1Qmpln1dVLYnQb8w8SnqJgIjx 2FwUifNI/r4tGvx8DbdRtFC9h4q3TY0yZUT5TmZv99k78vxV2BIr0lH8jTjPS7Sy7ed3 Aq/A== 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:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=pSSdiBG49kFS7rct1U+VK+URGnRSo9XvLSOyzh1Z/7I=; b=FL8pTc9HtTxc8Ezth4b4cZERtkM8LXK4NUHp0+vHISfUgazgzzQSo6t1HWG4zannNG aSCiMGFDFnaHvRgvCxtR4nZkZFEk722lwh9FgvXOaNm9FtUdCvlWP78y885tuG0g8W1q slQ4c92dLmY56sFtbz9406a0gsvOqua/+OJVjLizWz5bZ9mjdZX+DKV/cZ3AT3EAKqZh 2FPVeH0BuV8CZvS3BrQD2VBehrOf8C3uZdkD4CDr/nnURADcwjvjgp0ACXdcYwnzac17 nAodmOyv52HtswHHKxeOMOYUzxyDaZhBo4tlg6CWH+v3GtsYba+RcBdOHKP/lgggIDPp L22A== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l65si25763411pfb.69.2018.05.09.12.41.04; Wed, 09 May 2018 12:41:18 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965553AbeEITis (ORCPT + 99 others); Wed, 9 May 2018 15:38:48 -0400 Received: from Galois.linutronix.de ([146.0.238.70]:34559 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S964944AbeEITiq (ORCPT ); Wed, 9 May 2018 15:38:46 -0400 Received: from localhost ([127.0.0.1] helo=bazinga.breakpoint.cc) by Galois.linutronix.de with esmtp (Exim 4.80) (envelope-from ) id 1fGUv9-0005v9-36; Wed, 09 May 2018 21:38:39 +0200 From: Sebastian Andrzej Siewior To: linux-kernel@vger.kernel.org Cc: tglx@linutronix.de, Peter Zijlstra , Ingo Molnar , linux-mm@kvack.org, Shaohua Li , linux-raid@vger.kernel.org, Anna-Maria Gleixner , Sebastian Andrzej Siewior Subject: [PATCH 1/8] bdi: use refcount_t for reference counting instead atomic_t Date: Wed, 9 May 2018 21:36:38 +0200 Message-Id: <20180509193645.830-2-bigeasy@linutronix.de> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180509193645.830-1-bigeasy@linutronix.de> References: <20180509193645.830-1-bigeasy@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org refcount_t type and corresponding API should be used instead of atomic_t wh= en the variable is used as a reference counter. This allows to avoid accidental refcounter overflows that might lead to use-after-free situations. Suggested-by: Peter Zijlstra Signed-off-by: Sebastian Andrzej Siewior --- include/linux/backing-dev-defs.h | 3 ++- include/linux/backing-dev.h | 4 ++-- mm/backing-dev.c | 12 ++++++------ 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/include/linux/backing-dev-defs.h b/include/linux/backing-dev-d= efs.h index 0bd432a4d7bd..81c75934ca5b 100644 --- a/include/linux/backing-dev-defs.h +++ b/include/linux/backing-dev-defs.h @@ -12,6 +12,7 @@ #include #include #include +#include =20 struct page; struct device; @@ -76,7 +77,7 @@ enum wb_reason { */ struct bdi_writeback_congested { unsigned long state; /* WB_[a]sync_congested flags */ - atomic_t refcnt; /* nr of attached wb's and blkg */ + refcount_t refcnt; /* nr of attached wb's and blkg */ =20 #ifdef CONFIG_CGROUP_WRITEBACK struct backing_dev_info *__bdi; /* the associated bdi, set to NULL diff --git a/include/linux/backing-dev.h b/include/linux/backing-dev.h index 72ca0f3d39f3..c28a47cbe355 100644 --- a/include/linux/backing-dev.h +++ b/include/linux/backing-dev.h @@ -404,13 +404,13 @@ static inline bool inode_cgwb_enabled(struct inode *i= node) static inline struct bdi_writeback_congested * wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t = gfp) { - atomic_inc(&bdi->wb_congested->refcnt); + refcount_inc(&bdi->wb_congested->refcnt); return bdi->wb_congested; } =20 static inline void wb_congested_put(struct bdi_writeback_congested *conges= ted) { - if (atomic_dec_and_test(&congested->refcnt)) + if (refcount_dec_and_test(&congested->refcnt)) kfree(congested); } =20 diff --git a/mm/backing-dev.c b/mm/backing-dev.c index 7441bd93b732..7984a872073e 100644 --- a/mm/backing-dev.c +++ b/mm/backing-dev.c @@ -450,10 +450,10 @@ wb_congested_get_create(struct backing_dev_info *bdi,= int blkcg_id, gfp_t gfp) if (new_congested) { /* !found and storage for new one already allocated, insert */ congested =3D new_congested; - new_congested =3D NULL; rb_link_node(&congested->rb_node, parent, node); rb_insert_color(&congested->rb_node, &bdi->cgwb_congested_tree); - goto found; + spin_unlock_irqrestore(&cgwb_lock, flags); + return congested; } =20 spin_unlock_irqrestore(&cgwb_lock, flags); @@ -463,13 +463,13 @@ wb_congested_get_create(struct backing_dev_info *bdi,= int blkcg_id, gfp_t gfp) if (!new_congested) return NULL; =20 - atomic_set(&new_congested->refcnt, 0); + refcount_set(&new_congested->refcnt, 1); new_congested->__bdi =3D bdi; new_congested->blkcg_id =3D blkcg_id; goto retry; =20 found: - atomic_inc(&congested->refcnt); + refcount_inc(&congested->refcnt); spin_unlock_irqrestore(&cgwb_lock, flags); kfree(new_congested); return congested; @@ -486,7 +486,7 @@ void wb_congested_put(struct bdi_writeback_congested *c= ongested) unsigned long flags; =20 local_irq_save(flags); - if (!atomic_dec_and_lock(&congested->refcnt, &cgwb_lock)) { + if (!refcount_dec_and_lock(&congested->refcnt, &cgwb_lock)) { local_irq_restore(flags); return; } @@ -794,7 +794,7 @@ static int cgwb_bdi_init(struct backing_dev_info *bdi) if (!bdi->wb_congested) return -ENOMEM; =20 - atomic_set(&bdi->wb_congested->refcnt, 1); + refcount_set(&bdi->wb_congested->refcnt, 1); =20 err =3D wb_init(&bdi->wb, bdi, 1, GFP_KERNEL); if (err) { --=20 2.17.0