Received: by 2002:ac8:5491:0:b0:40f:fb00:664b with SMTP id h17csp607581qtq; Thu, 10 Aug 2023 10:21:05 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHO9iGdn84zNuDa0xXT5KH+BdO1n1l34tRS96BTGPEaQgie5q74p951KK54V5lcSgZGrYR3 X-Received: by 2002:a05:6512:3582:b0:4fe:95e:159a with SMTP id m2-20020a056512358200b004fe095e159amr2189910lfr.23.1691687859602; Thu, 10 Aug 2023 10:17:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691687859; cv=none; d=google.com; s=arc-20160816; b=hIKlOCCmrb97mhEp1OSUcotOeJIXolIieirQbedIM1bxreHU3EE5l2SfXT9SL2fjvH v9xkrq9A3CQayOmL4wFu/h1jL24M8pf6bNOyls+fi7FaBDi9gXmRhFGQlmqfLX1rrzs6 gzwOR+o+3nI6B4ftY6swl8KLb+kBUPYG+nKc78T/XngZqCxRYxTTqoSCCy/yTC6LrOUu +lzDUYgi4XEF2gauV9miycHULUhrgPkjUMcA7tqkl1X7GTyTLWNV6iD9xxAyM7Nd4Uyu WA7xPLxPdUYb4Il80iKD+hqrx7K2eILBTPTiUMJ7GZPUD60KPMHj/NxIhRyhV9lK1Mbp UHcA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=mwkogVir+aH9lwGglDFwEcp1cybyKG7KluQCWJj8CDk=; fh=SlTczw0bh+e+fxVtlOtfD5cEkkDBtpQt3faW9rEss/M=; b=XWSMnWbkrA3EnWwdeDTELkz/FMzJKEhOAJF/Q3q6xYIDK2ItrjGEntsqIU0j9SetRs UL3yhBAH8iWvYJAo5eNFriOrpNslwPw51hJH0zaMZRsqEuI8WaI8IKROmAHwgSoxk65T KViLwOxQ4ia6t5i5+svJgk/N2ZkHBOjYOHDK3m9oWuHV+CVw6zoSccrUOG6Kbrv9ux/Y bvsnY4IZrk/sLf+MNq2xrHM/o6l7WCjezEr2uNSuAyePgHwFO231n6yBtcVw4ULJP5p+ drdTEmh+T2VwHyHi6GZcagj1X5/u1eLLBE/6SJFSMfQAM4NibChTJ+ot77ULZbzYQI7N tlJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=ooGCXLyc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o22-20020a056402039600b00522e4d22117si1849263edv.76.2023.08.10.10.17.15; Thu, 10 Aug 2023 10:17:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=ooGCXLyc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235928AbjHJPhg (ORCPT + 99 others); Thu, 10 Aug 2023 11:37:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34362 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232201AbjHJPhe (ORCPT ); Thu, 10 Aug 2023 11:37:34 -0400 Received: from mail-il1-x12a.google.com (mail-il1-x12a.google.com [IPv6:2607:f8b0:4864:20::12a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 018FC26B6 for ; Thu, 10 Aug 2023 08:37:33 -0700 (PDT) Received: by mail-il1-x12a.google.com with SMTP id e9e14a558f8ab-34984bff431so3609305ab.2 for ; Thu, 10 Aug 2023 08:37:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1691681852; x=1692286652; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=mwkogVir+aH9lwGglDFwEcp1cybyKG7KluQCWJj8CDk=; b=ooGCXLyctROezjPLyOuJ9wcLTxpSVm11cpq1ZV7QNPqLOC4D+7Lb0N2iKFTTVru3V5 JaNFZAI5Wot6a+ZnQLVc5AvMFagexbeCK0Ine/+siTUjfltUxCrYM2NhWfVfOnU6nnxL xfS5qxGV+eF7DV+DI+5jd8OsiRkSBOfnbeCEynWsMXTG4DE+zizMuqilMSxVYp66D22G cAB46IjI33e+HorPCMIMKR3LKVsYAQjGYpH0j5Vy3djYi9BMwyEod8YESFa9+l9N+BXR mJleXwuQUW3S990avKjBJyeMrQ4TU3KXgPl86sbPFkr2C1/fL1BirxHM3NbZF0V14t1/ mCvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691681852; x=1692286652; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mwkogVir+aH9lwGglDFwEcp1cybyKG7KluQCWJj8CDk=; b=FDltclpgquZI5YBSb7yvI2Le3crlSkXrPuZ1Sohu3gPKNWdEsVQaDgGT0epdkSPyb7 XyA/ldMoXGndZnk07gZ6qwqh8UOFaSbcJlP02nj/pccHewetEBMYKn7iSNCFrShaO/Ps RpMkxux+/02bWDojMZI1owAYgLCH57EcIb4uUlL0JP+Bv8evmaoaW8GlKTzx2FVloqZ6 8Oaj+M1r9u8wlA8AQiA7MNma3820n4LHGfBB8A6tOFQit6ttvYInViO2JKNGrscHbCgG fTgaPjskf+RyaZQ9897HmtAJ6bc2V2642miSQCzwl3X1yBcJ0Cs72lm7CRf60vWEXz7Q Aszg== X-Gm-Message-State: AOJu0YwQYwSrc0oVcb23v93grPghn/f1OhpHT/ahQyeDGi50oyfjLvQq HA+Lfis18KqeJnMYxbF93UftuRJZqtxzZrZhYK4= X-Received: by 2002:a92:c544:0:b0:346:fe2:125a with SMTP id a4-20020a92c544000000b003460fe2125amr4155929ilj.26.1691681852295; Thu, 10 Aug 2023 08:37:32 -0700 (PDT) MIME-Version: 1.0 References: <20230727062910.6337-1-andrew.yang@mediatek.com> In-Reply-To: <20230727062910.6337-1-andrew.yang@mediatek.com> From: Nhat Pham Date: Thu, 10 Aug 2023 08:37:21 -0700 Message-ID: Subject: Re: [PATCH v2] zsmalloc: Fix races between modifications of fullness and isolated To: Andrew Yang Cc: Minchan Kim , Sergey Senozhatsky , Andrew Morton , Matthias Brugger , AngeloGioacchino Del Regno , Sebastian Andrzej Siewior , wsd_upstream@mediatek.com, casper.li@mediatek.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jul 26, 2023 at 11:30=E2=80=AFPM Andrew Yang wrote: > > Since fullness and isolated share the same unsigned int, > modifications of them should be protected by the same lock. > > Signed-off-by: Andrew Yang > Fixes: c4549b871102 ("zsmalloc: remove zspage isolation for migration") > --- > v2: Moving comment too > --- > mm/zsmalloc.c | 14 +++++++++----- > 1 file changed, 9 insertions(+), 5 deletions(-) > > diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c > index 32f5bc4074df..b58f957429f0 100644 > --- a/mm/zsmalloc.c > +++ b/mm/zsmalloc.c > @@ -1777,6 +1777,7 @@ static void replace_sub_page(struct size_class *cla= ss, struct zspage *zspage, > > static bool zs_page_isolate(struct page *page, isolate_mode_t mode) > { > + struct zs_pool *pool; > struct zspage *zspage; > > /* > @@ -1786,9 +1787,10 @@ static bool zs_page_isolate(struct page *page, iso= late_mode_t mode) > VM_BUG_ON_PAGE(PageIsolated(page), page); > > zspage =3D get_zspage(page); > - migrate_write_lock(zspage); > + pool =3D zspage->pool; > + spin_lock(&pool->lock); > inc_zspage_isolation(zspage); > - migrate_write_unlock(zspage); > + spin_unlock(&pool->lock); > > return true; > } > @@ -1854,12 +1856,12 @@ static int zs_page_migrate(struct page *newpage, = struct page *page, > kunmap_atomic(s_addr); > > replace_sub_page(class, zspage, newpage, page); > + dec_zspage_isolation(zspage); > /* > * Since we complete the data copy and set up new zspage structur= e, > * it's okay to release the pool's lock. > */ > spin_unlock(&pool->lock); > - dec_zspage_isolation(zspage); > migrate_write_unlock(zspage); > > get_page(newpage); > @@ -1876,14 +1878,16 @@ static int zs_page_migrate(struct page *newpage, = struct page *page, > > static void zs_page_putback(struct page *page) > { > + struct zs_pool *pool; > struct zspage *zspage; > > VM_BUG_ON_PAGE(!PageIsolated(page), page); > > zspage =3D get_zspage(page); > - migrate_write_lock(zspage); > + pool =3D zspage->pool; > + spin_lock(&pool->lock); > dec_zspage_isolation(zspage); > - migrate_write_unlock(zspage); > + spin_unlock(&pool->lock); > } > > static const struct movable_operations zsmalloc_mops =3D { > -- > 2.18.0 > > I think this fixes an issue of mine that has been bugging me for the past couple of weeks :) Thanks a lot, Andrew! This should be added to other Linux stable versions too, right? (with the caveat that before 6.2, use class's lock instead of pool's lock). Reviewed-by: Nhat Pham