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 46899C28D13 for ; Sat, 20 Aug 2022 02:07:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8E8A46B0073; Fri, 19 Aug 2022 22:07:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 898CD8D0002; Fri, 19 Aug 2022 22:07:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 739756B0075; Fri, 19 Aug 2022 22:07:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 64D126B0073 for ; Fri, 19 Aug 2022 22:07:41 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 38B371204D7 for ; Sat, 20 Aug 2022 02:07:41 +0000 (UTC) X-FDA: 79818334722.27.B780113 Received: from mail-ej1-f42.google.com (mail-ej1-f42.google.com [209.85.218.42]) by imf03.hostedemail.com (Postfix) with ESMTP id 0A89E20059 for ; Sat, 20 Aug 2022 02:06:03 +0000 (UTC) Received: by mail-ej1-f42.google.com with SMTP id u15so3149875ejt.6 for ; Fri, 19 Aug 2022 19:06:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=user-agent:in-reply-to:content-disposition:mime-version:references :reply-to:message-id:subject:cc:to:from:date:from:to:cc; bh=PGhNSQLIn/ZoIFIjGbm3ePERCEF0hihTt1y3R9iOg80=; b=EjpOGt+n+VN/1y/8qbnTOTLkdvTXGd+Eyu2sa89rqbJW2T+djoGepA5BY4y0Cab7N/ 4uTltTKW/kCCFesRxXG8wyyk3YJywtLYPwq1wXc+G9g+3AYdXBuRdqESxkfmwy7XjyI0 bJTFJFFugORG1om/MuxxuGUQi/QlhyTvMnYWwBEo4g2se4TbINfNhpHnl18v1oVzh9H/ Dww/kXsdQ/7AA0Fxo3G8ovXkQwhqOTLCntE8Kl87Xvxrbl24bwPqnxed2lHxaLjhYl97 mN3SeRDjI0AQpHanJPYcTSG1dlFCZBheT2T6CPLZ5zHllIAHBt5XnvFMjk1MWBYesTHp OLBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=user-agent:in-reply-to:content-disposition:mime-version:references :reply-to:message-id:subject:cc:to:from:date:x-gm-message-state:from :to:cc; bh=PGhNSQLIn/ZoIFIjGbm3ePERCEF0hihTt1y3R9iOg80=; b=ITPqyxRBH9Nj7Ajft/i7nSPLOKpL5LQmJhiyAqzqZ2Gs9Xdia8AAJXeCglQV2Z0gTN Gm9j+zBPNeLCW/qsqFiDoD+2iddZOTpHujGFdHDctN8gVOuVQWDGPCpzIt8XUFhW+3yu iOXJV9s01INwEmWEAa+2KXgDclIssCUjWec+UDqjIh18YmX2x/2H2UoTn9+LijDJU3bZ pPwicbNwbGflDfrvjCOrGMQ660OZhkTjCKkU3yulAJE/0/TH1BVDtqY2KahCCsTrzQDf +/LgaVI28k4U0hbJm3vnN9taDOV4dGlptMWhWAX0TlU4dQxV/047aPPTi1vvbazsaiL/ sDow== X-Gm-Message-State: ACgBeo2nI9K2uxVWUm/E1hYT3kR2ebaoFpGvS68QmqDuIUfA+rb+i8Pj 4sX71Nj8wVm4UkB9WtLjfi8= X-Google-Smtp-Source: AA6agR7G9k+/d7JV7sNAZQ5LDRtXkrTgcCZ/VHRhnNf8pdCjVF2oMv2AQzXjhGCb9krBii0wYNHtSw== X-Received: by 2002:a17:907:6297:b0:72f:9aad:fcb with SMTP id nd23-20020a170907629700b0072f9aad0fcbmr6397591ejc.161.1660961162617; Fri, 19 Aug 2022 19:06:02 -0700 (PDT) Received: from localhost ([185.92.221.13]) by smtp.gmail.com with ESMTPSA id gq10-20020a170906e24a00b00734b3194ecesm3062400ejb.163.2022.08.19.19.06.01 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 19 Aug 2022 19:06:01 -0700 (PDT) Date: Sat, 20 Aug 2022 02:06:01 +0000 From: Wei Yang To: Michal Hocko Cc: Abel Wu , Andrew Morton , Vlastimil Babka , Mel Gorman , Muchun Song , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Wei Yang Subject: Re: [PATCH v2] mm/mempolicy: fix lock contention on mems_allowed Message-ID: <20220820020601.vxeotpde5obuauqt@master> Reply-To: Wei Yang References: <20220811124157.74888-1-wuyun.abel@bytedance.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20170113 (1.7.2) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660961164; h=from:from:sender:reply-to: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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=PGhNSQLIn/ZoIFIjGbm3ePERCEF0hihTt1y3R9iOg80=; b=bmx/ocPPkUCOw4fCpQrl5vvT8laIjMt/TozvNebCQjIxTdIeENN99nSyH2LM4Drdd1qu7f zdZ09B0HZJEpDwwaKyWb4Q9zEG0ChvgNRgPqtp1iW8Q3Ynx/j8g7BnAm8XeGzfQZlYsai3 Jsz5jDnYXzScJMnx2Q8Bf9XqLH1WqPs= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=EjpOGt+n; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf03.hostedemail.com: domain of richard.weiyang@gmail.com designates 209.85.218.42 as permitted sender) smtp.mailfrom=richard.weiyang@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660961164; a=rsa-sha256; cv=none; b=zk17Y5UXGWmWZcRZf37FiDWM1XQqmp+cOIIWNRIXJGpivG5f+cSOMg+TnF+H2osNnG39ru wVaH0n0bNyGkViDWu5cb2F7U7WeRvlUKPhz8B2rbuiasPBjxM85GfFe0BgBBZvwWwA+MsR VXLrb+bhA4En9NuFSRKtZ/fX7A0v+dg= Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=EjpOGt+n; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf03.hostedemail.com: domain of richard.weiyang@gmail.com designates 209.85.218.42 as permitted sender) smtp.mailfrom=richard.weiyang@gmail.com X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 0A89E20059 X-Stat-Signature: c55mysnizzjux7j86n4ekagrifqd7ef4 X-Rspam-User: X-HE-Tag: 1660961163-233891 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000199, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Thu, Aug 11, 2022 at 04:11:21PM +0200, Michal Hocko wrote: >fix the lkml address (fat fingers, sorry) > >On Thu 11-08-22 16:06:37, Michal Hocko wrote: >> [Cc Wei Yang who is author of 78b132e9bae9] >> >> On Thu 11-08-22 20:41:57, 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. >> > >> > 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) { >> > mpol_set_nodemask(pol) { >> > task_lock(t); // t could be A >> > 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. >> >> Just to clarify. With an unfortunate timing and those two nodemasks >> overlap the end user effect could be a premature OOM because some nodes >> wouldn't be considered, right? >> >> > 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) >> > ------------------------------------------------------- >> > pol = mpol_new(); >> > mmap_write_lock(A->mm); >> > cpuset_being_rebound = cpusetA; >> > update_tasks_nodemask(cpusetA) { >> > foreach t in cpusetA { >> > cpuset_change_task_nodemask(t) { >> > mpol_set_nodemask(pol) { >> > task_lock(t); // t could be A >> > mask = f(A->mems_allowed); >> > update t->mems_allowed; >> > pol.create(pol, mask); >> > task_unlock(t); >> > } >> > } >> > foreach v in A->mm { >> > if (cpuset_being_rebound == cpusetA) >> > 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). >> > >> > Fixes: 78b132e9bae9 ("mm/mempolicy: remove or narrow the lock on current") >> > Signed-off-by: Abel Wu >> Thanks for pointing out. This looks correct. Reviewed-by: Wei Yang