Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp2280166imm; Mon, 16 Jul 2018 05:31:31 -0700 (PDT) X-Google-Smtp-Source: AAOMgpeIAgGBv193hioJKZaqER8xpGDpBXJmfILJ+Ch2wBnSNiAzUi4Hm6uOeEzJ6SG3BDg8uiBc X-Received: by 2002:a63:b504:: with SMTP id y4-v6mr15025416pge.247.1531744291317; Mon, 16 Jul 2018 05:31:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531744291; cv=none; d=google.com; s=arc-20160816; b=t8jpyT5yL2GHByxj4cL/LMMF30B4RlNQknPKgTMVjzjZf06Im/8uyIBQ45DsK9YglT EWym8bq/KMFjBXqt6+F2xVhy4J63mhsQ2ho0L0iKpxjL+D8O8CpiwusnaXzOeHWlfoGK P03DyPyFI2h3n7sgwrvAXInbOgONkeITY4mTCAYYa/wYvwmR3jDYmeTakGcCgzYZ9rkm OUyobcHH20kKRtRPMUz3Inmk7R/MNV+B75BGAkU0yl058jMnP2vUZlU9vzv34NJmb7Uw HFCfFII8351R1u7pCmqEU8ual21a+gfM9eNmx7hYpNp45/dh7gad54ys1Q7+EKWECTVA EF5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:arc-authentication-results; bh=mLV3SHqiEws2z6u/CwCaicWCzZXL5gwaT3lh33MJUJg=; b=qfJBKL92NwyTZrFWvBjtxtxxyBrEKPzFZ+BlGWhjhedBIye5UhISEmi2Ml5VVC9mLW Lz+kbgK3svZE0mUfiRF5FwIugcy3QU9HbZ7iXAFkKet7wWFGZ7f+r5q1zXtk0opISQS+ uLiPTED9338mmxBw43pm8KiDedr0Z7K92GF3bnUFlH7icqAUzpI+JRKoYp66b7Y2Dpvd GYT7AO8YmIZxHb/00HeImJ5vOMQa2NC4jlT9ihW4+BXYhYtSlvA0+yByGpSi8R7Qnkvt +QIg9unvMBJDpcSUcqcgDEnSHwPAOkNvp3zSRYJwUgFetz+GqIW+UcmGjm05d2XyaHus jS6w== 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 s11-v6si29588641plp.464.2018.07.16.05.31.15; Mon, 16 Jul 2018 05:31: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; 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 S1729969AbeGPM5z (ORCPT + 99 others); Mon, 16 Jul 2018 08:57:55 -0400 Received: from Galois.linutronix.de ([146.0.238.70]:51720 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728466AbeGPM5z (ORCPT ); Mon, 16 Jul 2018 08:57:55 -0400 Received: from bigeasy by Galois.linutronix.de with local (Exim 4.80) (envelope-from ) id 1ff2eE-0004t0-JI; Mon, 16 Jul 2018 14:30:38 +0200 Date: Mon, 16 Jul 2018 14:30:38 +0200 From: Sebastian Andrzej Siewior To: linux-kernel@vger.kernel.org, Andrew Morton Cc: tglx@linutronix.de, Peter Zijlstra , Ingo Molnar , linux-mm@kvack.org Subject: Re: [PATCH 3/6] bdi: Use refcount_t for reference counting instead atomic_t Message-ID: <20180716123038.hxdc3dv7h3gyfjpd@linutronix.de> References: <20180703200141.28415-1-bigeasy@linutronix.de> <20180703200141.28415-4-bigeasy@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20180703200141.28415-4-bigeasy@linutronix.de> User-Agent: NeoMutt/20180622 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2018-07-03 22:01:38 [+0200], To linux-kernel@vger.kernel.org wrote: > refcount_t type and corresponding API should be used instead of atomic_t when > the variable is used as a reference counter. This allows to avoid accidental > refcounter overflows that might lead to use-after-free situations. Andrew, is it okay for you to collect this one (and 4/6 of this series, both bdi)? The prerequisites are already in Linus' tree. > Cc: linux-mm@kvack.org > Cc: Andrew Morton > Suggested-by: Peter Zijlstra > Acked-by: Peter Zijlstra (Intel) > 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-defs.h > index 24251762c20c..9a6bc0951cfa 100644 > --- a/include/linux/backing-dev-defs.h > +++ b/include/linux/backing-dev-defs.h > @@ -12,6 +12,7 @@ > #include > #include > #include > +#include > > struct page; > struct device; > @@ -75,7 +76,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 */ > > #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 *inode) > 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; > } > > static inline void wb_congested_put(struct bdi_writeback_congested *congested) > { > - if (atomic_dec_and_test(&congested->refcnt)) > + if (refcount_dec_and_test(&congested->refcnt)) > kfree(congested); > } > > diff --git a/mm/backing-dev.c b/mm/backing-dev.c > index 2e5d3df0853d..55a233d75f39 100644 > --- a/mm/backing-dev.c > +++ b/mm/backing-dev.c > @@ -438,10 +438,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 = new_congested; > - new_congested = 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; > } > > spin_unlock_irqrestore(&cgwb_lock, flags); > @@ -451,13 +451,13 @@ wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp) > if (!new_congested) > return NULL; > > - atomic_set(&new_congested->refcnt, 0); > + refcount_set(&new_congested->refcnt, 1); > new_congested->__bdi = bdi; > new_congested->blkcg_id = blkcg_id; > goto retry; > > found: > - atomic_inc(&congested->refcnt); > + refcount_inc(&congested->refcnt); > spin_unlock_irqrestore(&cgwb_lock, flags); > kfree(new_congested); > return congested; > @@ -474,7 +474,7 @@ void wb_congested_put(struct bdi_writeback_congested *congested) > unsigned long flags; > > 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; > } > @@ -804,7 +804,7 @@ static int cgwb_bdi_init(struct backing_dev_info *bdi) > if (!bdi->wb_congested) > return -ENOMEM; > > - atomic_set(&bdi->wb_congested->refcnt, 1); > + refcount_set(&bdi->wb_congested->refcnt, 1); > > err = wb_init(&bdi->wb, bdi, 1, GFP_KERNEL); > if (err) { > -- > 2.18.0 Sebastian