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 529CFC433FE for ; Wed, 11 May 2022 21:45:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 42F176B0074; Wed, 11 May 2022 17:45:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3DE856B0075; Wed, 11 May 2022 17:45:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 27FFC6B0078; Wed, 11 May 2022 17:45:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 1A4776B0074 for ; Wed, 11 May 2022 17:45:35 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id DDAC130C77 for ; Wed, 11 May 2022 21:45:34 +0000 (UTC) X-FDA: 79454794188.03.7FD6CCF Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf28.hostedemail.com (Postfix) with ESMTP id 1E6BEC00A9 for ; Wed, 11 May 2022 21:45:13 +0000 (UTC) Received: by mail-pl1-f182.google.com with SMTP id i1so3115583plg.7 for ; Wed, 11 May 2022 14:45:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=hccI8UlqfXJMYenm6/39/T+QVSnq+XdDQON/tzN24oY=; b=rzrJHZm5ihs/+enyg9JHpz6lDWL+hLa4gt+I2QhT/IYCn2y0ahrrz2bHH7V1i5bv8a 08SrUK/4uy8smVTxNcmUTNjeOI8ap1t9n4zHVpKttxJwJhpLyVv8ZGGH+LlTf6Bbp+US WIqVxFlj76v/CzJp664eyB163i6Oiu8Oc1gkoXw7JxKKP+fP1tsCOpMoe/BFSs86lLp+ kJiyjsvA6qLP+Ynt/HK+nFKxlqRG1X4Q2ov721tQK8n+cLoYuJLblXNmGV1BobKi3uZN P42m4OmoyH56wMEhRhJJYVJs0PzW4sxv4i7T/jn+eEh/vGTuvWaUUZG43GRxu3IMfP2/ g/HQ== X-Gm-Message-State: AOAM530xpUfx30xSYi+GTI5AkM65XrZIPCIHHrJ8uOVpSDV+xTfpr7/z 3LrHDQqELtzr9WnwxrbdoSI= X-Google-Smtp-Source: ABdhPJyqwwqMWMSTHIkPjyk0V3hrhrXMPefLSWa46Y0DRvr8ESWCK43x0POV9JQ/7C6WKYMm+wlv6Q== X-Received: by 2002:a17:90a:db45:b0:1d9:29d0:4c6e with SMTP id u5-20020a17090adb4500b001d929d04c6emr7428285pjx.46.1652305533462; Wed, 11 May 2022 14:45:33 -0700 (PDT) Received: from sultan-box.localdomain ([204.152.216.102]) by smtp.gmail.com with ESMTPSA id az12-20020a170902a58c00b0015e8d4eb2d5sm2312664plb.287.2022.05.11.14.45.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 May 2022 14:45:32 -0700 (PDT) Date: Wed, 11 May 2022 14:45:30 -0700 From: Sultan Alsawaf To: Minchan Kim Cc: stable@vger.kernel.org, Nitin Gupta , Sergey Senozhatsky , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] zsmalloc: Fix races between asynchronous zspage free and page migration Message-ID: References: <20220509024703.243847-1-sultan@kerneltoast.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Queue-Id: 1E6BEC00A9 X-Stat-Signature: uejmrbg4gom5ramwwd79kf9158kbnrng Authentication-Results: imf28.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf28.hostedemail.com: domain of sultan.kerneltoast@gmail.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=sultan.kerneltoast@gmail.com X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1652305513-80554 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 Wed, May 11, 2022 at 02:07:19PM -0700, Minchan Kim wrote: > Then, how about this? Your proposal is completely wrong still. My original patch is fine; we can stick with that. > diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c > index 9152fbde33b5..2f205c18aee4 100644 > --- a/mm/zsmalloc.c > +++ b/mm/zsmalloc.c > @@ -1716,12 +1716,31 @@ static enum fullness_group putback_zspage(struct size_class *class, > * To prevent zspage destroy during migration, zspage freeing should > * hold locks of all pages in the zspage. > */ > -static void lock_zspage(struct zspage *zspage) > +static void lock_zspage(struct zs_pool *pool, struct zspage *zspage) > { > - struct page *page = get_first_page(zspage); > - > + struct page *page; > + int nr_locked; > + struct page *locked_pages[ZS_MAX_PAGES_PER_ZSPAGE]; > + struct address_space *mapping; > +retry: > + nr_locked = 0; > + memset(locked_pages, 0, sizeof(struct page) * ARRAY_SIZE(locked_pages)); This memset() zeroes out memory past the end of the array because it is an array of pointers, not an array of page structs; the sizeof() is incorrect. > + page = get_first_page(zspage); You can't use get_first_page() outside of the migrate lock. > do { > lock_page(page); You can't lock a page that you don't own. > + locked_pages[nr_locked++] = page; > + /* > + * subpage in the zspage could be migrated under us so > + * verify it. Once it happens, retry the lock sequence. > + */ > + mapping = page_mapping(page) This doesn't compile. > + if (mapping != pool->inode->i_mapping || > + page_private(page) != (unsigned long)zspage) { > + do { > + unlock_page(locked_pages[--nr_locked]); > + } while (nr_locked > 0) This doesn't compile. > + goto retry; > + } There's no need to unlock all of the pages that were locked so far because once a page is locked, it cannot be migrated. > } while ((page = get_next_page(page)) != NULL); > } You can't use get_next_page() outside of the migrate lock. > > @@ -1987,7 +2006,7 @@ static void async_free_zspage(struct work_struct *work) > > list_for_each_entry_safe(zspage, tmp, &free_pages, list) { > list_del(&zspage->list); > - lock_zspage(zspage); > + lock_zspage(pool, zspage); > > get_zspage_mapping(zspage, &class_idx, &fullness); > VM_BUG_ON(fullness != ZS_EMPTY); Sultan