From patchwork Tue Apr 28 16:13:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Schatzberg X-Patchwork-Id: 11515027 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 CC72815E6 for ; Tue, 28 Apr 2020 16:15:22 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8FE562054F for ; Tue, 28 Apr 2020 16:15:22 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="uGSupcYC" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8FE562054F Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id B4A528E0005; Tue, 28 Apr 2020 12:15:21 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id AFA368E0001; Tue, 28 Apr 2020 12:15:21 -0400 (EDT) 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 9E84A8E0005; Tue, 28 Apr 2020 12:15:21 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0201.hostedemail.com [216.40.44.201]) by kanga.kvack.org (Postfix) with ESMTP id 84E5E8E0001 for ; Tue, 28 Apr 2020 12:15:21 -0400 (EDT) Received: from smtpin09.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 416C1181AC9C6 for ; Tue, 28 Apr 2020 16:15:21 +0000 (UTC) X-FDA: 76757763642.09.toy36_4bce3a871b41a X-Spam-Summary: 2,0,0,e58a662daa7ca1cf,d41d8cd98f00b204,schatzberg.dan@gmail.com,,RULES_HIT:41:355:379:387:541:973:982:988:989:1260:1311:1314:1345:1437:1515:1535:1543:1711:1730:1747:1777:1792:1801:2198:2199:2393:2559:2562:2903:2910:3138:3139:3140:3141:3142:3355:3653:3865:3866:3867:3868:3870:3871:3874:4118:4250:4605:5007:6119:6261:6653:6742:6743:7903:8603:9413:10004:11026:11658:11914:12043:12291:12296:12297:12517:12519:12679:12895:13161:13229:13894:14096:14181:14394:14687:14721:21433:21444:21450:21451:21627:21666:21740:30054:30055:30070:30090,0,RBL:209.85.160.194:@gmail.com:.lbl8.mailshell.net-66.100.201.100 62.50.0.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: toy36_4bce3a871b41a X-Filterd-Recvd-Size: 7206 Received: from mail-qt1-f194.google.com (mail-qt1-f194.google.com [209.85.160.194]) by imf37.hostedemail.com (Postfix) with ESMTP for ; Tue, 28 Apr 2020 16:15:20 +0000 (UTC) Received: by mail-qt1-f194.google.com with SMTP id h26so17441688qtu.8 for ; Tue, 28 Apr 2020 09:15:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=+k3R5gkJ3ThXa/sxWlKmei/8r5dAQaHYp0uZa9XHTdo=; b=uGSupcYCQmVXHJYLwbPxSYic57xfSrLkCxz0QnALa4gnsTo3F0Wc0GLYWaarIVvq/8 rERW42Q1IwlW1gQ7AgrwvUpZSd/ex3/bSnKLxXxbG1wePqhfH+GxeXrWNAZxkMcmB4/E sw2+x+jJvRmyB6K0GwEt5rekChvp+7kHgLYDpbAGOV3HvPFh4w4oUDf9L5geFoRCEOzc iU6s6S7KV8Uf7yVZbiv/DnDFjMULeN5A7GCNEfVYvzYJBMFKY1EedjHjs9p0bmsJ7nFh qn9pnjA8qU3EcUwJM22kliMwSu8r3mvNr7Ti0K1gCNF6mGR/ErEfOmjj4ZZh5mpq+L5L pl5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=+k3R5gkJ3ThXa/sxWlKmei/8r5dAQaHYp0uZa9XHTdo=; b=l503hqlIFG7HTBW6z+DBgRSoq7S+1bzPexhD53BfY0SafQo8DSACgAqcEBf2ZXoERs lC1207AnCI0GBOYvp3l8ih6/iwJPEc3qyxY7FH82SlTz+ctqWyaFJUvUu0m5lLWv/NgF 2Vwu2coSuxHDvx6U8L1Z63UshyA3N2wa86KiKFa/uuL1aoxExhIpWAtfs9NERvMVjnNo SrVOU+WdAkV92Z5DHd0zCzsjzpZDIjAC0/5L6YlCzx+OWwOWYaYRQbPcTJ3aIuQOEoKA WqsQSeCUR92JfmmI9T4hDWnsdgZSvDmYa3f+vKkbHpEEUtQaf49pnn2QMvNuFayf7roB wxZA== X-Gm-Message-State: AGi0PuYUtEHVb96Zw32ami/oY/ecfVsMglPFVv7T2+V8Ko7h/nntiVpm 4qCTZkgdAICjO3Fz62o5UTM= X-Google-Smtp-Source: APiQypLu2y08gRjZA8/C0Up38Umb9v/JQk+D6QuLE/UmWOCPxIzZlATRtCWbuOeIEGgKHpF5NAWlkA== X-Received: by 2002:ac8:27ed:: with SMTP id x42mr29217040qtx.231.1588090520087; Tue, 28 Apr 2020 09:15:20 -0700 (PDT) Received: from dschatzberg-fedora-PC0Y6AEN.thefacebook.com ([2620:10d:c091:480::1:3e4a]) by smtp.gmail.com with ESMTPSA id z2sm14087421qkc.28.2020.04.28.09.15.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Apr 2020 09:15:19 -0700 (PDT) From: Dan Schatzberg To: Cc: Dan Schatzberg , Jens Axboe , Alexander Viro , Jan Kara , Amir Goldstein , Tejun Heo , Li Zefan , Johannes Weiner , Michal Hocko , Vladimir Davydov , Andrew Morton , Hugh Dickins , Roman Gushchin , Shakeel Butt , Chris Down , Yang Shi , Ingo Molnar , "Peter Zijlstra (Intel)" , Mathieu Desnoyers , "Kirill A. Shutemov" , Andrea Arcangeli , Thomas Gleixner , linux-block@vger.kernel.org (open list:BLOCK LAYER), linux-kernel@vger.kernel.org (open list), linux-fsdevel@vger.kernel.org (open list:FILESYSTEMS (VFS and infrastructure)), cgroups@vger.kernel.org (open list:CONTROL GROUP (CGROUP)), linux-mm@kvack.org (open list:CONTROL GROUP - MEMORY RESOURCE CONTROLLER (MEMCG)) Subject: [PATCH v5 0/4] Charge loop device i/o to issuing cgroup Date: Tue, 28 Apr 2020 12:13:46 -0400 Message-Id: <20200428161355.6377-1-schatzberg.dan@gmail.com> X-Mailer: git-send-email 2.21.1 MIME-Version: 1.0 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: Changes since V5: * Fixed a missing css_put when failing to allocate a worker * Minor style changes Changes since V4: Only patches 1 and 2 have changed. * Fixed irq lock ordering bug * Simplified loop detach * Added support for nesting memalloc_use_memcg Changes since V3: * Fix race on loop device destruction and deferred worker cleanup * Ensure charge on shmem_swapin_page works just like getpage * Minor style changes Changes since V2: * Deferred destruction of workqueue items so in the common case there is no allocation needed Changes since V1: * Split out and reordered patches so cgroup charging changes are separate from kworker -> workqueue change * Add mem_css to struct loop_cmd to simplify logic The loop device runs all i/o to the backing file on a separate kworker thread which results in all i/o being charged to the root cgroup. This allows a loop device to be used to trivially bypass resource limits and other policy. This patch series fixes this gap in accounting. A simple script to demonstrate this behavior on cgroupv2 machine: ''' #!/bin/bash set -e CGROUP=/sys/fs/cgroup/test.slice LOOP_DEV=/dev/loop0 if [[ ! -d $CGROUP ]] then sudo mkdir $CGROUP fi grep oom_kill $CGROUP/memory.events # Set a memory limit, write more than that limit to tmpfs -> OOM kill sudo unshare -m bash -c " echo \$\$ > $CGROUP/cgroup.procs; echo 0 > $CGROUP/memory.swap.max; echo 64M > $CGROUP/memory.max; mount -t tmpfs -o size=512m tmpfs /tmp; dd if=/dev/zero of=/tmp/file bs=1M count=256" || true grep oom_kill $CGROUP/memory.events # Set a memory limit, write more than that limit through loopback # device -> no OOM kill sudo unshare -m bash -c " echo \$\$ > $CGROUP/cgroup.procs; echo 0 > $CGROUP/memory.swap.max; echo 64M > $CGROUP/memory.max; mount -t tmpfs -o size=512m tmpfs /tmp; truncate -s 512m /tmp/backing_file losetup $LOOP_DEV /tmp/backing_file dd if=/dev/zero of=$LOOP_DEV bs=1M count=256; losetup -D $LOOP_DEV" || true grep oom_kill $CGROUP/memory.events ''' Naively charging cgroups could result in priority inversions through the single kworker thread in the case where multiple cgroups are reading/writing to the same loop device. This patch series does some minor modification to the loop driver so that each cgroup can make forward progress independently to avoid this inversion. With this patch series applied, the above script triggers OOM kills when writing through the loop device as expected. Dan Schatzberg (3): loop: Use worker per cgroup instead of kworker mm: Charge active memcg when no mm is set loop: Charge i/o to mem and blk cg Johannes Weiner (1): mm: support nesting memalloc_use_memcg() drivers/block/loop.c | 248 ++++++++++++++++++++++----- drivers/block/loop.h | 14 +- fs/buffer.c | 6 +- fs/notify/fanotify/fanotify.c | 5 +- fs/notify/inotify/inotify_fsnotify.c | 5 +- include/linux/memcontrol.h | 6 + include/linux/sched/mm.h | 28 +-- kernel/cgroup/cgroup.c | 1 + mm/memcontrol.c | 11 +- mm/shmem.c | 4 +- 10 files changed, 248 insertions(+), 80 deletions(-)