Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.0 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 37CB9C433E2 for ; Wed, 15 Jul 2020 12:29:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1CD3C206E9 for ; Wed, 15 Jul 2020 12:29:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730278AbgGOM3Y (ORCPT ); Wed, 15 Jul 2020 08:29:24 -0400 Received: from [195.135.220.15] ([195.135.220.15]:42272 "EHLO mx2.suse.de" rhost-flags-FAIL-FAIL-OK-OK) by vger.kernel.org with ESMTP id S1729198AbgGOM3Y (ORCPT ); Wed, 15 Jul 2020 08:29:24 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 9C2BCAC83; Wed, 15 Jul 2020 12:29:26 +0000 (UTC) Received: by quack2.suse.cz (Postfix, from userid 1000) id 329081E12C9; Wed, 15 Jul 2020 14:29:23 +0200 (CEST) From: Jan Kara To: Cc: Amir Goldstein , Francesco Ruggeri , Jan Kara Subject: [PATCH] fanotify: Avoid softlockups when reading many events Date: Wed, 15 Jul 2020 14:29:21 +0200 Message-Id: <20200715122921.5995-1-jack@suse.cz> X-Mailer: git-send-email 2.16.4 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org When user provides large buffer for events and there are lots of events available, we can try to copy them all to userspace without scheduling which can softlockup the kernel (furthermore exacerbated by the contention on notification_lock). Add a scheduling point after copying each event. Note that usually the real underlying problem is the cost of fanotify event merging and the resulting contention on notification_lock but this is a cheap way to somewhat reduce the problem until we can properly address that. Reported-by: Francesco Ruggeri Signed-off-by: Jan Kara --- fs/notify/fanotify/fanotify_user.c | 5 +++++ 1 file changed, 5 insertions(+) This is a quick mending we can do immediately and is probably a good idea nevertheless... I'll queue it up if Amir agrees. diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index 63b5dffdca9e..d7f63aeca992 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -412,6 +412,11 @@ static ssize_t fanotify_read(struct file *file, char __user *buf, add_wait_queue(&group->notification_waitq, &wait); while (1) { + /* + * User can supply arbitrarily large buffer. Avoid softlockups + * in case there are lots of available events. + */ + cond_resched(); event = get_one_event(group, count); if (IS_ERR(event)) { ret = PTR_ERR(event); -- 2.16.4