From: Dan Carpenter <dan.carpenter@oracle.com>
To: Chanwoo Choi <cw00.choi@samsung.com>
Cc: MyungJoo Ham <myungjoo.ham@samsung.com>,
Guenter Roeck <linux@roeck-us.net>,
Sebastian Reichel <sre@kernel.org>, Chen-Yu Tsai <wens@csie.org>,
Hans de Goede <hdegoede@redhat.com>,
Felipe Balbi <balbi@kernel.org>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Heikki Krogerus <heikki.krogerus@linux.intel.com>,
linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org,
linux-usb@vger.kernel.org, linux-omap@vger.kernel.org,
kernel-janitors@vger.kernel.org
Subject: Re: [PATCH v2] extcon: fix extcon_get_extcon_dev() error handling
Date: Thu, 16 Dec 2021 18:59:16 +0300 [thread overview]
Message-ID: <20211216155916.GA7738@kadam> (raw)
In-Reply-To: <c8d18573-5dc1-4d45-f134-2a1dbb7590b6@samsung.com>
[-- Attachment #1: Type: text/plain, Size: 1711 bytes --]
On Thu, Dec 16, 2021 at 05:38:04PM +0900, Chanwoo Choi wrote:
> >
> > To be honest, I'm not sure how this differs from other functions which
> > return -EPROBE_DEFER. How do other functions guarantee they will only
> > be called from probe()?
>
> If it is possible to know extcon_get_extcon_dev() will be only callled on probe,
> it is no problem. But, it is not able to guarantee that extcon_get_extcon_dev()
> is called on probe. Because of this reason, this issue should be handled in each device driver.
>
> -EPROBE_DEFER is only for probe step. If return -EPROBE_DEFER except for probe,
> it is wrong return value.
The future is vast and unknowable. We can't really future proof code
and we should never try do that if it makes the code more complicated
right now.
When Andy submitted basically the same patch as me three years ago we
worried about future developers so we didn't merge his patch. But
three years later no non-probe() were introduced. Meanwhile the bad API
created bugs in the kernel for current users.
To some extent, we have to trust future developers to do sane things.
I have also created a static checker test for people who call
EPROBE_DEFER outside of probe functions. I don't know that this test
will work. It will take a few days for the call tree to be built.
Another option would be to change the warning from "is this called from
something besides probe()" to "is this called from an ioctl". That
would generate fewer false positives.
Or potentially, I could save a most recent function pointer in the call
tree. I'll play around with that in the coming months.
Anyway, I've attached my first draft just to show you my thinking on
this.
regards,
dan carpenter
[-- Attachment #2: smatch_kernel_probe.c --]
[-- Type: text/x-csrc, Size: 2518 bytes --]
/*
* Copyright (C) 2021 Oracle.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
*/
/*
* This module answers the question:
* is_probe_function()
* called_from_probe() which returns 1 for yes, -1 for maybe and 0 for no.
*
*/
#include "smatch.h"
static int my_id;
STATE(from_probe);
STATE(maybe);
static unsigned long is_probe_fn;
bool is_probe_function(void)
{
return is_probe_fn;
}
int called_from_probe(void)
{
struct smatch_state *state;
if (is_probe_function())
return 1;
state = get_state(my_id, "from_probe", NULL);
if (state == &from_probe)
return 1;
if (state == &maybe)
return -1;
return 0;
}
static struct smatch_state *merge_func(struct smatch_state *s1, struct smatch_state *s2)
{
return &maybe;
}
static void match_probe_call(struct expression *expr)
{
char *name;
name = get_member_name(expr);
if (!name)
return;
if (strstr(name, "->probe"))
sql_insert_caller_info(expr, PROBE_FN, -1, "", "");
free_string(name);
}
static void select_probe_fn(const char *name, struct symbol *sym, char *key, char *value)
{
is_probe_fn = 1;
}
static void match_call_info(struct expression *expr)
{
int call;
call = called_from_probe();
if (!call)
return;
sql_insert_caller_info(expr, CALLED_FROM_PROBE, -1, "", (call == 1) ? "y" : "m");
}
static void select_from_probe(const char *name, struct symbol *sym, char *key, char *value)
{
if (strcmp(value, "y") == 0)
set_state(my_id, "from_probe", NULL, &from_probe);
else
set_state(my_id, "from_probe", NULL, &maybe);
}
void register_kernel_probe(int id)
{
my_id = id;
if (option_project != PROJ_KERNEL)
return;
add_merge_hook(my_id, &merge_func);
add_function_data(&is_probe_fn);
add_hook(&match_probe_call, FUNCTION_CALL_HOOK);
select_caller_info_hook(&select_probe_fn, PROBE_FN);
add_hook(&match_call_info, FUNCTION_CALL_HOOK);
select_caller_info_hook(&select_from_probe, CALLED_FROM_PROBE);
}
[-- Attachment #3: check_EPROBE_DEFER.c --]
[-- Type: text/x-csrc, Size: 1619 bytes --]
/*
* Copyright (C) 2021 Oracle.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
*/
#include "smatch.h"
#include "smatch_slist.h"
static int my_id;
static void check_for_EPROBE_DEFER(struct expression *expr)
{
sval_t sval;
char *macro;
if (!expr || expr->type != EXPR_PREOP || expr->op != '-')
return;
expr = expr->unop;
if (!get_value(expr, &sval) || sval.value != 517)
return;
macro = get_macro_name(expr->pos);
if (!macro || strcmp(macro, "EPROBE_DEFER") != 0)
return;
sm_warning("returning EPROBE_DEFER from non probe() function");
}
static void match_assign(struct expression *expr)
{
/* "ret = ERR_PTR(-EPROBE_DEFER)" generates a fake assignment so
* that is covered here.
*/
check_for_EPROBE_DEFER(expr->right);
}
static void match_return(struct expression *expr)
{
check_for_EPROBE_DEFER(expr);
}
void check_EPROBE_DEFER(int id)
{
my_id = id;
if (option_project != PROJ_KERNEL)
return;
add_hook(&match_assign, ASSIGNMENT_HOOK);
add_hook(&match_return, RETURN_HOOK);
}
next prev parent reply other threads:[~2021-12-16 16:00 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <CGME20211123084357epcas1p14833147710153f9606f14941ac8b0d96@epcas1p1.samsung.com>
2021-11-23 8:43 ` [PATCH v2] extcon: fix extcon_get_extcon_dev() error handling Dan Carpenter
2021-11-23 14:14 ` Hans de Goede
2021-11-23 14:48 ` Guenter Roeck
2021-11-23 15:20 ` Heikki Krogerus
2021-12-16 6:39 ` Chanwoo Choi
2021-12-16 7:52 ` Dan Carpenter
2021-12-16 8:24 ` Chanwoo Choi
2021-12-16 8:05 ` Dan Carpenter
2021-12-16 8:38 ` Chanwoo Choi
2021-12-16 15:59 ` Dan Carpenter [this message]
2021-12-17 1:31 ` Chanwoo Choi
2021-12-16 9:08 ` Hans de Goede
2021-12-17 6:28 ` [PATCH v3] " Dan Carpenter
2021-12-20 1:20 ` Chanwoo Choi
2022-02-03 5:24 ` Chanwoo Choi
2022-02-16 1:12 ` Chanwoo Choi
2022-01-03 17:46 ` Sebastian Reichel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20211216155916.GA7738@kadam \
--to=dan.carpenter@oracle.com \
--cc=balbi@kernel.org \
--cc=cw00.choi@samsung.com \
--cc=gregkh@linuxfoundation.org \
--cc=hdegoede@redhat.com \
--cc=heikki.krogerus@linux.intel.com \
--cc=kernel-janitors@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-omap@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=linux@roeck-us.net \
--cc=myungjoo.ham@samsung.com \
--cc=sre@kernel.org \
--cc=wens@csie.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox