From patchwork Thu May 28 13:54:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Schatzberg X-Patchwork-Id: 11575889 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 95F5B912 for ; Thu, 28 May 2020 13:55:24 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5185720814 for ; Thu, 28 May 2020 13:55:24 +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="hmVwED5z" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5185720814 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 6BD15800B7; Thu, 28 May 2020 09:55:23 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 6472680010; Thu, 28 May 2020 09:55:23 -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 44C59800B7; Thu, 28 May 2020 09:55:23 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0061.hostedemail.com [216.40.44.61]) by kanga.kvack.org (Postfix) with ESMTP id 2558F80010 for ; Thu, 28 May 2020 09:55:23 -0400 (EDT) Received: from smtpin02.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id D0CE8181AEF00 for ; Thu, 28 May 2020 13:55:22 +0000 (UTC) X-FDA: 76866274884.02.lace64_11cc7452b6f39 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin02.hostedemail.com (Postfix) with ESMTP id AD9DC1D5EC for ; Thu, 28 May 2020 13:55:22 +0000 (UTC) X-Spam-Summary: 2,0,0,fd74934d797491f7,d41d8cd98f00b204,schatzberg.dan@gmail.com,,RULES_HIT:1:2:41:69:355:379:387:541:800:960:973:981:988:989:1260:1311:1314:1345:1359:1437:1515:1605:1730:1747:1777:1792:1801:2194:2199:2393:2553:2559:2562:2693:2894:3138:3139:3140:3141:3142:3865:3866:3867:3868:3870:3871:3872:3874:4049:4250:4321:4605:5007:6119:6261:6653:6742:6743:7576:7875:7903:8603:9413:9592:10004:11026:11232:11473:11658:11914:12043:12114:12296:12297:12438:12517:12519:12555:12679:12895:12986:13161:13221:13227:13229:13894:14096:14394:14687:21433:21444:21450:21451:21627:21666:21740:21809:21990:30054:30055:30064:30070:30080:30090,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,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: lace64_11cc7452b6f39 X-Filterd-Recvd-Size: 10468 Received: from mail-qk1-f196.google.com (mail-qk1-f196.google.com [209.85.222.196]) by imf28.hostedemail.com (Postfix) with ESMTP for ; Thu, 28 May 2020 13:55:22 +0000 (UTC) Received: by mail-qk1-f196.google.com with SMTP id b27so3123827qka.4 for ; Thu, 28 May 2020 06:55:22 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=xdQ5z1ksn2zGVq7EzDi+MjQVhReBbbpTpmOESCfEEX4=; b=hmVwED5zUKuJxKCfzDCS2KiXSvVHTHkr7UcIWpKBx03rriA533svefREB4D+PiTzm3 ecrfa+dd7H6xGbgDgi4grae6QgeljFodUTYDAvJsnS7Vd9nwwi9U50+y0E6/AJ+uyjbx b5iBc1LJvwzYrcQhhRJUo91Gvk7dPcXStiCclPD3xabbqYcQaAzMAApjbnj3Wxzh4Ap6 imu3NpGpZWRt2mrqo3O4rPj+X7dzJJ67fXsU1pvjaCw4btThuvzH8XDBK2x6splXdd9e 25+IlF5Oh/SlIhHIgk5XJQ+770lsR1JjvnQ+Bg7NePQtyNnkfLzf70fHhvMH7u+wpTcC SKTQ== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=xdQ5z1ksn2zGVq7EzDi+MjQVhReBbbpTpmOESCfEEX4=; b=ByLtLS8FmreQtDoJlez8tedg+IZuLH3Mvrm7czAZwJ4cxB2Vug1BDwN0vUPqqFKqeg bP5g86VdOFOWnRjLZ68JmHtaQYvMJDY4ApqAt6NkABQSd25RrWGkSUtzqEfiEqzIXUlK xho8h7IiGE/e14oPL/8qbfanRBS+B2CgRyvRsMddS6/Ry0zZSwtWTytxVFupjP808oJX pmdUPdVWjzYgYLbAw1/AcNY/xXfVbi6bpzqYNd878tjEk8JJmdVyBFyhxfs+nDpLG0eT arZrHWOBhtjuMoFi3nHBbKkctP1hRGqPxOCdwi6kG8KIjcBjCEoLVwIeOB4h+s4mPUk6 PBgw== X-Gm-Message-State: AOAM5314rzQKh+8R2yGmHKxBkGnfr0cKqOyt7kP9jURMbMrgJqpxFOhC i4DNaYptk7h4Y3LqB3tfbq0= X-Google-Smtp-Source: ABdhPJyEoyMbQ8G21O8guSL9i01ABUiPOY3z4UBZnTPGSCVe+vyGkea+k5buVcG0lU+uzQj4QJZHrw== X-Received: by 2002:a05:620a:64f:: with SMTP id a15mr2866901qka.10.1590674121502; Thu, 28 May 2020 06:55:21 -0700 (PDT) Received: from dschatzberg-fedora-PC0Y6AEN.thefacebook.com ([2620:10d:c091:480::1:1cb7]) by smtp.gmail.com with ESMTPSA id l186sm4890889qkf.89.2020.05.28.06.55.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 May 2020 06:55:20 -0700 (PDT) From: Dan Schatzberg To: Cc: Johannes Weiner , Shakeel Butt , Roman Gushchin , Naresh Kamboju , Jens Axboe , Alexander Viro , Jan Kara , Amir Goldstein , Tejun Heo , Li Zefan , Michal Hocko , Vladimir Davydov , Andrew Morton , Hugh Dickins , Chris Down , Yafang Shao , Yang Shi , Thomas Gleixner , "Peter Zijlstra (Intel)" , Ingo Molnar , Mathieu Desnoyers , "Kirill A. Shutemov" , Andrea Arcangeli , 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 2/4] mm: support nesting memalloc_use_memcg() Date: Thu, 28 May 2020 09:54:37 -0400 Message-Id: <20200528135444.11508-3-schatzberg.dan@gmail.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20200528135444.11508-1-schatzberg.dan@gmail.com> References: <20200528135444.11508-1-schatzberg.dan@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: AD9DC1D5EC X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam05 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: From: Johannes Weiner The memalloc_use_memcg() function to override the default memcg accounting context currently doesn't nest. But the patches to make the loop driver cgroup-aware will end up nesting: [ 98.137605] alloc_page_buffers+0x210/0x288 [ 98.141799] __getblk_gfp+0x1d4/0x400 [ 98.145475] ext4_read_block_bitmap_nowait+0x148/0xbc8 [ 98.150628] ext4_mb_init_cache+0x25c/0x9b0 [ 98.154821] ext4_mb_init_group+0x270/0x390 [ 98.159014] ext4_mb_good_group+0x264/0x270 [ 98.163208] ext4_mb_regular_allocator+0x480/0x798 [ 98.168011] ext4_mb_new_blocks+0x958/0x10f8 [ 98.172294] ext4_ext_map_blocks+0xec8/0x1618 [ 98.176660] ext4_map_blocks+0x1b8/0x8a0 [ 98.180592] ext4_writepages+0x830/0xf10 [ 98.184523] do_writepages+0xb4/0x198 [ 98.188195] __filemap_fdatawrite_range+0x170/0x1c8 [ 98.193086] filemap_write_and_wait_range+0x40/0xb0 [ 98.197974] ext4_punch_hole+0x4a4/0x660 [ 98.201907] ext4_fallocate+0x294/0x1190 [ 98.205839] loop_process_work+0x690/0x1100 [ 98.210032] loop_workfn+0x2c/0x110 [ 98.213529] process_one_work+0x3e0/0x648 [ 98.217546] worker_thread+0x70/0x670 [ 98.221217] kthread+0x1b8/0x1c0 [ 98.224452] ret_from_fork+0x10/0x18 where loop_process_work() sets the memcg override to the memcg that submitted the IO request, and alloc_page_buffers() sets the override to the memcg that instantiated the cache page, which may differ. Make memalloc_use_memcg() return the old memcg and convert existing users to a stacking model. Delete the unused memalloc_unuse_memcg(). Signed-off-by: Johannes Weiner Reviewed-by: Shakeel Butt Acked-by: Roman Gushchin Reported-by: Naresh Kamboju --- fs/buffer.c | 6 +++--- fs/notify/fanotify/fanotify.c | 5 +++-- fs/notify/inotify/inotify_fsnotify.c | 5 +++-- include/linux/sched/mm.h | 28 +++++++++------------------- 4 files changed, 18 insertions(+), 26 deletions(-) diff --git a/fs/buffer.c b/fs/buffer.c index a60f60396cfa..585416dec6a2 100644 --- a/fs/buffer.c +++ b/fs/buffer.c @@ -851,13 +851,13 @@ struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size, struct buffer_head *bh, *head; gfp_t gfp = GFP_NOFS | __GFP_ACCOUNT; long offset; - struct mem_cgroup *memcg; + struct mem_cgroup *memcg, *old_memcg; if (retry) gfp |= __GFP_NOFAIL; memcg = get_mem_cgroup_from_page(page); - memalloc_use_memcg(memcg); + old_memcg = memalloc_use_memcg(memcg); head = NULL; offset = PAGE_SIZE; @@ -876,7 +876,7 @@ struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size, set_bh_page(bh, page, offset); } out: - memalloc_unuse_memcg(); + memalloc_use_memcg(old_memcg); mem_cgroup_put(memcg); return head; /* diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c index 5435a40f82be..6b869d95bfb6 100644 --- a/fs/notify/fanotify/fanotify.c +++ b/fs/notify/fanotify/fanotify.c @@ -353,6 +353,7 @@ struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group, gfp_t gfp = GFP_KERNEL_ACCOUNT; struct inode *id = fanotify_fid_inode(inode, mask, data, data_type); const struct path *path = fsnotify_data_path(data, data_type); + struct mem_cgroup *old_memcg; /* * For queues with unlimited length lost events are not expected and @@ -366,7 +367,7 @@ struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group, gfp |= __GFP_RETRY_MAYFAIL; /* Whoever is interested in the event, pays for the allocation. */ - memalloc_use_memcg(group->memcg); + old_memcg = memalloc_use_memcg(group->memcg); if (fanotify_is_perm_event(mask)) { struct fanotify_perm_event *pevent; @@ -451,7 +452,7 @@ struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group, } } out: - memalloc_unuse_memcg(); + memalloc_use_memcg(old_memcg); return event; } diff --git a/fs/notify/inotify/inotify_fsnotify.c b/fs/notify/inotify/inotify_fsnotify.c index 2ebc89047153..52f38e6e81b7 100644 --- a/fs/notify/inotify/inotify_fsnotify.c +++ b/fs/notify/inotify/inotify_fsnotify.c @@ -69,6 +69,7 @@ int inotify_handle_event(struct fsnotify_group *group, int ret; int len = 0; int alloc_len = sizeof(struct inotify_event_info); + struct mem_cgroup *old_memcg; if (WARN_ON(fsnotify_iter_vfsmount_mark(iter_info))) return 0; @@ -93,9 +94,9 @@ int inotify_handle_event(struct fsnotify_group *group, * trigger OOM killer in the target monitoring memcg as it may have * security repercussion. */ - memalloc_use_memcg(group->memcg); + old_memcg = memalloc_use_memcg(group->memcg); event = kmalloc(alloc_len, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL); - memalloc_unuse_memcg(); + memalloc_use_memcg(old_memcg); if (unlikely(!event)) { /* diff --git a/include/linux/sched/mm.h b/include/linux/sched/mm.h index c49257a3b510..95e8bfb0cab1 100644 --- a/include/linux/sched/mm.h +++ b/include/linux/sched/mm.h @@ -316,31 +316,21 @@ static inline void memalloc_nocma_restore(unsigned int flags) * __GFP_ACCOUNT allocations till the end of the scope will be charged to the * given memcg. * - * NOTE: This function is not nesting safe. + * NOTE: This function can nest. Users must save the return value and + * reset the previous value after their own charging scope is over */ -static inline void memalloc_use_memcg(struct mem_cgroup *memcg) +static inline struct mem_cgroup * +memalloc_use_memcg(struct mem_cgroup *memcg) { - WARN_ON_ONCE(current->active_memcg); + struct mem_cgroup *old = current->active_memcg; current->active_memcg = memcg; -} - -/** - * memalloc_unuse_memcg - Ends the remote memcg charging scope. - * - * This function marks the end of the remote memcg charging scope started by - * memalloc_use_memcg(). - */ -static inline void memalloc_unuse_memcg(void) -{ - current->active_memcg = NULL; + return old; } #else -static inline void memalloc_use_memcg(struct mem_cgroup *memcg) -{ -} - -static inline void memalloc_unuse_memcg(void) +static inline struct mem_cgroup * +memalloc_use_memcg(struct mem_cgroup *memcg) { + return NULL; } #endif