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 Received: from picard.linux.it (picard.linux.it [213.254.12.146]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A1A5AC433EF for ; Tue, 21 Dec 2021 09:24:05 +0000 (UTC) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 832183C9230 for ; Tue, 21 Dec 2021 10:24:03 +0100 (CET) Received: from in-2.smtp.seeweb.it (in-2.smtp.seeweb.it [IPv6:2001:4b78:1:20::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by picard.linux.it (Postfix) with ESMTPS id 2B0763C8E4D for ; Tue, 21 Dec 2021 10:23:52 +0100 (CET) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by in-2.smtp.seeweb.it (Postfix) with ESMTPS id 2BB6A601C5F for ; Tue, 21 Dec 2021 10:23:51 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1640078630; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=sARLPFIxy5KgOoIqnEHiaght1AjtT/+CbduNpITionc=; b=cskVeSmoxVqIG30KYvLDKkgPR/+RQjkmO7/gWRM1Vh/Me5hBdnbP7qz0aQfMv5OjnGDjYt L+hDoSsAs3nD3m+kuRNuwPyz5ND3HdI38E8rYR71jvySLFpZZIlzmHew/O8QV1UbEzSu9/ YY801Kx0kzU50J2VqjIQIkLbbN/CD9c= Received: from mail-yb1-f197.google.com (mail-yb1-f197.google.com [209.85.219.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-262-ozCBr_H7NJWjt-oI4l9DGw-1; Tue, 21 Dec 2021 04:23:48 -0500 X-MC-Unique: ozCBr_H7NJWjt-oI4l9DGw-1 Received: by mail-yb1-f197.google.com with SMTP id q123-20020a252a81000000b00609e97bb74bso3859828ybq.5 for ; Tue, 21 Dec 2021 01:23:48 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=sARLPFIxy5KgOoIqnEHiaght1AjtT/+CbduNpITionc=; b=EPazzex7WkA9Y9tQMexbqj8YVbrt2nxCBc7DreOO+tSjdbdoC/b5kU3/efZQo7czfe UCAUALkVfUnNACIWkdCweQicG/YdT8Y3SDIa4Fy0aGeTmS3hfbA15f/6aL65W509MeqM 1ZEJESvBBUURcNGGxIBIB7u2k3qZzc6dindGon4APPz1MWk0PdMEohPjbi1a+wHZu/SM J42ulN1FrJwh0pdNQ5Sgf1I3kCTSKpITcZwjSX6l+7BIvRxfhcGnijT2U8sD2TJCGICj fGlvDR+TjGO1elp+HGQruKoGQu178zKVrv3PQ3xJus322Ps5QfLZdvf5s72ppf9KDUWe uL+w== X-Gm-Message-State: AOAM530DOXJAv9hsIPimr/ZgQXPdb1gu1NABUwi4d8Cycbw+4zvUVMg7 ag/ydcV45vcrX6solUoJrYxnqY2gtCOBtgTbtCa72dAoVQGR7f6SsSGJHh5kVtRidFzC+pmjWae PIOLu4kS3qT0onCOmLl0UG5Q/Wy4= X-Received: by 2002:a25:ce94:: with SMTP id x142mr3265151ybe.145.1640078628122; Tue, 21 Dec 2021 01:23:48 -0800 (PST) X-Google-Smtp-Source: ABdhPJwcJ0jJ1BeB9L856nvcVSr53cINOEO/mvyEkSrW7clzJ7OaTAhXYRMG5U3bgxEvZP9TtxH6KdD2aUrznKXDGq4= X-Received: by 2002:a25:ce94:: with SMTP id x142mr3265126ybe.145.1640078627705; Tue, 21 Dec 2021 01:23:47 -0800 (PST) MIME-Version: 1.0 References: <20211220095416.583323-1-liwang@redhat.com> In-Reply-To: From: Li Wang Date: Tue, 21 Dec 2021 17:23:35 +0800 Message-ID: To: Cyril Hrubis Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=liwan@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Virus-Scanned: clamav-milter 0.102.4 at in-2.smtp.seeweb.it X-Virus-Status: Clean Subject: Re: [LTP] [PATCH v3 1/3] lib: add functions to adjust oom score X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: LTP List Content-Type: multipart/mixed; boundary="===============1586548727==" Errors-To: ltp-bounces+ltp=archiver.kernel.org@lists.linux.it Sender: "ltp" --===============1586548727== Content-Type: multipart/alternative; boundary="00000000000002342205d3a49068" --00000000000002342205d3a49068 Content-Type: text/plain; charset="UTF-8" On Tue, Dec 21, 2021 at 5:17 PM Cyril Hrubis wrote: > Hi! > > > > v2 --> v3 > > > > * rename to tst_disable_oom_protection > > > > * support set PID as 0 to protect current process > > > > > > > +static void set_oom_score_adj(pid_t pid, int value) > > > > +{ > > > > + int val; > > > > + char score_path[64]; > > > > + > > > > + if (access("/proc/self/oom_score_adj", F_OK) == -1) { > > > We need to check here also /proc/PID/oom_score_adj, i.e. score_path. > > > > > > > Good catch, I would add a 'W_OK' checking and skip the setting with > > a reminder message if run without root. > > > > how about this? > > > > if (access(score_path, W_OK) == -1) { > > tst_res(TINFO, "Warning: %s cannot be accessed for writing, > > please check if test run with root user.", > > score_path); > > Hmm, I guess that we should produce TINFO if the file does not exist and > TWARN if we cannot write to it. Something as: > Not exactly, if someone gives a wrong PID, that also cannot find the score_path. So we shouldn't skip OOM adjustment only with printing the TFINO. > > if (access(score_path, F_OK)) { > tst_res(TINFO, > "'%s' does not exist, skipping OOM score adjustement", > score_path); > return; > } > > if (access(score_path, W_OK)) { > tst_res(TWARN, "'%s' not writeable, are you root?", score_path); > return; > } > As Petr points out, only root user can set a negative value to oom_score_adj, this W_OK is not enough for ordinary users. Consider about situation, I'd suggest go with non-safe macros and add additional check in the last. e.g. --- a/lib/tst_memutils.c +++ b/lib/tst_memutils.c @@ -108,17 +108,21 @@ static void set_oom_score_adj(pid_t pid, int value) else sprintf(score_path, "/proc/%d/oom_score_adj", pid); - if (access(score_path, R_OK | W_OK) == -1) { - tst_res(TINFO, "Warning: %s cannot be accessed for reading/writing, - please check if test run with root user.", - score_path); - return - } - - SAFE_FILE_PRINTF(score_path, "%d", value); - SAFE_FILE_SCANF(score_path, "%d", &val); - if (val != value) + if (access(score_path, F_OK) == -1) + tst_brk(TBROK, "%s does not exist, please check if PID is valid"); + + FILE_PRINTF(score_path, "%d", value); + FILE_SCANF(score_path, "%d", &val); + + if (val != value) { + if (value < 0) { + tst_res(TINFO, "Warning: %s cannot be set to negative value, + please check if test run with root user.", + score_path); + return + } tst_brk(TBROK, "oom_score_adj = %d, but expect %d.", val, value); + } } -- Regards, Li Wang --00000000000002342205d3a49068 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


On Tue, Dec 21, 2021 at 5:17 PM Cyril Hrubis <chrubis@suse.cz> wrote:
Hi!
> > >=C2=A0 =C2=A0 =C2=A0v2 --> v3
> > >=C2=A0 =C2=A0 =C2=A0 =C2=A0* rename to tst_disable_oom_protec= tion
> > >=C2=A0 =C2=A0 =C2=A0 =C2=A0* support set PID as 0 to protect = current process
> >
> > > +static void set_oom_score_adj(pid_t pid, int value)
> > > +{
> > > +=C2=A0 =C2=A0 =C2=A0int val;
> > > +=C2=A0 =C2=A0 =C2=A0char score_path[64];
> > > +
> > > +=C2=A0 =C2=A0 =C2=A0if (access("/proc/self/oom_score_a= dj", F_OK) =3D=3D -1) {
> > We need to check here also /proc/PID/oom_score_adj, i.e. score_pa= th.
> >
>
> Good catch, I would add a 'W_OK' checking and skip the setting= with
> a reminder message if run without root.
>
> how about this?
>
> if (access(score_path, W_OK) =3D=3D -1) {
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0tst_res(TINFO, "Warning: %s cann= ot be accessed for writing,
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0please ch= eck if test run with root user.",
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0score_pat= h);

Hmm, I guess that we should produce TINFO if the file does not exist and TWARN if we cannot write to it. Something as:

Not exactly, = if someone=C2=A0gives a wrong PID, that also cannot find
the score_path. So we shouldn'= ;t skip OOM adjustment only
with printing the TFINO.

=C2=A0

if (access(score_path, F_OK)) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 tst_res(TINFO,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "'%s' = does not exist, skipping OOM score adjustement",
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 score_path);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return;
}

if (access(score_path, W_OK)) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 tst_res(TWARN, "'%s' not writeable= , are you root?", score_path);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return;
}

As Petr points out, only root user can set a negative val= ue to oom_score_adj,
this W_OK is not enough for ordinary users.


<= /div>
Consider a= bout situation, I'd suggest go with non-safe macros and add
additional check in the la= st.

<= div class=3D"gmail_default" style=3D"font-size:small">e.g.

--- a/lib/tst_memuti= ls.c
+++ b/lib/tst_memutils.c
@@ -108,17 +108,21 @@ static void set_o= om_score_adj(pid_t pid, int value)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 else
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 sprintf(score_path,= "/proc/%d/oom_score_adj", pid);
=C2=A0
- =C2=A0 =C2=A0 =C2= =A0 if (access(score_path, R_OK | W_OK) =3D=3D -1) {
- =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 tst_res(TINFO, "Warning: %s cannot be = accessed for reading/writing,
- =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 please check if test run with root u= ser.",
- =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 score_path);
- =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 return
- =C2=A0 =C2=A0 =C2=A0 }
-
- =C2=A0 =C2=A0 = =C2=A0 SAFE_FILE_PRINTF(score_path, "%d", value);
- =C2=A0 =C2= =A0 =C2=A0 SAFE_FILE_SCANF(score_path, "%d", &val);
- =C2= =A0 =C2=A0 =C2=A0 if (val !=3D value)
+ =C2=A0 =C2=A0 =C2=A0 if (access(= score_path, F_OK) =3D=3D -1)
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 tst_brk(TBROK, "%s does not exist, please check if PID is vali= d");
+
+ =C2=A0 =C2=A0 =C2=A0 FILE_PRINTF(score_path, "%d&q= uot;, value);
+ =C2=A0 =C2=A0 =C2=A0 FILE_SCANF(score_path, "%d&quo= t;, &val);
+
+ =C2=A0 =C2=A0 =C2=A0 if (val !=3D value) {
+ = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (value < 0) {
+ = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 tst_res(TINFO, "Warning: %s cannot be set to negative value,
+ = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 please check if test run with root user.&qu= ot;,
+ =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 score_path);
+ =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return
+ = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 }
=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 tst_brk(TBROK, "oom_score_adj =3D %= d, but expect %d.", val, value);
+ =C2=A0 =C2=A0 =C2=A0 }
=C2=A0= }


--
Regards,
Li Wang
--00000000000002342205d3a49068-- --===============1586548727== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline -- Mailing list info: https://lists.linux.it/listinfo/ltp --===============1586548727==--