From patchwork Tue Nov 10 13:53:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marco Elver X-Patchwork-Id: 11894383 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6BAF015E6 for ; Tue, 10 Nov 2020 13:53:29 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id F17C120780 for ; Tue, 10 Nov 2020 13:53:28 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="DUJbPjRA" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org F17C120780 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 2986B6B0036; Tue, 10 Nov 2020 08:53:28 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 247FA6B005C; Tue, 10 Nov 2020 08:53:28 -0500 (EST) 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 0C2256B005D; Tue, 10 Nov 2020 08:53:28 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id D4B706B0036 for ; Tue, 10 Nov 2020 08:53:27 -0500 (EST) Received: from smtpin01.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 62BC78249980 for ; Tue, 10 Nov 2020 13:53:27 +0000 (UTC) X-FDA: 77468650854.01.grain21_30007f5272f5 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin01.hostedemail.com (Postfix) with ESMTP id 43BF410045BE3 for ; Tue, 10 Nov 2020 13:53:27 +0000 (UTC) X-Spam-Summary: 1,0,0,01ecbc10a05357e2,d41d8cd98f00b204,3vzuqxwukcdoahranckkcha.ykihejqt-iigrwyg.knc@flex--elver.bounces.google.com,,RULES_HIT:41:152:355:379:541:800:960:967:973:988:989:1260:1277:1313:1314:1345:1431:1437:1516:1518:1534:1541:1593:1594:1711:1730:1747:1777:1792:2380:2393:2525:2553:2559:2563:2682:2685:2859:2902:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3152:3353:3743:3865:3867:3868:3870:3871:3872:3874:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4250:4321:5007:6119:6261:6653:7903:9025:9707:9969:10004:10400:11026:11473:11658:11914:12043:12296:12297:12438:12555:12679:12895:12986:13069:13161:13180:13221:13229:13311:13357:14096:14097:14181:14394:14659:14721:14777:21080:21324:21444:21451:21627:21740:21795:21939:21966:30051:30054:30070:30090,0,RBL:209.85.222.201:@flex--elver.bounces.google.com:.lbl8.mailshell.net-66.100.201.100 62.18.0.100;04yrp19rjab9yufcxtyrdhmnsznztopwjwinmizmhfh9w4fojitxz1as18mhucg.yfoeeyd7ufu919sg3zk5c1ipcajt7zsis8 znxwuq7u X-HE-Tag: grain21_30007f5272f5 X-Filterd-Recvd-Size: 4709 Received: from mail-qk1-f201.google.com (mail-qk1-f201.google.com [209.85.222.201]) by imf11.hostedemail.com (Postfix) with ESMTP for ; Tue, 10 Nov 2020 13:53:26 +0000 (UTC) Received: by mail-qk1-f201.google.com with SMTP id x85so8745127qka.14 for ; Tue, 10 Nov 2020 05:53:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:message-id:mime-version:subject:from:to:cc; bh=HVbq6NPlJzIOUbEGDX3ZF6qnq7VBRGwp+e76lTdscsU=; b=DUJbPjRAjovkdHD6ThomANkCAK7ZZQ2u6Fo8LeXwtDmilCOYXqj54JeRjRCs+KsnrH 0zFqj2cnA4eWrJbIm53srtIYPZH347mSVSZGW/4xKblX9MLrJoLTRweJ/Yqe9UGGbfv3 Bh2azZcDM80dtBeeB9F6G5qgldG3UK1R2Hd3IObypqYaGbV4WQAA1+TIxM3TwpnQ44SX pPYKj9Zat0WMbYTL2oj/CkMOqD+ddZVos+MJ0Jx2fNE5lcxYiKZu2BEjCj36b261kGwZ iSd6cGfSPJpyrXFlWquKJmUcparCI+eXF0A3pxVHxJFluNPTTvtN9dgdUbUnIVYkzold 8PbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:message-id:mime-version:subject:from :to:cc; bh=HVbq6NPlJzIOUbEGDX3ZF6qnq7VBRGwp+e76lTdscsU=; b=e3sT80W2N367BjTK+PRB56k+w4EfiNY3MhJua1K/1ci0+GREqWAJhabhhZ7Rd3wjHE Vuv70tOORyKyQhv9Kfonc1ae0bC7l+VeJDqH+tRjs39NnURLFbJz1ZC2aRiwSmizk1ZJ ZSVI/vYUakgAaG4WeM4Ma3jtT5sG1+JJ/jZdcsq8qhnichGF7yeRbCJH1uBvGg7MuWyU lOKlVZwd58zYtydkvjXCZnfIZmj4ak0QVAzTqjUUyWCgLpBSBowxlR3avfeIoQeYh+3F G907RlEo1O4QP4lmWTEt+cZGhvRPs0d/+l+yd0n8uXfxSWYWEnnCoZ5zL+0dV0QkfQEn bpsw== X-Gm-Message-State: AOAM533JlG03VLedRIY56XTaBQmVNT1mgwGeJANeH7oJi7CJ3+o5Ii26 tBxE2kIGWaa7tAGAjrU9K+d48oB4kA== X-Google-Smtp-Source: ABdhPJxngRBwrANr83qdaq0tL4gdsguRoCVoeHVqd7EocjSsbDLXyXPYCuolOIfM1+G1PNrv/qQdOwtg1w== X-Received: from elver.muc.corp.google.com ([2a00:79e0:15:13:f693:9fff:fef4:2449]) (user=elver job=sendgmr) by 2002:a05:6214:174f:: with SMTP id dc15mr18927895qvb.26.1605016405908; Tue, 10 Nov 2020 05:53:25 -0800 (PST) Date: Tue, 10 Nov 2020 14:53:20 +0100 Message-Id: <20201110135320.3309507-1-elver@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.29.2.222.g5d2a92d10f8-goog Subject: [PATCH] kfence: Avoid stalling work queue task without allocations From: Marco Elver To: elver@google.com, akpm@linux-foundation.org Cc: glider@google.com, dvyukov@google.com, jannh@google.com, mark.rutland@arm.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, kasan-dev@googlegroups.com, Anders Roxell 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: To toggle the allocation gates, we set up a delayed work that calls toggle_allocation_gate(). Here we use wait_event() to await an allocation and subsequently disable the static branch again. However, if the kernel has stopped doing allocations entirely, we'd wait indefinitely, and stall the worker task. This may also result in the appropriate warnings if CONFIG_DETECT_HUNG_TASK=y. Therefore, introduce a 1 second timeout and use wait_event_timeout(). If the timeout is reached, the static branch is disabled and a new delayed work is scheduled to try setting up an allocation at a later time. Note that, this scenario is very unlikely during normal workloads once the kernel has booted and user space tasks are running. It can, however, happen during early boot after KFENCE has been enabled, when e.g. running tests that do not result in any allocations. Link: https://lkml.kernel.org/r/CADYN=9J0DQhizAGB0-jz4HOBBh+05kMBXb4c0cXMS7Qi5NAJiw@mail.gmail.com Reported-by: Anders Roxell Signed-off-by: Marco Elver --- mm/kfence/core.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mm/kfence/core.c b/mm/kfence/core.c index 9358f42a9a9e..933b197b8634 100644 --- a/mm/kfence/core.c +++ b/mm/kfence/core.c @@ -592,7 +592,11 @@ static void toggle_allocation_gate(struct work_struct *work) /* Enable static key, and await allocation to happen. */ atomic_set(&allocation_gate, 0); static_branch_enable(&kfence_allocation_key); - wait_event(allocation_wait, atomic_read(&allocation_gate) != 0); + /* + * Await an allocation. Timeout after 1 second, in case the kernel stops + * doing allocations, to avoid stalling this worker task for too long. + */ + wait_event_timeout(allocation_wait, atomic_read(&allocation_gate) != 0, HZ); /* Disable static key and reset timer. */ static_branch_disable(&kfence_allocation_key);