Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp5924891ybi; Sun, 21 Jul 2019 05:29:51 -0700 (PDT) X-Google-Smtp-Source: APXvYqyzxK7Zu9rbPkYXGVDb9UbbvYEsqpXSJnrpvD6M5u205Iie+zRl90oP6ToIfe1WqvEwOZLE X-Received: by 2002:a17:90a:b903:: with SMTP id p3mr70098830pjr.79.1563712191288; Sun, 21 Jul 2019 05:29:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563712191; cv=none; d=google.com; s=arc-20160816; b=sYyRFXXGkbXnSF4CsAvsrw+5YnpNFHYh2x38y2Jc3Kb9DiTNox6qF+Et7ka725nJwh RbxA5sFZWAua9XMgB89yboh37gb0MA67+SWFjXgfYjz7sm4ag+w0WB30DRNNLK47A5en 2roJjSqbKramVu7Gx1s81D0d5YbeD54Rkuvdekzj9tNuTLHcn0HwSREb3cIeo4rceOgI 85fgEp7+azLINAhNby5rP8RPvuPao29kVbVER36WWs9NXd5P83nNqHtqZ+WXTQUlo7ej MQn6cEw9GhSm8wA3nuc1BnjBHe+EvpZpEUcbz1r3JT9SDiasYF9Aq0uHKv09Hsgvx8o7 M9BA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=xeqzbQpEZe7708HS+/cbBpCLpTegfOHcZddgt74Ff14=; b=GYHTovgzRvp9hZ/xdBLpeZZOA9h9DuU7cF8ZACGBg2sGSrR5TMDe3fTtDlakpZ/Sxr nb2qgRKMgLxcWvlXLkeFR1SJFdhcXVdzBBGN0GeBpQE+pIGVJkFUOyxtYqTCB67mGPMF Sz5ILbze3lOyU+kw7AZXNOknIgkMTMlKa+i/vyu3LWkP9XeX93weuNGJaAFM36KVA+Et UfCUAaDwyRkq59aWZ+oMQm+ccDPvC0KnwxqabLmR2+0jwdD1FGmArLqfE1REaPL9Dj2b 2l8iY03HGpzCjwifjUXEqy1zdIIvJykDQzThemPMHo8x6leWYBOyGQWWhltpJTv2MTrX yDRQ== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y15si7291784pjp.90.2019.07.21.05.29.35; Sun, 21 Jul 2019 05:29:51 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726432AbfGUM2Q (ORCPT + 99 others); Sun, 21 Jul 2019 08:28:16 -0400 Received: from mx1.redhat.com ([209.132.183.28]:45020 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726187AbfGUM2Q (ORCPT ); Sun, 21 Jul 2019 08:28:16 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 422DB3082E24; Sun, 21 Jul 2019 12:28:15 +0000 (UTC) Received: from redhat.com (ovpn-120-23.rdu2.redhat.com [10.10.120.23]) by smtp.corp.redhat.com (Postfix) with SMTP id DD5C75F7C0; Sun, 21 Jul 2019 12:28:06 +0000 (UTC) Date: Sun, 21 Jul 2019 08:28:05 -0400 From: "Michael S. Tsirkin" To: paulmck@linux.vnet.ibm.com Cc: aarcange@redhat.com, akpm@linux-foundation.org, christian@brauner.io, davem@davemloft.net, ebiederm@xmission.com, elena.reshetova@intel.com, guro@fb.com, hch@infradead.org, james.bottomley@hansenpartnership.com, jasowang@redhat.com, jglisse@redhat.com, keescook@chromium.org, ldv@altlinux.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-parisc@vger.kernel.org, luto@amacapital.net, mhocko@suse.com, mingo@kernel.org, namit@vmware.com, peterz@infradead.org, syzkaller-bugs@googlegroups.com, viro@zeniv.linux.org.uk, wad@chromium.org Subject: RFC: call_rcu_outstanding (was Re: WARNING in __mmdrop) Message-ID: <20190721081933-mutt-send-email-mst@kernel.org> References: <0000000000008dd6bb058e006938@google.com> <000000000000964b0d058e1a0483@google.com> <20190721044615-mutt-send-email-mst@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190721044615-mutt-send-email-mst@kernel.org> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.46]); Sun, 21 Jul 2019 12:28:15 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Paul, others, So it seems that vhost needs to call kfree_rcu from an ioctl. My worry is what happens if userspace starts cycling through lots of these ioctls. Given we actually use rcu as an optimization, we could just disable the optimization temporarily - but the question would be how to detect an excessive rate without working too hard :) . I guess we could define as excessive any rate where callback is outstanding at the time when new structure is allocated. I have very little understanding of rcu internals - so I wanted to check that the following more or less implements this heuristic before I spend time actually testing it. Could others pls take a look and let me know? Thanks! Signed-off-by: Michael S. Tsirkin diff --git a/kernel/rcu/tiny.c b/kernel/rcu/tiny.c index 477b4eb44af5..067909521d72 100644 --- a/kernel/rcu/tiny.c +++ b/kernel/rcu/tiny.c @@ -125,6 +125,25 @@ void synchronize_rcu(void) } EXPORT_SYMBOL_GPL(synchronize_rcu); +/* + * Helpful for rate-limiting kfree_rcu/call_rcu callbacks. + */ +bool call_rcu_outstanding(void) +{ + unsigned long flags; + struct rcu_data *rdp; + bool outstanding; + + local_irq_save(flags); + rdp = this_cpu_ptr(&rcu_data); + outstanding = rcu_segcblist_empty(&rdp->cblist); + outstanding = rcu_ctrlblk.donetail != rcu_ctrlblk.curtail; + local_irq_restore(flags); + + return outstanding; +} +EXPORT_SYMBOL_GPL(call_rcu_outstanding); + /* * Post an RCU callback to be invoked after the end of an RCU grace * period. But since we have but one CPU, that would be after any diff --git a/kernel/rcu/tree.c b/kernel/rcu/tree.c index a14e5fbbea46..d4b9d61e637d 100644 --- a/kernel/rcu/tree.c +++ b/kernel/rcu/tree.c @@ -2482,6 +2482,24 @@ static void rcu_leak_callback(struct rcu_head *rhp) { } +/* + * Helpful for rate-limiting kfree_rcu/call_rcu callbacks. + */ +bool call_rcu_outstanding(void) +{ + unsigned long flags; + struct rcu_data *rdp; + bool outstanding; + + local_irq_save(flags); + rdp = this_cpu_ptr(&rcu_data); + outstanding = rcu_segcblist_empty(&rdp->cblist); + local_irq_restore(flags); + + return outstanding; +} +EXPORT_SYMBOL_GPL(call_rcu_outstanding); + /* * Helper function for call_rcu() and friends. The cpu argument will * normally be -1, indicating "currently running CPU". It may specify