Received: by 2002:a05:7412:da14:b0:e2:908c:2ebd with SMTP id fe20csp1816614rdb; Mon, 9 Oct 2023 04:07:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEScFF1Z1HK+EdSmoS5OSjwUcuqmit8zSngUAhFACQuxDbgjCrAaN0pgxIzFHnoz63AVYWF X-Received: by 2002:a17:90b:4b86:b0:279:e5e:ea1b with SMTP id lr6-20020a17090b4b8600b002790e5eea1bmr12696935pjb.5.1696849646073; Mon, 09 Oct 2023 04:07:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696849646; cv=none; d=google.com; s=arc-20160816; b=m60JVPWh9AVvWK5ZDAOl/11ozI/BwBq7wcNzSOQscUz4AsY4PkYZLuAJ5zYT0qcNle g8xKflWWXlnkMUBKJeGsWepS1/+XrPx7kmS3PmMHG20kjxFfCa9G3SkS4kDJHfbjhdps ITxJcNv00unq3xpgK/zTbNLkXmRHiNM48e5N+mniUBR+4aRPTQxvWxBszdBgZ/VaYGSE P3+jDfA3bFwgj4u2JmRpUGLUT7TjDakq6cpdn/eeSkfwqIoFJNHbkQa5icOJDcntLQxL 8ry6TJHDZDjVSG1nsnXJzM1jnGTnubsVxIh8QKR2Nhhi8rHjB41qEJqZ/nPChugs1QKU UPaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date:dkim-signature; bh=94GrlV6BpXzh9G3J0ul8d01iN4TaQgOhd/Tfg+JX6og=; fh=b5D7CCW9LXmb8R1E8+0AReajK3q+lbHjiaW7UnLpHqU=; b=H5BTmw7avHtu9RZR1i4dG0Tnxk9CS6hGDiIL3wRAtfq+FyDzvqWbS1jRqVGn9XdXKK FO7zhwBW9C1jv+HyoYHxsEZyNe8LuUg5n3A3IB9QY4U4ATRcXismewrpPmwVBlimGVx0 Q6iJlMW889vWOxzjykmRDKjQUz3Cf0Nbd4Sufgt+DWRjfTYbDbaa8IlXjGgG5gLB9UJL i6QVTvMjQHAvUcyGwX02PUVLox6TmlqKxNvWFrCHcQlKYLAdroSsJkwWqtBjoDa9gelx TS3HaQCbypi4pSiu0yerJCbP2b66NADXzbZIsjojieBbg9IWr+B69vXdGFPSegl/7Vpi A+lg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=mP6FAKp7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id mt12-20020a17090b230c00b002790b712b6asi6877051pjb.39.2023.10.09.04.07.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 04:07:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=mP6FAKp7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 26934802AF26; Mon, 9 Oct 2023 04:07:25 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346146AbjJILHV (ORCPT + 99 others); Mon, 9 Oct 2023 07:07:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40854 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346015AbjJILHU (ORCPT ); Mon, 9 Oct 2023 07:07:20 -0400 Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2065494 for ; Mon, 9 Oct 2023 04:07:18 -0700 (PDT) Received: by mail-ej1-x631.google.com with SMTP id a640c23a62f3a-991c786369cso731086466b.1 for ; Mon, 09 Oct 2023 04:07:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696849636; x=1697454436; darn=vger.kernel.org; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=94GrlV6BpXzh9G3J0ul8d01iN4TaQgOhd/Tfg+JX6og=; b=mP6FAKp7VpXGO/fULV1JVkxA+MiPiUfMkfkGCSMWfJU02Po0NOcv8jmtVIo5I/K1UD +svSl8CVudsIRn19Gz3lG7KK37cH5w0ESvCCWP2AGcdxE92Qo1s84k1DXscnrHJszPWf Ayt27IvJRqtJVcE8NvI5Sa435N89iOhaqSfB3oouyVX6T4gPJ0wR0bqwP0QIrbXbyubt hXP62syheD/4xayygaSFhp+aDsN4bYDqikGKAwxhMULEW5qsQNaPL7n5aL9n2HdUBYjX nYIGBv5U9TGbuuQsucvuutfi+xihBfpio5aktffnb/WAVUFQRqJDOU1nlIuedH6pPXpS 7ZQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696849636; x=1697454436; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=94GrlV6BpXzh9G3J0ul8d01iN4TaQgOhd/Tfg+JX6og=; b=TIkCgUIxjTvIl8GHuVRAnDUDfE6aerDyx69kAsEcV675TYX+MFVPU6I9BqYwLM1B+J JrmMEe01KXnRn9eafoORB5UkCdrTIQmlR/rd1m4pMt6Rz5CtMU+rEJfLiwUvA0hcgfvG jUhx/wZbrGxIHUeWkYFMqOIab8G2SINwDibX5c+zVjflSFyKquBE6VECtBIaulg9GN0B 4m5jfdZthcDccliwB67p90ssMeWZ7dtEUXSP2qOn9vlaKyc5pTjzsbGyL2nX7BRnsEKC IyDga6V0nMU0PMDbh5KOEqLU+oJ5/IQhB0tn9a4ib2tPSzVyFNvyx6mEa7A8NMxDsOjK zs8w== X-Gm-Message-State: AOJu0YxsPFRt71/V9mCbP8QWO2oSOa8ZzW8AYzi3nWmvCZHh7Q121DIW VAMFv2aFfeTHqdWTTJ6IT0LNcE+j6Q== X-Received: by 2002:a17:906:209:b0:9a9:e5bb:eddc with SMTP id 9-20020a170906020900b009a9e5bbeddcmr14496587ejd.16.1696849636330; Mon, 09 Oct 2023 04:07:16 -0700 (PDT) Received: from p183 ([46.53.254.83]) by smtp.gmail.com with ESMTPSA id r11-20020a170906364b00b009a1a5a7ebacsm6506570ejb.201.2023.10.09.04.07.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Oct 2023 04:07:15 -0700 (PDT) Date: Mon, 9 Oct 2023 14:07:14 +0300 From: Alexey Dobriyan To: akpm@linux-foundation.org Cc: linux-kernel@vger.kernel.org, Hugh Dickins , Shuah Khan Subject: [PATCH v3] proc: test /proc/${pid}/statm Message-ID: <0721ca69-7bb4-40aa-8d01-0c5f91e5f363@p183> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 09 Oct 2023 04:07:25 -0700 (PDT) From: Swarup Laxman Kotiaklapudi [more or less rewritten. --adobriyan] My original comment lied, output can be "0 A A B 0 0 0\n" (see comment in the code). I don't quite understand why get_mm_counter(mm, MM_FILEPAGES) + get_mm_counter(mm, MM_SHMEMPAGES) can stay positive but get_mm_counter(mm, MM_ANONPAGES) is always 0 after everything is unmapped but that's just me. Signed-off-by: Swarup Laxman Kotiaklapudi Signed-off-by: Alexey Dobriyan --- tools/testing/selftests/proc/proc-empty-vm.c | 97 +++++++++++++++++++++++++-- 1 file changed, 92 insertions(+), 5 deletions(-) --- a/tools/testing/selftests/proc/proc-empty-vm.c +++ b/tools/testing/selftests/proc/proc-empty-vm.c @@ -303,6 +303,95 @@ static int test_proc_pid_smaps_rollup(pid_t pid) } } +static const char *parse_u64(const char *p, const char *const end, uint64_t *rv) +{ + *rv = 0; + for (; p != end; p += 1) { + if ('0' <= *p && *p <= '9') { + assert(!__builtin_mul_overflow(*rv, 10, rv)); + assert(!__builtin_add_overflow(*rv, *p - '0', rv)); + } else { + break; + } + } + assert(p != end); + return p; +} + +/* + * There seems to be 2 types of valid output: + * "0 A A B 0 0 0\n" for dynamic exeuctables, + * "0 0 0 B 0 0 0\n" for static executables. + */ +static int test_proc_pid_statm(pid_t pid) +{ + char buf[4096]; + snprintf(buf, sizeof(buf), "/proc/%u/statm", pid); + int fd = open(buf, O_RDONLY); + if (fd == -1) { + perror("open /proc/${pid}/statm"); + return EXIT_FAILURE; + } + + ssize_t rv = read(fd, buf, sizeof(buf)); + close(fd); + + assert(rv >= 0); + assert(rv <= sizeof(buf)); + if (0) { + write(1, buf, rv); + } + + const char *p = buf; + const char *const end = p + rv; + + /* size */ + assert(p != end && *p++ == '0'); + assert(p != end && *p++ == ' '); + + uint64_t resident; + p = parse_u64(p, end, &resident); + assert(p != end && *p++ == ' '); + + uint64_t shared; + p = parse_u64(p, end, &shared); + assert(p != end && *p++ == ' '); + + uint64_t text; + p = parse_u64(p, end, &text); + assert(p != end && *p++ == ' '); + + assert(p != end && *p++ == '0'); + assert(p != end && *p++ == ' '); + + /* data */ + assert(p != end && *p++ == '0'); + assert(p != end && *p++ == ' '); + + assert(p != end && *p++ == '0'); + assert(p != end && *p++ == '\n'); + + assert(p == end); + + /* + * "text" is "mm->end_code - mm->start_code" at execve(2) time. + * munmap() doesn't change it. It can be anything (just link + * statically). It can't be 0 because executing to this point + * implies at least 1 page of code. + */ + assert(text > 0); + + /* + * These two are always equal. Always 0 for statically linked + * executables and sometimes 0 for dynamically linked executables. + * There is no way to tell one from another without parsing ELF + * which is too much for this test. + */ + assert(resident == shared); + + return EXIT_SUCCESS; +} + int main(void) { int rv = EXIT_SUCCESS; @@ -389,11 +478,9 @@ int main(void) if (rv == EXIT_SUCCESS) { rv = test_proc_pid_smaps_rollup(pid); } - /* - * TODO test /proc/${pid}/statm, task_statm() - * ->start_code, ->end_code aren't updated by munmap(). - * Output can be "0 0 0 2 0 0 0\n" where "2" can be anything. - */ + if (rv == EXIT_SUCCESS) { + rv = test_proc_pid_statm(pid); + } /* Cut the rope. */ int wstatus;