Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp116401imu; Mon, 19 Nov 2018 18:57:38 -0800 (PST) X-Google-Smtp-Source: AJdET5fIUCjyR9EyYtY2xxZiQ0qUOl+lNokB2/tNjBGp9UcyvRg+d7tafzfBCljjiplmTTuC4EW9 X-Received: by 2002:a62:a1a:: with SMTP id s26mr303011pfi.31.1542682658094; Mon, 19 Nov 2018 18:57:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542682658; cv=none; d=google.com; s=arc-20160816; b=L9Is+liBKQQ3zJVpurzwVQViEUirqZH9JzL0WXR5CFLxARrMQIJPbhus2GJFQwBypu uKvue3QfbbLYSN7P4pYBY3xO5uQDyEo9jBdKxvZ3UKig21LH+N+EApWZUjBiE8Qsmmxu wlnFQeLDD/VovgxDIv/g3sk+XjlGX9+DOSpEacVYrsxYm5nYZF1khwgeOr2wChSBukWa B1Sugyr6PuSr4ZuggmYQskT0nZJ2fOOsD2FVegTWjpBJwgRpdH8rWlTwbnvlqZsP/1Vd j33dId47IpNTG9eDG19R3jfjhWOLmfzJodiAkyPGeZBa3XMmxkFsgaOOiR2q0qmZV9p2 MP0A== 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:in-reply-to :references:subject:cc:to:mime-version:user-agent:from:date :message-id; bh=PPPC5u28kbDCviNOkpIv7LAtG5HQif/YqEMDi2sFPb4=; b=btgBO+90tcNnHOHz8cdPsU9cqP/tvPBLVH0oAVD6uUTP6ydSpXi3pUvOGcXBlXf8cR ljehhHvH9W+YvzTTWigo/Eee66/kiEHRVL3y6Ak4czpc/iRyM44/lVbkpjGjIFTPeaAF O6by5TA9J5/yB3ynkGu4C81aZisHfv563dAZeDXOnre/NEoTUmkw/ZHcpTU3uftAWC0/ PPlCgxGXnOwPsqUqN8n/jTO7LbEuBiQBJXA2YpfnBELPKpev8Igwanjs3bsjGaqvpNi3 iGaIsBWm930/BKmmfdnGu+iziciKCx8M1TR5342H9x1z7HAQe6vXX4+7Dj5Buo/VBdNM dMEg== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j14si30964276pgg.44.2018.11.19.18.57.22; Mon, 19 Nov 2018 18:57:38 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731111AbeKTMpE (ORCPT + 99 others); Tue, 20 Nov 2018 07:45:04 -0500 Received: from szxga07-in.huawei.com ([45.249.212.35]:57176 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726559AbeKTMpD (ORCPT ); Tue, 20 Nov 2018 07:45:03 -0500 Received: from DGGEMS401-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id 66F0CE3842D95; Tue, 20 Nov 2018 10:18:19 +0800 (CST) Received: from [127.0.0.1] (10.177.29.68) by DGGEMS401-HUB.china.huawei.com (10.3.19.201) with Microsoft SMTP Server id 14.3.408.0; Tue, 20 Nov 2018 10:18:17 +0800 Message-ID: <5BF36EE9.9090808@huawei.com> Date: Tue, 20 Nov 2018 10:18:17 +0800 From: zhong jiang User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20120428 Thunderbird/12.0.1 MIME-Version: 1.0 To: Wengang Wang CC: , , , , , , Subject: Re: [PATCH] mm: use this_cpu_cmpxchg_double in put_cpu_partial References: <20181117013335.32220-1-wen.gang.wang@oracle.com> In-Reply-To: <20181117013335.32220-1-wen.gang.wang@oracle.com> Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7bit X-Originating-IP: [10.177.29.68] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2018/11/17 9:33, Wengang Wang wrote: > The this_cpu_cmpxchg makes the do-while loop pass as long as the > s->cpu_slab->partial as the same value. It doesn't care what happened to > that slab. Interrupt is not disabled, and new alloc/free can happen in the > interrupt handlers. Theoretically, after we have a reference to the it, > stored in _oldpage_, the first slab on the partial list on this CPU can be > moved to kmem_cache_node and then moved to different kmem_cache_cpu and > then somehow can be added back as head to partial list of current > kmem_cache_cpu, though that is a very rare case. If that rare case really > happened, the reading of oldpage->pobjects may get a 0xdead0000 > unexpectedly, stored in _pobjects_, if the reading happens just after > another CPU removed the slab from kmem_cache_node, setting lru.prev to > LIST_POISON2 (0xdead000000000200). The wrong _pobjects_(negative) then > prevents slabs from being moved to kmem_cache_node and being finally freed. > > We see in a vmcore, there are 375210 slabs kept in the partial list of one > kmem_cache_cpu, but only 305 in-use objects in the same list for > kmalloc-2048 cache. We see negative values for page.pobjects, the last page > with negative _pobjects_ has the value of 0xdead0004, the next page looks > good (_pobjects is 1). > > For the fix, I wanted to call this_cpu_cmpxchg_double with > oldpage->pobjects, but failed due to size difference between > oldpage->pobjects and cpu_slab->partial. So I changed to call > this_cpu_cmpxchg_double with _tid_. I don't really want no alloc/free > happen in between, but just want to make sure the first slab did expereince > a remove and re-add. This patch is more to call for ideas. Have you hit the really issue or just review the code ? I did hit the issue and fixed in the upstream patch unpredictably by the following patch. e5d9998f3e09 ("slub: make ->cpu_partial unsigned int") Thanks, zhong jiang > Signed-off-by: Wengang Wang > --- > mm/slub.c | 20 +++++++++++++++++--- > 1 file changed, 17 insertions(+), 3 deletions(-) > > diff --git a/mm/slub.c b/mm/slub.c > index e3629cd..26539e6 100644 > --- a/mm/slub.c > +++ b/mm/slub.c > @@ -2248,6 +2248,7 @@ static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain) > { > #ifdef CONFIG_SLUB_CPU_PARTIAL > struct page *oldpage; > + unsigned long tid; > int pages; > int pobjects; > > @@ -2255,8 +2256,12 @@ static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain) > do { > pages = 0; > pobjects = 0; > - oldpage = this_cpu_read(s->cpu_slab->partial); > > + tid = this_cpu_read(s->cpu_slab->tid); > + /* read tid before reading oldpage */ > + barrier(); > + > + oldpage = this_cpu_read(s->cpu_slab->partial); > if (oldpage) { > pobjects = oldpage->pobjects; > pages = oldpage->pages; > @@ -2283,8 +2288,17 @@ static void put_cpu_partial(struct kmem_cache *s, struct page *page, int drain) > page->pobjects = pobjects; > page->next = oldpage; > > - } while (this_cpu_cmpxchg(s->cpu_slab->partial, oldpage, page) > - != oldpage); > + /* we dont' change tid, but want to make sure it didn't change > + * in between. We don't really hope alloc/free not happen on > + * this CPU, but don't want the first slab be removed from and > + * then re-added as head to this partial list. If that case > + * happened, pobjects may read 0xdead0000 when this slab is just > + * removed from kmem_cache_node by other CPU setting lru.prev > + * to LIST_POISON2. > + */ > + } while (this_cpu_cmpxchg_double(s->cpu_slab->partial, s->cpu_slab->tid, > + oldpage, tid, page, tid) == 0); > + > if (unlikely(!s->cpu_partial)) { > unsigned long flags; >