From patchwork Mon Jun 15 14:06:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Weiner X-Patchwork-Id: 11605135 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 9DC4E60D for ; Mon, 15 Jun 2020 14:08:59 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6A248207D3 for ; Mon, 15 Jun 2020 14:08:59 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=cmpxchg-org.20150623.gappssmtp.com header.i=@cmpxchg-org.20150623.gappssmtp.com header.b="GCccnrQQ" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6A248207D3 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=cmpxchg.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 586886B0002; Mon, 15 Jun 2020 10:08:58 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 535936B0003; Mon, 15 Jun 2020 10:08:58 -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 3FE2C6B0005; Mon, 15 Jun 2020 10:08:58 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0059.hostedemail.com [216.40.44.59]) by kanga.kvack.org (Postfix) with ESMTP id 254616B0002 for ; Mon, 15 Jun 2020 10:08:58 -0400 (EDT) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id AB5E8801BB98 for ; Mon, 15 Jun 2020 14:08:57 +0000 (UTC) X-FDA: 76931627514.24.bread13_1f05b3e26df7 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin24.hostedemail.com (Postfix) with ESMTP id 5C17276BBB for ; Mon, 15 Jun 2020 14:07:39 +0000 (UTC) X-Spam-Summary: 2,0,0,7a967b9e6ad0a0d6,d41d8cd98f00b204,hannes@cmpxchg.org,,RULES_HIT:41:355:379:541:800:960:973:988:989:1260:1311:1314:1345:1437:1515:1535:1543:1711:1730:1747:1777:1792:2393:2559:2562:2736:2895:2897:2987:3138:3139:3140:3141:3142:3354:3865:3866:3867:3868:3870:3871:3874:4117:4250:4321:5007:6117:6238:6261:6653:7875:7903:8660:9592:10004:11026:11473:11658:11914:12043:12296:12297:12438:12517:12519:12555:12679:12740:12895:12986:13148:13221:13229:13230:13870:13894:14093:14096:14181:14394:14721:14824:21080:21324:21444:21451:21622:21810:21939:21990:30012:30045:30054:30056:30070,0,RBL:209.85.160.194:@cmpxchg.org:.lbl8.mailshell.net-66.100.201.201 62.2.0.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:60,LUA_SUMMARY:none X-HE-Tag: bread13_1f05b3e26df7 X-Filterd-Recvd-Size: 6132 Received: from mail-qt1-f194.google.com (mail-qt1-f194.google.com [209.85.160.194]) by imf27.hostedemail.com (Postfix) with ESMTP for ; Mon, 15 Jun 2020 14:07:37 +0000 (UTC) Received: by mail-qt1-f194.google.com with SMTP id j32so12612235qte.10 for ; Mon, 15 Jun 2020 07:07:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=STVn3sZd24uT+6Abwx3hGrjt3KiHGonv7dCNXNNdXc8=; b=GCccnrQQ+F14prS1VzHQOSHnlhzYUWa/2xFrs2CsD2SO70HA6PTmJTScI0KFtJeBOc qmo/pwzQ42F5oj3P0MQl2aKylMW9BPqXthu5sim3japhSFdTQVi5qd9c3vRqO8284pNE anskV2Y91HtgHpNQkW+Qijlori0G/w9Do6FKTQ6CG1epkWn4iisZN7Fs78d7lxRLJp96 FntORj5A661/Ztro6W9JpfAn5tp63iJV/Q2qAqGBWTfP5sXEzQMKSwCeNcs1na2ncJeE qtcb7eewI3tvvRmv9KRr+kWaKsgg0FxTlz3tX7YOi3HxT3sxNbGcceazGEQ2OZBCWuKb 9Bfw== 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=STVn3sZd24uT+6Abwx3hGrjt3KiHGonv7dCNXNNdXc8=; b=oKlx0NispzYBWCo88SgdUkeIlYQ72y1yYTHrvui/ZVPwbykm2qBg/Wv80tA7me7suu zSGS4PDOO763Dl7WEmwyCgJ0lrG1JxYxMor3HqZxkwoqCF2iK6YQuPOjA0+nkf9Kakfo bMapN+DiGRJislW54dVG3ovw9zJuUEp58MxsojHkfyc/dkb5Y6vbfpduwzT3ViYQepbK uBt7K53XmDtMCUCfKUvz9JRgX0lWqVnubxkyeaMEGAq7cJDKverKY7qxMf1XxRrBYX/R WV3S8LgnjOzdPleb0EC15b/URJQor9c4Xqs28bKxnM0U9tKmLilf77JXOqehhyiRkiDM hDaA== X-Gm-Message-State: AOAM531EYdN+HLAG4z31RBlaBiDfiqhd8KvJhM68XucTUkImlPjuSwGb PqpWpGShyOmDlVdqRpkSYtrGdw== X-Google-Smtp-Source: ABdhPJxFqBJgLwdN8IHNXFvxCzqBbX4KT88lV6RW+axueoCkAN7sfbOvB2co5bFTrGyLo5vjzmPHvg== X-Received: by 2002:ac8:6f79:: with SMTP id u25mr16186587qtv.183.1592230056852; Mon, 15 Jun 2020 07:07:36 -0700 (PDT) Received: from localhost (70.44.39.90.res-cmts.bus.ptd.net. [70.44.39.90]) by smtp.gmail.com with ESMTPSA id p44sm12789120qta.12.2020.06.15.07.07.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jun 2020 07:07:36 -0700 (PDT) From: Johannes Weiner To: Andrew Morton Cc: Tejun Heo , Michal Hocko , Roman Gushchin , Chris Down , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@fb.com Subject: [PATCH] mm: memcontrol: handle div0 crash race condition in memory.low Date: Mon, 15 Jun 2020 10:06:58 -0400 Message-Id: <20200615140658.601684-1-hannes@cmpxchg.org> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 X-Rspamd-Queue-Id: 5C17276BBB X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam03 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: Tejun reports seeing rare div0 crashes in memory.low stress testing: [37228.504582] RIP: 0010:mem_cgroup_calculate_protection+0xed/0x150 [37228.505059] Code: 0f 46 d1 4c 39 d8 72 57 f6 05 16 d6 42 01 40 74 1f 4c 39 d8 76 1a 4c 39 d1 76 15 4c 29 d1 4c 29 d8 4d 29 d9 31 d2 48 0f af c1 <49> f7 f1 49 01 c2 4c 89 96 38 01 00 00 5d c3 48 0f af c7 31 d2 49 [37228.506254] RSP: 0018:ffffa14e01d6fcd0 EFLAGS: 00010246 [37228.506769] RAX: 000000000243e384 RBX: 0000000000000000 RCX: 0000000000008f4b [37228.507319] RDX: 0000000000000000 RSI: ffff8b89bee84000 RDI: 0000000000000000 [37228.507869] RBP: ffffa14e01d6fcd0 R08: ffff8b89ca7d40f8 R09: 0000000000000000 [37228.508376] R10: 0000000000000000 R11: 00000000006422f7 R12: 0000000000000000 [37228.508881] R13: ffff8b89d9617000 R14: ffff8b89bee84000 R15: ffffa14e01d6fdb8 [37228.509397] FS: 0000000000000000(0000) GS:ffff8b8a1f1c0000(0000) knlGS:0000000000000000 [37228.509917] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [37228.510442] CR2: 00007f93b1fc175b CR3: 000000016100a000 CR4: 0000000000340ea0 [37228.511076] Call Trace: [37228.511561] shrink_node+0x1e5/0x6c0 [37228.512044] balance_pgdat+0x32d/0x5f0 [37228.512521] kswapd+0x1d7/0x3d0 [37228.513346] ? wait_woken+0x80/0x80 [37228.514170] kthread+0x11c/0x160 [37228.514983] ? balance_pgdat+0x5f0/0x5f0 [37228.515797] ? kthread_park+0x90/0x90 [37228.516593] ret_from_fork+0x1f/0x30 This happens when parent_usage == siblings_protected. We check that usage is bigger than protected, which should imply parent_usage being bigger than siblings_protected. However, we don't read (or even update) these values atomically, and they can be out of sync as the memory state changes under us. A bit of fluctuation around the target protection isn't a big deal, but we need to handle the div0 case. Check the parent state explicitly to make sure we have a reasonable positive value for the divisor. Fixes: 8a931f801340 ("mm: memcontrol: recursive memory.low protection") Reported-by: Tejun Heo Cc: Signed-off-by: Johannes Weiner Acked-by: Michal Hocko Acked-by: Chris Down --- mm/memcontrol.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 0b38b6ad547d..5de0a9035b5f 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -6360,11 +6360,16 @@ static unsigned long effective_protection(unsigned long usage, * We're using unprotected memory for the weight so that if * some cgroups DO claim explicit protection, we don't protect * the same bytes twice. + * + * Check both usage and parent_usage against the respective + * protected values. One should imply the other, but they + * aren't read atomically - make sure the division is sane. */ if (!(cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_RECURSIVE_PROT)) return ep; - - if (parent_effective > siblings_protected && usage > protected) { + if (parent_effective > siblings_protected && + parent_usage > siblings_protected && + usage > protected) { unsigned long unclaimed; unclaimed = parent_effective - siblings_protected;