Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp5906125ybl; Tue, 14 Jan 2020 17:28:26 -0800 (PST) X-Google-Smtp-Source: APXvYqxoGRHgJSsjxzdlagKnwrJIQ4bubsWSmoRIanA7DwE/XH6Ici22nLqybY4Ib4bnD/3z3e4e X-Received: by 2002:a05:6830:1385:: with SMTP id d5mr1080197otq.61.1579051705926; Tue, 14 Jan 2020 17:28:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579051705; cv=none; d=google.com; s=arc-20160816; b=nbvbWrBG7HHABVps7nyKi8sc7tX4AgS+XSyeDQB0Zne6B3B8Xj+GEqA3UWGFkgcFCN OS+7NNd6ANEqHM/jj2wv32Z6+v2xSGgne6H1zjqnsbi0CLbCyR767Su25SzsQ2aJE1rp 0YxWqGKWzTnjEbKx7ZqJ8BlRKE04rZQ0NS+g1A0BUn5Vlz7+mrjLFO8L7gzTNN/Wnrf5 IdVwiA5vmWFY7QjTF2b1QafmVufrxC3iUT5e8semw0mnyv6pXrjbJtkflxhtft1BePaa Me7REiACjt5cObAjayX0OZF/TXOzMgCjsF1G3gdlGTr1b8tdqqmlIGeFNT52wgrVuav/ loRQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=Kg+p/VmE2ROhKV/f4JfIzn9grk12cA50+GiyFmuccAU=; b=EVvdxFvMc8yWMCJSlAl4tjK3QUa4KBop83gL3wJ3P3NkDgB+8ZfAiSgO0njL7Es14V n+yw9mjUITCHmy+VZJY65exL68GQe4LlyE7rZntH/sR/aSgxrNm7mMMI3QwYTtum1RUW rT5f0Fi6eEAKbJxBN+E2yB+P/5PXb1YG3/YdHSmYDxkMaLXqMnXsnHABQq/PhwxciN9S iM8q6vCd2NNR/Wnn2XESbJasSB6kWiL7HMhmvp1KdjzLpvCj/JYEyPa8Qywom+TAA2uS mXIytu6gN/Bp0xjV99h6sP53z3ZKW6fl6M//Zf+z6jpDTeBq30TCFq9akibttzUjS3kB mA0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=aQmn7cGy; 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 j20si8349911oie.212.2020.01.14.17.28.14; Tue, 14 Jan 2020 17:28:25 -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=aQmn7cGy; 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 S1728982AbgAOB1C (ORCPT + 99 others); Tue, 14 Jan 2020 20:27:02 -0500 Received: from mail-ua1-f74.google.com ([209.85.222.74]:36576 "EHLO mail-ua1-f74.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728916AbgAOB1B (ORCPT ); Tue, 14 Jan 2020 20:27:01 -0500 Received: by mail-ua1-f74.google.com with SMTP id s42so2580146uad.3 for ; Tue, 14 Jan 2020 17:27:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=Kg+p/VmE2ROhKV/f4JfIzn9grk12cA50+GiyFmuccAU=; b=aQmn7cGyi4DTCWWDs/8MKLAnnwva8Ubb5n+C5BOd3SmYTQ9GvqFJGRc9/m0UDfBAUo egHMx514bdLFhyEiqHW3Z6seaUi1zIef7UrcDyE/3fiNXQla98SmA45FRe2iHeVwvAio PVSpj49XmlCQ4CrADmBE4g4SdQ5Kbzt73FH6yn5FvJdgXy3ETfbWjP4Rh2Pl/0326NRJ UpkwGmw5oLVNWMtH2KBOarSK1YxJheBtmQGEe0zctVuswXKNxBWM6DeX+IgYeM7oC/Gm /v9NF8x7Zx+XkVCLhCSh4E/+NhtJE3DTs5SlkNOb4Y50DYO+aXv2qjlcJ9qoREboroH2 1UoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=Kg+p/VmE2ROhKV/f4JfIzn9grk12cA50+GiyFmuccAU=; b=RcAu48jPpUdculSPBhhOz6r1Ettfzy22I+d7H7uUdfHliwZUyK2ooArBLDSCYTrVVG OPFtap3D+nW7UtU+ASOdUGoRUEyzVcn5c8dMToEJDbl2xY4AgpNLpk1K45SQrTVzVxQD XeYPSospw4Jm81iLHIzWeEJNFaLa6QIxSKWqo5DNi2Hzh9V6Y1IE1ntohB3PsS2BgmAt XiYaX26tQm6w4jDxjhoEGdwNTsRBM4uCGq0c6gsHX8gZPytxrQXXY3ToRvW8RVpVH5PL BOUry4G9K6ZM+/LVk4tKt2Y0Hm2lU7gm4IWKDxsBQkOGyFJABEYQ3kuozwhswqTpLfhd nHNw== X-Gm-Message-State: APjAAAVxsgHhbofwZ90mNrh+Vt4qJWhJyOdA4uvnF7X/BW/Zs8lCjb/9 G6sAhxOyorqt/IvYvBvWpJqtssEhMHeZIuMqhw== X-Received: by 2002:ab0:7450:: with SMTP id p16mr16108545uaq.20.1579051620011; Tue, 14 Jan 2020 17:27:00 -0800 (PST) Date: Tue, 14 Jan 2020 17:26:46 -0800 In-Reply-To: <20200115012651.228058-1-almasrymina@google.com> Message-Id: <20200115012651.228058-3-almasrymina@google.com> Mime-Version: 1.0 References: <20200115012651.228058-1-almasrymina@google.com> X-Mailer: git-send-email 2.25.0.rc1.283.g88dfdc4193-goog Subject: [PATCH v10 3/8] hugetlb_cgroup: add reservation accounting for private mappings From: Mina Almasry To: mike.kravetz@oracle.com, rientjes@google.com, shakeelb@google.com Cc: shuah@kernel.org, almasrymina@google.com, gthelen@google.com, akpm@linux-foundation.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, cgroups@vger.kernel.org, aneesh.kumar@linux.vnet.ibm.com Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Normally the pointer to the cgroup to uncharge hangs off the struct page, and gets queried when it's time to free the page. With hugetlb_cgroup reservations, this is not possible. Because it's possible for a page to be reserved by one task and actually faulted in by another task. The best place to put the hugetlb_cgroup pointer to uncharge for reservations is in the resv_map. But, because the resv_map has different semantics for private and shared mappings, the code patch to charge/uncharge shared and private mappings is different. This patch implements charging and uncharging for private mappings. For private mappings, the counter to uncharge is in resv_map->reservation_counter. On initializing the resv_map this is set to NULL. On reservation of a region in private mapping, the tasks hugetlb_cgroup is charged and the hugetlb_cgroup is placed is resv_map->reservation_counter. On hugetlb_vm_op_close, we uncharge resv_map->reservation_counter. Signed-off-by: Mina Almasry --- Changes in v10: - Fixed cases where the mapping is private but cgroup accounting is disabled. Changes in V9: - Updated for reparenting of hugetlb reservation accounting. --- include/linux/hugetlb.h | 10 +++++++++ include/linux/hugetlb_cgroup.h | 27 ++++++++++++++++++++++++ mm/hugetlb.c | 38 +++++++++++++++++++++++++++++++++- mm/hugetlb_cgroup.c | 28 ------------------------- 4 files changed, 74 insertions(+), 29 deletions(-) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index dea6143aa0685..5491932ea5758 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -46,6 +46,16 @@ struct resv_map { long adds_in_progress; struct list_head region_cache; long region_cache_count; +#ifdef CONFIG_CGROUP_HUGETLB + /* + * On private mappings, the counter to uncharge reservations is stored + * here. If these fields are 0, then either the mapping is shared, or + * cgroup accounting is disabled for this resv_map. + */ + struct page_counter *reservation_counter; + unsigned long pages_per_hpage; + struct cgroup_subsys_state *css; +#endif }; extern struct resv_map *resv_map_alloc(void); void resv_map_release(struct kref *ref); diff --git a/include/linux/hugetlb_cgroup.h b/include/linux/hugetlb_cgroup.h index eab8a70d5bcb5..8c320accefe87 100644 --- a/include/linux/hugetlb_cgroup.h +++ b/include/linux/hugetlb_cgroup.h @@ -25,6 +25,33 @@ struct hugetlb_cgroup; #define HUGETLB_CGROUP_MIN_ORDER 2 #ifdef CONFIG_CGROUP_HUGETLB +enum hugetlb_memory_event { + HUGETLB_MAX, + HUGETLB_NR_MEMORY_EVENTS, +}; + +struct hugetlb_cgroup { + struct cgroup_subsys_state css; + + /* + * the counter to account for hugepages from hugetlb. + */ + struct page_counter hugepage[HUGE_MAX_HSTATE]; + + /* + * the counter to account for hugepage reservations from hugetlb. + */ + struct page_counter reserved_hugepage[HUGE_MAX_HSTATE]; + + atomic_long_t events[HUGE_MAX_HSTATE][HUGETLB_NR_MEMORY_EVENTS]; + atomic_long_t events_local[HUGE_MAX_HSTATE][HUGETLB_NR_MEMORY_EVENTS]; + + /* Handle for "hugetlb.events" */ + struct cgroup_file events_file[HUGE_MAX_HSTATE]; + + /* Handle for "hugetlb.events.local" */ + struct cgroup_file events_local_file[HUGE_MAX_HSTATE]; +}; static inline struct hugetlb_cgroup *hugetlb_cgroup_from_page(struct page *page, bool reserved) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 62a4cf3db4090..f1b63946ee95c 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -666,6 +666,17 @@ struct resv_map *resv_map_alloc(void) INIT_LIST_HEAD(&resv_map->regions); resv_map->adds_in_progress = 0; +#ifdef CONFIG_CGROUP_HUGETLB + /* + * Initialize these to 0. On shared mappings, 0's here indicate these + * fields don't do cgroup accounting. On private mappings, these will be + * re-initialized to the proper values, to indicate that hugetlb cgroup + * reservations are to be un-charged from here. + */ + resv_map->reservation_counter = NULL; + resv_map->pages_per_hpage = 0; + resv_map->css = NULL; +#endif INIT_LIST_HEAD(&resv_map->region_cache); list_add(&rg->link, &resv_map->region_cache); @@ -3194,7 +3205,11 @@ static void hugetlb_vm_op_close(struct vm_area_struct *vma) reserve = (end - start) - region_count(resv, start, end); - kref_put(&resv->refs, resv_map_release); +#ifdef CONFIG_CGROUP_HUGETLB + hugetlb_cgroup_uncharge_counter(resv->reservation_counter, + (end - start) * resv->pages_per_hpage, + resv->css); +#endif if (reserve) { /* @@ -3204,6 +3219,8 @@ static void hugetlb_vm_op_close(struct vm_area_struct *vma) gbl_reserve = hugepage_subpool_put_pages(spool, reserve); hugetlb_acct_memory(h, -gbl_reserve); } + + kref_put(&resv->refs, resv_map_release); } static int hugetlb_vm_op_split(struct vm_area_struct *vma, unsigned long addr) @@ -4550,6 +4567,7 @@ int hugetlb_reserve_pages(struct inode *inode, struct hstate *h = hstate_inode(inode); struct hugepage_subpool *spool = subpool_inode(inode); struct resv_map *resv_map; + struct hugetlb_cgroup *h_cg; long gbl_reserve; /* This should never happen */ @@ -4583,12 +4601,30 @@ int hugetlb_reserve_pages(struct inode *inode, chg = region_chg(resv_map, from, to); } else { + /* Private mapping. */ resv_map = resv_map_alloc(); if (!resv_map) return -ENOMEM; chg = to - from; + if (hugetlb_cgroup_charge_cgroup(hstate_index(h), + chg * pages_per_huge_page(h), + &h_cg, true)) { + kref_put(&resv_map->refs, resv_map_release); + return -ENOMEM; + } + +#ifdef CONFIG_CGROUP_HUGETLB + /* + * Since this branch handles private mappings, we attach the + * counter to uncharge for this reservation off resv_map. + */ + resv_map->reservation_counter = + &h_cg->reserved_hugepage[hstate_index(h)]; + resv_map->pages_per_hpage = pages_per_huge_page(h); +#endif + set_vma_resv_map(vma, resv_map); set_vma_resv_flags(vma, HPAGE_RESV_OWNER); } diff --git a/mm/hugetlb_cgroup.c b/mm/hugetlb_cgroup.c index c434f69f38354..ddfdf19a9ad35 100644 --- a/mm/hugetlb_cgroup.c +++ b/mm/hugetlb_cgroup.c @@ -23,34 +23,6 @@ #include #include -enum hugetlb_memory_event { - HUGETLB_MAX, - HUGETLB_NR_MEMORY_EVENTS, -}; - -struct hugetlb_cgroup { - struct cgroup_subsys_state css; - - /* - * the counter to account for hugepages from hugetlb. - */ - struct page_counter hugepage[HUGE_MAX_HSTATE]; - - /* - * the counter to account for hugepage reservations from hugetlb. - */ - struct page_counter reserved_hugepage[HUGE_MAX_HSTATE]; - - atomic_long_t events[HUGE_MAX_HSTATE][HUGETLB_NR_MEMORY_EVENTS]; - atomic_long_t events_local[HUGE_MAX_HSTATE][HUGETLB_NR_MEMORY_EVENTS]; - - /* Handle for "hugetlb.events" */ - struct cgroup_file events_file[HUGE_MAX_HSTATE]; - - /* Handle for "hugetlb.events.local" */ - struct cgroup_file events_local_file[HUGE_MAX_HSTATE]; -}; - #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val)) #define MEMFILE_IDX(val) (((val) >> 16) & 0xffff) #define MEMFILE_ATTR(val) ((val) & 0xffff) -- 2.25.0.rc1.283.g88dfdc4193-goog