From patchwork Wed Jan 17 16:14:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Poimboeuf X-Patchwork-Id: 13521987 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6B7E7C47DAF for ; Wed, 17 Jan 2024 16:15:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F10196B0106; Wed, 17 Jan 2024 11:15:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EC17C6B0107; Wed, 17 Jan 2024 11:15:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D2D666B0108; Wed, 17 Jan 2024 11:15:13 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id AB6756B0107 for ; Wed, 17 Jan 2024 11:15:13 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 7C29A1613C7 for ; Wed, 17 Jan 2024 16:15:13 +0000 (UTC) X-FDA: 81689302506.15.7B04A2B Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf14.hostedemail.com (Postfix) with ESMTP id CC1C6100021 for ; Wed, 17 Jan 2024 16:15:10 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=WGlSJQ3j; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf14.hostedemail.com: domain of jpoimboe@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=jpoimboe@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1705508111; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=o3K4TPl78GOzKHIjcRo4cqwYWgTS0PNWTuRXct9Lkh0=; b=ap03psUy6S6ifxAJPkyaVX334Yy13QkPEckq8Z/GjS6D1hbcuw8CT+miUNNcLeBJtNhCbV 8CcawLdTNlUxu8HLz+Q8WonMPNXQAM0tbLNwItG5729/wJW5vUSairXXww0ZHQOMrWWKrA kSQs+dU71o2GpFvsepZilmIWx4pQ7Ys= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=WGlSJQ3j; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf14.hostedemail.com: domain of jpoimboe@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=jpoimboe@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1705508111; a=rsa-sha256; cv=none; b=iYcKPurWrMuexZ5wX/S7e4T8fwWbrZubwbHIUHEJ4OAd9n0NCCzQZsIBH5QH9XIXjPejUD U0cX0hAbYcRIGTju1fPpZcX/icIrtF6twwK2EKu1U1wakN28Ao1PnBtfUkqSYYLnnf62M+ DumS//tVNcTDIYOjms2AFlzzb/Me1OQ= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id BA8E6B810C3; Wed, 17 Jan 2024 16:15:08 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4ED50C43394; Wed, 17 Jan 2024 16:15:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1705508108; bh=eP1aE7eGJrI56wu/JUZuR/oHJxD8hIQm2thQjg7z0tE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WGlSJQ3jQyZPQFC++EdqLNjVx8Yadpn7xElFro4BLyKUqFFpQuAncr1EOLlt3uyuI +sxXmuKxtLjUOA+r4tkrjzwa0w2fXlZGffuba9K/H5yHuoLqmQz6tLk8/uuRsnNwvp 0OESLveyoKN0OwCncqn6LD6mHP7NTY6OGGx7qOg0g8puA1fNuqUsAJCkH0E6nK2ru8 j4UkgHz40xh0P0O1yVFYkRg0lALH6UXwp8Ff3UgZgnlfnXSn/RGd8P6ZwFHx957AUc 3JfjzKdG1G9q/wZM6RZ1qlXsBSPYbFSAjWGhKG1QGsZTEw1T2W7rDmqsCvi4cGD8dl B1HUqFkOztYmA== From: Josh Poimboeuf To: Linus Torvalds , Jeff Layton , Chuck Lever , Shakeel Butt , Roman Gushchin , Johannes Weiner , Michal Hocko Cc: linux-kernel@vger.kernel.org, Jens Axboe , Tejun Heo , Vasily Averin , Michal Koutny , Waiman Long , Muchun Song , Jiri Kosina , cgroups@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH RFC 1/4] fs/locks: Fix file lock cache accounting, again Date: Wed, 17 Jan 2024 08:14:43 -0800 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: CC1C6100021 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 8m1xeh5437ptscg95wcw783y7tcdrre5 X-HE-Tag: 1705508110-170131 X-HE-Meta: U2FsdGVkX18wElrqMOTIWDkzeGUiVdhrQcX+y/F4qlnb9WXPD3CJ2pW06MKTqLfCDw03zQKvDxncfxb81od3tmlSflxZWAX3d2B7QiJUL3+/wpvxl2o9juCQ5c1Jvl4FARJxOVnW8DUIvmsieLY6R9YlMtlQ4xwC2/Ti5Hh7h2M1I/yI0PipOhEtZDsj3e5s9r06Y9Gim+ALLqgp8R1EHfBriHeOY99FpJIDKkfnroeGnmclArB/P6BQl0rqiltdYCnkPp8xYZK8htN7gf8b3g+WHaoyJkkhvR2O32s3y6f/h4Q+gQPPdIh4Bs6D8TZBrjzRPztf/DDh+qFJz7zeGbJnQZVtqTeV1sIa0bp8fYBKyAet/K0an4qQfP4aBV1Tdva+/A4dO/3pJeA3p564jIsgKXbzatmRB7qsdBl/LTWfesxmj+sWwhxbO1kTX8UzmhHiZsjSr+pdcHYW8JGfBc1TIEE5AoQfVWwN5bISaKvrWpPf8mNAhSB5hJLqdqjh4cdtaLFlhMXmNav+qSxmXKEq24VQVqiz0Zp9X0Vx0l0vwB7uaRA7NeYKQWNzjZ8ygxAcgdbxWgaLb64j6qCqdu0HTK7BWmJDxXCf4GUydFqapVzLCFY5KETGYTNLOY6GllWZpWrciSOnIz9nxKKxnuVz3L9JuP+5DtO7Pigib2XfN6S3YO2VC35+hUAWxSa20lQSdA4qQjT6NQzu8NMgsi2IPK0g60a629lqNaGYMpHKfiCe081VjVIpxzm9+ctUsh9Np94xxjI50W75QyVpGMXL/QzdBaAj5F2ksnuD56kEosaPbk8yb/4ZR9OVYCxFh/WBuIXMDwzYv3FjPEK7So1cqwkVeZOJxnSFYieNnext3NwF6jGdSTkgNeNgOSoFy6YfgdVdiSifiT930wSB6FqvuD+gOEmsP5pUTrfn8GaE7CB+rtWh9w== 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: List-Subscribe: List-Unsubscribe: A container can exceed its memcg limits by allocating a bunch of file locks. This bug was originally fixed by commit 0f12156dff28 ("memcg: enable accounting for file lock caches"), but was later reverted by commit 3754707bcc3e ("Revert "memcg: enable accounting for file lock caches"") due to performance issues. Unfortunately those performance issues were never addressed and the bug has remained unfixed for over two years. Fix it by default but allow users to disable it with a cmdline option (flock_accounting=off). Signed-off-by: Josh Poimboeuf --- .../admin-guide/kernel-parameters.txt | 17 +++++++++++ fs/locks.c | 30 +++++++++++++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt index 6ee0f9a5da70..91987b06bc52 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -1527,6 +1527,23 @@ See Documentation/admin-guide/sysctl/net.rst for fb_tunnels_only_for_init_ns + flock_accounting= + [KNL] Enable/disable accounting for kernel + memory allocations related to file locks. + Format: { on | off } + Default: on + on: Enable kernel memory accounting for file + locks. This prevents task groups from + exceeding their memcg allocation limits. + However, it may cause slowdowns in the + flock() system call. + off: Disable kernel memory accounting for + file locks. This may allow a rogue task + to DoS the system by forcing the kernel + to allocate memory beyond the task + group's memcg limits. Not recommended + unless you have trusted user space. + floppy= [HW] See Documentation/admin-guide/blockdev/floppy.rst. diff --git a/fs/locks.c b/fs/locks.c index cc7c117ee192..235ac56c557d 100644 --- a/fs/locks.c +++ b/fs/locks.c @@ -2905,15 +2905,41 @@ static int __init proc_locks_init(void) fs_initcall(proc_locks_init); #endif +static bool flock_accounting __ro_after_init = true; + +static int __init flock_accounting_cmdline(char *str) +{ + if (!str) + return -EINVAL; + + if (!strcmp(str, "off")) + flock_accounting = false; + else if (!strcmp(str, "on")) + flock_accounting = true; + else + return -EINVAL; + + return 0; +} +early_param("flock_accounting", flock_accounting_cmdline); + +#define FLOCK_ACCOUNTING_MSG "WARNING: File lock accounting is disabled, container-triggered host memory exhaustion possible!\n" + static int __init filelock_init(void) { int i; + slab_flags_t flags = SLAB_PANIC; + + if (!flock_accounting) + pr_err(FLOCK_ACCOUNTING_MSG); + else + flags |= SLAB_ACCOUNT; flctx_cache = kmem_cache_create("file_lock_ctx", - sizeof(struct file_lock_context), 0, SLAB_PANIC, NULL); + sizeof(struct file_lock_context), 0, flags, NULL); filelock_cache = kmem_cache_create("file_lock_cache", - sizeof(struct file_lock), 0, SLAB_PANIC, NULL); + sizeof(struct file_lock), 0, flags, NULL); for_each_possible_cpu(i) { struct file_lock_list_struct *fll = per_cpu_ptr(&file_lock_list, i);