Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp3532298ybi; Mon, 27 May 2019 01:29:49 -0700 (PDT) X-Google-Smtp-Source: APXvYqxxopbxyaExvjzbuQ6gSc7uWLKXMN/FuN6THQJHyIImeby7CewZf6fjiDGwBn2iUWH5dLNp X-Received: by 2002:a17:902:2907:: with SMTP id g7mr100108936plb.114.1558945789740; Mon, 27 May 2019 01:29:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558945789; cv=none; d=google.com; s=arc-20160816; b=cB1n2chBviit7vFe7hbloChgjlgp96WKRbeIOzL5EYzOvIU5fp1ZQ3N8Zpyj+hbAJZ h35EHKI4wLM0BiFdksT6wMkfesi3gmSvmKuZKW4WrbNMol+4dfm5h+faz5JkxNCzasrr ynbwkwzbhjyYMpielovixjGqjXmBzOcjDba0WHjGEqJioLw5AylKi7oGlxT3thC/eNwT j6ava5doZuUsZ1Q+O5zKmzxoenyLYrHuwmTvZbl9dCbZ79Fpx2lGE61mV8q4uI14NlIS bZ9gQT4nHfiTGwOhrAfXDkd80mF7TLaEEBX2MX3mjl3KLLvBiYhXsU24GYvL4xb7dUZe ln4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=s658wFeksNRFSKjgZoYcRljTvH0m7MCJR8yA+py9GLU=; b=Gm3oGt/LRu3PiRnJAwkYhnQP0fDI1hVAl0aW2L+welGNhqzL1d8VyvbuUy25tAwgDO mLQkDug2qQpWvu4w1D6QEjsxj1OJqxnMcbfIpH2iQcMGROX+pFBGaiMl4h//aZNcg14w SsmTBFL8RvJnMLLinE6qmEe53nYbku9dsPXDQ1MPI3BmFNRt5juN0DGyyZmHafAO3CL6 Hy/ULfuuuPUJbfc4UouZVdjso8lZr68kHy+2SjcJ1w2UlbmHKYXHTc6zIWXGADIKd5Sn h7XLg/Qs//4UegFWTf5OGD3S/ftfurFSRbo6li5IbWkFz19tDr6NnAOCeke5FZE5xvsL Y4bg== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z19si19632805pfc.265.2019.05.27.01.29.35; Mon, 27 May 2019 01:29:49 -0700 (PDT) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726452AbfE0I1W (ORCPT + 99 others); Mon, 27 May 2019 04:27:22 -0400 Received: from mga12.intel.com ([192.55.52.136]:56802 "EHLO mga12.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726073AbfE0I1W (ORCPT ); Mon, 27 May 2019 04:27:22 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 27 May 2019 01:27:21 -0700 X-ExtLoop1: 1 Received: from yhuang-dev.sh.intel.com ([10.239.159.29]) by fmsmga001.fm.intel.com with ESMTP; 27 May 2019 01:27:18 -0700 From: "Huang, Ying" To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , Hugh Dickins , "Paul E . McKenney" , Minchan Kim , Johannes Weiner , Tim Chen , Mel Gorman , =?UTF-8?q?J=C3=A9r=C3=B4me=20Glisse?= , Michal Hocko , Andrea Arcangeli , Yang Shi , David Rientjes , Rik van Riel , Jan Kara , Dave Jiang , Daniel Jordan , Andrea Parri Subject: [PATCH -mm] mm, swap: Simplify total_swapcache_pages() with get_swap_device() Date: Mon, 27 May 2019 16:27:14 +0800 Message-Id: <20190527082714.12151-1-ying.huang@intel.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Huang Ying total_swapcache_pages() may race with swapper_spaces[] allocation and freeing. Previously, this is protected with a swapper_spaces[] specific RCU mechanism. To simplify the logic/code complexity, it is replaced with get/put_swap_device(). The code line number is reduced too. Although not so important, the swapoff() performance improves too because one synchronize_rcu() call during swapoff() is deleted. Signed-off-by: "Huang, Ying" Cc: Hugh Dickins Cc: Paul E. McKenney Cc: Minchan Kim Cc: Johannes Weiner Cc: Tim Chen Cc: Mel Gorman Cc: Jérôme Glisse Cc: Michal Hocko Cc: Andrea Arcangeli Cc: Yang Shi Cc: David Rientjes Cc: Rik van Riel Cc: Jan Kara Cc: Dave Jiang Cc: Daniel Jordan Cc: Andrea Parri --- mm/swap_state.c | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/mm/swap_state.c b/mm/swap_state.c index f509cdaa81b1..b84c58b572ca 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -73,23 +73,19 @@ unsigned long total_swapcache_pages(void) unsigned int i, j, nr; unsigned long ret = 0; struct address_space *spaces; + struct swap_info_struct *si; - rcu_read_lock(); for (i = 0; i < MAX_SWAPFILES; i++) { - /* - * The corresponding entries in nr_swapper_spaces and - * swapper_spaces will be reused only after at least - * one grace period. So it is impossible for them - * belongs to different usage. - */ - nr = nr_swapper_spaces[i]; - spaces = rcu_dereference(swapper_spaces[i]); - if (!nr || !spaces) + /* Prevent swapoff to free swapper_spaces */ + si = get_swap_device(swp_entry(i, 1)); + if (!si) continue; + nr = nr_swapper_spaces[i]; + spaces = swapper_spaces[i]; for (j = 0; j < nr; j++) ret += spaces[j].nrpages; + put_swap_device(si); } - rcu_read_unlock(); return ret; } @@ -611,20 +607,16 @@ int init_swap_address_space(unsigned int type, unsigned long nr_pages) mapping_set_no_writeback_tags(space); } nr_swapper_spaces[type] = nr; - rcu_assign_pointer(swapper_spaces[type], spaces); + swapper_spaces[type] = spaces; return 0; } void exit_swap_address_space(unsigned int type) { - struct address_space *spaces; - - spaces = swapper_spaces[type]; + kvfree(swapper_spaces[type]); nr_swapper_spaces[type] = 0; - rcu_assign_pointer(swapper_spaces[type], NULL); - synchronize_rcu(); - kvfree(spaces); + swapper_spaces[type] = NULL; } static inline void swap_ra_clamp_pfn(struct vm_area_struct *vma, -- 2.20.1