From patchwork Mon Feb 24 22:17:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Schatzberg X-Patchwork-Id: 11401849 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 0258814E3 for ; Mon, 24 Feb 2020 22:18:07 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B620E21927 for ; Mon, 24 Feb 2020 22:18:06 +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="XEpAdGo0" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B620E21927 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 F08E56B0005; Mon, 24 Feb 2020 17:18:05 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id E91326B0006; Mon, 24 Feb 2020 17:18:05 -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 D59EC6B0007; Mon, 24 Feb 2020 17:18:05 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0209.hostedemail.com [216.40.44.209]) by kanga.kvack.org (Postfix) with ESMTP id B5E8E6B0005 for ; Mon, 24 Feb 2020 17:18:05 -0500 (EST) Received: from smtpin15.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 6104B824556B for ; Mon, 24 Feb 2020 22:18:05 +0000 (UTC) X-FDA: 76526434530.15.brick17_63b057289701e X-Spam-Summary: 2,0,0,b88e59aa30899641,d41d8cd98f00b204,schatzberg.dan@gmail.com,,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:2903: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:11026: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-66.100.201.100 62.18.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:33,LUA_SUMMARY:none X-HE-Tag: brick17_63b057289701e X-Filterd-Recvd-Size: 6189 Received: from mail-qk1-f196.google.com (mail-qk1-f196.google.com [209.85.222.196]) by imf37.hostedemail.com (Postfix) with ESMTP for ; Mon, 24 Feb 2020 22:18:04 +0000 (UTC) Received: by mail-qk1-f196.google.com with SMTP id h4so10220604qkm.0 for ; Mon, 24 Feb 2020 14:18:04 -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=tuG46v7pUNtXQODDHrjX+i51IKR3M2oYfS7laFORPp4=; b=XEpAdGo026I77uzxt7/HRuwG0Z3ke5/JatppxUYtFyVC7CZO/badnU2tLHdHpc7SQs LxRFS9iknH4NyfX/AQqvLkzYm0urksQlsKHuN5mQwQ2OSRx8CermDM7iWC9OTgoj7LDw qpOIhqv5qlSSt4FWsAmR/8ZEEwtKaLN9TECZ6IGCtzzy+ulUSUz2nw1DP1/DSnxY5EcI 4hBqe8dOgP+t95iSSDy1aS67ojRZrqZiTQFfAFBxWBE1AayChECN+sXvulVRYglTIkEq wzdtBxOaWYMUH+YEG9gFBXdZF41ySIe+aKtyt/pXvBIaw4SBHmwq0f9riXNn4n3sb5BN Tc9w== 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=tuG46v7pUNtXQODDHrjX+i51IKR3M2oYfS7laFORPp4=; b=Lnm7aHItSEhN7ld7S3UZAJe4i2OVbx5LuAyVfLY3gmuuGAml0IrxXMfEsizNzIhRmK 6OOIw4qr7VOac/V/YVy9Zb3Kme56yInXujZt9ud8SyaLRRpY4mKl0NgakEG4LmgPt449 XYNFIZNBQMob2vclzxyyyqcWti9CGIho/0RbjsGFv83DKxnGQaC+RzzMCXXxrbHLSYRv G/oFreLYcZa8yfYVO4TL6CYHJdgvtCvy005kljxai9iUnNx/qgQNY21E7isLkclsr09e OaL4iBhGY2CsAp7XVXKXNNIZwYBirMEg6ai0TCpJu+hVMHLWqchI3btRglIYJkXZvh9V d3TA== X-Gm-Message-State: APjAAAW4sl/uN4mLxG8G9AdRZEJH8u8xLb/pXlSSKhPVcOy1VQOZda1b Y1WYR7VsTRkFOHJt7Knr99E= X-Google-Smtp-Source: APXvYqwuPk7+hhjm59kepkZOFZVJxYndmI2m8lWGURnJAjQVfx6w4pZF1QXH2n28q2DSmYnrMolx/w== X-Received: by 2002:a37:7746:: with SMTP id s67mr51588255qkc.127.1582582684427; Mon, 24 Feb 2020 14:18:04 -0800 (PST) Received: from dschatzberg-fedora-PC0Y6AEN.thefacebook.com ([2620:10d:c091:500::2:b19b]) by smtp.gmail.com with ESMTPSA id o17sm6648870qtj.80.2020.02.24.14.18.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Feb 2020 14:18:03 -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: Mon, 24 Feb 2020 17:17:44 -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 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 drivers/block/loop.c | 246 +++++++++++++++++++++++++++++++------ drivers/block/loop.h | 14 ++- include/linux/memcontrol.h | 6 + kernel/cgroup/cgroup.c | 1 + mm/memcontrol.c | 11 +- mm/shmem.c | 4 +- 6 files changed, 235 insertions(+), 47 deletions(-)