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 X-Spam-Level: X-Spam-Status: No, score=-3.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,FAKE_REPLY_C,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5CFDAC432BE for ; Fri, 27 Aug 2021 22:58:50 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 97BC460FE6 for ; Fri, 27 Aug 2021 22:58:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 97BC460FE6 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=morinfr.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 1547F8D0001; Fri, 27 Aug 2021 18:58:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 104EE6B0071; Fri, 27 Aug 2021 18:58:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F353E8D0001; Fri, 27 Aug 2021 18:58:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0157.hostedemail.com [216.40.44.157]) by kanga.kvack.org (Postfix) with ESMTP id D74B46B006C for ; Fri, 27 Aug 2021 18:58:47 -0400 (EDT) Received: from smtpin08.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 67F6E18041908 for ; Fri, 27 Aug 2021 22:58:47 +0000 (UTC) X-FDA: 78522377094.08.B61DF07 Received: from smtp4-g21.free.fr (smtp4-g21.free.fr [212.27.42.4]) by imf21.hostedemail.com (Postfix) with ESMTP id CD67ED01DE3D for ; Fri, 27 Aug 2021 22:58:46 +0000 (UTC) Received: from bender.morinfr.org (unknown [82.64.86.27]) by smtp4-g21.free.fr (Postfix) with ESMTPS id 9E29819F4EA; Sat, 28 Aug 2021 00:58:43 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=morinfr.org ; s=20170427; h=In-Reply-To:Content-Type:MIME-Version:Message-ID:Subject:To: From:Date:Sender:Reply-To:Cc:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=7DJVtMpzm9oxuyqntlCJVVhZoeJfYs38uQ3cZd0skFs=; b=4rlRF3Hd5lDAg9L8xjFB/XxBl8 k1Mmg1v5cHtDuzaaYMcznDw4/DDEOGB3yNMe9scXQmd32494EbEXscjyb0dajfSnWIwpeR11/pufy aZpJf7iVXLCTGi4Q6qkXqQnASllosSdNLgXpKv3HZp+xp/Xnnt0RlkQItUEgiR7FlH4U=; Received: from guillaum by bender.morinfr.org with local (Exim 4.92) (envelope-from ) id 1mJko7-0008O4-B4; Sat, 28 Aug 2021 00:58:43 +0200 Date: Sat, 28 Aug 2021 00:58:43 +0200 From: Guillaume Morin To: almasrymina@google.com, mike.kravetz@oracle.com, cgroups@vger.kernel.org, guillaume@morinfr.org, linux-mm@kvack.org Subject: Re: [BUG] potential hugetlb css refcounting issues Message-ID: <20210827225841.GA30891@bender.morinfr.org> Mail-Followup-To: almasrymina@google.com, mike.kravetz@oracle.com, cgroups@vger.kernel.org, guillaume@morinfr.org, linux-mm@kvack.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <8a4f2fbc-76e8-b67b-f110-30beff2228f5@oracle-com> User-Agent: Mutt/1.10.1 (2018-07-13) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=morinfr.org header.s=20170427 header.b=4rlRF3Hd; spf=none (imf21.hostedemail.com: domain of guillaume@morinfr.org has no SPF policy when checking 212.27.42.4) smtp.mailfrom=guillaume@morinfr.org; dmarc=none X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: CD67ED01DE3D X-Stat-Signature: yzcps6ctywjfzctgwzeez1u69qdt7n61 X-HE-Tag: 1630105126-206207 X-Bogosity: Ham, tests=bogofilter, spamicity=0.002816, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Hello Mike, I really appreciate the quick reply Mike Kravets wrote: > There have been other hugetlb cgroup fixes since 5.10. I do not believe > they are related to the underflow issue you have seen. Just FYI. Yes, I am aware. Actually I did my best to look at all recent changes not backported to 5.10 and couldn't find anything related. I tried to cherry-pick a couple of fixes in case but the bug did not go away. > However, when a vma is split both resulting vmas would be 'owners' of > private mapping reserves without incrementing the refcount which would > lead to the underflow you describe. Indeed and I do know that programs running on my reproducer machines do split vmas. >> 2. After 08cf9faf75580, __free_huge_page() decrements the css >> refcount for _each_ page unconditionally by calling >> hugetlb_cgroup_uncharge_page_rsvd(). But a per-page reference count >> is only taken *per page* outside the reserve case in >> alloc_huge_page() (i.e hugetlb_cgroup_charge_cgroup_rsvd() is called >> only if deferred_reserve is true). In the reserve case, there is >> only one css reference linked to the resv map (taken in >> hugetlb_reserve_pages()). This also leads to an underflow of the >> counter. A similar scheme to HPageRestoreReserve can be used to >> track which pages were allocated in the deferred_reserve case and >> call hugetlb_cgroup_uncharge_page_rsvd() only for these during >> freeing. > I am not sure about the above analysis. It is true that > hugetlb_cgroup_uncharge_page_rsvd is called unconditionally in > free_huge_page. However, IIUC hugetlb_cgroup_uncharge_page_rsvd will > only decrement the css refcount if there is a non-NULL hugetlb_cgroup > pointer in the page. And, the pointer in the page would only be set > in the 'deferred_reserve' path of alloc_huge_page. Unless I am > missing something, they seem to balance. Now that you explain, I am pretty sure that you're right and I was wrong. I'll confirm that I can't reproduce without my change for 2. Thank you, Guillaume. -- Guillaume Morin From mboxrd@z Thu Jan 1 00:00:00 1970 From: Guillaume Morin Subject: Re: [BUG] potential hugetlb css refcounting issues Date: Sat, 28 Aug 2021 00:58:43 +0200 Message-ID: <20210827225841.GA30891@bender.morinfr.org> References: <8a4f2fbc-76e8-b67b-f110-30beff2228f5@oracle-com> Mime-Version: 1.0 Return-path: DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=morinfr.org ; s=20170427; h=In-Reply-To:Content-Type:MIME-Version:Message-ID:Subject:To: From:Date:Sender:Reply-To:Cc:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=7DJVtMpzm9oxuyqntlCJVVhZoeJfYs38uQ3cZd0skFs=; b=4rlRF3Hd5lDAg9L8xjFB/XxBl8 k1Mmg1v5cHtDuzaaYMcznDw4/DDEOGB3yNMe9scXQmd32494EbEXscjyb0dajfSnWIwpeR11/pufy aZpJf7iVXLCTGi4Q6qkXqQnASllosSdNLgXpKv3HZp+xp/Xnnt0RlkQItUEgiR7FlH4U=; Content-Disposition: inline In-Reply-To: <8a4f2fbc-76e8-b67b-f110-30beff2228f5@oracle-com> List-ID: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: almasrymina-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org, mike.kravetz-QHcLZuEGTsvQT0dZR+AlfA@public.gmane.org, cgroups-u79uwXL29TY76Z2rM5mHXA@public.gmane.org, guillaume-/FyPzM6KSZdAfugRpC6u6w@public.gmane.org, linux-mm-Bw31MaZKKs3YtjvyW6yDsg@public.gmane.org Hello Mike, I really appreciate the quick reply Mike Kravets wrote: > There have been other hugetlb cgroup fixes since 5.10. I do not believe > they are related to the underflow issue you have seen. Just FYI. Yes, I am aware. Actually I did my best to look at all recent changes not backported to 5.10 and couldn't find anything related. I tried to cherry-pick a couple of fixes in case but the bug did not go away. > However, when a vma is split both resulting vmas would be 'owners' of > private mapping reserves without incrementing the refcount which would > lead to the underflow you describe. Indeed and I do know that programs running on my reproducer machines do split vmas. >> 2. After 08cf9faf75580, __free_huge_page() decrements the css >> refcount for _each_ page unconditionally by calling >> hugetlb_cgroup_uncharge_page_rsvd(). But a per-page reference count >> is only taken *per page* outside the reserve case in >> alloc_huge_page() (i.e hugetlb_cgroup_charge_cgroup_rsvd() is called >> only if deferred_reserve is true). In the reserve case, there is >> only one css reference linked to the resv map (taken in >> hugetlb_reserve_pages()). This also leads to an underflow of the >> counter. A similar scheme to HPageRestoreReserve can be used to >> track which pages were allocated in the deferred_reserve case and >> call hugetlb_cgroup_uncharge_page_rsvd() only for these during >> freeing. > I am not sure about the above analysis. It is true that > hugetlb_cgroup_uncharge_page_rsvd is called unconditionally in > free_huge_page. However, IIUC hugetlb_cgroup_uncharge_page_rsvd will > only decrement the css refcount if there is a non-NULL hugetlb_cgroup > pointer in the page. And, the pointer in the page would only be set > in the 'deferred_reserve' path of alloc_huge_page. Unless I am > missing something, they seem to balance. Now that you explain, I am pretty sure that you're right and I was wrong. I'll confirm that I can't reproduce without my change for 2. Thank you, Guillaume. -- Guillaume Morin