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=-8.3 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=unavailable 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 4CAD9CA9ECE for ; Thu, 31 Oct 2019 17:47:42 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 0B6682067D for ; Thu, 31 Oct 2019 17:47:41 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="EaxrJWLP" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0B6682067D 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 8172F6B0003; Thu, 31 Oct 2019 13:47:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7C8D86B0005; Thu, 31 Oct 2019 13:47:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6B7926B0006; Thu, 31 Oct 2019 13:47:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0168.hostedemail.com [216.40.44.168]) by kanga.kvack.org (Postfix) with ESMTP id 4B6596B0003 for ; Thu, 31 Oct 2019 13:47:41 -0400 (EDT) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with SMTP id DEE091801C183 for ; Thu, 31 Oct 2019 17:47:40 +0000 (UTC) X-FDA: 76104812280.20.arm26_8bf480407950 X-HE-Tag: arm26_8bf480407950 X-Filterd-Recvd-Size: 9970 Received: from mail-pf1-f193.google.com (mail-pf1-f193.google.com [209.85.210.193]) by imf33.hostedemail.com (Postfix) with ESMTP for ; Thu, 31 Oct 2019 17:47:39 +0000 (UTC) Received: by mail-pf1-f193.google.com with SMTP id d13so4867652pfq.2 for ; Thu, 31 Oct 2019 10:47:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=D4IpMr9Q2vIVypObg4yLY/Jn9WPLxRmhgpjGt2be+yE=; b=EaxrJWLPw0PwefXBdNfCI3NN3ViWbfwS28AnL9dbF+gIpU4/v4BUjM+1mjeTpokV4i PLynn1hfwJvZlWx/M+rt9kTiPWXglGVHcvU1R510nIM0RxZoH92YlrO+N6gLDkm0Z467 gWvEar74I0yVZ5FcvEcG0hrJW4hAGo8sTnE/phIYFo/dWO16qtRwCguFAO80iv/nsE61 EDkrmQrP+DaFZAwaLwSGC9+YU3YnDlyTpzIRLN18HQClpPxtXohO0tVARbRlgrEppZLm waeYTDB+gpHiar/Lrba2wJWbD2xrrQh8dssBd1BrP2elbfrH9uzH2zREokd5SewgB6rv Xh2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=D4IpMr9Q2vIVypObg4yLY/Jn9WPLxRmhgpjGt2be+yE=; b=ZKvseSVRlKfluWBcd86pDB/IRWYbbxPTNqnsQL2nNdtEGPFGkQVMBzhog+88Xbj2a7 /8Lywwd2T5UIvsI7nT8OaTYQ+iWOjePFHTiTTmA7WHVaWF9mzH7FgGm7tfj1yJx1ldFW zXrkL7ksc2/GQmhNXX0cd4dXN5yF/m9LEfidlXkp+X7ipBwKUlvLACaUR+VTJG/pL0HB A3Wn9E19FUSmcOkE28n9hn1GpURE1pRwjodV321M1RoYo+TGX82IJouhq98gPf2ZnXsp qTbZMz7UUkinOQIWISC1imbpwzu7yl3oJHl3+FX1c3ed+feD/L9gxWtcLFBoVPrEkt8H fNPA== X-Gm-Message-State: APjAAAVYnb+mVdHhiLSI1ApGHafs7EK8jLSdvA4NmCQ1pnPg0prccpVE j6+iYmBKKTFhj84xRPxLz88= X-Google-Smtp-Source: APXvYqxKYabaLwBCK9EM42f37Bdz1ancD1yj7WBy1GTJ7FA548B9VQCL1PN33zDUvFa1FX50z9ExVg== X-Received: by 2002:a63:4e13:: with SMTP id c19mr8144284pgb.225.1572544058706; Thu, 31 Oct 2019 10:47:38 -0700 (PDT) Received: from [192.168.86.235] (c-73-241-150-58.hsd1.ca.comcast.net. [73.241.150.58]) by smtp.gmail.com with ESMTPSA id i123sm4165002pfe.145.2019.10.31.10.47.36 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 31 Oct 2019 10:47:37 -0700 (PDT) Subject: Re: [PATCH v2] net: fix sk_page_frag() recursion from memory reclaim To: Shakeel Butt , Tejun Heo , Michal Hocko Cc: "David S. Miller" , netdev@vger.kernel.org, Kernel Team , LKML , Josef Bacik , Eric Dumazet , Jakub Kicinski , Johannes Weiner , Linux MM , Mel Gorman , Andrew Morton References: <20191019170141.GQ18794@devbig004.ftw2.facebook.com> <20191024205027.GF3622521@devbig004.ftw2.facebook.com> From: Eric Dumazet Message-ID: <11f688a6-0288-0ec4-f925-7b8f16ec011b@gmail.com> Date: Thu, 31 Oct 2019 10:47:35 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit 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 10/31/19 10:35 AM, Shakeel Butt wrote: > +Michal Hocko > > On Thu, Oct 24, 2019 at 1:50 PM Tejun Heo wrote: >> >> sk_page_frag() optimizes skb_frag allocations by using per-task >> skb_frag cache when it knows it's the only user. The condition is >> determined by seeing whether the socket allocation mask allows >> blocking - if the allocation may block, it obviously owns the task's >> context and ergo exclusively owns current->task_frag. >> >> Unfortunately, this misses recursion through memory reclaim path. >> Please take a look at the following backtrace. >> >> [2] RIP: 0010:tcp_sendmsg_locked+0xccf/0xe10 >> ... >> tcp_sendmsg+0x27/0x40 >> sock_sendmsg+0x30/0x40 >> sock_xmit.isra.24+0xa1/0x170 [nbd] >> nbd_send_cmd+0x1d2/0x690 [nbd] >> nbd_queue_rq+0x1b5/0x3b0 [nbd] >> __blk_mq_try_issue_directly+0x108/0x1b0 >> blk_mq_request_issue_directly+0xbd/0xe0 >> blk_mq_try_issue_list_directly+0x41/0xb0 >> blk_mq_sched_insert_requests+0xa2/0xe0 >> blk_mq_flush_plug_list+0x205/0x2a0 >> blk_flush_plug_list+0xc3/0xf0 >> [1] blk_finish_plug+0x21/0x2e >> _xfs_buf_ioapply+0x313/0x460 >> __xfs_buf_submit+0x67/0x220 >> xfs_buf_read_map+0x113/0x1a0 >> xfs_trans_read_buf_map+0xbf/0x330 >> xfs_btree_read_buf_block.constprop.42+0x95/0xd0 >> xfs_btree_lookup_get_block+0x95/0x170 >> xfs_btree_lookup+0xcc/0x470 >> xfs_bmap_del_extent_real+0x254/0x9a0 >> __xfs_bunmapi+0x45c/0xab0 >> xfs_bunmapi+0x15/0x30 >> xfs_itruncate_extents_flags+0xca/0x250 >> xfs_free_eofblocks+0x181/0x1e0 >> xfs_fs_destroy_inode+0xa8/0x1b0 >> destroy_inode+0x38/0x70 >> dispose_list+0x35/0x50 >> prune_icache_sb+0x52/0x70 >> super_cache_scan+0x120/0x1a0 >> do_shrink_slab+0x120/0x290 >> shrink_slab+0x216/0x2b0 >> shrink_node+0x1b6/0x4a0 >> do_try_to_free_pages+0xc6/0x370 >> try_to_free_mem_cgroup_pages+0xe3/0x1e0 >> try_charge+0x29e/0x790 >> mem_cgroup_charge_skmem+0x6a/0x100 >> __sk_mem_raise_allocated+0x18e/0x390 >> __sk_mem_schedule+0x2a/0x40 >> [0] tcp_sendmsg_locked+0x8eb/0xe10 >> tcp_sendmsg+0x27/0x40 >> sock_sendmsg+0x30/0x40 >> ___sys_sendmsg+0x26d/0x2b0 >> __sys_sendmsg+0x57/0xa0 >> do_syscall_64+0x42/0x100 >> entry_SYSCALL_64_after_hwframe+0x44/0xa9 >> >> In [0], tcp_send_msg_locked() was using current->page_frag when it >> called sk_wmem_schedule(). It already calculated how many bytes can >> be fit into current->page_frag. Due to memory pressure, >> sk_wmem_schedule() called into memory reclaim path which called into >> xfs and then IO issue path. Because the filesystem in question is >> backed by nbd, the control goes back into the tcp layer - back into >> tcp_sendmsg_locked(). >> >> nbd sets sk_allocation to (GFP_NOIO | __GFP_MEMALLOC) which makes >> sense - it's in the process of freeing memory and wants to be able to, >> e.g., drop clean pages to make forward progress. However, this >> confused sk_page_frag() called from [2]. Because it only tests >> whether the allocation allows blocking which it does, it now thinks >> current->page_frag can be used again although it already was being >> used in [0]. >> >> After [2] used current->page_frag, the offset would be increased by >> the used amount. When the control returns to [0], >> current->page_frag's offset is increased and the previously calculated >> number of bytes now may overrun the end of allocated memory leading to >> silent memory corruptions. >> >> Fix it by adding gfpflags_normal_context() which tests sleepable && >> !reclaim and use it to determine whether to use current->task_frag. >> >> v2: Eric didn't like gfp flags being tested twice. Introduce a new >> helper gfpflags_normal_context() and combine the two tests. >> >> Signed-off-by: Tejun Heo >> Cc: Josef Bacik >> Cc: Eric Dumazet >> Cc: stable@vger.kernel.org >> --- >> include/linux/gfp.h | 23 +++++++++++++++++++++++ >> include/net/sock.h | 11 ++++++++--- >> 2 files changed, 31 insertions(+), 3 deletions(-) >> >> diff --git a/include/linux/gfp.h b/include/linux/gfp.h >> index fb07b503dc45..61f2f6ff9467 100644 >> --- a/include/linux/gfp.h >> +++ b/include/linux/gfp.h >> @@ -325,6 +325,29 @@ static inline bool gfpflags_allow_blocking(const gfp_t gfp_flags) >> return !!(gfp_flags & __GFP_DIRECT_RECLAIM); >> } >> >> +/** >> + * gfpflags_normal_context - is gfp_flags a normal sleepable context? >> + * @gfp_flags: gfp_flags to test >> + * >> + * Test whether @gfp_flags indicates that the allocation is from the >> + * %current context and allowed to sleep. >> + * >> + * An allocation being allowed to block doesn't mean it owns the %current >> + * context. When direct reclaim path tries to allocate memory, the >> + * allocation context is nested inside whatever %current was doing at the >> + * time of the original allocation. The nested allocation may be allowed >> + * to block but modifying anything %current owns can corrupt the outer >> + * context's expectations. >> + * >> + * %true result from this function indicates that the allocation context >> + * can sleep and use anything that's associated with %current. >> + */ >> +static inline bool gfpflags_normal_context(const gfp_t gfp_flags) >> +{ >> + return (gfp_flags & (__GFP_DIRECT_RECLAIM | __GFP_MEMALLOC)) == >> + __GFP_DIRECT_RECLAIM; > > I think we should be checking PF_MEMALLOC here instead. Something like: > > return gfpflags_allow_blocking(gfp_flags) && !(current->flags & PF_MEMALLOC); > > In my limited understanding, __GFP_MEMALLOC gives access to reserve > but we have overloaded PF_MEMALLOC to also define the reclaim context. > There are PF_MEMALLOC users which does not use __GFP_MEMALLOC like > iscsi_sw_tcp_pdu_xmit() which can call sock_sendmsg(). Why would this layer not set sk->sk_allocation to GFP_ATOMIC ? And it also might call sk_set_memalloc() too. Please double check scsi layer, I am pretty sure it did well at some point.