From patchwork Fri Jan 7 18:07:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pintu Kumar X-Patchwork-Id: 12706885 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 934C9C433EF for ; Fri, 7 Jan 2022 18:09:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BA1B06B0085; Fri, 7 Jan 2022 13:09:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B511F6B0087; Fri, 7 Jan 2022 13:09:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A3FCD6B0088; Fri, 7 Jan 2022 13:09:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0051.hostedemail.com [216.40.44.51]) by kanga.kvack.org (Postfix) with ESMTP id 9581F6B0085 for ; Fri, 7 Jan 2022 13:09:05 -0500 (EST) Received: from smtpin27.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 4DAB597315 for ; Fri, 7 Jan 2022 18:09:05 +0000 (UTC) X-FDA: 79004277450.27.3F2E933 Received: from alexa-out-sd-01.qualcomm.com (alexa-out-sd-01.qualcomm.com [199.106.114.38]) by imf12.hostedemail.com (Postfix) with ESMTP id E3A9E40008 for ; Fri, 7 Jan 2022 18:09:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; i=@quicinc.com; q=dns/txt; s=qcdkim; t=1641578944; x=1673114944; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=BmhPFW1q0OdAFsLIXcbkLXvE9OceVGBzXPJvOxfchrY=; b=AUKHzQY8ro0+efovNl//0MZluu9pWlNYreSuKEiEcZDeDv1jKFQZjGN1 tWotfKhYQN4HkGX2kA/Z4bYiFk7D+SpOzIka2j0QfiCcFdFJBoNAIxMha 7AsMi50wp0vi9DOEqD5QXJkRG7K4syMmn1b0pNFTXshfxhDh8Dum83jif k=; Received: from unknown (HELO ironmsg02-sd.qualcomm.com) ([10.53.140.142]) by alexa-out-sd-01.qualcomm.com with ESMTP; 07 Jan 2022 10:09:02 -0800 X-QCInternal: smtphost Received: from unknown (HELO nasanex01a.na.qualcomm.com) ([10.52.223.231]) by ironmsg02-sd.qualcomm.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Jan 2022 10:09:02 -0800 Received: from blr-ubuntu-498.qualcomm.com (10.80.80.8) by nasanex01a.na.qualcomm.com (10.52.223.231) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.922.19; Fri, 7 Jan 2022 10:08:56 -0800 From: Pintu Kumar To: , , , , , , , , , , , , , , , , , CC: Subject: [PATCH v2] sysinfo: include availram field in sysinfo struct Date: Fri, 7 Jan 2022 23:37:34 +0530 Message-ID: <1641578854-14232-1-git-send-email-quic_pintu@quicinc.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1641483250-18839-1-git-send-email-quic_pintu@quicinc.com> References: <1641483250-18839-1-git-send-email-quic_pintu@quicinc.com> MIME-Version: 1.0 X-Originating-IP: [10.80.80.8] X-ClientProxiedBy: nasanex01a.na.qualcomm.com (10.52.223.231) To nasanex01a.na.qualcomm.com (10.52.223.231) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=quicinc.com header.s=qcdkim header.b=AUKHzQY8; spf=pass (imf12.hostedemail.com: domain of quic_pintu@quicinc.com designates 199.106.114.38 as permitted sender) smtp.mailfrom=quic_pintu@quicinc.com; dmarc=pass (policy=none) header.from=quicinc.com X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: E3A9E40008 X-Stat-Signature: 6mi35gopyxgz6rksaqbimb3owzyhegjm X-HE-Tag: 1641578943-65617 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 sysinfo member does not have any "available ram" field and the bufferram field is not much helpful either, to get a rough estimate of available ram needed for allocation. One needs to parse MemAvailable field separately from /proc/meminfo to get this info instead of directly getting if from sysinfo itself. Thus, this patch introduce a new field as availram in sysinfo so that all the info total/free/available can be retrieved from one place itself. There are couple of places in kernel as well where this can be improved. For example: In fs/proc/meminfo.c: meminfo_proc_show: si_meminfo(&i); available = si_mem_available(); Now with this change the second call be avoided. Thus, we can directly do: show_val_kb(m, "MemAvailable: ", i.availram); Note, this also requires update in procfs for free and other commands. Like in free command as well we frist call sysinfo then again parse /proc/meminfo to get available field. This can be avoided too with higher kernel version. A sample output with single sysinfo call is shown below: Total RAM: 248376 kB Free RAM: 231540 kB Avail RAM: 230448 kB Signed-off-by: Pintu Kumar Signed-off-by: Pintu Agarwal --- include/uapi/linux/sysinfo.h | 3 ++- kernel/sys.c | 4 ++++ mm/page_alloc.c | 2 ++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/include/uapi/linux/sysinfo.h b/include/uapi/linux/sysinfo.h index 435d5c2..fe84c6a 100644 --- a/include/uapi/linux/sysinfo.h +++ b/include/uapi/linux/sysinfo.h @@ -19,7 +19,8 @@ struct sysinfo { __kernel_ulong_t totalhigh; /* Total high memory size */ __kernel_ulong_t freehigh; /* Available high memory size */ __u32 mem_unit; /* Memory unit size in bytes */ - char _f[20-2*sizeof(__kernel_ulong_t)-sizeof(__u32)]; /* Padding: libc5 uses this.. */ + __kernel_ulong_t availram; /* Memory available for allocation */ + char _f[20-3*sizeof(__kernel_ulong_t)-sizeof(__u32)]; /* Padding: libc5 uses this.. */ }; #endif /* _LINUX_SYSINFO_H */ diff --git a/kernel/sys.c b/kernel/sys.c index ecc4cf0..7059515 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2671,6 +2671,7 @@ static int do_sysinfo(struct sysinfo *info) info->freeram <<= bitcount; info->sharedram <<= bitcount; info->bufferram <<= bitcount; + info->availram <<= bitcount; info->totalswap <<= bitcount; info->freeswap <<= bitcount; info->totalhigh <<= bitcount; @@ -2700,6 +2701,7 @@ struct compat_sysinfo { u32 freeram; u32 sharedram; u32 bufferram; + u32 availram; u32 totalswap; u32 freeswap; u16 procs; @@ -2732,6 +2734,7 @@ COMPAT_SYSCALL_DEFINE1(sysinfo, struct compat_sysinfo __user *, info) s.freeram >>= bitcount; s.sharedram >>= bitcount; s.bufferram >>= bitcount; + s.availram >>= bitcount; s.totalswap >>= bitcount; s.freeswap >>= bitcount; s.totalhigh >>= bitcount; @@ -2747,6 +2750,7 @@ COMPAT_SYSCALL_DEFINE1(sysinfo, struct compat_sysinfo __user *, info) s_32.freeram = s.freeram; s_32.sharedram = s.sharedram; s_32.bufferram = s.bufferram; + s_32.availram = s.availram; s_32.totalswap = s.totalswap; s_32.freeswap = s.freeswap; s_32.procs = s.procs; diff --git a/mm/page_alloc.c b/mm/page_alloc.c index b5d62e1..5013c75 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -5786,6 +5786,7 @@ void si_meminfo(struct sysinfo *val) val->sharedram = global_node_page_state(NR_SHMEM); val->freeram = global_zone_page_state(NR_FREE_PAGES); val->bufferram = nr_blockdev_pages(); + val->availram = si_mem_available(); val->totalhigh = totalhigh_pages(); val->freehigh = nr_free_highpages(); val->mem_unit = PAGE_SIZE; @@ -5807,6 +5808,7 @@ void si_meminfo_node(struct sysinfo *val, int nid) val->totalram = managed_pages; val->sharedram = node_page_state(pgdat, NR_SHMEM); val->freeram = sum_zone_node_page_state(nid, NR_FREE_PAGES); + val->availram = si_mem_available(); #ifdef CONFIG_HIGHMEM for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) { struct zone *zone = &pgdat->node_zones[zone_type];