From patchwork Mon Jul 24 06:22:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhongkun He X-Patchwork-Id: 13323468 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4B8D2C001DE for ; Mon, 24 Jul 2023 06:22:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DE4486B0075; Mon, 24 Jul 2023 02:22:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D94566B0078; Mon, 24 Jul 2023 02:22:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C34226B007B; Mon, 24 Jul 2023 02:22:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id AAB436B0075 for ; Mon, 24 Jul 2023 02:22:14 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 6C6B7C0599 for ; Mon, 24 Jul 2023 06:22:14 +0000 (UTC) X-FDA: 81045510588.05.D78577F Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) by imf14.hostedemail.com (Postfix) with ESMTP id 9D180100010 for ; Mon, 24 Jul 2023 06:22:11 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=KFYjcOVL; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf14.hostedemail.com: domain of hezhongkun.hzk@bytedance.com designates 209.85.214.171 as permitted sender) smtp.mailfrom=hezhongkun.hzk@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690179731; a=rsa-sha256; cv=none; b=H1h3EygMZphT7Hi9s+YxSOWMGoCo3l6iHSaREKax2mhWbj9a/Mv0YG02ryMES9/j66LRS3 ZFAsszidkcOkeDxlK+VY380LZMP6iTXzJXO9+Dcr6u1QkgndSB3m1Lwcs/3utNSy3lOnhc 3GkNiML8InVcBBkfXFgqD9bNzC2o418= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=KFYjcOVL; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf14.hostedemail.com: domain of hezhongkun.hzk@bytedance.com designates 209.85.214.171 as permitted sender) smtp.mailfrom=hezhongkun.hzk@bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690179731; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=zMxhq1yiZbeuYK1cuOPzx7JANSxn1juq0pUBihEL+Wk=; b=4q9Iub5GBKzGL6g43LkSUILbUaAjC1Z6Nep1Zd7bQCF89AjusUA8s8IwFYgdSVj9gCokkL cwTsgpIfJyvScA1Z/dXEU2UTDrijZhRS7MgQe/I2B0ieZnQSrpxCX9QHTnhFziuRddwy/Z gZ9JVVvKSD1x09Sop0QrNqqNumXnqKg= Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-1b8b2886364so19032645ad.0 for ; Sun, 23 Jul 2023 23:22:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1690179730; x=1690784530; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=zMxhq1yiZbeuYK1cuOPzx7JANSxn1juq0pUBihEL+Wk=; b=KFYjcOVLyGA1d4rfaoLPdLB/e6IjDH5icirSxJhNketSHU37mMtGFferynYbZDdVDr SBHHg1RIVmDI1fNLSXGw64oy5igBfOWadAkh1lGrBGeXMBa1ulxQGdtmi8/J7UhGyGZ5 PEqbCcd8mvm1vPkw4Rr/lF18ynZhCmEe2Nseic1Cu+mBDAkH1xnTwwZWgykAo+8kBVSo K+5y+jfTjdVUDLBZ1x9Lr90D1oTQILAGJl5dG+myCccy2uIl1c+6SRN9JGvOolhslNK4 nVA38dojo5rsWjEmeGe9RFjEZoFNh+SK6kqQBoGX1ixHQeZl5xBToafr9ey+tHo56CfZ eOtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690179730; x=1690784530; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=zMxhq1yiZbeuYK1cuOPzx7JANSxn1juq0pUBihEL+Wk=; b=if7r9BJ4Gjq+vGPF0JkxQYXubsF3iMVrJe+/h3AmzQfuCJHWrF4HOIFtgAZGWkBWJG g5MJDIasF8DmooS2IPA7kT1oaecF/P+GboL0ke8JeizNoKAbJW73Z3uq5oimMgLno6B+ vMRUdNGrnyt2VxyjmvhvGPPR+n7j8hOhIi4wIAGQS2D6hTKK6NH58TapVSvTm3zDqrwz wWPATgvyWDYzzdui1xubQIHr813eyRHQRrXHG7dmTarbbS4TmA8Yt3yjXjGxAAkFr69M kMQBM/ttQm2j5ZVT3mQ47+7FjrfYnCC6aFv+3kkewrQ21PX4q+2zd1ne5Ik3EShzZpCY CY1Q== X-Gm-Message-State: ABy/qLY7eCYqVY1bV1M5pXqq1HbWPUQ/4cDPz+MVLBV+j5wWHVk+q2Qn Sgk0bMxN/T8f4RZmJKseBa/Yzw== X-Google-Smtp-Source: APBJJlHWsnLRWbTHoG+BCImPXA8ex14v4jkicKDRgJKk7Nt/usxhGPfBJt1FuUdKcnDc5doUpcUiAQ== X-Received: by 2002:a17:902:e995:b0:1b6:ab53:c7a5 with SMTP id f21-20020a170902e99500b001b6ab53c7a5mr6059293plb.46.1690179730618; Sun, 23 Jul 2023 23:22:10 -0700 (PDT) Received: from Tower.bytedance.net ([203.208.167.146]) by smtp.gmail.com with ESMTPSA id l14-20020a170902f68e00b001b7e63cfa19sm7983504plg.234.2023.07.23.23.22.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 23 Jul 2023 23:22:09 -0700 (PDT) From: Zhongkun He To: minchan@kernel.org, senozhatsky@chromium.org, mhocko@suse.com Cc: david@redhat.com, yosryahmed@google.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Zhongkun He Subject: [RFC PATCH v2 1/2] memcg: Add support for zram object charge Date: Mon, 24 Jul 2023 14:22:02 +0800 Message-Id: <20230724062202.2244188-1-hezhongkun.hzk@bytedance.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 9D180100010 X-Stat-Signature: tpgpzbcp4jgsnf5gbsori567ejz9csa3 X-HE-Tag: 1690179731-921524 X-HE-Meta: U2FsdGVkX1+GdukPG+NUZPdv2rbGCDIhjcvMMPAIn6STFFL9JJslh4ICATZnIa8kJvF4Ld+2IhDLtk6nAGY/bJTvC3oPg/TUApzkf8GOima72GtxnMdtisQ70uMCN1yyV5rx/RMqtmBkow9R6bjpRqBgOW/4TjcBlse4UJIKvuh2Iy3Ii+J1phH4TXHoU+KKsJTKBcaGOSeGLxNXAiTwUMFRdxCOIzl45X6O5QnnI2FUFvkEg+pqprUOYQT0PF71DuCVJSur4L6pKtE5tn5ymY03otTx5w00jXSVrq3+B6KwRYXZeSTRzHf9ax/YUfYCbDk2ZFs80EUIiC4VMp1bKoCuyJ3ue/3zUJIWUN3BuH5oB+0oFnYfLjQwrBHWj4LQVqUxvIq5vwLx5VyzCbxMsSXU2EG9LhmuQV6ZgLmYybWUS+2aIOBPCaBw/w8zZnLzTWBNMjh7NR5IqLVgVFLjR5tg9E6fdWYE2cITtag9KnZPp0GTBqHQnHYdavQNmgdahZAQtZcW5e8MD3/2ZY+sxP1he+2ds07AKT64d/dsu6Ua5b6WMazKPuWIy4pC0BtlEWEtxSdPQAI7XXA0W2AbcsLcOT7QzUF68xWgRQVorHwpTwojvlsBWVUNPfBLz9+EqvK6agPnT8Od+d0M8U1fR5MDDPjqd7nXaOcgdCUH2Zzxn51uxXmlrArdzqJDE51kxBHYLk1zk0lvkKmTpMssHAGIFkt4Zz4mX54i+ZYFg4npiLE0Cpjuc8pKKCfmaV7hX2+lKA9wuEYwS4In3P6kIZTCpKQ+UUsVCVydANLdj6MV6hDrEFeuSrow9xOkNlj5NhL9HR5ctGarWuQlri3n4NTe/piij9RDvQbRmqOpxfEJBYQFSKDNnva61HmT8dR18wT5Ln7No8jeVtC+zVlpghoZ9VekGIIG75HFMJBRbtbjmEAajjGQcuLlrSURFwUhUU3tj+MCPbUiaj5pTQ4 pwXCkWPI jwZJGR2S9ohoBJKWQs6eYzASuW8adjfMfG6lqpeeTum89FK1rWRc2Z9zwT8KneoIpUb9ZgUt4fXanFXZnJqFfBWR82LlCBjgEoHPpLpkzjT1uCElSs8NOitIJRzSggm5PHH6dtb/QuihXvpaev2KehC9ECvIufnPTx0kiBNNySe/RmlfZI+bk5WEiozkNFvJqMGSupAWp6AgeQF+Eqrq+bTyhMeqxZoEGF0v/qpIwL6ejIrRwR3s4kWPJnTOS+pUqHbgHVEq3za3CazSASmks7kbAeSvBgOBVH3RG4IrbpNwKZya+O/5L9GRuFaLiX4v8P9XJVj3uJi8a61U5xcIUpWCNN7a04Rp6SWUtp0weQksJbOMUIMenaWvISkW3BISMDCzSDx4Y18vPI8qCd6BtNXamGazjQ48eSZuQ X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The compressed RAM(ZRAM) is currently charged to kernel, not to any memory cgroup, which can escape their cgroup memory containment if the memory of a task is limited by memcgroup. it will swap out the memory to zram swap device when the memory is insufficient. In that case, the memory hard limit will be invalidated. So, it should makes sense to charge the compressed RAM to the page's memory cgroup. As we know, zram can be used in two ways, direct and indirect, this patchset can charge memory in both cases. Direct zram usage by process within a cgroup will fail to charge if there is no memory. Indirect zram usage by process within a cgroup via swap in PF_MEMALLOC context, will charge successfully. Signed-off-by: Zhongkun He --- include/linux/memcontrol.h | 12 ++++++++++++ mm/memcontrol.c | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 5818af8eca5a..24bac877bc83 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1819,6 +1819,9 @@ static inline void count_objcg_event(struct obj_cgroup *objcg, rcu_read_unlock(); } +int obj_cgroup_charge_zram(struct obj_cgroup *objcg, gfp_t gfp, + size_t size); +void obj_cgroup_uncharge_zram(struct obj_cgroup *objcg, size_t size); #else static inline bool mem_cgroup_kmem_disabled(void) { @@ -1880,6 +1883,15 @@ static inline void count_objcg_event(struct obj_cgroup *objcg, { } +int obj_cgroup_charge_zram(struct obj_cgroup *objcg, gfp_t gfp, + size_t size) +{ + return 0; +} + +void obj_cgroup_uncharge_zram(struct obj_cgroup *objcg, size_t size) +{ +} #endif /* CONFIG_MEMCG_KMEM */ #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index e8ca4bdcb03c..118544acf895 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3059,6 +3059,7 @@ struct obj_cgroup *get_obj_cgroup_from_page(struct page *page) } return objcg; } +EXPORT_SYMBOL(get_obj_cgroup_from_page); static void memcg_account_kmem(struct mem_cgroup *memcg, int nr_pages) { @@ -3409,6 +3410,29 @@ void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size) refill_obj_stock(objcg, size, true); } +int obj_cgroup_charge_zram(struct obj_cgroup *objcg, gfp_t gfp, + size_t size) +{ + if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) + return 0; + + /* + * Indirect zram usage in PF_MEMALLOC, charging must succeed. + * Direct zram usage, charging may failed. + */ + return obj_cgroup_charge(objcg, gfp, size); +} +EXPORT_SYMBOL(obj_cgroup_charge_zram); + +void obj_cgroup_uncharge_zram(struct obj_cgroup *objcg, size_t size) +{ + if (!cgroup_subsys_on_dfl(memory_cgrp_subsys)) + return; + + obj_cgroup_uncharge(objcg, size); +} +EXPORT_SYMBOL(obj_cgroup_uncharge_zram); + #endif /* CONFIG_MEMCG_KMEM */ /* From patchwork Mon Jul 24 06:22:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhongkun He X-Patchwork-Id: 13323469 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7E50EC001B0 for ; Mon, 24 Jul 2023 06:22:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 04E7A6B0074; Mon, 24 Jul 2023 02:22:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F40E46B0078; Mon, 24 Jul 2023 02:22:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DE4086B007B; Mon, 24 Jul 2023 02:22:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id C8ED86B0074 for ; Mon, 24 Jul 2023 02:22:30 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 9682F1608DE for ; Mon, 24 Jul 2023 06:22:30 +0000 (UTC) X-FDA: 81045511260.12.7A01B6D Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) by imf17.hostedemail.com (Postfix) with ESMTP id D1FCD40009 for ; Mon, 24 Jul 2023 06:22:28 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b="g8XFmi/X"; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf17.hostedemail.com: domain of hezhongkun.hzk@bytedance.com designates 209.85.214.180 as permitted sender) smtp.mailfrom=hezhongkun.hzk@bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690179748; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=vQ0+FHFKuAIc1z56nGRfE+KN8lTLE329HHeSqqWKPM0=; b=zBydM8NSxCp4S+80EAl0/W/R9PGHA45lv0Y8yjgs9WWMpTED014nHsRhMb8e28YWSzqM5t +xfjYJyvkrXI3eTIQKJnOoVgGgStrXRQdY/85/DujbyopZKfzAWU9W8a7gRzHaaPF98fXR FHEuL6EKz39ZphPxl0EVt7vYQSdGtDo= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b="g8XFmi/X"; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf17.hostedemail.com: domain of hezhongkun.hzk@bytedance.com designates 209.85.214.180 as permitted sender) smtp.mailfrom=hezhongkun.hzk@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690179748; a=rsa-sha256; cv=none; b=BXyzCy2eHT/gHfMt9XzYjL35VuxnQKfyBaEU9izFIbsaEWO8r8WdRYxqoHPCWTsOxwXj1O NcAw2m9nrDQfC0Clsd3aHDYMIuLdva4bsS6p8+92h5/c0tBjidT0rM75KENwsR7tZ9oKOq Dm7V3tPVx/QTj8dTXB5SULvqGhRx7P0= Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-1bba2318546so5261185ad.1 for ; Sun, 23 Jul 2023 23:22:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1690179747; x=1690784547; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=vQ0+FHFKuAIc1z56nGRfE+KN8lTLE329HHeSqqWKPM0=; b=g8XFmi/Xu7Q/IPaOlyMdCeZDLC2qDIUrES87VDacv5L4RIvIl2r0gW/KhtEcTINWF6 85uL6rrA8LQjGriLE4rjl7XVCGU8czhLCTXmr4/dBUFZmwaukf40MyoppTsVSXGPnMED yuLs+xCiBxSTLrm8Wp+vlyQRp022Dqdc5p/LMHi5gv8ZOTY7L8QxjhIb1B56LA/FH6ml b4vSr/xbX35wV5C08h/v8anQ2+KiEZecKkvJfOPAvGWtQd/3dA75o4GonVIh8w3yJFiC QQonyK5SW6Ifk7GmdSnw1GrPioh0+m5Mf///5dFHYd8C1LlCS2KyorB9zGkd8VXm/aPc Ge3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690179747; x=1690784547; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=vQ0+FHFKuAIc1z56nGRfE+KN8lTLE329HHeSqqWKPM0=; b=iNpgfTH2Nm5i4grcd/Ic2Y/mU8RXQsmAerrnHCptxmOQ7b+m3dpsZukI4lJdcRJS1F SSOBBNREAkvzCP2oCRXIbvfSW5p2Jvnk4DYI3oXukJAVzn8WytonXOABQcp0Jtm82wmj yodZdmd5O5DcxSEj5CZltOPvKhAOCNUUSjLcBxsg3XtM6O3tA6+LDM8Y0Ay3TjpHy8hv HxA5ev2bfuiD3UKJf9cNYTwlxu7NdHIZi8MTfBnnvy90LhG4q2ipMtGFvnsxEIhs89t8 SubGYcnmEky1JVjrIQlp+k6OWHSfwpXkTrMgD1tW9fuGp96A910oSIRI2A28yRUhaPdF C8ag== X-Gm-Message-State: ABy/qLa4X/zcxm6CvXQNTKeU4Gj6TeGanzkapI7jEIjGnaoTOUMDGW+Y 7aSpSi768O/SEasJ2aL9N3knYw== X-Google-Smtp-Source: APBJJlEolK+aUii1/yBS9Re9OkeH47TWX8ggnozCaLtCJpFpuy5aQEWLtvsSzNsXA7eaGPyusMtcLQ== X-Received: by 2002:a17:902:d2cd:b0:1b8:a389:43ef with SMTP id n13-20020a170902d2cd00b001b8a38943efmr12250391plc.24.1690179747454; Sun, 23 Jul 2023 23:22:27 -0700 (PDT) Received: from Tower.bytedance.net ([203.208.167.146]) by smtp.gmail.com with ESMTPSA id ix20-20020a170902f81400b001b80760fd04sm1443236plb.112.2023.07.23.23.22.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 23 Jul 2023 23:22:26 -0700 (PDT) From: Zhongkun He To: minchan@kernel.org, senozhatsky@chromium.org, mhocko@suse.com Cc: david@redhat.com, yosryahmed@google.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Zhongkun He Subject: [RFC PATCH v2 2/2] zram: charge the compressed RAM to the page's memcgroup Date: Mon, 24 Jul 2023 14:22:19 +0800 Message-Id: <20230724062219.2244240-1-hezhongkun.hzk@bytedance.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: D1FCD40009 X-Stat-Signature: s8utzikq51x4m17in3jgwwhpk9ikwgkk X-Rspam-User: X-HE-Tag: 1690179748-434170 X-HE-Meta: U2FsdGVkX18N58Bk2Uzps1jKUjc46Ok+tZM71EGveBeQyV4XZcviTTWtgeagwLgYSFks4fcpAlcLYY2vt26BHnMBlyErKloCRdg6C2rxvjiXk1qeK8Iw2IRVbqf4sa3WAKNSAWnbZIoRSBBCi4sV8kCwm5zXCZ4rIJ/xJ7k6K2sPFoe5aAr0yFVnYz1xxT/R51VhIbTnzRbNawrAhdhMT+rzakytSSYhrCmak6OBaXWEAo7zeZyNECOZEQ14ivVUJArqn13jX7/1Vh2s7GzrDUgUfBIQv0gQwkpi93YYUmnqhHoann2g3NgjD9USIUopc7607OPgZktn7G5QI5mjL3ivpUx2M/qzg3Hhh2UFpD0CGv/s4XNH/2r2asFsr6MoBmhDQtvg4fkjviMXa7eLEjHXXMyLMu/ytoHd3Wom9a1/pTjwiiJXSaOb9+/Qfj6HblPJbC4b1UfVQJ2vnov3M+Azou9DAqIEaKhEdZF3s2iWB9T13j5mba0vnGv1dexd850gkMc1yKEAczwE/GjpVH5/c2+MdHOXdvAdTbxRuSVw+U9K/w3VcolYu6M/bxMmsL4AFNllk/Htg5eYcm4wD0VZLIEF9qqjEqoIDwJdSsATiM+70Ao7hHvKMTc4hXXqAX9Q2uE67QtMxgHWH/AHzdL729cp8+yHZ+fQq6wn96z/iw/x+Fs2wNlOpO3xYTMVrQagzToV7NO8lHbkm33mN3mYfxptjygXJX1N719DUBzwFCiY75IbZkN7M+WXc04mQE9ANubABDpakvGN7gOMgoVKg44XuTRrvUAXwi6Ph/UeCvBtYW/rLoQUrrQM06cqz+u6Q+iMyzeURAipmD/WDEoD9xH8upWYtgVnMS9mfmEXxx6gZ4cmU+Fm1Bat1oF0mwv9rT2F7nr7MMmGEIOBIRL8bXdEmOUvBAhTrfq75A/gTwAxfVwOXjNG9n8yud/Jpx1aGA8fsHgXGkA3BIo yiwgIjEj yT2nyTv7Bwcm59irQMC1hvbE4iSQA4FQMrya5TPOB6cW/e6d6KjSkvDPa9ALKYvW/HUftvzbFeDRSjQ2qKGVP82m9sn/GTkt6tA1NYqLZOTer3O+UOckId4MRWM2zFJC8YDLbFuqxYE3ClPVGAQAjnH1v2+8rj8S63qCKIk10X342gatfVY9uT79ELEbLYisf++bcBwIWe4Li6ih6sGWu/WvbEOkjcLsdWRj1w6kqVa4Rpq/13NiNtfGpYLfzAMGKp/0DplYehNFw6SBH+pj5ap1sfYQfSAmoGnrKn6SE80Bu5zmUKautzyRRDV4/lmhh+S87k3hQ78g3QS9z4jhoeY75ofIj8QwgHbuGGmQm4yBgdWoKwkMy9vL6AqDcnJvO6+hiPYkDMxSDgCfdt4irxc6bXxY2KMZzwItG X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The compressed RAM is currently charged to kernel, not to any memory cgroup. This patch can charge the pages regardless of direct or indirect zram usage. Direct zram usage by process within a cgroup will fail to charge if there is no memory. Indirect zram usage by process within a cgroup via swap in PF_MEMALLOC context, wqwill charge successfully. This allows some limit overrun, but not enough to matter in practice.Charge compressed page once, mean a page will be freed.the size of compressed page is less than or equal to the page to be freed. The numbers of excess depend on the compression ratio only. The maximum amount will not exceed 400KB, and will be smaller than the hard limit finally, So not an unbounded way. Signed-off-by: Zhongkun He --- drivers/block/zram/zram_drv.c | 46 +++++++++++++++++++++++++++++++++++ drivers/block/zram/zram_drv.h | 1 + 2 files changed, 47 insertions(+) diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c index 5676e6dd5b16..1e685a430c95 100644 --- a/drivers/block/zram/zram_drv.c +++ b/drivers/block/zram/zram_drv.c @@ -33,6 +33,7 @@ #include #include #include +#include #include "zram_drv.h" @@ -135,6 +136,18 @@ static void zram_set_obj_size(struct zram *zram, zram->table[index].flags = (flags << ZRAM_FLAG_SHIFT) | size; } +static inline void zram_set_obj_cgroup(struct zram *zram, u32 index, + struct obj_cgroup *objcg) +{ + zram->table[index].objcg = objcg; +} + +static inline struct obj_cgroup *zram_get_obj_cgroup(struct zram *zram, + u32 index) +{ + return zram->table[index].objcg; +} + static inline bool zram_allocated(struct zram *zram, u32 index) { return zram_get_obj_size(zram, index) || @@ -1256,6 +1269,7 @@ static bool zram_meta_alloc(struct zram *zram, u64 disksize) static void zram_free_page(struct zram *zram, size_t index) { unsigned long handle; + struct obj_cgroup *objcg; #ifdef CONFIG_ZRAM_MEMORY_TRACKING zram->table[index].ac_time = 0; @@ -1289,6 +1303,13 @@ static void zram_free_page(struct zram *zram, size_t index) goto out; } + objcg = zram_get_obj_cgroup(zram, index); + if (objcg) { + obj_cgroup_uncharge_zram(objcg, zram_get_obj_size(zram, index)); + obj_cgroup_put(objcg); + zram_set_obj_cgroup(zram, index, NULL); + } + handle = zram_get_handle(zram, index); if (!handle) return; @@ -1419,6 +1440,7 @@ static int zram_write_page(struct zram *zram, struct page *page, u32 index) struct zcomp_strm *zstrm; unsigned long element = 0; enum zram_pageflags flags = 0; + struct obj_cgroup *objcg; mem = kmap_atomic(page); if (page_same_filled(mem, &element)) { @@ -1494,6 +1516,14 @@ static int zram_write_page(struct zram *zram, struct page *page, u32 index) return -ENOMEM; } + objcg = get_obj_cgroup_from_page(page); + if (objcg && obj_cgroup_charge_zram(objcg, comp_len)) { + zcomp_stream_put(zram->comps[ZRAM_PRIMARY_COMP]); + zs_free(zram->mem_pool, handle); + obj_cgroup_put(objcg); + return -ENOMEM; + } + dst = zs_map_object(zram->mem_pool, handle, ZS_MM_WO); src = zstrm->buffer; @@ -1526,6 +1556,7 @@ static int zram_write_page(struct zram *zram, struct page *page, u32 index) } else { zram_set_handle(zram, index, handle); zram_set_obj_size(zram, index, comp_len); + zram_set_obj_cgroup(zram, index, objcg); } zram_slot_unlock(zram, index); @@ -1575,6 +1606,7 @@ static int zram_recompress(struct zram *zram, u32 index, struct page *page, u32 threshold, u32 prio, u32 prio_max) { struct zcomp_strm *zstrm = NULL; + struct obj_cgroup *objcg; unsigned long handle_old; unsigned long handle_new; unsigned int comp_len_old; @@ -1669,6 +1701,17 @@ static int zram_recompress(struct zram *zram, u32 index, struct page *page, if (threshold && comp_len_new >= threshold) return 0; + objcg = zram_get_obj_cgroup(zram, index); + if (objcg) { + obj_cgroup_get(objcg); + if (obj_cgroup_charge_zram(objcg, GFP_KERNEL, comp_len_new)) { + zcomp_stream_put(zram->comps[prio]); + obj_cgroup_put(objcg); + return -ENOMEM; + } + } + /* * No direct reclaim (slow path) for handle allocation and no * re-compression attempt (unlike in zram_write_bvec()) since @@ -1683,6 +1726,8 @@ static int zram_recompress(struct zram *zram, u32 index, struct page *page, __GFP_MOVABLE); if (IS_ERR_VALUE(handle_new)) { zcomp_stream_put(zram->comps[prio]); + obj_cgroup_uncharge_zram(objcg, comp_len_new); + obj_cgroup_put(objcg); return PTR_ERR((void *)handle_new); } @@ -1696,6 +1741,7 @@ static int zram_recompress(struct zram *zram, u32 index, struct page *page, zram_set_handle(zram, index, handle_new); zram_set_obj_size(zram, index, comp_len_new); zram_set_priority(zram, index, prio); + zram_set_obj_cgroup(zram, index, objcg); atomic64_add(comp_len_new, &zram->stats.compr_data_size); atomic64_inc(&zram->stats.pages_stored); diff --git a/drivers/block/zram/zram_drv.h b/drivers/block/zram/zram_drv.h index ca7a15bd4845..959d721d5474 100644 --- a/drivers/block/zram/zram_drv.h +++ b/drivers/block/zram/zram_drv.h @@ -72,6 +72,7 @@ struct zram_table_entry { #ifdef CONFIG_ZRAM_MEMORY_TRACKING ktime_t ac_time; #endif + struct obj_cgroup *objcg; }; struct zram_stats {