From patchwork Thu Feb 20 16:51:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Schatzberg X-Patchwork-Id: 11394605 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 C3BA2159A for ; Thu, 20 Feb 2020 16:52:25 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 86C8324672 for ; Thu, 20 Feb 2020 16:52:25 +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="GMVJvohK" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 86C8324672 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 BF5E56B0036; Thu, 20 Feb 2020 11:52:24 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id BA6786B0072; Thu, 20 Feb 2020 11:52:24 -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 AE4526B0073; Thu, 20 Feb 2020 11:52:24 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0228.hostedemail.com [216.40.44.228]) by kanga.kvack.org (Postfix) with ESMTP id 9710D6B0036 for ; Thu, 20 Feb 2020 11:52:24 -0500 (EST) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 3C1578248D52 for ; Thu, 20 Feb 2020 16:52:24 +0000 (UTC) X-FDA: 76511098608.16.laugh03_4f253bf1b8410 X-Spam-Summary: 2,0,0,7e924ccad875c6b2,d41d8cd98f00b204,schatzberg.dan@gmail.com,:schatzberg.dan@gmail.com:axboe@kernel.dk:tj@kernel.org:lizefan@huawei.com:hannes@cmpxchg.org:mhocko@kernel.org:vdavydov.dev@gmail.com:akpm@linux-foundation.org:hughd@google.com:guro@fb.com:shakeelb@google.com:chris@chrisdown.name:yang.shi@linux.alibaba.com:tglx@linutronix.de:linux-block@vger.kernel.org:linux-kernel@vger.kernel.org:cgroups@vger.kernel.org:,RULES_HIT:41:355:379:387:541:973:982:988:989:1260:1311:1314:1345:1437:1515:1535:1542:1711:1730:1747:1777:1792:2198:2199:2393:2559:2562:2910:3138:3139:3140:3141:3142:3354:3653:3865:3866:3867:3868:3870:3871:3874:4117:4250:4605:5007:6119:6261:6653:6742:7903:8603:9413:10004:11658:11914:12043:12291:12296:12297:12517:12519:12679:12683:12895:13161:13229:13894:14096:14181:14394:14687:14721:21433:21444:21450:21451:21627:21666:21740:30054:30070,0,RBL:209.85.222.196:@gmail.com:.lbl8.mailshell.net-62.18.0.100 66.100.201.100,CacheIP:none,Bayesian:0.5,0.5,0. 5,Netche X-HE-Tag: laugh03_4f253bf1b8410 X-Filterd-Recvd-Size: 6012 Received: from mail-qk1-f196.google.com (mail-qk1-f196.google.com [209.85.222.196]) by imf40.hostedemail.com (Postfix) with ESMTP for ; Thu, 20 Feb 2020 16:52:23 +0000 (UTC) Received: by mail-qk1-f196.google.com with SMTP id h4so4220482qkm.0 for ; Thu, 20 Feb 2020 08:52:23 -0800 (PST) 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=6XRtdyMfa57xpaqGTGErpzosHOBJSLkhLdrjsJIgZOk=; b=GMVJvohK6Uqosdx++Q1UIJDcIwc443iVBHapmNkU3PGw0Gp/Zvb4sb4UC5bp2Vqnjh 3CNcPOyt+wKRKUoubWndo5lXBiYketttaeW/0TFZNT506r2ehW2MDLqDwKxIN4eTRL6l Bbe/9ss0aC0grEW3nn0C41VmK4UZ0iE4dj4Z897q8UHKgHY2T1n9bsgu9G2OYcIDvx+B u5klU2+Lw4xtVQXGaop6kWnEt+i2w3JLr/ZPKKBW51NDmVrAy3ZO3uQda80EIss6Qyv+ H/1orrfd8yQMyvriQ2xSQnzBmiCfFYpDXHVpOfkmdv3fLAt2kdPWJQVF2RC+gEgheTWP kuGQ== 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=6XRtdyMfa57xpaqGTGErpzosHOBJSLkhLdrjsJIgZOk=; b=TJxaQrUZEn9t86HZ7FZYzJ9PVJy0/+WA7OLtVFZojmXRGmsphjz/n7oTopuXAnfQCc kfcoikt5VU0AZNDtm0e4zfMWpIALopVQ7qp1OzSpecgVdEYl1k6ird6CDy9ScbdcT9np NVLIv66BEJ+Dz2nx/g+1OtYfWGM929buqcCrI1GnSjDQ5S/kTfsWEkyNxggjLdAry00y mVGeWkvcnsCVpqtmRUzODOOnKq8+v7FPqD1E0zWlUZoFgAASbpZykgYFmNGENtkMxUwq tBldyKwlwzN8uRkNxUkdoRqsTcUYoCa96pdURryWpI1uXzghmbWrylQhekiRx8vCrId8 R6qA== X-Gm-Message-State: APjAAAUG3+7w82ciZSxtV3zj067epn3eXjwbWj5VxC+tosoxNs30B/q2 bg1ykzb/9D277AhniT3+UWo= X-Google-Smtp-Source: APXvYqy4l4khgWGFmnhDAt5LoEZMVrOKFHi8JHSfYZBxEDUKoj4Q/uPqL9B528B9p2SSMSXiU0yT2A== X-Received: by 2002:a37:a08f:: with SMTP id j137mr28457353qke.467.1582217543184; Thu, 20 Feb 2020 08:52:23 -0800 (PST) Received: from dschatzberg-fedora-PC0Y6AEN.thefacebook.com ([2620:10d:c091:500::3:edb0]) by smtp.gmail.com with ESMTPSA id t55sm43567qte.24.2020.02.20.08.52.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Feb 2020 08:52:22 -0800 (PST) From: Dan Schatzberg To: Cc: Dan Schatzberg , Jens Axboe , Tejun Heo , Li Zefan , Johannes Weiner , Michal Hocko , Vladimir Davydov , Andrew Morton , Hugh Dickins , Roman Gushchin , Shakeel Butt , Chris Down , Yang Shi , Thomas Gleixner , linux-block@vger.kernel.org (open list:BLOCK LAYER), linux-kernel@vger.kernel.org (open list), cgroups@vger.kernel.org (open list:CONTROL GROUP (CGROUP)), linux-mm@kvack.org (open list:CONTROL GROUP - MEMORY RESOURCE CONTROLLER (MEMCG)) Subject: [PATCH v3 0/3] Charge loop device i/o to issuing cgroup Date: Thu, 20 Feb 2020 11:51:50 -0500 Message-Id: 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 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 drivers/block/loop.c | 229 +++++++++++++++++++++++++++++++------ drivers/block/loop.h | 14 ++- include/linux/memcontrol.h | 6 + kernel/cgroup/cgroup.c | 1 + mm/memcontrol.c | 11 +- mm/shmem.c | 2 +- 6 files changed, 217 insertions(+), 46 deletions(-)