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=-6.8 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS 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 4486DC2BA19 for ; Wed, 22 Apr 2020 01:15:00 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id BF89E206D5 for ; Wed, 22 Apr 2020 01:14:59 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=fb.com header.i=@fb.com header.b="eXa6Z1WY"; dkim=pass (1024-bit key) header.d=fb.onmicrosoft.com header.i=@fb.onmicrosoft.com header.b="IZprp4bs" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org BF89E206D5 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=fb.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 3FF6E8E0005; Tue, 21 Apr 2020 21:14:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3B0668E0003; Tue, 21 Apr 2020 21:14:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 277678E0005; Tue, 21 Apr 2020 21:14:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0032.hostedemail.com [216.40.44.32]) by kanga.kvack.org (Postfix) with ESMTP id 0F3FA8E0003 for ; Tue, 21 Apr 2020 21:14:59 -0400 (EDT) Received: from smtpin11.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id CC4738248068 for ; Wed, 22 Apr 2020 01:14:58 +0000 (UTC) X-FDA: 76733721876.11.cook61_e3f23b04270a X-HE-Tag: cook61_e3f23b04270a X-Filterd-Recvd-Size: 15312 Received: from mx0b-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) by imf17.hostedemail.com (Postfix) with ESMTP for ; Wed, 22 Apr 2020 01:14:57 +0000 (UTC) Received: from pps.filterd (m0109331.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 03M16DOq003580; Tue, 21 Apr 2020 18:13:15 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=date : from : to : cc : subject : message-id : references : content-type : in-reply-to : mime-version; s=facebook; bh=8ITAcx4wyy5WX/q2ZNk3VNXPKJVkp/6aEeA8uNBEWP0=; b=eXa6Z1WYDNwB4i0CLRJARt+Nd8uLFPBXU91UJt04Z2hx0rNjwVtITpiaabBHJsqjQq6N hfbB3y5n8QjF7BwoavmZaTMUPuPv4W+mV9gqyw+RlecP2sIUuHBt3CaZNUiyxEC+VxuZ DxOLmRmQ7MzyiVQv3xH7xLuQEBMFWvYJs6c= Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com with ESMTP id 30fycfu4t5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT); Tue, 21 Apr 2020 18:13:15 -0700 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (100.104.31.183) by o365-in.thefacebook.com (100.104.36.103) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1847.3; Tue, 21 Apr 2020 18:13:14 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Bmcy/jrKXXItHenIsuMkxnXFiV/dG5OkgQH9Ngvl5YkSNrEjHb1GMsJfwzklzGNGCSdEys9FXkYveZ78gAWx6jhRZKSSpvHQ3U770brVUQfmXlrCDvhDX30YiF6ZGvqH+7tvgInho/xB0YHNrHqw6pc/jf0N6LYNFQcFtBJjVKf3ib+7jxY7hfMutYr35ohCmvEInK4kCgmI9Bg8E5skD5rg2TRpDHge0rlXOqTjpG6izAysxv2Z0ya42hFkwozIe1E9WrmLuVv2kjw8I4ihyJDpQ3I66r9iwIcG5DoIEaulpXi1IF0N0CQKKRIw6ZJZUvqjh2kJWPGSngHrHhvm7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=8ITAcx4wyy5WX/q2ZNk3VNXPKJVkp/6aEeA8uNBEWP0=; b=TLrlZPTVpVrtn9kz+IiY24ra8KqrXEzaXV//0mJGUAlX8WKpP/4nn8QQnA44qGG/5WxGP6RTwuELuD7sNH/b9OACrPVihMQJUWAYIFgUBRw0Yti4g3kB72M46TyBC9RFcmivo1Ituja1bqFBX9CZous/C6ozTfGmNPvkeXQ0EFolLgGKIR3RmdEuq+dCxSWoAlidIZwt0S6cxJTHhwZuGX+7+Yu2pD/ygcrqQXsH1x6zNxiEDRe1rWKFDEqo2HHHYc5pAmBPKplj6Ytw6D+uV79nmcDLUy7QItybezuN0cmaT6W7gid9rI2sx+xCTzRH+cbDkw7YaAhBqIXUwuy1Mw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=fb.com; dmarc=pass action=none header.from=fb.com; dkim=pass header.d=fb.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.onmicrosoft.com; s=selector2-fb-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=8ITAcx4wyy5WX/q2ZNk3VNXPKJVkp/6aEeA8uNBEWP0=; b=IZprp4bsRdZIh9XhzMphZfNKn0YxIWNh3Jyc/WSHIUZdXVxwXTnd/rdkUzHE4NMwvRmnnHNTQ47r0CMRkRqPhPo+M9JjF1/4dcpH3ggcFRdURjdPL0OeSubfUAk7PHXFHesBDgmKcOV2YGmQviIR6ioe0WttA52yLugBf+/Qtlk= Received: from BYAPR15MB4136.namprd15.prod.outlook.com (2603:10b6:a03:96::24) by BYAPR15MB2982.namprd15.prod.outlook.com (2603:10b6:a03:f8::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2921.29; Wed, 22 Apr 2020 01:13:13 +0000 Received: from BYAPR15MB4136.namprd15.prod.outlook.com ([fe80::bdf9:6577:1d2a:a275]) by BYAPR15MB4136.namprd15.prod.outlook.com ([fe80::bdf9:6577:1d2a:a275%7]) with mapi id 15.20.2921.030; Wed, 22 Apr 2020 01:13:12 +0000 Date: Tue, 21 Apr 2020 18:13:07 -0700 From: Roman Gushchin To: Dan Schatzberg CC: no To-header on input <";"@kvack.org>, Jens Axboe , Alexander Viro , Jan Kara , Amir Goldstein , Tejun Heo , Li Zefan , Johannes Weiner , Michal Hocko , Vladimir Davydov , Andrew Morton , Hugh Dickins , Shakeel Butt , Chris Down , Yang Shi , Ingo Molnar , "Peter Zijlstra (Intel)" , Mathieu Desnoyers , "Kirill A. Shutemov" , Andrea Arcangeli , Thomas Gleixner , "open list:BLOCK LAYER" , open list , "open list:FILESYSTEMS (VFS and infrastructure)" , "open list:CONTROL GROUP (CGROUP)" , "open list:CONTROL GROUP - MEMORY RESOURCE CONTROLLER (MEMCG)" Subject: Re: [PATCH 2/4] mm: support nesting memalloc_use_memcg() Message-ID: <20200422011307.GA47525@carbon.DHCP.thefacebook.com> References: <20200420223936.6773-1-schatzberg.dan@gmail.com> <20200420223936.6773-3-schatzberg.dan@gmail.com> Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200420223936.6773-3-schatzberg.dan@gmail.com> X-ClientProxiedBy: MWHPR08CA0051.namprd08.prod.outlook.com (2603:10b6:300:c0::25) To BYAPR15MB4136.namprd15.prod.outlook.com (2603:10b6:a03:96::24) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from carbon.DHCP.thefacebook.com (2620:10d:c090:400::5:47e9) by MWHPR08CA0051.namprd08.prod.outlook.com (2603:10b6:300:c0::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2937.13 via Frontend Transport; Wed, 22 Apr 2020 01:13:10 +0000 X-Originating-IP: [2620:10d:c090:400::5:47e9] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: f7b51c51-fb24-45a7-a798-08d7e65a53e2 X-MS-TrafficTypeDiagnostic: BYAPR15MB2982: X-Microsoft-Antispam-PRVS: X-FB-Source: Internal X-MS-Oob-TLC-OOBClassifiers: OLM:8273; X-Forefront-PRVS: 03818C953D X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR15MB4136.namprd15.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(10019020)(136003)(376002)(346002)(39860400002)(366004)(396003)(6666004)(5660300002)(54906003)(7416002)(16526019)(8676002)(2906002)(4326008)(107886003)(9686003)(478600001)(81156014)(33656002)(316002)(186003)(6916009)(55016002)(66946007)(1076003)(6506007)(7696005)(66556008)(86362001)(66476007)(52116002)(8936002);DIR:OUT;SFP:1102; Received-SPF: None (protection.outlook.com: fb.com does not designate permitted sender hosts) X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: xInwkfBDMebe0BMe9zwaQ39uJCMW68s3HRGbqnPc125vWP8Zy83C4398H0snf0paiOz94wj0zN1/phdsC2mnQJx8J/Wel6Ay6Ojze69u/fsmmyO67ScbIK1vhuqG8FXabeBNhbG9gYS1ddOI8cPt7b7uY0VZpYdrJg247r9MxIOh51t+7fmG9ZKPAR5SxaPDPLAYXWABbxmhbzZP412iJrnNxvn6EuIcsRNqCIJpVND2uaU6ZXaVPiISfaxmyRiTOfSxYf5Q0/vI8RTsYR95bc9ScVcFj1rSSocZfHE6sVRsX6wqa+5qTGc7eoAFAYZk8xacFYj3XzgnF27cJ0TkrjdXRU7qV46FQ1WlevhKX/IJipWvNlMhNMVV1WaskJ/dKntt2EOdh64tj0+LdgDmFxekGsQoIIESwoe/8q0SvZENO6MXiuSxm/vR5aKXXu1N X-MS-Exchange-AntiSpam-MessageData: qj+p7E44C9fs0NIDWFo5EewuZ/3ON88lq9aYmanpY7wvzCUlN65VxbcGI0Ae1IZ3SWz77oUUH3N0K2y2q/MQbNojDlLYtLfXoc3mDrreM4wQOCq3w10LPuGT3RI7uqS1BCj30QrNILrklvC69oL3sGznTRkIUQpjPTw11zeVqDyHfbPaDexrobfjq7SiLeFq X-MS-Exchange-CrossTenant-Network-Message-Id: f7b51c51-fb24-45a7-a798-08d7e65a53e2 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Apr 2020 01:13:12.7441 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 8ae927fe-1255-47a7-a2af-5f3a069daaa2 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: WA9C4ybKcP814sJegR9JHFZe29w5WkfPXoH2M8/doM502Q3kv9KAxJImUZ6bL4gr X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR15MB2982 X-OriginatorOrg: fb.com X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.138,18.0.676 definitions=2020-04-21_10:2020-04-21,2020-04-21 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 malwarescore=0 suspectscore=1 spamscore=0 priorityscore=1501 mlxscore=0 lowpriorityscore=0 bulkscore=0 clxscore=1011 adultscore=0 phishscore=0 mlxlogscore=999 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2003020000 definitions=main-2004220006 X-FB-Internal: deliver 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: On Mon, Apr 20, 2020 at 06:39:30PM -0400, Dan Schatzberg wrote: > 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: Dan Schatzberg Acked-by: Roman Gushchin One small nit below. Thanks! > --- > 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 599a0bf7257b..e39e05985323 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, *oldmemcg; I'd rename it to old_memcg. > > if (retry) > gfp |= __GFP_NOFAIL; > > memcg = get_mem_cgroup_from_page(page); > - memalloc_use_memcg(memcg); > + oldmemcg = 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(oldmemcg); > mem_cgroup_put(memcg); > return head; > /* > diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c > index 5435a40f82be..54c787cd6efb 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 *oldmemcg; > > /* > * 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); > + oldmemcg = 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(oldmemcg); > return event; > } > > diff --git a/fs/notify/inotify/inotify_fsnotify.c b/fs/notify/inotify/inotify_fsnotify.c > index 2ebc89047153..d27c6e83cea6 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 *oldmemcg; > > 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); > + oldmemcg = memalloc_use_memcg(group->memcg); > event = kmalloc(alloc_len, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL); > - memalloc_unuse_memcg(); > + memalloc_use_memcg(oldmemcg); > > 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 > > -- > 2.24.1 >