Received: by 10.223.164.202 with SMTP id h10csp677685wrb; Wed, 15 Nov 2017 06:16:36 -0800 (PST) X-Google-Smtp-Source: AGs4zMbrhZ4LQd0+CqYLaLUsVGgEDIJfh2uFnkeej7zJRrwTISGpg9mNBo9mAmD4HreWgYRUXiS0 X-Received: by 10.98.234.9 with SMTP id t9mr17817094pfh.92.1510755395975; Wed, 15 Nov 2017 06:16:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510755395; cv=none; d=google.com; s=arc-20160816; b=GLDjzADaTmY67X+BCWhKFRun0VTqev0mVUzwuV7Yn8KPzKSSpHmypmSsnFR+/hD9im y31DN9n2pRAJ1pAwNvpRh0dUljU4R0JJ/ja/YLkOJZMYeFIWIkuKn4U+IHBFV7pCO1QG QkKbgbhrpEhMz8qXlDoB6FpBtg79f1XyjIwQVzCp/YmRUiylZPC4Ms7iZaCkoRDeRY6a iOCTzxjCH6JuShjM83+9k9bT+KtqWUXTl1H1HoGkwEF6AuWWyTo1IrnX25C/9DtRQbF2 CLiVRXBEP5fNLH8Eja2lsuKjd7cnd+OYQ55J7oRGleHbXYSYSQKzXzq+Q4AG85EzRN6X /2kw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=Cla8xd6BTMBbrcUbK+hqLTEaGlnyEiyyQgLXSfu0jIo=; b=MEimzB60978jTbkf6Asl5h6Lq2+u3dGYntp3YfefL4y13G0ao2qMM7MOBfnigJ3Zpa mwLZQ74/tMzYvYSwYUHurEPLMUAjVGoOydvU7vs82yOb0khI8eZkMe0BlmJHOveVFCSB XyS5wXXgNVLWSAwKuQvhyHdE3zpjCNwDWr02TgOH2NOlgqAaJLlXRFB5dDHRa/ozIhDr MT/ODc7LDtDyWeYCQtPe5u7c5UvVHa8PcUcOJH+9vIoavsBbPbmZaYf0ZM+U9ueSpHTL 3btKzpt1iw3QyT1It9HSMWL+amwWeKbM2e+amaOPpJBAirbhU26U9Ty448fqdeQ85v3c cHDg== 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 i3si18273468pld.296.2017.11.15.06.16.23; Wed, 15 Nov 2017 06:16:35 -0800 (PST) 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 S1758180AbdKOOKJ (ORCPT + 89 others); Wed, 15 Nov 2017 09:10:09 -0500 Received: from mga09.intel.com ([134.134.136.24]:49150 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1758100AbdKOOJM (ORCPT ); Wed, 15 Nov 2017 09:09:12 -0500 Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 15 Nov 2017 06:09:12 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.44,399,1505804400"; d="scan'208";a="2131363" Received: from elena-thinkpad-x230.fi.intel.com ([10.237.72.87]) by FMSMGA003.fm.intel.com with ESMTP; 15 Nov 2017 06:09:07 -0800 From: Elena Reshetova To: mingo@redhat.com Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, peterz@infradead.org, gregkh@linuxfoundation.org, viro@zeniv.linux.org.uk, tj@kernel.org, hannes@cmpxchg.org, lizefan@huawei.com, acme@kernel.org, alexander.shishkin@linux.intel.com, eparis@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, luto@kernel.org, keescook@chromium.org, tglx@linutronix.de, dvhart@infradead.org, ebiederm@xmission.com, linux-mm@kvack.org, axboe@kernel.dk, Elena Reshetova Subject: [PATCH 16/16] bdi: convert bdi_writeback_congested.refcnt from atomic_t to refcount_t Date: Wed, 15 Nov 2017 16:03:40 +0200 Message-Id: <1510754620-27088-17-git-send-email-elena.reshetova@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1510754620-27088-1-git-send-email-elena.reshetova@intel.com> References: <1510754620-27088-1-git-send-email-elena.reshetova@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 bdi_writeback_congested.refcnt is used as pure reference counter. Convert it to refcount_t and fix up the operations. **Important note for maintainers: Some functions from refcount_t API defined in lib/refcount.c have different memory ordering guarantees than their atomic counterparts. The full comparison can be seen in https://lkml.org/lkml/2017/11/15/57 and it is hopefully soon in state to be merged to the documentation tree. Normally the differences should not matter since refcount_t provides enough guarantees to satisfy the refcounting use cases, but in some rare cases it might matter. Please double check that you don't have some undocumented memory guarantees for this variable usage. For the bdi_writeback_congested.refcnt it might make a difference in following places: - wb_congested_put() in include/linux/backing-dev.h: decrement in refcount_dec_and_test() only provides RELEASE ordering and control dependency on success vs. fully ordered atomic counterpart - wb_congested_put() in mm/backing-dev.c: decrement in refcount_dec_and_lock() only provides RELEASE ordering, control dependency on success and hold spin_lock() on success vs. fully ordered atomic counterpart. Note, there is no difference in spin lock locking. Suggested-by: Kees Cook Reviewed-by: David Windsor Reviewed-by: Hans Liljestrand Signed-off-by: Elena Reshetova --- include/linux/backing-dev-defs.h | 3 ++- include/linux/backing-dev.h | 4 ++-- mm/backing-dev.c | 14 ++++++++------ 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/include/linux/backing-dev-defs.h b/include/linux/backing-dev-defs.h index bfe86b5..0b1bcce 100644 --- a/include/linux/backing-dev-defs.h +++ b/include/linux/backing-dev-defs.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -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 */ #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 e54e7e0..d6bac2e 100644 --- a/include/linux/backing-dev.h +++ b/include/linux/backing-dev.h @@ -402,13 +402,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 74b52df..e92a20f 100644 --- a/mm/backing-dev.c +++ b/mm/backing-dev.c @@ -440,14 +440,17 @@ wb_congested_get_create(struct backing_dev_info *bdi, int blkcg_id, gfp_t gfp) node = &parent->rb_left; else if (congested->blkcg_id > blkcg_id) node = &parent->rb_right; - else - goto found; + else { + refcount_inc(&congested->refcnt); + goto found; + } } if (new_congested) { /* !found and storage for new one already allocated, insert */ congested = new_congested; new_congested = NULL; + refcount_set(&congested->refcnt, 1); rb_link_node(&congested->rb_node, parent, node); rb_insert_color(&congested->rb_node, &bdi->cgwb_congested_tree); goto found; @@ -460,13 +463,12 @@ 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, 0); new_congested->__bdi = bdi; new_congested->blkcg_id = blkcg_id; goto retry; found: - atomic_inc(&congested->refcnt); spin_unlock_irqrestore(&cgwb_lock, flags); kfree(new_congested); return congested; @@ -483,7 +485,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; } @@ -793,7 +795,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.7.4 From 1584045433859330342@xxx Tue Nov 14 12:44:06 +0000 2017 X-GM-THRID: 1584045433859330342 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread