From patchwork Tue Jul 3 20:01:38 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sebastian Andrzej Siewior X-Patchwork-Id: 10505141 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 4DBA46035E for ; Tue, 3 Jul 2018 20:04:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3CC8B286FB for ; Tue, 3 Jul 2018 20:04:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 302F528CCA; Tue, 3 Jul 2018 20:04:14 +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=-2.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A4C1B28CA4 for ; Tue, 3 Jul 2018 20:04:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B64A66B0269; Tue, 3 Jul 2018 16:04:12 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id B39996B026A; Tue, 3 Jul 2018 16:04:12 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A51EE6B026B; Tue, 3 Jul 2018 16:04:12 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-wr0-f199.google.com (mail-wr0-f199.google.com [209.85.128.199]) by kanga.kvack.org (Postfix) with ESMTP id 3DFF66B0269 for ; Tue, 3 Jul 2018 16:04:12 -0400 (EDT) Received: by mail-wr0-f199.google.com with SMTP id i14-v6so1491324wrq.1 for ; Tue, 03 Jul 2018 13:04:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=1AjRxU9aYejEC3VeKKDOPukbSqF0pzPmp604UvJGrz0=; b=Ofh87Ok8zZTjjcsLH7t7zvyWrbciZz19/JhYZsSN5l8VJbJA5L4sHOO+6C4N/n1nJG qyzm0y/uBno7RYymfkz3v0eS7a3KFVpZOOvCy/2VZk8hzxLsE42FhS+XtrdsqPCRh9oT ge+p5YUCEJlW5Cjk3lPEVndaoVOcnaNvz5YHHwia9nFS3rgsCX/rk2aeE0DV3KzamUEe td6PT7kTSdb6mIenoyXiCOm4OLXe0HWTK1MlaVd4abLztA/M5heRluiyppheomFOV9T+ Mz/Ny2egOojBEdlS3mcYjCCr4VysP85RPO/8iFZN4Kx0zk+4Rur16zR8rduuZGzUPOgn 9CoQ== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of bigeasy@linutronix.de designates 2a01:7a0:2:106d:700::1 as permitted sender) smtp.mailfrom=bigeasy@linutronix.de X-Gm-Message-State: APt69E0cAv0jK28aDvXRWepXvlhqEj12c96K/KK09RJJosOUKsqefT2G 0xgke0Hr23us7MF8xlB8zpgE63OBtGQSIzKiVBHwlnipfHdmhtvUCEQlv6sHtufjT24GNFydbo9 /yGHoto1bdL06/IVxWTKkJFmaNnWQnppKlNzoQqjYgqEEld/FFwCzZmx7NCZnHShc9A== X-Received: by 2002:adf:9aeb:: with SMTP id a98-v6mr15104193wrc.110.1530648251814; Tue, 03 Jul 2018 13:04:11 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdxjJ9qmhwv+l0r95X/3pVpX+QWVDp4ulAi8ihLtbry8FuYzLV6/1q35rCPbPNlcABL+AB1 X-Received: by 2002:adf:9aeb:: with SMTP id a98-v6mr15104164wrc.110.1530648250966; Tue, 03 Jul 2018 13:04:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530648250; cv=none; d=google.com; s=arc-20160816; b=TcKsnR+e4/0wSllKHcjWw+XVfJ5SNOnpyCuj8LR5uBNYCdzkTupqI114mILSBoDGEA p+TMTtbgX9SySFgeYgPXatCi7fZlXnOuATupHpe6QZ5W0x7+ti4VAdH2MSePcwhLJ1gK VvD3qGhY7BqyKZti4ZQKutt9/I8S7rFOYI7JadXwvI56pdvIgGOoXNtgMN8O/LhpJTIf ZabY59oDM56dT5FKwPwhp/uzQJJ4ThGOnoYWqfmwd93pnC4gZrc92VcQNe6Um8cnB2rz w1Oeqffw0jY6MTaNrNQyeEU1DaXC7wfLKqUbgYIgYT4zpaHjcQRzBazDtHoyfIhAIw3N 2JGA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:arc-authentication-results; bh=1AjRxU9aYejEC3VeKKDOPukbSqF0pzPmp604UvJGrz0=; b=nur0A2SXg4dI+DkezbKuRKAR0HJTqbrZUAuFNcC4O6m7GW0XxN6NVeo2Opt4G1Ei2X iAsVhP/CaoY52uLv20B0AjrfE4mPDAhpmz5yxA9wzQ9HJ1k3rIw7M9mUtXVzRZ/rgDB3 lZge+isZWi3X4qO3pTVyv6shJtmoREO0FjgVd03QkG22N6j0iB9vtDmfVW52LaBAnjW7 a3qqSNTPfyIDTyCsE4wutVPYjFAMfYUfsqhPmYy3qzxOn0JjzbjAtsvC6YIbc7mDrtdG qEDyd/0ZR6uLnm9hTtCEJ4MAssUgQIvO/3NzIUhj9tCz9ltyu+emeuKHV8gzDPhT9bbL 6J4Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of bigeasy@linutronix.de designates 2a01:7a0:2:106d:700::1 as permitted sender) smtp.mailfrom=bigeasy@linutronix.de Received: from Galois.linutronix.de (Galois.linutronix.de. [2a01:7a0:2:106d:700::1]) by mx.google.com with ESMTPS id r6-v6si1619149wrv.78.2018.07.03.13.04.10 for (version=TLS1_2 cipher=AES128-SHA bits=128/128); Tue, 03 Jul 2018 13:04:10 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of bigeasy@linutronix.de designates 2a01:7a0:2:106d:700::1 as permitted sender) client-ip=2a01:7a0:2:106d:700::1; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of bigeasy@linutronix.de designates 2a01:7a0:2:106d:700::1 as permitted sender) smtp.mailfrom=bigeasy@linutronix.de Received: from localhost ([127.0.0.1] helo=bazinga.breakpoint.cc) by Galois.linutronix.de with esmtp (Exim 4.80) (envelope-from ) id 1faRUu-0001tM-9L; Tue, 03 Jul 2018 22:02:00 +0200 From: Sebastian Andrzej Siewior To: linux-kernel@vger.kernel.org Cc: tglx@linutronix.de, Peter Zijlstra , Ingo Molnar , Sebastian Andrzej Siewior , linux-mm@kvack.org, Andrew Morton Subject: [PATCH 3/6] bdi: Use refcount_t for reference counting instead atomic_t Date: Tue, 3 Jul 2018 22:01:38 +0200 Message-Id: <20180703200141.28415-4-bigeasy@linutronix.de> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180703200141.28415-1-bigeasy@linutronix.de> References: <20180703200141.28415-1-bigeasy@linutronix.de> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP 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. 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) {