Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp371658rwb; Tue, 13 Dec 2022 19:11:59 -0800 (PST) X-Google-Smtp-Source: AA0mqf7gE8EO/8Zbyafwre+//CuhazXbajh8SltChu29a2aXxJWE3p9Dp7mSd6D7fuyaOJM6SlKQ X-Received: by 2002:a05:6a20:b557:b0:ad:2c7c:9510 with SMTP id ev23-20020a056a20b55700b000ad2c7c9510mr18835832pzb.19.1670987518898; Tue, 13 Dec 2022 19:11:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670987518; cv=none; d=google.com; s=arc-20160816; b=DgN0IWBYTKluq/Eoo23HKC5PypWTbbPeuGL+VRhrFfdwAz4Waetdns29BcTBpUPvNo f7pjUCdUyRGLb51roFPJVTUJJbBqJRYt9M1BsKjWTXNLnfEnvIgokO0hQkjeLsY2jinf K8kU30AdVP+LM092bXf1eXu+PNj9BbD7iKqHftdMxTcxkKg0EtKN95yb1vFQoJc9ZuQt 3VyrBlitpYRLSi7wCBm1nzemg/BUtNOyycpb4LnwKaSNCp1/Sec7vo2OhWNMo+xYGdJg y7ul/EuAf2QI0CdpJPxfG8+umWZbjlkxsMoW72mK9QbmyDoNd6aZVb2vdu8jUz0oKH0Q ugUw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=QJTsGV35CYOraEoRISqpKpr/2uwkfrZs9x/0odiJU0M=; b=I5A9ltQRbt4QGVli5aLuTjuNVApkfJ6lA1DypkSuWf1sMHvlU/8llF8PVeJ0y1kHko U1U43x66hmMWyzaDMRzwPYXjrwMT3ZFE5/is/S+DivGsmN7Ct480GkiNvmXYzjdOg5zi NIs25JSJhaYPcWqNLQhzFsJ9wUkz1nM5dGPjh3Sw+VSjGJzSdbv/N2EVUK3eqQPIhfx+ ySLypaKXx7Hz3VULDN6ju+QwoIl44r8Zzv/m2vAIAEiL5VM99GIymP4F5N1iW5Kti3gv dKeW5ffAHxoCKQDFYPoVYLzhTDq+XA0Ex635PUmF5dRrwk88zPUGgAJsREUq5tePkXxM 2G0A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=cfca+OB7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u20-20020a634714000000b0047839ae135csi13892905pga.434.2022.12.13.19.11.49; Tue, 13 Dec 2022 19:11:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=cfca+OB7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237054AbiLNCwf (ORCPT + 71 others); Tue, 13 Dec 2022 21:52:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46262 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236608AbiLNCwV (ORCPT ); Tue, 13 Dec 2022 21:52:21 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6DADA17AB5 for ; Tue, 13 Dec 2022 18:51:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670986292; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QJTsGV35CYOraEoRISqpKpr/2uwkfrZs9x/0odiJU0M=; b=cfca+OB7wUvQs+nnyPs5gOIEsfk3zUtlcqHM4lL17r//GII9ubIz5fkVI8epPrbtv94c0b QS3LwDYSuKwb7ZL1meRlHktYti/n4Jbkvr2pZDNSIek/0r0NL2AMFylT4q8XFf77UVfc8c MaggVteFr8nKv3IT2BzPxzUmWBQ/3qo= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-279-J8NTIGfpNlG_jTgAlDFv2Q-1; Tue, 13 Dec 2022 21:51:31 -0500 X-MC-Unique: J8NTIGfpNlG_jTgAlDFv2Q-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C42023832786; Wed, 14 Dec 2022 02:51:30 +0000 (UTC) Received: from localhost (ovpn-8-24.pek2.redhat.com [10.72.8.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id ECD2540ED784; Wed, 14 Dec 2022 02:51:29 +0000 (UTC) From: Ming Lei To: Jens Axboe , Tejun Heo Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Zhong Jinghua , Yu Kuai , Dennis Zhou , Ming Lei Subject: [PATCH 3/3] lib/percpu-refcount: drain ->release() in perpcu_ref_exit() Date: Wed, 14 Dec 2022 10:51:01 +0800 Message-Id: <20221214025101.1268437-4-ming.lei@redhat.com> In-Reply-To: <20221214025101.1268437-1-ming.lei@redhat.com> References: <20221214025101.1268437-1-ming.lei@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The pattern of wait_event(percpu_ref_is_zero()) has been used in several kernel components, and this way actually has the following risk: - percpu_ref_is_zero() can be returned just between atomic_long_sub_and_test() and ref->data->release(ref) - given the refcount is found as zero, percpu_ref_exit() could be called, and the host data structure is freed - then use-after-free is triggered in ->release() when the user host data structure is freed after percpu_ref_exit() returns Reported-by: Zhong Jinghua Fixes: 2b0d3d3e4fcf ("percpu_ref: reduce memory footprint of percpu_ref in fast path") Signed-off-by: Ming Lei --- include/linux/percpu-refcount.h | 41 ++++++++++++++++++++++----------- lib/percpu-refcount.c | 22 ++++++++++++++++++ 2 files changed, 50 insertions(+), 13 deletions(-) diff --git a/include/linux/percpu-refcount.h b/include/linux/percpu-refcount.h index 006c6aae261e..6ef29ebffd58 100644 --- a/include/linux/percpu-refcount.h +++ b/include/linux/percpu-refcount.h @@ -55,6 +55,7 @@ #include #include #include +#include struct percpu_ref; typedef void (percpu_ref_func_t)(struct percpu_ref *); @@ -104,6 +105,7 @@ struct percpu_ref_data { bool force_atomic:1; bool allow_reinit:1; bool auto_exit:1; + bool being_release:1; struct rcu_head rcu; struct percpu_ref *ref; }; @@ -137,6 +139,7 @@ void percpu_ref_kill_and_confirm(struct percpu_ref *ref, void percpu_ref_resurrect(struct percpu_ref *ref); void percpu_ref_reinit(struct percpu_ref *ref); bool percpu_ref_is_zero(struct percpu_ref *ref); +wait_queue_head_t *percpu_ref_get_switch_waitq(void); /** * percpu_ref_kill - drop the initial ref @@ -319,6 +322,29 @@ static inline bool percpu_ref_tryget_live(struct percpu_ref *ref) return ret; } +/* Internal helper, please do not call it outside */ +static inline void __percpu_ref_put_many(struct percpu_ref *ref, + unsigned long nr) +{ + struct percpu_ref_data *data = ref->data; + struct percpu_ref copy = *ref; + bool release = false; + + data->being_release = 1; + if (unlikely(atomic_long_sub_and_test(nr, &data->count))) { + data->release(ref); + release = true; + } + data->being_release = 0; + + if (release) { + if (data->auto_exit) + percpu_ref_exit(©); + /* re-use switch waitq for ack the release done */ + wake_up_all(percpu_ref_get_switch_waitq()); + } +} + /** * percpu_ref_put_many - decrement a percpu refcount * @ref: percpu_ref to put @@ -337,19 +363,8 @@ static inline void percpu_ref_put_many(struct percpu_ref *ref, unsigned long nr) if (__ref_is_percpu(ref, &percpu_count)) this_cpu_sub(*percpu_count, nr); - else { - struct percpu_ref_data *data = ref->data; - struct percpu_ref copy = *ref; - bool release = false; - - if (unlikely(atomic_long_sub_and_test(nr, &data->count))) { - data->release(ref); - release = true; - } - - if (release && data->auto_exit) - percpu_ref_exit(©); - } + else + __percpu_ref_put_many(ref, nr); rcu_read_unlock(); } diff --git a/lib/percpu-refcount.c b/lib/percpu-refcount.c index c0cadf92948f..fd50eda233ed 100644 --- a/lib/percpu-refcount.c +++ b/lib/percpu-refcount.c @@ -140,6 +140,22 @@ void percpu_ref_exit(struct percpu_ref *ref) if (!data) return; + /* + * We may reach here because wait_event(percpu_ref_is_zero()) + * returns, and ->release() may not be completed or even started + * ye, then use-after-free is caused, so drain ->release() here + */ + if (!data->auto_exit) { + /* + * Order reading the atomic count in percpu_ref_is_zero + * and reading data->being_release. The counter pair is + * the one implied in atomic_long_sub_and_test() called + * from __percpu_ref_put_many(). + */ + smp_rmb(); + wait_event(percpu_ref_switch_waitq, !data->being_release); + } + spin_lock_irqsave(&percpu_ref_switch_lock, flags); ref->percpu_count_ptr |= atomic_long_read(&ref->data->count) << __PERCPU_REF_FLAG_BITS; @@ -480,3 +496,9 @@ void percpu_ref_resurrect(struct percpu_ref *ref) spin_unlock_irqrestore(&percpu_ref_switch_lock, flags); } EXPORT_SYMBOL_GPL(percpu_ref_resurrect); + +wait_queue_head_t *percpu_ref_get_switch_waitq() +{ + return &percpu_ref_switch_waitq; +} +EXPORT_SYMBOL_GPL(percpu_ref_get_switch_waitq); -- 2.38.1