Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3324170imu; Mon, 19 Nov 2018 14:13:45 -0800 (PST) X-Google-Smtp-Source: AJdET5dm5HH2tIBJrMMScz3rtHIV7vL51eXiyA8YxHEODmXQmvseziWlntbnDSu9FoTABZm+sKWz X-Received: by 2002:a63:6645:: with SMTP id a66mr13443907pgc.390.1542665625391; Mon, 19 Nov 2018 14:13:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542665625; cv=none; d=google.com; s=arc-20160816; b=Wrco2R5y5qHX7GubOgJxqRUqOekFYGTM3p7GCTGhysaPsyMiBwIXUeqWNN4zQ6U/VT SEPl13LZl71lr9ay1O+U1gaLberSoUE37myictbmigt9d95KA3QAXlv/yWot1JkiRvnP mc44SmehLUFkqu3WDGjTxKXFpSbkGw0+lGiWbkLbXdsFbOANs49VNPA4rP2szZfhxH4C VffuNsX6lkDlmxTMUYq/O6ksBLoXZAQbI/NpwYb7AcjECOiqLFmmwPBjAk2Bq0A/IqGd aT8kR4By5VVNx/vz+pmzp5XTIBHEcxqaJ6elxq+FXSgZkDZas688tlOpOsebNXoPq5gI BZGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :message-id:in-reply-to:subject:cc:to:from:date:dkim-signature; bh=x18zujgfCKPt3T5ACkYo8+FKF7H3NMDCTuUkJ/syaNs=; b=LAxZpD18+tsJrCKOBWgGQ4Lzxf4sUNje8zvet3JIlHyLD3AAozjBVpy+SBS8B/P3LI uXl6R8azXw2bp8reeXFZQKEnlF3c/LjXOBsdcy+G4iEFMYi0nR12O1tBzq2SjSOfY1y7 TkvNOodU65Sp0mDtHx2zSqnxQ2Zrd9yPDpHaruu88dszkfeanQnWBgwaS9G9eQEjsV9f VcU876OnakkBvNc0wqfUAh/F4sRC7UiYm26Mi1tfClZ+Oj3xV10TaW4heQwdkHJOtZQZ y5kz8C4l2FAmGFYfV5m16rSqymu0ai6taostoenoRvmu54TPVOPx9SbGTRXwQIAn1w/S WPHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=VcNgZvq7; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 207si1495934pfz.249.2018.11.19.14.13.30; Mon, 19 Nov 2018 14:13:45 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=VcNgZvq7; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731487AbeKTIhW (ORCPT + 99 others); Tue, 20 Nov 2018 03:37:22 -0500 Received: from mail-pl1-f193.google.com ([209.85.214.193]:33886 "EHLO mail-pl1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731313AbeKTIhV (ORCPT ); Tue, 20 Nov 2018 03:37:21 -0500 Received: by mail-pl1-f193.google.com with SMTP id f12-v6so15244454plo.1 for ; Mon, 19 Nov 2018 14:11:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:in-reply-to:message-id:references :user-agent:mime-version; bh=x18zujgfCKPt3T5ACkYo8+FKF7H3NMDCTuUkJ/syaNs=; b=VcNgZvq7pV7RWUGxMq60xdZzFKYXEGZh5GSnzr5KOZbm57SP3DWhNJ8le8PAVyvIXp yHY4bkcTj4dVPZZgUWCpVTD8L1olJ5cuQiY7Mtgh+AnGvSmUtdTc77U2fB0AaUdRRCq1 b8kll4LiQg7uZfSxlMv/fhb2O4aShkkk1qisgia5ikz+yvjy3JmfaoglIN9FRbLvNX5x 3uVVvbcQhsf2Iu8Id6QGnJknSRjXaYLICROUwFZ9NP5yqCB3ppKX4Oqq75gHCPOj6iXi L3Kqks0bXe6fUo/cbagRSq8O5Xg3Y44ezoHudYBtw+HQGwgWqCHhBG+xokCTq8jnul8B s6aA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:in-reply-to:message-id :references:user-agent:mime-version; bh=x18zujgfCKPt3T5ACkYo8+FKF7H3NMDCTuUkJ/syaNs=; b=W0Pg7RCTppRj4Uhi1gkL9v1jq101OBslJnvNKo1Y7lquI2qSVIOcAq1yixtYYHbmVT eFjREJNjrq3tcrsj1xlbcy2iin4+qn04basKx5hGnKyLUvEk2uFcys4Hul0ejyNcXHs/ HujTX34hVvDYSvlMOImRVDi610E7FrfA2gTdJnMncL0oenlFAp6q6GY06t4v3hgNrGL9 NCMtr1dyKWuzWj8WZln+02dJ4G3+x9ZqpTt/oOJmIAa74l2mMWXLowOspglybDmFg6yw MM2pAcf59MoJSG+uQ0kfZ2UwsJMx5HwohvMMRjcVcEADwDflE2HOmDEGfnRQ9rjmyKhd rItA== X-Gm-Message-State: AA+aEWZaCjgM5AFcv6z24r4RGWPWt1fiN3nwP7kSWxtRBouDsYvZ59aE fFqeya6iP803HFAerCF7RRGdpX1pZeQ= X-Received: by 2002:a17:902:43e4:: with SMTP id j91mr3182911pld.147.1542665496347; Mon, 19 Nov 2018 14:11:36 -0800 (PST) Received: from [100.112.89.103] ([104.133.8.103]) by smtp.gmail.com with ESMTPSA id y11-v6sm6444597pfk.70.2018.11.19.14.11.34 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 19 Nov 2018 14:11:35 -0800 (PST) Date: Mon, 19 Nov 2018 14:11:27 -0800 (PST) From: Hugh Dickins X-X-Sender: hugh@eggly.anvils To: Yu Zhao cc: Hugh Dickins , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] mm: fix swap offset when replacing shmem page In-Reply-To: <20181119010924.177177-1-yuzhao@google.com> Message-ID: References: <20181119004719.156411-1-yuzhao@google.com> <20181119010924.177177-1-yuzhao@google.com> User-Agent: Alpine 2.11 (LSU 23 2013-08-11) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, 18 Nov 2018, Yu Zhao wrote: > We used to have a single swap address space with swp_entry_t.val > as its radix tree index. This is not the case anymore. Now Each > swp_type() has its own address space and should use swp_offset() > as radix tree index. > > Signed-off-by: Yu Zhao This fix is a great find, thank you! But completely mis-described! And could you do a smaller patch, keeping swap_index, that can go to stable without getting into trouble with the recent xarrifications? Fixes: bde05d1ccd51 ("shmem: replace page if mapping excludes its zone") Cc: stable@vger.kernel.org # 3.5+ Seems shmem_replace_page() has been wrong since the day I wrote it: good enough to work on swap "type" 0, which is all most people ever use (especially those few who need shmem_replace_page() at all), but broken once there are any non-0 swp_type bits set in the higher order bits. > --- > mm/shmem.c | 11 +++++++---- > 1 file changed, 7 insertions(+), 4 deletions(-) > > diff --git a/mm/shmem.c b/mm/shmem.c > index d44991ea5ed4..685faa3e0191 100644 > --- a/mm/shmem.c > +++ b/mm/shmem.c > @@ -1509,11 +1509,13 @@ static int shmem_replace_page(struct page **pagep, gfp_t gfp, > { > struct page *oldpage, *newpage; > struct address_space *swap_mapping; > - pgoff_t swap_index; > + swp_entry_t entry; Please keep swap_index as well as adding entry. > int error; > > + VM_BUG_ON(!PageSwapCache(*pagep)); > + I'd prefer you to drop that, it has no bearing on this patch; we used to have it, along with lots of other VM_BUG_ONs in here, but they outlived their usefulness, and don't need reintroducing - they didn't help at all to prevent the actual bug you've found. > oldpage = *pagep; > - swap_index = page_private(oldpage); > + entry.val = page_private(oldpage); entry.val = page_private(oldpage); swap_index = swp_offset(entry); > swap_mapping = page_mapping(oldpage); > > /* > @@ -1532,7 +1534,7 @@ static int shmem_replace_page(struct page **pagep, gfp_t gfp, > __SetPageLocked(newpage); > __SetPageSwapBacked(newpage); > SetPageUptodate(newpage); > - set_page_private(newpage, swap_index); > + set_page_private(newpage, entry.val); Yes. > SetPageSwapCache(newpage); > > /* > @@ -1540,7 +1542,8 @@ static int shmem_replace_page(struct page **pagep, gfp_t gfp, > * a nice clean interface for us to replace oldpage by newpage there. > */ > xa_lock_irq(&swap_mapping->i_pages); > - error = shmem_replace_entry(swap_mapping, swap_index, oldpage, newpage); > + error = shmem_replace_entry(swap_mapping, swp_offset(entry), > + oldpage, newpage); I'd prefer to omit that hunk, to avoid the xa_lock_irq() in the context; the patch is just as good if we keep the swap_index variable. > if (!error) { > __inc_node_page_state(newpage, NR_FILE_PAGES); > __dec_node_page_state(oldpage, NR_FILE_PAGES); > -- > 2.19.1.1215.g8438c0b245-goog Thanks, Hugh