From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.3 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D4D00C433E1 for ; Mon, 24 Aug 2020 15:37:57 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 875B620838 for ; Mon, 24 Aug 2020 15:37:57 +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="VQndbuWT" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 875B620838 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 349586B0031; Mon, 24 Aug 2020 11:37:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2F8396B0032; Mon, 24 Aug 2020 11:37:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1BF468D0003; Mon, 24 Aug 2020 11:37:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0093.hostedemail.com [216.40.44.93]) by kanga.kvack.org (Postfix) with ESMTP id 0644B6B0031 for ; Mon, 24 Aug 2020 11:37:57 -0400 (EDT) Received: from smtpin21.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id B994D8248047 for ; Mon, 24 Aug 2020 15:37:56 +0000 (UTC) X-FDA: 77185867752.21.flock30_630e78327054 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin21.hostedemail.com (Postfix) with ESMTP id 417A4180442C4 for ; Mon, 24 Aug 2020 15:37:54 +0000 (UTC) X-HE-Tag: flock30_630e78327054 X-Filterd-Recvd-Size: 11230 Received: from mail-qk1-f193.google.com (mail-qk1-f193.google.com [209.85.222.193]) by imf41.hostedemail.com (Postfix) with ESMTP for ; Mon, 24 Aug 2020 15:37:53 +0000 (UTC) Received: by mail-qk1-f193.google.com with SMTP id i20so7759794qkk.8 for ; Mon, 24 Aug 2020 08:37:53 -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=mquvtW23tRDFpTzlE8ED31sKF5mD/fykcSWQYhD45BU=; b=VQndbuWT0DpMkgBvLsSdvUtHrXVHJg/0kHEHKUEC1QP49Pr+63PJxHzhd3pNfj3cE2 w+V+dNXbF3sTSirWF7gaOJ3T+HfJvWyurbegfBgH+mov0Yeeo/+Y8XDUDYXNK4vAg1+P vYeWh9Kifo/FMz/LtHY0tuaz6IRCY1GwT0Vhnp2FWB88M49/XXUINpHWJKeANg4iVJfw EQbtf2vlcCQMYqWipvj0qJWCizJlBz8NU8YzlRD2ARqb1lhqZoQQFl/tD3VV4gRG9SHX tBD20mk3xwRvhLfu5R6xHCDr5zRliPHMga3p6S/zDQkp1VL0/pw9bxQhBkmHQpbE3thu 4oNQ== 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=mquvtW23tRDFpTzlE8ED31sKF5mD/fykcSWQYhD45BU=; b=rGEbBc7k8o9V1hb5c6ao+zSuLtgycVKXE+aa8pVk3M2VDmw6MR3Qyvce1H1rRRPnrc +QfrpwB0wWTeWCfXDuMtEC364KXYa7K2Nl1iAehHnEk/2FN0S3h02azCFCrhl1qRFFrb GPwPQedFWINDYTwdpn3hYmt4/ZnMg3n7HkZh1VIDs2eKAGo1oesSXcZY4kH7J/tf8aH7 FD626KVhDfzkj3O0x47LKCGYjBZPoJjg5/0DegnOnge4HUAzupCrrfbCTYRk2nCarfvL CEP/XWZWQPzRqomPwxT7LLiWCBciCsbGaMF27hvlBBw76gf5jc4pEaM9bbgpI3bQin7R 30dA== X-Gm-Message-State: AOAM531HiklzQTMYR4CyU8q0dT3bqagDxI4N6x/jUhttxfr2nbWLnmcl /Dtt7eOzEM/09iLBXzr8lkA= X-Google-Smtp-Source: ABdhPJzJjdDOQad2laWQlBUh5PHQTffnEUZUiD1WjHvuoiLiKZf3WYj1pDuFgKRE59dbEmD+DMzBCA== X-Received: by 2002:a37:8484:: with SMTP id g126mr5112716qkd.230.1598283472942; Mon, 24 Aug 2020 08:37:52 -0700 (PDT) Received: from dschatzberg-fedora-PC0Y6AEN.thefacebook.com ([2620:10d:c091:480::1:dd21]) by smtp.gmail.com with ESMTPSA id m17sm10942758qkn.45.2020.08.24.08.37.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Aug 2020 08:37:52 -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 , Joonsoo Kim , Chris Down , Yafang Shao , Yang Shi , Peter Zijlstra , Ingo Molnar , Mathieu Desnoyers , Daniel Jordan , Michel Lespinasse , 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 2/4] mm: support nesting memalloc_use_memcg() Date: Mon, 24 Aug 2020 11:36:00 -0400 Message-Id: <20200824153607.6595-3-schatzberg.dan@gmail.com> X-Mailer: git-send-email 2.21.3 In-Reply-To: <20200824153607.6595-1-schatzberg.dan@gmail.com> References: <20200824153607.6595-1-schatzberg.dan@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 417A4180442C4 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam02 Content-Transfer-Encoding: quoted-printable 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 +++++++++------------------- mm/memcontrol.c | 6 +++--- 5 files changed, 21 insertions(+), 29 deletions(-) diff --git a/fs/buffer.c b/fs/buffer.c index d468ed9981e0..804170cb59fe 100644 --- a/fs/buffer.c +++ b/fs/buffer.c @@ -842,13 +842,13 @@ struct buffer_head *alloc_page_buffers(struct page = *page, unsigned long size, struct buffer_head *bh, *head; gfp_t gfp =3D GFP_NOFS | __GFP_ACCOUNT; long offset; - struct mem_cgroup *memcg; + struct mem_cgroup *memcg, *old_memcg; =20 if (retry) gfp |=3D __GFP_NOFAIL; =20 memcg =3D get_mem_cgroup_from_page(page); - memalloc_use_memcg(memcg); + old_memcg =3D memalloc_use_memcg(memcg); =20 head =3D NULL; offset =3D PAGE_SIZE; @@ -867,7 +867,7 @@ struct buffer_head *alloc_page_buffers(struct page *p= age, 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 c942910a8649..c8fd563e02a3 100644 --- a/fs/notify/fanotify/fanotify.c +++ b/fs/notify/fanotify/fanotify.c @@ -533,6 +533,7 @@ static struct fanotify_event *fanotify_alloc_event(st= ruct fsnotify_group *group, unsigned int fid_mode =3D FAN_GROUP_FLAG(group, FANOTIFY_FID_BITS); struct inode *child =3D NULL; bool name_event =3D false; + struct mem_cgroup *old_memcg; =20 if ((fid_mode & FAN_REPORT_DIR_FID) && dirid) { /* @@ -580,7 +581,7 @@ static struct fanotify_event *fanotify_alloc_event(st= ruct fsnotify_group *group, gfp |=3D __GFP_RETRY_MAYFAIL; =20 /* Whoever is interested in the event, pays for the allocation. */ - memalloc_use_memcg(group->memcg); + old_memcg =3D memalloc_use_memcg(group->memcg); =20 if (fanotify_is_perm_event(mask)) { event =3D fanotify_alloc_perm_event(path, gfp); @@ -608,7 +609,7 @@ static struct fanotify_event *fanotify_alloc_event(st= ruct fsnotify_group *group, event->pid =3D get_pid(task_tgid(current)); =20 out: - memalloc_unuse_memcg(); + memalloc_use_memcg(old_memcg); return event; } =20 diff --git a/fs/notify/inotify/inotify_fsnotify.c b/fs/notify/inotify/ino= tify_fsnotify.c index a65cf8c9f600..8017a51561c4 100644 --- a/fs/notify/inotify/inotify_fsnotify.c +++ b/fs/notify/inotify/inotify_fsnotify.c @@ -66,6 +66,7 @@ static int inotify_one_event(struct fsnotify_group *gro= up, u32 mask, int ret; int len =3D 0; int alloc_len =3D sizeof(struct inotify_event_info); + struct mem_cgroup *old_memcg; =20 if ((inode_mark->mask & FS_EXCL_UNLINK) && path && d_unlinked(path->dentry)) @@ -87,9 +88,9 @@ static int inotify_one_event(struct fsnotify_group *gro= up, u32 mask, * trigger OOM killer in the target monitoring memcg as it may have * security repercussion. */ - memalloc_use_memcg(group->memcg); + old_memcg =3D memalloc_use_memcg(group->memcg); event =3D kmalloc(alloc_len, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL); - memalloc_unuse_memcg(); + memalloc_use_memcg(old_memcg); =20 if (unlikely(!event)) { /* diff --git a/include/linux/sched/mm.h b/include/linux/sched/mm.h index f889e332912f..b8fde48d44a9 100644 --- a/include/linux/sched/mm.h +++ b/include/linux/sched/mm.h @@ -312,31 +312,21 @@ static inline void memalloc_nocma_restore(unsigned = int flags) * __GFP_ACCOUNT allocations till the end of the scope will be charged t= o 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 =3D current->active_memcg; current->active_memcg =3D memcg; -} - -/** - * memalloc_unuse_memcg - Ends the remote memcg charging scope. - * - * This function marks the end of the remote memcg charging scope starte= d by - * memalloc_use_memcg(). - */ -static inline void memalloc_unuse_memcg(void) -{ - current->active_memcg =3D 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 =20 diff --git a/mm/memcontrol.c b/mm/memcontrol.c index b807952b4d43..b2468c80085d 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -5271,12 +5271,12 @@ static struct cgroup_subsys_state * __ref mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) { struct mem_cgroup *parent =3D mem_cgroup_from_css(parent_css); - struct mem_cgroup *memcg; + struct mem_cgroup *memcg, *old_memcg; long error =3D -ENOMEM; =20 - memalloc_use_memcg(parent); + old_memcg =3D memalloc_use_memcg(parent); memcg =3D mem_cgroup_alloc(); - memalloc_unuse_memcg(); + memalloc_use_memcg(old_memcg); if (IS_ERR(memcg)) return ERR_CAST(memcg); =20 --=20 2.24.1