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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 85BDAC25B06 for ; Thu, 11 Aug 2022 08:43:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 01E378E0001; Thu, 11 Aug 2022 04:43:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F0F5D6B0075; Thu, 11 Aug 2022 04:43:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DD7858E0001; Thu, 11 Aug 2022 04:43:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id CE14F6B0074 for ; Thu, 11 Aug 2022 04:43:36 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id A7FFCC077D for ; Thu, 11 Aug 2022 08:43:36 +0000 (UTC) X-FDA: 79786673232.10.38999AF Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) by imf31.hostedemail.com (Postfix) with ESMTP id 4669720181 for ; Thu, 11 Aug 2022 08:43:35 +0000 (UTC) Received: by mail-pf1-f177.google.com with SMTP id b133so15936765pfb.6 for ; Thu, 11 Aug 2022 01:43:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc; bh=lPWGy4oKPBEqayKpF00Kr7fLSntX8eH+HLycUNnPzWY=; b=TKNAY5DMmWPsUFB1Wa89v8zyOj4EVDpIP4AknSdQfHo9BqyxoEkOJgDNmzNkBQNAMG SZfGoH+1RdTVl1vmm+l0PoN12Mtm7fFTQkyBIGIBtS+WQ42FRRHKnNOj+C5C+TksvoAG wdrKZTK5/TgqdwKZDZtMiYlyhadrrmObnUKKfDNZtLu6kE+7jkp1hV+Rns1ACIOUIzeo CF5V+L1sWU3s88okabfa6IF7mvDqCAY+iCR+VVBhKT4D1oWSg4K+fZAmaQqd97qWalfi ycyPIK9oE0ZRsQH2wQkB7aBa4fT4jDn6wiPhgsVmcso9vu0BkoMG161Kd9KZTqFGd3un 5ICg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc; bh=lPWGy4oKPBEqayKpF00Kr7fLSntX8eH+HLycUNnPzWY=; b=0EKtDedjBnDwQIVa7B+xHhSYNrdfvByF0Pd6jKHAsng35np8gr+pq8kYgfq/GHivjY XLndwAoCD+Me6Tk/iSSyeYA3+ZmJhUg8Q0DGBfx1zanwFFFvAq5UpukjU5YtIX6aOp5R AurhNrdjEllX2mu6stVjHbVjr7JkNXxmvRhGnBLBx/G3ic3AvWmHhDVd621i0Dfj2NhG thvh+9GaqRGBHxVq/EcWwkoJWO3SDr8LBzPlnaqBV1UqlCuwRx8MAz+kahynmYVgpGLF uNZm3kFz5qAuVzYqo3QsZQ7UoyOBKjP+gR0xnFQ47Z6UqMZN2fM6yws5KX0SZ65lKDG+ BYEg== X-Gm-Message-State: ACgBeo2UBYosiTEmmQW2v3rDXTlroTWrQx4H7ij/Cv4+0qlgJbaj5wXh goy/rdcUnAuEmLDOb4zPgMEEjw== X-Google-Smtp-Source: AA6agR4bUxoCD4Nwny0TXSKv5BtfxlGKcb25Zvty6m0u8Irjfklmr9/rDC1JoSk1AZGgHULBU3fFPA== X-Received: by 2002:aa7:8d04:0:b0:52e:1778:bcde with SMTP id j4-20020aa78d04000000b0052e1778bcdemr31418527pfe.58.1660207413976; Thu, 11 Aug 2022 01:43:33 -0700 (PDT) Received: from [10.94.58.189] ([139.177.225.254]) by smtp.gmail.com with ESMTPSA id a67-20020a624d46000000b0052d8405bcd2sm3453305pfb.163.2022.08.11.01.43.30 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 11 Aug 2022 01:43:33 -0700 (PDT) Message-ID: <7ece0714-2646-4f1a-60b6-aaafc1135b1e@bytedance.com> Date: Thu, 11 Aug 2022 16:43:28 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.12.0 Subject: Re: [PATCH] mm/mempolicy: fix lock contention on mems_allowed Content-Language: en-US To: Michal Hocko Cc: Andrew Morton , Vlastimil Babka , Mel Gorman , Muchun Song , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Abel Wu References: <20220809104927.44366-1-wuyun.abel@bytedance.com> From: Abel Wu In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit ARC-Authentication-Results: i=1; imf31.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=TKNAY5DM; spf=pass (imf31.hostedemail.com: domain of wuyun.abel@bytedance.com designates 209.85.210.177 as permitted sender) smtp.mailfrom=wuyun.abel@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660207416; a=rsa-sha256; cv=none; b=Y3BWOhq9MOsCrVadVt6LNAH5O9kTtEEhLnq8nI2s5xPfPvtTPetkvP5ivL3plYrwIsepzf RFb8vqZf6WCzcZBLoToxDBNpqB3IkFfiqt9S2oWmYFeK6s+FOCAELSE/A9ndwMwDa5jBh6 ZoKNqNDJUaWQOYrZJAxdumlEuNMPJxg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660207416; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=lPWGy4oKPBEqayKpF00Kr7fLSntX8eH+HLycUNnPzWY=; b=o7slp2qtGdsH0K1EaJcI5f4+RdsVGvdBualGStocqzLL0uGgUkamE++XPRoojnzpGI2Q+Q 1VhHpYXQQvy4kNhDdYaw8lBjmRC/+M/HbGSFgobEbKAfJy82TK91z1iv951gf53mjO2r9g ailiK1WxFVTfcbEJlm4yTJZjr7PEKUc= Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=TKNAY5DM; spf=pass (imf31.hostedemail.com: domain of wuyun.abel@bytedance.com designates 209.85.210.177 as permitted sender) smtp.mailfrom=wuyun.abel@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 4669720181 X-Stat-Signature: indkd1ztt6gmch1jatzfunokpsj9jjpu X-Rspam-User: X-HE-Tag: 1660207415-730261 X-Bogosity: Ham, tests=bogofilter, spamicity=0.056905, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On 8/9/22 8:11 PM, Michal Hocko Wrote: > On Tue 09-08-22 18:49:27, Abel Wu wrote: >> The mems_allowed field can be modified by other tasks, so it >> isn't safe to access it with alloc_lock unlocked even in the >> current process context. > > It would be useful to describe the racing scenario and the effect it > would have. 78b132e9bae9 hasn't really explained thinking behind and why > it was considered safe to drop the lock. I assume it was based on the > fact that the operation happens on the current task but this is hard to > tell. > Sorry for my poor description. Say there are two tasks: A from cpusetA is performing set_mempolicy(2), and B is changing cpusetA's cpuset.mems. A (set_mempolicy) B (echo xx > cpuset.mems) pol = mpol_new(); update_tasks_nodemask(cpusetA) { foreach t in cpusetA { cpuset_change_task_nodemask(t) { task_lock(t); // t could be A mpol_set_nodemask(pol) { new = f(A->mems_allowed); update t->mems_allowed; pol.create(pol, new); } task_unlock(t); task_lock(A); A->mempolicy = pol; task_unlock(A); } } } In this case A's pol->nodes is computed by old mems_allowed, and could be inconsistent with A's new mems_allowed. While it is different when replacing vmas' policy: the pol->nodes is gone wild only when current_cpuset_is_being_rebound(): A (mbind) B (echo xx > cpuset.mems) cpuset_being_rebound = cpusetA; update_tasks_nodemask(cpusetA) { foreach t in cpusetA { cpuset_change_task_nodemask(t) { task_lock(t); // t could be A pol = mpol_new(); mmap_write_lock(A->mm); mpol_set_nodemask(pol) { mask = f(A->mems_allowed); update t->mems_allowed; pol.create(pol, mask); } task_unlock(t); } foreach v in A->mm { if (current_cpuset_is_being_rebound()) pol.rebind(pol, cpuset.mems); v->vma_policy = pol; } mmap_write_unlock(A->mm); mmap_write_lock(t->mm); mpol_rebind_mm(t->mm); mmap_write_unlock(t->mm); } } cpuset_being_rebound = NULL; In this case, the cpuset.mems, which has already done updating, is finally used for calculating pol->nodes, rather than A->mems_allowed. So it is OK to call mpol_set_nodemask() with alloc_lock unlocked when doing mbind(2). Best Regards, Abel