Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp708818pxj; Thu, 10 Jun 2021 10:42:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzkQZvR5GEx4P5xhQZ0xs0dm0VVKCEZADIzQ63Zu37nq+0CprB9Y/OHbdidlII3rQ1/FrnE X-Received: by 2002:a05:6402:4301:: with SMTP id m1mr612431edc.303.1623346951654; Thu, 10 Jun 2021 10:42:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623346951; cv=none; d=google.com; s=arc-20160816; b=vsi4T7cr5Ol3BRdO0o7TmO8UaMLme/6wVN+10aCI/zhjiy3Kn/teLR5ujephTBdcaQ 1fORoLCRf7Iu7pcTrt1erAx6FLaRAjDhU4SYrWEqYVMj+J9cABxJNfYe3DXeaeIhLUyi Ca8L2+AHrII4oKp7zCwJrrnpf1qosnTG+wpaaUDRugRjRZKp1QA9C8amJVkLkFbS5BKn bYfFHseTBdfTJDxHnzt9mjYXeWyr+mtbEKGqL3bMHIWbXe5vN1lSni8z29RdP6YIpud4 RvHBSmayRA2f0GtB3ECzqWkb3G137GwgpNCqVxJB2i80ojKcdbUTanh8/u7TvfgMPMbu zarw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=aWOZidZP0x7GLTaV0tNbVWE6yJUvGbKMUKncxkGIKXc=; b=RgPyuHoNQJHqOfL/lzSgqDwsQdfchhEXOyG5HlliggqKSGvg3xXGeiunygzrXioz0d LiRWtAQigKEhgTkmJScY+zwrDzX5TICkDUhUVVCucxkY4Pg3y8KIA0GRmhXVYkam08QX lvrukVXjJUzVNlR1zxIAFGRXJu+bSXu9u29yYCPGrPApUyhEJpgLnvI91fPVKqo68qa3 GekFWWS/A47LoraHPl+n/UeQkKobfxMzaJk5YFAmQzG5ssxEcLBdz1md7Ep+er4OPEk2 e1y3LL8too9kXHmRZ+df3UbFLKG+ZdQ5vbeD8oAklfmLajS6UWknppaV1ot1RPh8+tro 4+3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=XPC6uYHL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b11si2882385ejd.10.2021.06.10.10.42.07; Thu, 10 Jun 2021 10:42:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=XPC6uYHL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231164AbhFJRm6 (ORCPT + 99 others); Thu, 10 Jun 2021 13:42:58 -0400 Received: from mail-qt1-f182.google.com ([209.85.160.182]:46970 "EHLO mail-qt1-f182.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230479AbhFJRm5 (ORCPT ); Thu, 10 Jun 2021 13:42:57 -0400 Received: by mail-qt1-f182.google.com with SMTP id a15so436533qtx.13; Thu, 10 Jun 2021 10:40:56 -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=aWOZidZP0x7GLTaV0tNbVWE6yJUvGbKMUKncxkGIKXc=; b=XPC6uYHLKCsy9omlrkaT8PtT4NlJhA4tDwIVebFGd3BKERAT8QsJZ5Q4mcqyl4Wpar uLbl9EK46Q5uyJ8hiT+QFeCqqL8DzIrIV9uImuGmRorRcyIPZ11KS7Kr1GkySBLMRNzP UUIJkekElO9+gtz56QtAKcdpKrFm9ofEi3WlwmkSHIn2zZHJHxLAHs3jSYCzHYdDcS+a zP45WFAebM7tEJjwI0oCGEq+sjCMPfs8MERECC0QCT0Em1G/tZYgXqmGvlBneZ5zeuFc j/syk/iQ6rQYop9pxjVnh1/Q2sH5y6Ocb6NbmJ1mmv5DgQ6flUPSVRkYMz+/xWeP1brp B+zw== 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=aWOZidZP0x7GLTaV0tNbVWE6yJUvGbKMUKncxkGIKXc=; b=oUGnkyQeaeaMyoTIR6K6anu+cUWNGIN8DBAk26lrGQ8HQE0h76PB+Fe7CKiLLgyXvk wvQ0LQeo3HddVqU9bU5cBw7SSGD1+l4fbi3st02O10fb2JwwAqbu3YWkfUT31dzcLtPE LJqrPqeu1avBI7PXWAIgGk8C+Z+ARnOuB7yYpiLBVlSbBYKeWim6PPYVIV3kUTP3GXjU ZwgYtlG2wjPMaOO+8A7j04zVCAuqud3mHvRwCtuhG1MtNjKcNxOoRPlhWwmpnZkegRDi 2VUPCvMRdJaPrsGwB0wYrcJkR9jseRkOT2ZMmih4FlnjNr4BEZTur3Y+PqWmgHcgnFNO skLw== X-Gm-Message-State: AOAM533SBE5+Usu7GzpGaLdkTeZdezHI0jkKWmLhljS1i/ObC33Qteg6 kr/OhcwIkUlt1GGp0FfZ9xbTh+QUoCc= X-Received: by 2002:ac8:7311:: with SMTP id x17mr790791qto.3.1623346795690; Thu, 10 Jun 2021 10:39:55 -0700 (PDT) Received: from localhost ([2620:10d:c091:480::1:be7a]) by smtp.gmail.com with ESMTPSA id 104sm2667808qta.90.2021.06.10.10.39.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Jun 2021 10:39:55 -0700 (PDT) From: Dan Schatzberg To: Andrew Morton Cc: 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:MEMORY MANAGEMENT) Subject: [PATCH V14 0/3] Charge loop device i/o to issuing cgroup Date: Thu, 10 Jun 2021 10:39:41 -0700 Message-Id: <20210610173944.1203706-1-schatzberg.dan@gmail.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Rebased on top of mm-tree As per the suggestion last time [1], could this go through the -mm tree to deal with memcg conflicts? [1] https://lore.kernel.org/lkml/CALvZod6FMQQC17Zsu9xoKs=dFWaJdMC2Qk3YiDPUUQHx8teLYg@mail.gmail.com/ Changes since V13: * Small change to get_mem_cgroup_from_mm to return early when the return is root memcg * Avoid allocating loop worker if the issuing blkcg is root_blkcg Changes since V12: * Small change to get_mem_cgroup_from_mm to avoid needing get_active_memcg Changes since V11: * Removed WQ_MEM_RECLAIM flag from loop workqueue. Technically, this can be driven by writeback, but this was causing a warning in xfs and likely other filesystems aren't equipped to be driven by reclaim at the VFS layer. * Included a small fix from Colin Ian King. * reworked get_mem_cgroup_from_mm to institute the necessary charge priority. Changes since V10: * Added page-cache charging to mm: Charge active memcg when no mm is set Changes since V9: * Rebased against linus's branch which now includes Roman Gushchin's patch this series is based off of Changes since V8: * Rebased on top of Roman Gushchin's patch (https://lkml.org/lkml/2020/8/21/1464) which provides the nesting support for setting active memcg. Dropped the patch from this series that did the same thing. Changes since V7: * Rebased against linus's branch Changes since V6: * Added separate spinlock for worker synchronization * Minor style changes 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 drivers/block/loop.c | 250 +++++++++++++++++++++++++++++++------ drivers/block/loop.h | 15 ++- include/linux/memcontrol.h | 6 + kernel/cgroup/cgroup.c | 1 + mm/filemap.c | 2 +- mm/memcontrol.c | 42 ++++--- mm/shmem.c | 4 +- 7 files changed, 258 insertions(+), 62 deletions(-) -- 2.30.2