diff options
author | Tejun Heo <tj@kernel.org> | 2011-12-15 17:31:35 -0500 |
---|---|---|
committer | Steven Rostedt <rostedt@goodmis.org> | 2011-12-21 07:27:02 -0500 |
commit | 38b78eb855409a05f9d370228bec1955e6878e08 (patch) | |
tree | 4f07483d49aaa53f7b6905a482f05ddcde4834de /kernel | |
parent | 762e1207889b3451c50d365b741af6f9ce958886 (diff) |
tracing: Factorize filter creation
There are four places where new filter for a given filter string is
created, which involves several different steps. This patch factors
those steps into create_[system_]filter() functions which in turn make
use of create_filter_{start|finish}() for common parts.
The only functional change is that if replace_filter_string() is
requested and fails, creation fails without any side effect instead of
being ignored.
Note that system filter is now installed after the processing is
complete which makes freeing before and then restoring filter string
on error unncessary.
-v2: Rebased to resolve conflict with 49aa29513e and updated both
create_filter() functions to always set *filterp instead of
requiring the caller to clear it to %NULL on entry.
Link: http://lkml.kernel.org/r/1323988305-1469-2-git-send-email-tj@kernel.org
Signed-off-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
Diffstat (limited to 'kernel')
-rw-r--r-- | kernel/trace/trace_events_filter.c | 283 |
1 files changed, 142 insertions, 141 deletions
diff --git a/kernel/trace/trace_events_filter.c b/kernel/trace/trace_events_filter.c index f04cc3136bd3..24aee7127451 100644 --- a/kernel/trace/trace_events_filter.c +++ b/kernel/trace/trace_events_filter.c | |||
@@ -1738,11 +1738,121 @@ static int replace_system_preds(struct event_subsystem *system, | |||
1738 | return -ENOMEM; | 1738 | return -ENOMEM; |
1739 | } | 1739 | } |
1740 | 1740 | ||
1741 | static int create_filter_start(char *filter_str, bool set_str, | ||
1742 | struct filter_parse_state **psp, | ||
1743 | struct event_filter **filterp) | ||
1744 | { | ||
1745 | struct event_filter *filter; | ||
1746 | struct filter_parse_state *ps = NULL; | ||
1747 | int err = 0; | ||
1748 | |||
1749 | WARN_ON_ONCE(*psp || *filterp); | ||
1750 | |||
1751 | /* allocate everything, and if any fails, free all and fail */ | ||
1752 | filter = __alloc_filter(); | ||
1753 | if (filter && set_str) | ||
1754 | err = replace_filter_string(filter, filter_str); | ||
1755 | |||
1756 | ps = kzalloc(sizeof(*ps), GFP_KERNEL); | ||
1757 | |||
1758 | if (!filter || !ps || err) { | ||
1759 | kfree(ps); | ||
1760 | __free_filter(filter); | ||
1761 | return -ENOMEM; | ||
1762 | } | ||
1763 | |||
1764 | /* we're committed to creating a new filter */ | ||
1765 | *filterp = filter; | ||
1766 | *psp = ps; | ||
1767 | |||
1768 | parse_init(ps, filter_ops, filter_str); | ||
1769 | err = filter_parse(ps); | ||
1770 | if (err && set_str) | ||
1771 | append_filter_err(ps, filter); | ||
1772 | return err; | ||
1773 | } | ||
1774 | |||
1775 | static void create_filter_finish(struct filter_parse_state *ps) | ||
1776 | { | ||
1777 | if (ps) { | ||
1778 | filter_opstack_clear(ps); | ||
1779 | postfix_clear(ps); | ||
1780 | kfree(ps); | ||
1781 | } | ||
1782 | } | ||
1783 | |||
1784 | /** | ||
1785 | * create_filter - create a filter for a ftrace_event_call | ||
1786 | * @call: ftrace_event_call to create a filter for | ||
1787 | * @filter_str: filter string | ||
1788 | * @set_str: remember @filter_str and enable detailed error in filter | ||
1789 | * @filterp: out param for created filter (always updated on return) | ||
1790 | * | ||
1791 | * Creates a filter for @call with @filter_str. If @set_str is %true, | ||
1792 | * @filter_str is copied and recorded in the new filter. | ||
1793 | * | ||
1794 | * On success, returns 0 and *@filterp points to the new filter. On | ||
1795 | * failure, returns -errno and *@filterp may point to %NULL or to a new | ||
1796 | * filter. In the latter case, the returned filter contains error | ||
1797 | * information if @set_str is %true and the caller is responsible for | ||
1798 | * freeing it. | ||
1799 | */ | ||
1800 | static int create_filter(struct ftrace_event_call *call, | ||
1801 | char *filter_str, bool set_str, | ||
1802 | struct event_filter **filterp) | ||
1803 | { | ||
1804 | struct event_filter *filter = NULL; | ||
1805 | struct filter_parse_state *ps = NULL; | ||
1806 | int err; | ||
1807 | |||
1808 | err = create_filter_start(filter_str, set_str, &ps, &filter); | ||
1809 | if (!err) { | ||
1810 | err = replace_preds(call, filter, ps, filter_str, false); | ||
1811 | if (err && set_str) | ||
1812 | append_filter_err(ps, filter); | ||
1813 | } | ||
1814 | create_filter_finish(ps); | ||
1815 | |||
1816 | *filterp = filter; | ||
1817 | return err; | ||
1818 | } | ||
1819 | |||
1820 | /** | ||
1821 | * create_system_filter - create a filter for an event_subsystem | ||
1822 | * @system: event_subsystem to create a filter for | ||
1823 | * @filter_str: filter string | ||
1824 | * @filterp: out param for created filter (always updated on return) | ||
1825 | * | ||
1826 | * Identical to create_filter() except that it creates a subsystem filter | ||
1827 | * and always remembers @filter_str. | ||
1828 | */ | ||
1829 | static int create_system_filter(struct event_subsystem *system, | ||
1830 | char *filter_str, struct event_filter **filterp) | ||
1831 | { | ||
1832 | struct event_filter *filter = NULL; | ||
1833 | struct filter_parse_state *ps = NULL; | ||
1834 | int err; | ||
1835 | |||
1836 | err = create_filter_start(filter_str, true, &ps, &filter); | ||
1837 | if (!err) { | ||
1838 | err = replace_system_preds(system, ps, filter_str); | ||
1839 | if (!err) { | ||
1840 | /* System filters just show a default message */ | ||
1841 | kfree(filter->filter_string); | ||
1842 | filter->filter_string = NULL; | ||
1843 | } else { | ||
1844 | append_filter_err(ps, filter); | ||
1845 | } | ||
1846 | } | ||
1847 | create_filter_finish(ps); | ||
1848 | |||
1849 | *filterp = filter; | ||
1850 | return err; | ||
1851 | } | ||
1852 | |||
1741 | int apply_event_filter(struct ftrace_event_call *call, char *filter_string) | 1853 | int apply_event_filter(struct ftrace_event_call *call, char *filter_string) |
1742 | { | 1854 | { |
1743 | struct filter_parse_state *ps; | ||
1744 | struct event_filter *filter; | 1855 | struct event_filter *filter; |
1745 | struct event_filter *tmp; | ||
1746 | int err = 0; | 1856 | int err = 0; |
1747 | 1857 | ||
1748 | mutex_lock(&event_mutex); | 1858 | mutex_lock(&event_mutex); |
@@ -1759,49 +1869,30 @@ int apply_event_filter(struct ftrace_event_call *call, char *filter_string) | |||
1759 | goto out_unlock; | 1869 | goto out_unlock; |
1760 | } | 1870 | } |
1761 | 1871 | ||
1762 | err = -ENOMEM; | 1872 | err = create_filter(call, filter_string, true, &filter); |
1763 | ps = kzalloc(sizeof(*ps), GFP_KERNEL); | ||
1764 | if (!ps) | ||
1765 | goto out_unlock; | ||
1766 | |||
1767 | filter = __alloc_filter(); | ||
1768 | if (!filter) { | ||
1769 | kfree(ps); | ||
1770 | goto out_unlock; | ||
1771 | } | ||
1772 | |||
1773 | replace_filter_string(filter, filter_string); | ||
1774 | |||
1775 | parse_init(ps, filter_ops, filter_string); | ||
1776 | err = filter_parse(ps); | ||
1777 | if (err) { | ||
1778 | append_filter_err(ps, filter); | ||
1779 | goto out; | ||
1780 | } | ||
1781 | 1873 | ||
1782 | err = replace_preds(call, filter, ps, filter_string, false); | ||
1783 | if (err) { | ||
1784 | filter_disable(call); | ||
1785 | append_filter_err(ps, filter); | ||
1786 | } else | ||
1787 | call->flags |= TRACE_EVENT_FL_FILTERED; | ||
1788 | out: | ||
1789 | /* | 1874 | /* |
1790 | * Always swap the call filter with the new filter | 1875 | * Always swap the call filter with the new filter |
1791 | * even if there was an error. If there was an error | 1876 | * even if there was an error. If there was an error |
1792 | * in the filter, we disable the filter and show the error | 1877 | * in the filter, we disable the filter and show the error |
1793 | * string | 1878 | * string |
1794 | */ | 1879 | */ |
1795 | tmp = call->filter; | 1880 | if (filter) { |
1796 | rcu_assign_pointer(call->filter, filter); | 1881 | struct event_filter *tmp = call->filter; |
1797 | if (tmp) { | 1882 | |
1798 | /* Make sure the call is done with the filter */ | 1883 | if (!err) |
1799 | synchronize_sched(); | 1884 | call->flags |= TRACE_EVENT_FL_FILTERED; |
1800 | __free_filter(tmp); | 1885 | else |
1886 | filter_disable(call); | ||
1887 | |||
1888 | rcu_assign_pointer(call->filter, filter); | ||
1889 | |||
1890 | if (tmp) { | ||
1891 | /* Make sure the call is done with the filter */ | ||
1892 | synchronize_sched(); | ||
1893 | __free_filter(tmp); | ||
1894 | } | ||
1801 | } | 1895 | } |
1802 | filter_opstack_clear(ps); | ||
1803 | postfix_clear(ps); | ||
1804 | kfree(ps); | ||
1805 | out_unlock: | 1896 | out_unlock: |
1806 | mutex_unlock(&event_mutex); | 1897 | mutex_unlock(&event_mutex); |
1807 | 1898 | ||
@@ -1811,7 +1902,6 @@ out_unlock: | |||
1811 | int apply_subsystem_event_filter(struct event_subsystem *system, | 1902 | int apply_subsystem_event_filter(struct event_subsystem *system, |
1812 | char *filter_string) | 1903 | char *filter_string) |
1813 | { | 1904 | { |
1814 | struct filter_parse_state *ps; | ||
1815 | struct event_filter *filter; | 1905 | struct event_filter *filter; |
1816 | int err = 0; | 1906 | int err = 0; |
1817 | 1907 | ||
@@ -1835,48 +1925,19 @@ int apply_subsystem_event_filter(struct event_subsystem *system, | |||
1835 | goto out_unlock; | 1925 | goto out_unlock; |
1836 | } | 1926 | } |
1837 | 1927 | ||
1838 | err = -ENOMEM; | 1928 | err = create_system_filter(system, filter_string, &filter); |
1839 | ps = kzalloc(sizeof(*ps), GFP_KERNEL); | 1929 | if (filter) { |
1840 | if (!ps) | 1930 | /* |
1841 | goto out_unlock; | 1931 | * No event actually uses the system filter |
1842 | 1932 | * we can free it without synchronize_sched(). | |
1843 | filter = __alloc_filter(); | 1933 | */ |
1844 | if (!filter) | 1934 | __free_filter(system->filter); |
1845 | goto out; | 1935 | system->filter = filter; |
1846 | 1936 | } | |
1847 | /* System filters just show a default message */ | ||
1848 | kfree(filter->filter_string); | ||
1849 | filter->filter_string = NULL; | ||
1850 | |||
1851 | /* | ||
1852 | * No event actually uses the system filter | ||
1853 | * we can free it without synchronize_sched(). | ||
1854 | */ | ||
1855 | __free_filter(system->filter); | ||
1856 | system->filter = filter; | ||
1857 | |||
1858 | parse_init(ps, filter_ops, filter_string); | ||
1859 | err = filter_parse(ps); | ||
1860 | if (err) | ||
1861 | goto err_filter; | ||
1862 | |||
1863 | err = replace_system_preds(system, ps, filter_string); | ||
1864 | if (err) | ||
1865 | goto err_filter; | ||
1866 | |||
1867 | out: | ||
1868 | filter_opstack_clear(ps); | ||
1869 | postfix_clear(ps); | ||
1870 | kfree(ps); | ||
1871 | out_unlock: | 1937 | out_unlock: |
1872 | mutex_unlock(&event_mutex); | 1938 | mutex_unlock(&event_mutex); |
1873 | 1939 | ||
1874 | return err; | 1940 | return err; |
1875 | |||
1876 | err_filter: | ||
1877 | replace_filter_string(filter, filter_string); | ||
1878 | append_filter_err(ps, system->filter); | ||
1879 | goto out; | ||
1880 | } | 1941 | } |
1881 | 1942 | ||
1882 | #ifdef CONFIG_PERF_EVENTS | 1943 | #ifdef CONFIG_PERF_EVENTS |
@@ -1894,7 +1955,6 @@ int ftrace_profile_set_filter(struct perf_event *event, int event_id, | |||
1894 | { | 1955 | { |
1895 | int err; | 1956 | int err; |
1896 | struct event_filter *filter; | 1957 | struct event_filter *filter; |
1897 | struct filter_parse_state *ps; | ||
1898 | struct ftrace_event_call *call; | 1958 | struct ftrace_event_call *call; |
1899 | 1959 | ||
1900 | mutex_lock(&event_mutex); | 1960 | mutex_lock(&event_mutex); |
@@ -1909,33 +1969,10 @@ int ftrace_profile_set_filter(struct perf_event *event, int event_id, | |||
1909 | if (event->filter) | 1969 | if (event->filter) |
1910 | goto out_unlock; | 1970 | goto out_unlock; |
1911 | 1971 | ||
1912 | filter = __alloc_filter(); | 1972 | err = create_filter(call, filter_str, false, &filter); |
1913 | if (!filter) { | ||
1914 | err = PTR_ERR(filter); | ||
1915 | goto out_unlock; | ||
1916 | } | ||
1917 | |||
1918 | err = -ENOMEM; | ||
1919 | ps = kzalloc(sizeof(*ps), GFP_KERNEL); | ||
1920 | if (!ps) | ||
1921 | goto free_filter; | ||
1922 | |||
1923 | parse_init(ps, filter_ops, filter_str); | ||
1924 | err = filter_parse(ps); | ||
1925 | if (err) | ||
1926 | goto free_ps; | ||
1927 | |||
1928 | err = replace_preds(call, filter, ps, filter_str, false); | ||
1929 | if (!err) | 1973 | if (!err) |
1930 | event->filter = filter; | 1974 | event->filter = filter; |
1931 | 1975 | else | |
1932 | free_ps: | ||
1933 | filter_opstack_clear(ps); | ||
1934 | postfix_clear(ps); | ||
1935 | kfree(ps); | ||
1936 | |||
1937 | free_filter: | ||
1938 | if (err) | ||
1939 | __free_filter(filter); | 1976 | __free_filter(filter); |
1940 | 1977 | ||
1941 | out_unlock: | 1978 | out_unlock: |
@@ -1954,43 +1991,6 @@ out_unlock: | |||
1954 | #define CREATE_TRACE_POINTS | 1991 | #define CREATE_TRACE_POINTS |
1955 | #include "trace_events_filter_test.h" | 1992 | #include "trace_events_filter_test.h" |
1956 | 1993 | ||
1957 | static int test_get_filter(char *filter_str, struct ftrace_event_call *call, | ||
1958 | struct event_filter **pfilter) | ||
1959 | { | ||
1960 | struct event_filter *filter; | ||
1961 | struct filter_parse_state *ps; | ||
1962 | int err = -ENOMEM; | ||
1963 | |||
1964 | filter = __alloc_filter(); | ||
1965 | if (!filter) | ||
1966 | goto out; | ||
1967 | |||
1968 | ps = kzalloc(sizeof(*ps), GFP_KERNEL); | ||
1969 | if (!ps) | ||
1970 | goto free_filter; | ||
1971 | |||
1972 | parse_init(ps, filter_ops, filter_str); | ||
1973 | err = filter_parse(ps); | ||
1974 | if (err) | ||
1975 | goto free_ps; | ||
1976 | |||
1977 | err = replace_preds(call, filter, ps, filter_str, false); | ||
1978 | if (!err) | ||
1979 | *pfilter = filter; | ||
1980 | |||
1981 | free_ps: | ||
1982 | filter_opstack_clear(ps); | ||
1983 | postfix_clear(ps); | ||
1984 | kfree(ps); | ||
1985 | |||
1986 | free_filter: | ||
1987 | if (err) | ||
1988 | __free_filter(filter); | ||
1989 | |||
1990 | out: | ||
1991 | return err; | ||
1992 | } | ||
1993 | |||
1994 | #define DATA_REC(m, va, vb, vc, vd, ve, vf, vg, vh, nvisit) \ | 1994 | #define DATA_REC(m, va, vb, vc, vd, ve, vf, vg, vh, nvisit) \ |
1995 | { \ | 1995 | { \ |
1996 | .filter = FILTER, \ | 1996 | .filter = FILTER, \ |
@@ -2109,12 +2109,13 @@ static __init int ftrace_test_event_filter(void) | |||
2109 | struct test_filter_data_t *d = &test_filter_data[i]; | 2109 | struct test_filter_data_t *d = &test_filter_data[i]; |
2110 | int err; | 2110 | int err; |
2111 | 2111 | ||
2112 | err = test_get_filter(d->filter, &event_ftrace_test_filter, | 2112 | err = create_filter(&event_ftrace_test_filter, d->filter, |
2113 | &filter); | 2113 | false, &filter); |
2114 | if (err) { | 2114 | if (err) { |
2115 | printk(KERN_INFO | 2115 | printk(KERN_INFO |
2116 | "Failed to get filter for '%s', err %d\n", | 2116 | "Failed to get filter for '%s', err %d\n", |
2117 | d->filter, err); | 2117 | d->filter, err); |
2118 | __free_filter(filter); | ||
2118 | break; | 2119 | break; |
2119 | } | 2120 | } |
2120 | 2121 | ||